/*
	
	jsmisc.js (c) Copyright 2005, 2006, 2007 by Lawyers Online Ltd.
	All Rights Reserved.  http://www.lawyersonline.co.uk/
	
	Authors: Craig Jones and Brian 'Astrolox' Wojtczak.
	
	$HeadURL: svn://hebe/Customers/WyeUsk/trunk/includes/tinyajax/jsmisc.js $
	$Id: jsmisc.js 1710 2007-07-18 10:47:59Z qbnut $
	
*/

/* ---------- 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;

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

brows_name = getbrowser();
checkcompat();

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()
{
	// 23/11/2005 - Revision 285 
	// This now detects 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
	{				// (Mozilla, Firefox, Opera, and friends)
		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
		{
			// Not MS Internet Explorer but DOM has document.all...
			iscompat = true;
			method = "MS";
		}
	}
	else if (document.layers)
	{
		// NS 4+ only (not tested)
		if ((browser_name == "Netscape") && (browser_ver >= 4))
		{
			iscompat = true;
			method = "NS4";
		}
		else
		{
			iscompat = false;
		}
	}
	else
	{
		iscompat = false;
	}
}

function switchvis( id, state ) {	
	
	if (method == "W3C") {
		
		object=document.getElementById( id )
		
		if (state == "off") {
			
			object.style.visibility="hidden";
			object.style.display="none";
			
		} else {
			
			object.style.visibility="visible";
			
			if ( id.substr( -3, 3 ) == '_tr' ) {
				object.style.display="table-row";
			} else {
				object.style.display="block";
			}
		}
		
	} else if (method == "MS") { 
		
		if (state == "off") {
			
			document.all[ id ].style.visibility="hidden";
			document.all[ id ].style.display="none";			
			
		} else {
			
			document.all[ id ].style.visibility="visible";
			
			if ( id.substr( -3, 3 ) == '_tr' ) {
				document.all[ id ].style.display="table-row";			
			} else {
				document.all[ id ].style.display="block";			
			}
		}
		
	} else if (method == "NS4") { /* NOT TESTED - not even sure this works */
		
		object=document.layers[ id ]
		
		if (state == "off") {
			
			object.visibility="hidden";
			object.display="none";
			
		} else {
			
			object.visibility="visible";
			
			if ( id.substr( -3, 3 ) == '_tr' ) {
				object.display="table-row";
			} else {
				object.display="block";
			}
		}
	}
	
}

function togglevis( id, mode )
{	
	var visible;
	
	if (method == "W3C") {
		
		object=document.getElementById( id )
		
		if (object.style.visibility=="visible") {
			
			switchvis( id, 'off' );
			visible = 0;
			
		} else {
			
			switchvis( id, 'on' );
			visible = 1;
		}
		
	} else if (method == "MS") {
		
		if (document.all[ id ].style.visibility=="visible") {
			
			switchvis( id, 'off' );
			visible = 0;
			
		} else {
			
			switchvis( id, 'on' );
			visible = 1;
		}
		
	} else if (method == "NS4") {	/* NOT TESTED */
		
		object=document.layers[ id ]
		
		if (object.style.visibility=="visible") {
			
			switchvis( id, 'off' );
			visible = 0;
			
		} else {
			
			switchvis( id, 'on' );
			visible = 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 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();
}

