function getValue(n){//Accepts name of form input, returns the value of the first one found
  var v = document.getElementsByName(n);
  if(v[0]) v = v[0];//defines v as first appearence of n in html
  if(v.value) return v.value;
  else return '';//if n has no value return ''
}


var clientDelivery = getValue('clientdelivery');
var statusBar = document.getElementById('statusBarBG');
var target;
var e = 0;
var d = 0;
var questionLength;
var extLink = true;
var address;
var mode = getValue('mode');
var currentPoid = getValue('poid');
var clphone = getValue('clphone');
var cluserid = getValue('cluserid');
var campaignID = getValue('pvp_campaign');
var campaignIDInt = getValue('campaignIDInt');
var detail = getValue('detail_code') != '' ? getValue('detail_code') : 'PHXEDU07';
var psrc  = (getValue('psrc') != '' ? getValue('psrc') : 'home');
var source = getValue('channel') != '' ? getValue('channel') : 'INET';
var vrefid = getValue('vrefid');
var v1 = (getValue('v1') != '' ? getValue('v1') : 'rfi');
var v2 = (getValue('v2') != '' ? getValue('v2') : '');
var v3 = (getValue('v3') != '' ? getValue('v3') : '');
var promo = '';
var qComplete = false;
var bttn;

/* ---------- Form Elements -------------*/

var frm = document.frmOffers;
var form = {};
for(var x=0;x<frm.length;x++){
	if(frm[x].name.indexOf('|') != -1) var name = frm[x].name.substring(0,frm[x].name.indexOf('|'));
	else var name = frm[x].name;
	form[name] = frm[x];
	if(frm[x].type == 'radio')name += frm[x].value; // new line
}

/* ---------- End Form Elements -------------*/


/* ------------------ Replace Listed Phone Numbers with specific CLUser's phone number ------------------------*/
if(clphone != ''){
  if(document.getElementById('phoneNumber')) document.getElementById('phoneNumber').innerHTML = clphone;//Replaces Phonenumber at top right
  if(document.getElementById('copyPhone')) document.getElementById('copyPhone').innerHTML = clphone;//Replaces phone number in conf page text
   var strPhoenixPrepop = 'channel='+source+'&detail_code='+detail+'&pvp_campaign='+campaignID+'&pvp_campaign_int='+campaignIDInt+'&cluserid='+cluserid+'&clphone='+clphone;
    //var strPhoenixPrepop = "channel=" + getValue("channel") + "&detail_code=" + getValue("detail_code")+'&pvp_campaign='+getValue("pvp_campaign")+'&pvp_campaign_int='+getValue("pvp_campaign_int")+'&cluserid='+getValue("cluserid")+'&clphone='+getValue("clphone");
    var today = new Date();
    var cookie_expiration = new Date(today.getTime() + 1 * 24 * 60 * 60 * 1000); // 1 day
    //function setCookie( name, value, expires, path, domain, secure )
    setCookie('rf_prepop',strPhoenixPrepop,cookie_expiration,'/','.phoenix.edu');  
}
//Runs function to detirmine if click to chat link should be shown
//showClickToChat();

/* ------------------ End Replace Listed Phone Numbers with specific CLUser's phone number ------------------------*/


/* ------------ Prepop State from IP address --------------------------*/
// Check to see if the country credit menu exists 4 times per second and when it does, call the country prepop function
var t;
function kickoffCheckMenu() {
	t = setInterval(demographicsMenuExists,250);
}

kickoffCheckMenu ();


// Checks to see if the state select menu exists on the page and calls the prepop when it does
function demographicsMenuExists() {
	if (document.getElementById('state|24431_07417_4_96|105700|REQUIRED|ID') && document.getElementById('program_interest|24431_07417_4_96|105588|REQUIRED|ID')) {
		clearInterval(t);
		demographicsPrepopulate();
	}
}

// Calls the function to prepopulate the country based on the user IP address
function demographicsPrepopulate() {
  // Country prepopulation
  var populateHandlerRequest = newXMLHttpRequest();
  var populateHandlerFunction = callback(populateHandlerRequest, "TEXT", populateStateZipCallback); // Requesting text until the Content-Type of the response gets fixed.
  sendXMLHttpRequest(populateHandlerRequest, populateHandlerFunction, "/AptiNet/wslookup/ipaddr");
}

// Parse URL and divide at question mark
var urlParamsDivider = '?';
var urlParts = String(window.location).split('?');


// Cycle through all the name/value pairs into an array
var stateOnString = 'false';
var program;

if(urlParts[1]){
	var urlPairs = urlParts[1].split('&');

	//loop through all the name value pairs to find state and zip
	for (e=0; e<urlPairs.length; e++){			
		if (urlPairs[e].substring(0,5) == 'state'){
			var urlValue = urlPairs[e].split('=');
			if (urlValue[1] != ''){
				stateOnString = 'true';
			}
		}
		if (urlPairs[e].substring(0,11) == 'postal_code'){
			var zipOnString;
		}
	}		
}


