function ListFind(string,searchvalue) {
 if (arguments[2]) {
  delimiter = arguments[2];
 } else {
  delimiter = ",";
 }
 list = string.split(delimiter);
 for (x = 0; x < list.length; x++) {
  if (list[x] == searchvalue) {
   return x + 1;
  }
 }
 return 0;
}

function stripAlphaChars(pstrSource) { 
	var m_strOut = new String(pstrSource); 
    m_strOut = m_strOut.replace(/[^0-9]/g, ''); 

    return m_strOut; 
}


function emailCheck (emailStr) {
	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);
	if (matchArray==null) { return false; }
	var user=matchArray[1];
	var domain=matchArray[2];
	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) { return false; }
	}
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) { return false; }
	}
	if (user.match(userPat)==null) { return false; }
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) { return false;  }
		}
		return true;
	}
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {
		if (domArr[i].search(atomPat)==-1) { return false;  }
	}
	if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) { return false; }
	if (len<2) { return false; }
	return true;
}

fnValidateLeadForm_March2010 = function(primaryForm){
	// note: this is a different version of the validator scripts to accommodate a single-form
	// lead form as designed by A. Duquette and incorporated in March, 2010. All fields are required.
	var flagError = false;
	var msg = '';
	var focusElement = '';
	var isChecked = false;
	var flagRequiredAddress = false;
	
	if (typeof(primaryForm.FirstName) != "undefined" && ListFind(primaryForm.listRequiredFields.value, 'FirstName') > 0) { 
		//First Name check
		if (primaryForm.FirstName.value == '') {
			flagError = true;
			msg = msg + 'Please enter your first name.\n';
			if (focusElement.length == 0) { focusElement = 'FirstName'; }
		}
	}

	if (typeof(primaryForm.LastName) != "undefined" && ListFind(primaryForm.listRequiredFields.value, 'LastName') > 0) { 
		//Last Name check
		if (primaryForm.LastName.value == '') {
			flagError = true;
			msg = msg + 'Please enter your last name.\n';
			if (focusElement.length == 0) { focusElement = 'LastName'; }
		}
		// stuff the Name field...
		primaryForm.Name.value = primaryForm.FirstName.value + ' ' + primaryForm.LastName.value;
	}

	if (typeof(primaryForm.SchoolName) != "undefined" && ListFind(primaryForm.listRequiredFields.value, 'SchoolName') > 0) { 
		//School Name check
		if (primaryForm.SchoolName.value == '') {
			flagError = true;
			msg = msg + 'Please enter your school.\n';
			if (focusElement.length == 0) { focusElement = 'SchoolName'; }
		}
	}

	if (typeof(primaryForm.City) != "undefined" && ListFind(primaryForm.listRequiredFields.value, 'City') > 0) { 
		//City check
		if (primaryForm.City.value == '') {
			flagError = true;
			msg = msg + 'Please enter your city.\n';
			if (focusElement.length == 0) { focusElement = 'City'; }
		}
	}

	if (typeof(primaryForm.State) != "undefined" && ListFind(primaryForm.listRequiredFields.value, 'State') > 0) { 
		// State Check
		if (primaryForm.State.selectedIndex < 0 || primaryForm.State.value.length == 0) {
			flagError = true;
			if (typeof(primaryForm.Country) != "undefined" && primaryForm.Country.value == 'CA') { 
				msg = msg + 'Please select a province.\n';
			} else {
				msg = msg + 'Please select a state.\n';
			}
			if (focusElement.length == 0) { focusElement = 'State'; }
		}
	}

	if (typeof(primaryForm.Email) != "undefined" && ListFind(primaryForm.listRequiredFields.value, 'Email') > 0) { 
		//Email check
		if ((primaryForm.Email.value == '') || (emailCheck(primaryForm.Email.value) == false)) {
			flagError = true;
			msg = msg + 'Please enter a valid email address.\n';
			if (focusElement.length == 0) { focusElement = 'Email'; }
		} else {
			if (typeof(primaryForm.ConfirmEmail) != "undefined" && ListFind(primaryForm.listRequiredFields.value, 'ConfirmEmail') > 0) { 
				//Email/ConfirmEmail check
				if (primaryForm.Email.value != primaryForm.ConfirmEmail.value) {
					flagError = true;
					msg = msg + 'Please enter confirm your email address.\n';
					if (focusElement.length == 0) { focusElement = 'ConfirmEmail'; }
				}
			}
		}
	}

//	if (typeof(primaryForm.ZipCode) != "undefined" && ListFind(primaryForm.listRequiredFields.value, 'ZipCode') > 0) { 
//		//Zip Code Check
//		if (primaryForm.ZipCode.value.length < 5) {
//			flagError = true;
//			msg = msg + 'Please enter your zip code.\n';
//			if (focusElement.length == 0) { focusElement = 'ZipCode'; }
//		}
//	}

	if (typeof(primaryForm.Phone) != "undefined" && ListFind(primaryForm.listRequiredFields.value, 'Phone') > 0) { 
		//Phone check
		tempPhone = stripAlphaChars(primaryForm.Phone.value);
		if (((primaryForm.Phone.value == '') || (tempPhone.length < 10)) && (primaryForm.organization_id.value == 0)) {
			flagError = true;
			msg = msg + 'Please enter your phone.\n';
			if (focusElement.length == 0) { focusElement = 'Phone'; }
		}
	}

	if (typeof(primaryForm.Role) != "undefined" && ListFind(primaryForm.listRequiredFields.value, 'Role') > 0) { 
		//Role check
		isChecked = false;		
		if (primaryForm.Role.length > 1) {
			// multiple radios
			for (x = 0; x < primaryForm.Role.length; x++) {
				if (primaryForm.Role[x].checked == true) {
					isChecked = true;
					break;
				}
			}
		} else {
			// single radio
			if (primaryForm.Role.checked == true) {
				isChecked = true;
			}
		}
		if (isChecked == false) {
			flagError = true;
			msg = msg + 'Please select a role.\n';
			if (focusElement.length == 0) { 
				if (primaryForm.Role.length > 1) {
					focusElement = 'Phone'; 
				} else {
					focusElement = 'Role'; 
				}
			}
		}
	}
	
	if (typeof(primaryForm.HowDidYouHear) != "undefined" && ListFind(primaryForm.listRequiredFields.value, 'HowDidYouHear') > 0) { 
		//HowDidYouHear check
		if (primaryForm.HowDidYouHear.selectedIndex < 1) {
			flagError = true;
			msg = msg + 'Please select a how you heard about Study Island.\n';
			if (focusElement.length == 0) { focusElement = 'HowDidYouHear'; }
		}
	}

	if (typeof(primaryForm.leadNote) != "undefined" && ListFind(primaryForm.listRequiredFields.value, 'leadNote') > 0) { 
		//leadNote check
		if (primaryForm.leadNote.value == '') {
			flagError = true;
			msg = msg + 'Please enter a message.\n';
			if (focusElement.length == 0) { focusElement = 'leadNote'; }
		}
	}

	if (flagError == true) {
		// errors -- alert & focus.
		alert(msg);
		try {
			document.getElementById(focusElement).focus();
		} catch(e) { }
		return false;
	} else {
		// no errors, store the lead
		
		// funky stuff...
		if (primaryForm.SchoolName.value != primaryForm.SchoolHidden.value) {
			// school is not matched
			primaryForm.organization_id.value = 0;
		}
		
		try {
			newID = ColdFusion.Ajax.submitForm(primaryForm.thisPrimaryFormName.value, '/cfc/sales/leads.cfc?method=fnInitLeadRecord_201003&returnFormat=json', callback, errorHandler, 'POST', false);
//			alert(newID);

//			// run Level 2 SEO exchange
//			if (typeof(primaryForm.convNameL2) != "undefined") { 
//				fnPostLevel2Data(primaryForm.convNameL2.value, primaryForm.FirstName.value + ' ' + primaryForm.LastName.value, primaryForm.Email.value, primaryForm.remoteIP.value, primaryForm.State[primaryForm.State.selectedIndex].value);
//			}
//			
//			// now, kick 'em to the return url...
//			location.href = primaryForm.returnURL.value;

			primaryForm.submit();
			return false;

		} catch(err) {
			 // alert('oh snap! the cfc failed to run: ' + err.description);
		}
			
		return false;
	}
}

