var x;
var layers = new Array();
var selected_id = null; 
var dom = document.getElementById; 
function swapIt(id, newclass) { 
var y;
	if (dom) { 
		if(selected_id != id) { 
			for (x in layers) {
				el = document.getElementById(layers[x]); 
				switch (el.className) {
					case "nav_on":
						el.className = 'nav_off'; 
						y = 'lyr_' + layers[x]; 
						document.getElementById(y).style.display = 'none';
						break;
					case "nav_indent_on":
						el.className = 'nav_indent_off'; 
						y = 'lyr_' + layers[x];
						document.getElementById(y).style.display = 'none';
						break;
				}
				
			}
			el = document.getElementById(id); 
			el.className = newclass; 
			y = 'lyr_' + id; 
			document.getElementById(y).style.display = 'block';
			el.style.cursor = "hand"; 
			el.style.cursor = "pointer"; 
		} 
	} 
} 

function SessionCookie () {
	document.cookie ="SessionCookie=Enabled";
	if (getCookieValue ("SessionCookie")=="Enabled")
		return true 
	else
		return false;
}

function writeSessionCookie (cookie, value) {
	if (SessionCookie()) {
		document.cookie = escape(cookie) + "=" + escape(value) + "; path=/";
		return true;
	}
		else return false;
}

function getCookieValue (cookie) {
var exp = new RegExp (escape(cookie) + "=([^;]+)");
	if (exp.test (document.cookie + ";")) {
		exp.exec (document.cookie + ";");
		return unescape(RegExp.$1);
	}
		else return false;
}

function PersistentCookie () {
	writePersistentCookie ("PersistentCookie", "Enabled", "minutes", 1);
	if (getCookieValue ("PersistentCookie")=="Enabled")
		return true  
	else 
		return false;
}

function writePersistentCookie (cookie, value, period, offset) {
var expire = new Date ();
	
	offset = offset / 1;
	switch (period.toLowerCase()) {
		case "minutes":
			expire.setMinutes(expire.getMinutes()+offset);
			break;
		case "hours":
			expire.setHours(expire.getHours()+offset);
			break;
		case "days":
			expire.setDate(expire.getDate()+offset);
			break;
		case "months":
			expire.setMonth(expire.getMonth()+offset);
			break;
		case "years":
			expire.setYear(expire.getFullYear()+offset);
			break;
	} 
	document.cookie = escape(cookie ) + "=" + escape(value) + "; expires=" + expire.toGMTString() + "; path=/";
}  

function deleteCookie (cookie) {
	if (getCookieValue (cookie)) 
		writePersistentCookie (cookie,"TBD","years", -1);  
	return true;     
}

function getQueryVariable(frURL) {
var querystr = window.location.search.substring(1);
var vars = querystr.split("&");
	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if (pair[0] == frURL) {
			return pair[1];
		}
	}
}

function affiliateIDCheck() {
var kbid;
	kbid = getQueryVariable("kbid");
	
	if(kbid == void(0)){
		kbidParam = getCookieValue ('kbid_param');
		if(kbidParam){
			kbid = kbidParam;
		}
	}
	
	if (kbid != "") {
		writeSessionCookie('kbid',kbid);
	}
}

function setDefaultLyr() {
var len;
var lyr;
	lyr = getQueryVariable("lyr");
	len = layers.length;
	for (i=0; i<len; i++) {
		if (lyr == layers[i]) {
			swapIt(lyr,'nav_on');
			break;
		}
	}
}

function init(formID) {
	affiliateIDCheck();
	setDefaultLyr();
	if (formID != "") {
		document.getElementById(formID).reset();
	}
}

var win=null;
function makeWin (mypage,myname,w,h,scroll,pos) {
    if(pos=="random") {
		LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;
		TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;
	}
	if(pos=="center") {
		LeftPosition=(screen.width)?(screen.width-w)/2:100;
		TopPosition=(screen.height)?(screen.height-h)/2:100;
	} else
		if ((pos!="center" && pos!="random") || pos==null) {
			LeftPosition=0;TopPosition=20
		}
	settings = 'width=' + w + ',height=' + h + ',top=' + TopPosition + ',left=' + LeftPosition + ',scrollbars=' + scroll + ',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';
	win=window.open(mypage,myname,settings);
	win.focus();
}


