//determines browser versions
bV = parseInt(navigator.appVersion);
ns4 = (document.layers);
ie4 = (document.all && !document.getElementById);
ie5 = (document.all && document.getElementById);
ns6 = (!document.all && document.getElementById);
opera = 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 (opera) {browser = 'opera';}
browser = browser + navigator.platform;

/*
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 
if state is not specified then the default is flip (2)
*/
function hideElement() {
	var  el = arguments[0];
	var state;
	if (arguments.length == 1) state = 2;
	else if (arguments.length > 1) state = arguments[1];
	if (ie4) {
		if (state == 1) el.style.display = "block";
		else if (state == 0) el.style.display ="none";
		else if (state == 3) el.style.visilility ="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";
		}

	} else if (ns4) {
		whichEl = eval("document." + el);
		if (state == 1) whichEl.visibility = "show";
		else if (state == 0) whichEl.visibility = "hide";
		else if (state == 2) {
			if (whichEl.visibility == "hide") whichEl.visibility = "show";
			else whichEl.visibility = "hide";
		}
	} else {
		if (state == 1) document.getElementById(el).style.display = "block";
		else if (state == 0) document.getElementById(el).style.display = "none";
		else if (state == 3) document.getElementById(el).style.visibility ="hidden";
		else if (state == 4) document.getElementById(el).style.visibility ="visible";
		else if (state == 2) {
			if (document.getElementById(el).style.display == "none" || document.getElementById(el).style.display == "") document.getElementById(el).style.display = "block";
			else document.getElementById(el).style.display = "none";

		}
		else if (state == 5) {
			if (document.getElementById(el).style.visibility == "hidden" || document.getElementById(el).style.visibility == "") document.getElementById(el).style.visibility = "visible";
			else document.getElementById(el).style.visibility = "hidden";

		}
	}
}

// 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)+";path=/";
	if(domain && domain != ''){
		cookiestring=cookiestring+";domain="+escape(domain);	
	}
	
	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();

if (args.referral_id) {
	setcookie('referral_id',args.referral_id,host);
}

if (args.pls1source_id) {
	setcookie('referral_id',args.pls1source_id,host);
}
	