// Populates the state and zip of user based on ip address if it is not an AOL proxy
function populateStateZipCallback(ipTextResponse) {
	/*console.log(ipTextResponse);  - To see what the web service is returning */
	if (stateOnString != 'true')  {
		if (ipTextResponse) {
			// Returns: IP_ADDRESS, COUNTRY, POSTAL, STATE, CITY, AREACODE, COUNTY, AOLPROXY
			// Check for AOL
			var blnAOL = ipTextResponse.substring(ipTextResponse.indexOf('<AOLPROXY>') + 10, ipTextResponse.indexOf('<AOLPROXY>') + 14);
			if ( blnAOL && blnAOL != 'TRUE' ){ /* if not an AOL proxy */
				// Populate State and Zip
				var strState = ipTextResponse.substring(ipTextResponse.indexOf('<STATE>') + 7,ipTextResponse.indexOf('</STATE>'));
				if (strState) {
					var objState = document.getElementById('state|24431_07417_4_96|105700|REQUIRED|ID');
					if (objState) {
						objState.options[r].selected = "SELECTED";
					}
				}

				var strZip = ipTextResponse.substring(ipTextResponse.indexOf('<POSTAL>') + 8,ipTextResponse.indexOf('</POSTAL>'));
				if (strZip && (strZip !== '-')) {
					var objZip = document.getElementById('postal_code|24431_07417_4_96|105702|REQUIRED|ID');
					if (objZip) {
						objZip.value = strZip;
					}
				}
			}
		}
	}
}

/* ------------ End Prepop State from IP address --------------------------*/
 
	
/* ------------------ Submit Rollover Functionality ------------------------*/
function addEvent(obj,evt,fn) {
  if (document.addEventListener) {
    addEvent = function (obj,evt,fn) {
      obj.addEventListener(evt,fn,false);
    }
  }
  else if (document.attachEvent) {
    addEvent = function (obj,evt,fn) {
      obj.attachEvent('on'+evt,fn);
    }
  }
  addEvent(obj,evt,fn);
}

      if(document.getElementById('stepSubmit')){
        var bttn = document.getElementById('stepSubmit');
        addEvent(bttn, 'mouseover', function(event){if(bttn.src.indexOf('_over.gif') == -1) bttn.src = bttn.src.slice(0,-4)+'_over.gif';});
        addEvent(bttn, 'mouseout', function(event){if(bttn.src.indexOf('_over.gif') != -1) bttn.src = bttn.src.slice(0,-9)+'.gif';});
        addEvent(bttn, 'mousedown', function(event){if(bttn.src.indexOf('_over.gif') != -1) bttn.src = bttn.src.slice(0,-9)+'.gif'; extLink = false;});
        addEvent(bttn, 'mouseup', function(event){if(bttn.src.indexOf('_over.gif') == -1) bttn.src = bttn.src.slice(0,-4)+'_over.gif';});
      };
/* ------------------ End Submit Rollover Functionality ------------------------*/


/* ------------------ Hints ------------------------*/

function addHint(element, text, cName) {//accepts element and postion, and classname, and true or false for highlight background
  //check if hint already exists, and replace hint
  if(element.nextSibling && element.nextSibling.className == cName){
    element.parentNode.removeChild(element.nextSibling);
    if(highlight == true) highlight = false;
  }
  var x = document.createElement('span');
  x.appendChild(document.createTextNode(text));
  if(cName) x.className = cName;
  else  x.className = 'hint';
  element.parentNode.appendChild(x);
}

function removeHint(element, cName) {//accepts element
  //check if hint already exists, and replace hint
  if(element.nextSibling && element.nextSibling.className == cName){
    element.parentNode.removeChild(element.nextSibling);
  }
}
if (pageNumber == 1){
	/* ------------------ Level of Education Space------------------------*/
	if(form.level_education){
		addHint(form.level_education, '', 'eduLvlHint');
	}
	/* ------------------ Zip Hint ------------------------*/
	if(form.postal_code){
		addHint(form.postal_code, 'Zip', 'zipHint');
	}
	/* ------------------ City Hint ------------------------*/
	if(form.city){
		addHint(form.city, 'City', 'cityHint');
	}
	/* ------------------ State Hint ------------------------*/
	if(form.state){
		addHint(form.state, 'State', 'stateHint');
	}
}

if (pageNumber == 2){
	/* ------------------ First Name Hint ------------------------*/
	if(form.first_name){
		addHint(form.first_name, 'First', 'firstNameHint', 'false');
	}
	/* ------------------ Last Name Hint ------------------------*/
	if(form.last_name){
		addHint(form.last_name, 'Last', 'lastNameHint', 'false');
	}
	
	/* ------------- Display City, State, and Zip under Address as a hint --------------*/
	if(form.address){
		addHint(form.address, form.city.value +', '+form.state.value+', '+form.postal_code.value, 'cityStateZipHint', 'false');
	}
}
/* ------------------ End Hints ------------------------*/

