
var f;
var e;
function valRegex(fn, r, err) {
	var s = '' + e[fn].value;
	var v = (s.search(r) == 0);
	return (v ? '' : err);
}
function valSelect(fn, err) {
	return (e[fn].selectedIndex == 0 ? err : '');
}
function valRadio(fn, err) {
	var rad = false;
	for (i = 0; i < e[fn].length; i++) {
		if(e[fn][i].checked) {
			rad = true;
		}
	}
	return (!rad ? err : '');
}
function putinvisitorid() {
	e["visitorID"].value = visitorID;
}
function putincid() {
	e["campaignID"].value = cidCode;
}

function changeField(){
 f = document.forms["registration"];
  if(f.country.value=="USA"  || document.forms["registration"].country.value=="Canada"){
      //f.province.value ="";
      //f.province.disabled =true;
	  f.area.disabled =false;
	}
   else{
     f.area.selectedIndex =0;
     f.area.disabled =true;
	 //f.province.disabled =false;
	  }
}

function validate() {
	f = document.forms["registration"];
	e = f.elements;
	var allerrs = '';
	allerrs += valRegex("first_name", /.*[a-zA-Z].*/, "* First Name\n");
	allerrs += valRegex("last_name", /.*[a-zA-Z].*[a-zA-Z].*/, "* Last Name\n");
	allerrs += valSelect("SRE032", "* Title\n");
	allerrs += valRegex("company", /.*[a-zA-Z].*/, "* Company\n");
	allerrs += valRegex("address1", /.*[a-zA-Z0-9].*/, "* Address 1\n");
	allerrs += valRegex("city", /.*[a-zA-Z].*/, "* City\n");
	//allerrs += valRegex("state", /.*[a-zA-Z0-9].*/, "* state/province\n");
	if (f.country.value=="USA" || f.country.value=="Canada") {
		allerrs += valRegex("area", /.*[a-zA-Z].*/, "* State/Province\n");
	} else {
		//allerrs += valRegex("zip", /.+/, "* Postal Code\n");
	}
	allerrs += valRegex("zip", /.*[a-zA-Z0-9].*/, "* Postal Code\n");
	allerrs += valSelect("country", "* Select Country\n");
	allerrs += valRegex("email", /[^\s@;!,]+@([a-zA-Z0-9\-]+\.)+[a-zA-Z0-9\-]{2,}/, "* Email\n");
	allerrs += valRegex("phone", /.*[0-9].*/, "* Phone\n");
	allerrs += valSelect("SRE030", "* What is your functional area?\n");
	allerrs += valSelect("SRE014", "* How many employees are at your location?\n");
	allerrs += valSelect("SRE077", "* How close is your company to buying technology in this area?\n");
	allerrs += valSelect("SRE033", "* What is your relationship to Avaya?\n");
	//allerrs += valSelect("SRE015", "* How many employees are in your company?\n");
	//allerrs += valRadio("SRE033","* What is your relationship to Avaya?\n");
	allerrs += valRadio("SRE003", "* Permission to phone\n");
	allerrs += valRadio("SRE001", "* Permission to email\n");
	allerrs += valRadio("SRE002", "* Permission to mail\n");
	putinvisitorid();
	putincid();
	/*if (!(allerrs == '')) {
		alert('Please check the following field(s): \n' + allerrs);
	} else {
		f.submit();
	}*/
	if (!(allerrs == '')) 	{ 
	s_vp_formError('Registration Form','Avaya_Inst_Bas_EN_Reg', allerrs);
	alert('Please check the following field(s): \n' + allerrs);
	}
	else { 
	s_vp_formSuccess('Registration Form','Avaya_Inst_Bas_EN_Reg');
	sendSubmitEvent(); } //Send submission vars to omniture
	
	return ((allerrs == '') ? true : false); 
}

function sendSubmitEvent(){
   s_linkType="o";
   s_linkName="Form Submission";
   s_events="event3";
   s_lnk=s_co(this);
   s_gs("avayacom");
}

function s_split(str,sep)
	{var si=0,sa=new Array(),i;while((str.length>0)&&(sep.length>0)){
	 i=str.indexOf(sep);if((!i)&&(sep!=str.substring(0,sep.length)))
	 break;if(i==-1){sa[si++] = str;break;}sa[si++]=str.substring(0,i);
	 str=str.substring(i+sep.length,str.length)}return sa}
		
		
function getQueryVariable(variable) {
	var query = window.location.search.substring(1);
	var vars = query.split("&");
		for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) {
			return pair[1];
		}
	}
}
		
var cidCode = getQueryVariable("cid");
var contsysid = getQueryVariable("contsysid");
var prepop = getQueryVariable("prepop");
