//determines browser versions - old style and may still be in use!
bV = parseInt(navigator.appVersion);
ns4 = (document.layers);
ie4 = (document.all && !document.getElementById);
ie5 = (document.all && document.getElementById);
ns6 = (!document.all && document.getElementById);
opra = navigator.userAgent.indexOf("Opera") > -1;

// simple string mathcing for what browser is currently being used
// handy for unique id in css call for caching

// set a variable for css identification
if (ns4) {browser = 'ns4';}
else if (ie4) {browser = 'ie4';}
else if (ie5) {browser = 'ie5';}
else if (ns6) {browser = 'ns6';}
else if (opra) {browser = 'opera';}
browser = browser + navigator.platform;

/* new-style browser detection - backported from release_3_5_branch */
// store browser version in lower case
var agt=navigator.userAgent.toLowerCase();
var appVer = navigator.appVersion.toLowerCase();
var is_minor = parseFloat(appVer);
var is_major = parseInt(is_minor);
// Opera
var is_opera = (agt.indexOf("opera") != -1);
var is_opera2 = (agt.indexOf("opera 2") != -1 || agt.indexOf("opera/2") != -1);
var is_opera3 = (agt.indexOf("opera 3") != -1 || agt.indexOf("opera/3") != -1);
var is_opera4 = (agt.indexOf("opera 4") != -1 || agt.indexOf("opera/4") != -1);
var is_opera5 = (agt.indexOf("opera 5") != -1 || agt.indexOf("opera/5") != -1);
var is_opera6 = (agt.indexOf("opera 6") != -1 || agt.indexOf("opera/6") != -1); 
var is_opera7 = (agt.indexOf("opera 7") != -1 || agt.indexOf("opera/7") != -1); 
var is_opera7up = (is_opera && !is_opera2 && !is_opera3 && !is_opera4 && !is_opera5 && !is_opera6);
var is_opera8up = (is_opera && !is_opera2 && !is_opera3 && !is_opera4 && !is_opera5 && !is_opera6 && !is_opera7);
// IE
var iePos  = appVer.indexOf('msie');
if (iePos !=-1) {
  is_minor = parseFloat(appVer.substring(iePos+5,appVer.indexOf(';',iePos))); 
  is_major = parseInt(is_minor);
}
// Kongueror/Safari
var is_konq = false;
var kqPos   = agt.indexOf('konqueror');
if (kqPos !=-1) {
	is_konq  = true;
	is_minor = parseFloat(agt.substring(kqPos+10,agt.indexOf(';',kqPos)));
	is_major = parseInt(is_minor);
}
var is_getElementById   = (document.getElementById) ? "true" : "false"; 
var is_getElementsByTagName = (document.getElementsByTagName) ? "true" : "false";
var is_documentElement = (document.documentElement) ? "true" : "false"; 

var is_safari = ((agt.indexOf('safari')!=-1))?true:false;
var is_khtml  = (is_safari || is_konq);

// Mozilla/Firefox/Gecko/Netscape
var is_gecko = ((!is_khtml)&&(navigator.product)&&(navigator.product.toLowerCase()=="gecko"))?true:false;
var is_gver  = 0;
if (is_gecko) is_gver=navigator.productSub;
var is_moz = false;
var is_moz_ver = 0;

if (is_gecko && (agt.indexOf('netscape')==-1) && (!is_khtml)) {
	   is_moz = true;
	   is_moz_ver = agt.indexOf('rv:');
	   is_moz_ver = agt.substring(is_moz_ver+3);
	   is_paren   = is_moz_ver.indexOf(')');
	   is_moz_ver = is_moz_ver.substring(0,is_paren);
	   is_minor = is_moz_ver;
	   is_major = parseInt(is_moz_ver);
}
var is_nav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
			&& (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
			&& (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1)
			&& (!is_khtml) && (!(is_moz)));

// Netscape6 is mozilla/5 + Netscape6/6.0!
if ((navigator.vendor)&& ((navigator.vendor=="Netscape6")||(navigator.vendor=="Netscape"))&& (is_nav)) {
   is_major = parseInt(navigator.vendorSub);
   is_minor = parseFloat(navigator.vendorSub);
}