function callback(response) {
	var result = ColdFusion.JSON.decode(response);
//	alert(result);
}

function errorHandler(code, msg) {
//	alert("Error!!! " + code + ": " + msg);
}

// bind error suppression
suppressBindError = function(c, m) { m = ''; }

// Level 2 SEO
var DID = '';

if (location.hostname.search(/studyisland.com/i) > -1) {
	DID = 41532; // studyisland main
} else {
	if (location.hostname.search(/studyisland.ca/i) > -1) {
		DID = 61865; // studyisland canada
	} else {
		if (location.hostname.search(/northstar/i) > -1) {
			DID = 55891; // northstar
		} else {
			DID = 41532; // studyisland main
		}
	}
}
 
var ConvName		= '';
var ConvDesc		= '';
var ConvSubTotal	= '';
var ConvTax			= '';
var ConvTotal		= '';
var ConvMisc1		= '';
var ConvMisc2		= '';
var ConvMisc3		= '';
var ConvMisc4		= '';
var ConvMisc5		= '';

fnPostLevel2Data = function(pConvName, pConvMisc1, pConvMisc2, pConvMisc3, pConvMisc5) {



	window.ConvName			= pConvName;
	window.ConvMisc1		= pConvMisc1;
	window.ConvMisc2		= pConvMisc2;
	window.ConvMisc3		= pConvMisc3;
	window.ConvMisc5		= pConvMisc5;

	pcheck=(window.location.protocol == "https:") ? "https://stats.sa-as.com/conversion.js":"http://stats.sa-as.com/conversion.js";

	headID = document.getElementsByTagName("head")[0];         
	newScript = document.createElement('script');
	newScript.setAttribute("type","text/javascript")
	newScript.setAttribute("src", pcheck)
	headID.appendChild(newScript);
} 