window.onload = initAll;

function initAll() {
	prePopulate();
	checkSched();
}

//------------------------------------------------------


function checkSched()
{
	var text = location.search;
	thefirstleft = text.indexOf("sched=");
	if (thefirstleft != -1)
	{
		theleft = thefirstleft + 6;
		theright = theleft + 4;
		thevalue = (text.substring(theleft, theright));
		if (thevalue == "true")
		{
			document.getElementById('CheckBoxScheduled').checked=true;
			//change page title
			document.title = "Request Your Visit";
		}
	}
}

function prePopulate() {
	var firstNameQ = displayItem("data_firstName");
	var lastNameQ = displayItem("data_lastName");
	var addressQ = displayItem("data_address");
	var cityQ = displayItem("data_city");
	var stateQ = displayItem("data_state");
	var zipQ = displayItem("data_zip");
	var yourEmailQ = displayItem("data_email");
	var downloadQ = displayItem("data_download");

	
	if (firstNameQ !=null && firstNameQ !="") {
		document.getElementById("TextBoxFirstName").value = firstNameQ;
	}
	if (lastNameQ !=null && lastNameQ !="") {
		document.getElementById("TextBoxLastName").value = lastNameQ;
	}
	if (addressQ !=null && addressQ !="") {
		document.getElementById("TextBoxPrimaryAddress").value = addressQ;
	}
	if (cityQ !=null && cityQ !="") {
		document.getElementById("TextBoxCity").value = cityQ;
	}
	if (stateQ !=null && stateQ !="") {
		setStateDropdown(stateQ);
		//document.getElementById("TextBoxFirstName").text = stateQ;
	}
	if (zipQ !=null && zipQ !="") {
		document.getElementById("TextBoxZipcode").value = zipQ;
	}
	if (yourEmailQ !=null && yourEmailQ !="") {
		document.getElementById("TextBoxEmail").value = yourEmailQ;
		//document.getElementById("TextBoxEmailConfirmation").value = yourEmailQ;
	}
	if (downloadQ !=null && downloadQ !="") {
		window.location.href = downloadQ;	}
}

function PageQuery(q) {
	if(q.length > 1) this.q = q.substring(1, q.length);
	else this.q = null;
	this.keyValuePairs = new Array();
	if(q) {
		for(var i=0; i < this.q.split("&").length; i++) {
		this.keyValuePairs[i] = this.q.split("&")[i];
		}
	}
	this.getKeyValuePairs = function() { return this.keyValuePairs; }
	this.getValue = function(s) {
		for(var j=0; j < this.keyValuePairs.length; j++) {
			if(this.keyValuePairs[j].split("=")[0] == s)
				return this.keyValuePairs[j].split("=")[1];
		}
		return false;
	}
	this.getParameters = function() {
		var a = new Array(this.getLength());
		for(var j=0; j < this.keyValuePairs.length; j++) {
			a[j] = this.keyValuePairs[j].split("=")[0];
		}
		return a;
	}
	this.getLength = function() { return this.keyValuePairs.length; } 
}
function queryString(key){
	var page = new PageQuery(window.location.search); 
	return unescape(page.getValue(key)); 
}
function displayItem(key){
	if(queryString(key)=='false') 
	{
		return false;
	}else{
		return queryString(key);
	}
}

//------------------------------------------------------

function setStateDropdown (str)
//this function is called from the changeLink function below.
//changes the 'community i'm interested in' dropdown in the kit request form to match the referring community
{
	xState=document.getElementById("DropDownListState").options;
	for (x = 0; x < xState.length -1; x++)
	//loops through the values until it finds the one matching the community code stored in the refCommunity variable.
	{ 	
		xVal = xState[x].value;
		
		if (xVal == str)
		//standard function - no filters
		{
			xState[x].selected = true;
			break;
		}
		else if (str == "YU")
		//filter for last state in dropdown list
		{
			xState[69].selected = true;
			break;
		}
	}


}

var theOC;

function delineateUrl(str)
{
	thefirstleft = str.indexOf("pageName=");
	theleft = thefirstleft + 9;
	if (thefirstleft != -1) {
		therest = str.length;
		firstSlice = str.substring(theleft,therest);
		theright = firstSlice.indexOf("&");
		if (theright == -1) {
			theright = firstSlice.length;

		}
		theReturn = (firstSlice.substring(0, theright));
		
		if (theReturn.search(/%3a/))
			{
				theReturn = decode(theReturn);
			}
		theOC = false;
	} else {
	//	theReturn = "/" + delineate(text);
		theReturn = "/" + delineatePrCampus(location.search);
		if (theReturn == "/") {
			theOC = false;
		} else {
			theOC = true;
		}
	}
	return theReturn;
}

function delineatePrCampus(str)
{
	thefirstleft = str.indexOf("PRIMARYCAMPUSID=");
	if (thefirstleft != -1) {
		theleft = thefirstleft + 16;
		theright = theleft + 3;
		theReturn = (str.substring(theleft, theright));
		theReturn = theReturn.toLowerCase();
		switch (theReturn)
		  {
			case "lpv": { theReturn = "lph"; break }
			case "opk": { theReturn = "tck"; break }
			case "etv": { theReturn = "eth"; break }
			default: { //nothing
						break }
		  }
	} else {
		theReturn = "";
	}

return theReturn
}

function OCcheck() {
	return theOC;
}