// jsmisc.js, by Craig Jones
// 
// Browser Detection by Craig Jones
// Div Expand/Collapse Code by Craig Jones
// Cookie Handlers by Brian 'Astrolox' Wojtczak.

/* ---------- VARS ---------- */

var expDays = 30;
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

var brows_name;
var brows_ver;
var agent = navigator.userAgent.toLowerCase();
var bstring;
var iscompat;
var method;

var openmenusections;

/* ---------- INIT ---------- */

brows_name = getbrowser();
checkcompat();

openmenusections=new Array();
// settogglevisinitial();

SetCookie('js','True');

/* ---------- FUNCTIONS ---------- */

function isbrowser(name)
{
	startofver = agent.indexOf(name) + 1;
	bstring = name;
	return startofver;
}
	
function getbrowser()
{
	if (isbrowser('msie')) 
		{ 
			browser = "Microsoft Internet Explorer"
			brows_ver = agent.charAt(startofver + bstring.length);
		}
	else if (isbrowser('netscape6')) 
		{ 
			browser = "Netscape" 						
			brows_ver = agent.charAt(startofver + bstring.length);		
		}
	else if (isbrowser('netscape')) 
		{ 
			browser = "Netscape" 						
			brows_ver = agent.charAt(startofver + bstring.length);		
		}
	else if (isbrowser('mozilla')) 
		{ 
			browser = "Mozilla" 						
			brows_ver = agent.charAt(startofver + bstring.length);		
		}
	else if (isbrowser('opera')) 
		{ 
			browser = "Opera" 						
			brows_ver = agent.charAt(startofver + bstring.length);		
		}				
	else 
		{ 
			// Assume that unknowns are Netscape - THIS MAY BE VERY WRONG !!
			browser = "Netscape"
			brows_ver = agent.charAt(startofver + bstring.length);			
		}
	return browser;
}	

function checkcompat()
{
	// Modified 23/11/2005 to detect the DOM Used rather than the browser
	// which makes the code more compatible across the browsers :)

	iscompat = false;
	method = "W3C";

	if (document.getElementById) // All W3C DOM Compliant Browsers
	{
		iscompat = true;
		method = "W3C";
	}
	else if (document.all) // Microsoft Internet Explorer 6 and below
	{
		// This will only work if MSIE is newer than v.5 - support in < v.5 IIRC is broken.
		if ((browser_name == "Microsoft Internet Explorer") && (browser_ver >= 5))
		{
			iscompat = true;
			method = "MS";
		}
		else if (browser_name == "Microsoft Internet Explorer")
		{
			iscompat = false;
		}
		else
		{
			iscompat = true;
		}
	}
	else if (document.layers)
	{
		// NS 4+ only (not tested)
		if ((browser_name == "Netscape") && (browser_ver >= 4))
		{
			iscompat = true;
		}
		else
		{
			iscompat = false;
		}
	}
	else
	{
		iscompat = false;
	}
}

function togglevis(id, mode)
{	
	/*

	V.1.5 - 23/11/2005	

	*/

	//
	// Modified 2003 by astrolox to add the image support
	// Modified 11/11/2003 by Craig Jones, to comment out image code until needed...
	// Modified 23/11/2005 to choose a type based on DOM Type not browser name
	//

	var visible;
	
	if (method == "W3C") // W3C DOM (Mozilla, Firefox, Opera, and friends)
	{
		object=document.getElementById( id+'_div' )
		// imgobject=document.getElementById( id+'_icon' )

		if (object.style.visibility=="visible")
		{
			object.style.visibility="hidden";
			object.style.display="none";

			visible = 0;
			
			// imgobject.src='/images/icon_plus.gif';
		}
		else
		{
			object.style.visibility="visible";
			object.style.display="block";
			
			visible = 1;

			// imgobject.src='/images/icon_minus.gif';
		}
	}
	
	else if (method == "MS") // MS DOM (Microsoft Internet Explorer)
	{
		if (document.all[ id+'_div' ].style.visibility=="visible")
		{
			document.all[ id+'_div' ].style.visibility="hidden";
			document.all[ id+'_div' ].style.display="none";			

			visible = 0;
			
			// document.all[ id+'_icon' ].src='/images/icon_plus.gif';
		}
		else
		{
			document.all[ id+'_div' ].style.visibility="visible";
			document.all[ id+'_div' ].style.display="block";			

			visible = 1;
			
			// document.all[ id+'_icon' ].src='/images/icon_minus.gif';
		}
	}

	else if (method == "NS4") // NS4 DOM (Netscape 4+ and older - not even sure this works, it's never been tested)
	{
		// imgobject=document.layers[ id+'_icon' ]
		
		object=document.layers[ id+'_div' ]

		if (object.style.visibility=="visible")
		{
			object.visibility="hidden";
			object.display="none";

			visible = 0;
			
			// imgobject.src='/images/icon_plus.gif';
		}
		else
		{
			object.visibility="visible";
			object.display="block";
			
			visible = 1;

			// imgobject.src='/images/icon_minus.gif';
		}
	}
	
	if (mode == 'click')
	{
		updateopensections(id,visible);
	}

}