// added by justin 02012007
setTrackingCookie();
function setTrackingCookie(){
  var id = getQueryStringValue(window.location.search.toLowerCase(), "p");
  var kbid = getQueryStringValue(window.location.search.toLowerCase(), "kbid");

  // only set the track cookie if we've been given a 'p' or a 'kbid'.
  // if we do get a 'p' or a 'kbid' value, overwrite the previous resellid.
  // if we get both 'p' and 'kbid' values, use the 'kbid' value.

  if(kbid){
  	id = kbid;
  } else {
  	var kbidParamCookie = getCookieValue ('kbid_param');
		if( kbidParamCookie){
			id = kbidParamCookie;
		}
	}
  
  if(id) {
    setTrackingCookieFromId(id);
  }
}

function setTrackingCookieFromId(id) {
	var expireDays = 30;
	var expireStamp = new Date(new Date().getTime() + expireDays * 24 * 3600 * 1000);
	var todaysDate = new Date();
	var cookieString = "";
	
	var dateString = "";
	var ampm = "AM";
	if (todaysDate.getHours > 11) ampm = "PM"; 

	var halfhours = todaysDate.getHours() % 12;
	dateString += addZero(todaysDate.getDate()) + "/"
		+ addZero(todaysDate.getMonth()) + "/"
		+ todaysDate.getFullYear() + " "
		+ addZero(halfhours) + ":"
		+ addZero(todaysDate.getMinutes()) + ":"
		+ addZero(todaysDate.getSeconds()) + " "
		+ ampm;
	
	// find the second to last '.' character and set that as the cookie domain
	var hostmatcharray = window.location.hostname.match(/(\.\w*){2}$/);
	var cookiedomain = hostmatcharray[0];
	cookieString += "reselltype=P&reselldate=" + escape(dateString);
	
	// only add the referrer, id and subid if they exist on the URL
	if (document.referrer) {
		cookieString += "&referrer=" + escape(document.referrer);
	}
	cookieString += "&resellid=" + id;
	
	var subid = getQueryStringValue(window.location.search.toLowerCase(), "subid");
	if (subid) {
		cookieString += "&subid=" + subid;
	}
	document.cookie = "track=" + cookieString + "; expires=" + expireStamp + "; path=/; domain=" + cookiedomain;
}

function addZero(number){ 
  return ((number < 10) ? "0" : "") + number 
}

function getQueryStringValue(sourceUrl, key) {
  var u = sourceUrl;
  var parmValue = "";
  u = u.slice(1);
  parms = u.split("&");
  var parmArray = [];
  for ( var i = 0; i < parms.length; i++){
    parmArray.push(parms[i].split("="));
  }
  for (var i=0;i<parmArray.length; i++) {
    if (parmArray[i][0] == key) {
      parmValue = parmArray[i][1];
    }
  }
  return parmValue;
}


function urlToHttps(passedURL){
  return passedURL.replace(/http\:/i,"https:");
}

function llcOverideFormSubmit(passedForm){
	var state = document.getElementById('incstate').value;
	if(state == 'California') {
		passedForm.action = "https://" + window.location.hostname + "/commerce/order/interview.jsf?entityType=LLC&stateName=California";
		// window.alert("In California LLC special case. action: " + passedForm.action);
} else {
		passedForm.action = urlToHttps(passedForm.action);
		// window.alert("In Standard LLC case. action: " + passedForm.action);
	}
	passedForm.submit();
}

function returnObjById(id) {
	if (document.getElementById)
        return document.getElementById(id);
    else if (document.all)
        return document.all[id];
    else if (document.layers)
        return document.layers[id];
	else
		return null;
}

//temporary fix for background image problem with IE.  this only works for IE 32-bit, so every other browser will throw an exception
try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}