var is_nav2 = (is_nav && (is_major == 2));
var is_nav3 = (is_nav && (is_major == 3));
var is_nav4 = (is_nav && (is_major == 4));
var is_nav4up = (is_nav && is_minor >= 4);  
var is_navonly      = (is_nav && ((agt.indexOf(";nav") != -1) ||
					  (agt.indexOf("; nav") != -1)) );

var is_nav6   = (is_nav && is_major==6); 
var is_nav6up = (is_nav && is_minor >= 6);

var is_nav5   = (is_nav && is_major == 5 && !is_nav6); 
var is_nav5up = (is_nav && is_minor >= 5);

var is_nav7   = (is_nav && is_major == 7);
var is_nav7up = (is_nav && is_minor >= 7);

var is_ie   = ((iePos!=-1) && (!is_opera) && (!is_khtml));
var is_ie3  = (is_ie && (is_major < 4));

var is_ie4   = (is_ie && is_major == 4);
var is_ie4up = (is_ie && is_minor >= 4);
var is_ie5   = (is_ie && is_major == 5);
var is_ie5up = (is_ie && is_minor >= 5);

var is_ie5_5  = (is_ie && (agt.indexOf("msie 5.5") !=-1));
var is_ie5_5up =(is_ie && is_minor >= 5.5);                

var is_ie6   = (is_ie && is_major == 6);
var is_ie6up = (is_ie && is_minor >= 6);

var is_ie7   = (is_ie && is_major == 7);
var is_ie7up = (is_ie && is_minor >= 7);


/*
Backported from release_3_5_branch
hide or unhide an element
takes input of the id of the element (usually a div) to be hidden or made to appear
and the state (optional) that it is made to be brought into for non-ns4 browsers the display property is used. This property makes the space that the element takes up dissapear. This causes bugs for forms and form elements. You need to render the page first and then hide the elements at the end.
for display attribute: 0 = hide, 1 = show, 2 = flip current state 
for visibility attribute: 3 = hide (but do not remove element), 4 = show, 5 = flip 
for display attribute but inline not block level: 6 = show inline
if state is not specified then the default is flip (2)
	note: this function hasn't been tested with NS4 in a long time and none of the option features have been implemented
hide/show covered state is the 3rd argument. true (default) it will call the hideShowCovered function which hides all the select boxes for IE, false it doesn't
*/
function hideElement() {
	var  el = document.getElementById(arguments[0]);
	var state;
	var IEfix = false;
	if (arguments.length == 1) state = 2;
	else if (arguments.length > 1) state = arguments[1];
	if (arguments.length > 2) IEfix = arguments[2]
	if (state == 1) el.style.display = "block";
	else if (state == 6) el.style.display = "inline";
	else if (state == 0) el.style.display = "none";
	else if (state == 3) el.style.visibility ="hidden";
	else if (state == 4) el.style.visibility ="visible";
	else if (state == 2) {
		if (el.style.display == "none" || el.style.display == "") el.style.display = "block";
		else el.style.display = "none";
	}
	else if (state == 5) {
		if (el.style.visibility == "hidden" || el.style.visibility == "") el.style.visibility = "visible";
		else el.style.visibility = "hidden";

	}
	if (IEfix) hideShowCovered(el);
}

// change the class associated with an id
// takes the id and the new class name
function changeClass(id, newClass) {
	if (ie4) {
		id.className = newClass;
	} else if (ns4) {
		whichEl = eval("document." + id);
		whichEl.className = newClass;
	} else {
		document.getElementById(id).className = newClass;
	}
}

function swapImage (id, newImage) {
	if (ie4) {
		id.src = newImage;
	} else if (ns4) {
		whichEl = eval("document." + id);
		whichEl.src = newImage;
	} else {
		document.getElementById(id).src = newImage;
	}
}