function updateopensections(id,visible)
{
        // ----- Javascript Version of same code that's found in layout/menu.php -----
        // ----- This is so that both Javascript enabled and Javascript disabled (eugh) browsers both -----
	// ----- work the same... -----

	var delimiter = ':';
	var omsstring = GetCookie('openmenusections');
	if(!omsstring)
	{
		SetCookie('openmenusections',id);
		return 0;
	}	

	var currentparts = new Array();
	currentparts = omsstring.split(delimiter);
	var newparts = new Array();
	if (visible == 1)
	{
		newparts = currentparts;
		newparts[newparts.length] = id;
	}
	else
	{
		var c = 0;
		for (i=0; i<currentparts.length; i++)
		{
			if (currentparts[i] != id)
			{
				newparts[c] = currentparts[i];
				c++;
			}
		}
	}
	omsstring = unsplit(delimiter,newparts);
	SetCookie('openmenusections',omsstring);
	return 1;
}

function settogglevisinitial()
{
	var omsstring = GetCookie('openmenusections');
	openmenusections = omsstring.split(':');

	for (count=0; count<openmenusections.length; count++)
	{
		openmenusections[openmenusections[count]] = 1;
	}
}

function unsplit(delimiter, input)
{
	var out = "";
	for (i=0; i<input.length; i++)
	{
		if (i>0)
		{
			out = out + delimiter;
		}
		out = out + input[i];
	}
	return out;
}

function offtothirdpartysite()
{
	alert('You are now entering a Third-Party Site.\n\nLawyers Online cannot be held responsible for the content of Third-Party Sites.');
};

function getCookieVal (offset)
{
        var endstr = document.cookie.indexOf (";", offset);
        if (endstr == -1)
        endstr = document.cookie.length;
        return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name)
{
        var arg = name + "=";
        var alen = arg.length;
        var clen = document.cookie.length;
        var i = 0;
        while (i < clen)
        {
                var j = i + alen;
                if (document.cookie.substring(i, j) == arg)
                return getCookieVal (j);
                i = document.cookie.indexOf(" ", i) + 1;
                if (i == 0) break;
        }
        return null;
}

function SetCookie (name, value)
{
        var argv = SetCookie.arguments;
        var argc = SetCookie.arguments.length;
        var expires = (argc > 2) ? argv[2] : null;
        var path = (argc > 3) ? argv[3] : null;
        var domain = (argc > 4) ? argv[4] : null;
        var secure = (argc > 5) ? argv[5] : false;
        document.cookie = name + "=" + escape (value) +
        ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
        ((path == null) ? "" : ("; path=" + path)) +
        ((domain == null) ? "" : ("; domain=" + domain)) +
        ((secure == true) ? "; secure" : "");
}

function DeleteCookie (name)
{
        var exp = new Date();
        exp.setTime (exp.getTime() - 1);
        var cval = GetCookie (name);
        document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

// ----- OpenSRS -----

function prepareSeal()
{
	window.open('','srsSeal','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=630,height=350');
}