// Macromedia image manipulation utilities
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
	var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
	if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_swapImgRestore() { //v3.0
	var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
	var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
	if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
    for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

function MM_closeBrWindow(theURL,winName,features) { //v2.0
	window.close(theURL,winName,features);
}
/*	Macromedia function to open a popup window
*
*	PARAMETERS
*		theURL   = The URL we are opening in the popup window
*		winName  = Unique DOM name to give to this window
*		features = Features desired in the popup window, as outlined below (separated by commas)
*	
*		FEATURE NAME	TYPE		DESCRIPTION
*		directories		boolean		Controls the standard browser directory buttons 
*		height			numeric		Specifies the height of the window in pixels 
*		location		boolean		Controls the Location entry field 
*		menubar			boolean		Controls the menu at the top of the window 
*		resizable		boolean		Controls the ability to resize the window 
*		scrollbars		boolean		Controls the horizontal and vertical scrollbars
*		status 			boolean		Controls the status bar at the bottom of the window 
*		toolbar			boolean		Controls the standard browser toolbar 
*		width			numeric		Specifies the width of the window in pixels 
*
*	EXAMPLE
*		<a href="javascript:MM_openBrWindow('mypage.html', 'myPopupWindow', 'location=yes,menubar=yes,width=500,height=400')">...</a>
*/
function MM_openBrWindow(theURL,winName,features) { //v2.0
	window.open(theURL,winName,features);
}

// Cookie functions
function getexpirydate(nodays) {
	var UTCstring;
	Today = new Date();
	nomilli=Date.parse(Today);
	Today.setTime(nomilli+nodays*24*60*60*1000);
	UTCstring = Today.toUTCString();
	return UTCstring;
}

function getcookie(cookiename) {
	var cookiestring=""+document.cookie;
	var index1=cookiestring.indexOf(cookiename);
	if (index1==-1 || cookiename=="") return "";
	var index2=cookiestring.indexOf(';',index1);
	if (index2==-1) index2=cookiestring.length;
	return unescape(cookiestring.substring(index1+cookiename.length+1,index2));
}

function setexpiringcookie(name,value,domain,expires) {
	var expire = new Date();
	expire.setTime(expire.getTime() + 60000 * expires);
	cookiestring=name+"="+escape(value)+";domain="+escape(domain)+";expires="+expire.toGMTString() + ";path=/"; 
	document.cookie=cookiestring;
	if(!getcookie(name)){
		return false;
	} else {
		return true;
	}
}

function setcookie(name,value,domain) {
	cookiestring=name+"="+escape(value)+";domain="+escape(domain)+";path=/";
	document.cookie=cookiestring;
	if(!getcookie(name)){
		return false;
	} else{
		return true;
	}
}

// Location/host functions
function getHost() {
    hostname = new String(document.location);
    return cleanName(hostname);
}
			
function cleanName(hostname) {    
    var index = hostname.indexOf('http://');
    if(index != -1) {
		hostname = hostname.substring(7,hostname.length);
    } else {
		index = hostname.indexOf('https://');
		if(index != -1) {
		    hostname = hostname.substring(8,hostname.length);
		}
	}
	index = hostname.indexOf('/');
	if(index != -1) {
		hostname = hostname.substring(0,index);
	}			    
	    index = hostname.indexOf(':');
	if(index != -1) {
		hostname = hostname.substring(0,index);
	}
	return hostname;
}

function getReferrer() {
	return cleanName(document.referrer);
}

/*
* This function parses comma separated name=value 
* argument pairs from the query string of the URL. 
* It stores the name=value pairs in 
* properties of an object and then returns that object
* 
* Jim K - From Orielly JSB pp 244
*/
function getArgs() {
    var args = new Object();
    // Get Query String
    var query = location.search.substring(1); 
    // Split query at the comma
    var pairs = query.split("&"); 
    // Begin loop through the querystring
    for(var i = 0; i < pairs.length; i++) {
  	  // Look for "name=value"
  	  var pos = pairs[i].indexOf('='); 
  	  // if not found, skip to next
  	  if (pos == -1) continue; 
  	  // Extract the name
  	  var argname = pairs[i].substring(0,pos); 
  	  // Extract the value
  	  var value = pairs[i].substring(pos+1); 
  	  // Store as a property
  	  args[argname] = unescape(value); 
    }
    return args; // Return the Object
}

// restrict the number of characters allowed in a textarea
function maxlength_js(obj,maxlength) {
	//For Netscape 6 (not NS7) we have to disable this
    if (navigator.userAgent.indexOf('Netscape6') != -1) {
		return;
	}
    if (obj.value.length >= maxlength) {
		obj.value = obj.value.slice(0,maxlength);
    }
}


// gather information related to how they got in, like referrer and referral_id
host = getHost();
if(getcookie('referrer') == "") {
	ref = String(getReferrer());
	if (host != ref) {
		if (ref != "") {
			ref = 'http://'+ref;
			setcookie('referrer',ref,host);
		}
	}
}

if(getcookie('referer') == "") {
	ref = String(document.referrer);
	if (ref.indexOf(host) == -1) {
		if (ref != "") {
			setcookie('referer',ref,host);
		}
	}
}

if(getcookie('entry_page') == "") {
	ref = String(document.location);
	if (ref != "") {
		setcookie('entry_page',ref,host);
	}
}

var args = getArgs();

// We set the cookie using a wildcard.  i.e) if url is www.{website}.com host parameter will be .{website}.com.   That should take care of propogating the cookie when the secure domain is say secure.{website}.com
if (args.referral_id) {
    var subdomain = host;
    var index = subdomain.indexOf('www.');
    if (index != -1) {
		subdomain = subdomain.substring(3,subdomain.length);
	}
    //alert("setting cookie " + args.referral_id + " , host is: " + subdomain);
    
    var num_days = 30;
	setexpiringcookie('referral_id',args.referral_id,subdomain, 60 * 24 * num_days);
}

if (args.pls1source_id) {
    var num_days = 30;
	setexpiringcookie('referral_id',args.pls1source_id,host, 60 * 24 * num_days);
}

/* Backported from release_3_5_branch */
function hideShowCovered(el) {
	if (!is_ie && !is_opera)
		return;
	function getVisib(obj){
		var value = obj.style.visibility;
		if (!value) {
			if (document.defaultView && typeof (document.defaultView.getComputedStyle) == "function") { // Gecko, W3C
				if (!is_khtml)
					value = document.defaultView.
						getComputedStyle(obj, "").getPropertyValue("visibility");
				else
					value = '';
			} else if (obj.currentStyle) { // IE
				value = obj.currentStyle.visibility;
			} else
				value = '';
		}
		return value;
	};

	var tags = new Array("applet", "iframe", "select");

	var p = getAbsolutePos(el);
	var EX1 = p.x;
	var EX2 = el.offsetWidth + EX1;
	var EY1 = p.y;
	var EY2 = el.offsetHeight + EY1;

	for (var k = tags.length; k > 0; ) {
		var ar = document.getElementsByTagName(tags[ --k]);
		var cc = null;

		for (var i = ar.length; i > 0;) {
			cc = ar[ --i];

			p = getAbsolutePos(cc);
			var CX1 = p.x;
			var CX2 = cc.offsetWidth + CX1;
			var CY1 = p.y;
			var CY2 = cc.offsetHeight + CY1;

			if (el.hidden || (CX1 > EX2) || (CX2 < EX1) || (CY1 > EY2) || (CY2 < EY1)) {
				if (!cc.__msh_save_visibility) {
					cc.__msh_save_visibility = getVisib(cc);
				}
				cc.style.visibility = cc.__msh_save_visibility;
			} else {
				if (!cc.__msh_save_visibility) {
					cc.__msh_save_visibility = getVisib(cc);
				}
				cc.style.visibility = "hidden";
			}
		}
	}
};

function getAbsolutePos(el) {
	var SL = 0, ST = 0;
	var is_div = /^div$/i.test(el.tagName);
	if (is_div && el.scrollLeft)
		SL = el.scrollLeft;
	if (is_div && el.scrollTop)
		ST = el.scrollTop;
	var r = { x: el.offsetLeft - SL, y: el.offsetTop - ST };
	if (el.offsetParent) {
		var tmp = this.getAbsolutePos(el.offsetParent);
		r.x += tmp.x;
		r.y += tmp.y;
	}
	return r;
};

