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 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;
}

function fnValidateLeadForm(primaryForm, secondaryForm, primaryFormName, secondaryFormName) {
	var flagError = false;
	var msg = '';
	var focusElement = '';
	
	// if someone was brilliant enough to pass along a form element named "id" or "name", we need to scarf
	// out the *ID* of the *FORMS* being passed to this function.	
	// note: if there are any other forms than lead forms on the page, do NOT name them "frm##"!	
//	aForms = document.getElementsByTagName('FORM');
//	primaryFormName = '';
//	secondaryFormName = '';
//	for (i = 0; i < aForms.length; i++) {
//		if (aForms[i].name.substring(0, 3) == 'frm') {
//			if (primaryFormName.length == 0) {
//				primaryFormName = aForms[i].name;
//			} else {
//				secondaryFormName = aForms[i].name;
//				break;
//			}
//		}
//	}

	primaryFormName = primaryForm.thisPrimaryFormName.value;
	secondaryFormName = primaryForm.thisSecondaryFormName.value;
	
	// ok, now that we know the form names that we're dealing with, let's do the form validation
	if (typeof(primaryForm.Name) != "undefined" && ListFind(primaryForm.listRequiredFields.value, 'Name') > 0) { 
		//Full Name check
		if (primaryForm.Name.value == '') {
			flagError = true;
			msg = msg + 'Please enter your name.\n';
			if (focusElement.length == 0) { focusElement = 'Name'; }
		}
	}
	
	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'; }
		}
	}
	
	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'; }
		}
	}

	if (typeof(primaryForm.Phone) != "undefined" && ListFind(primaryForm.listRequiredFields.value, 'Phone') > 0) { 
		//Phone check
		if ((primaryForm.Phone.value == '') || (primaryForm.Phone.value.length < 3)) {
			flagError = true;
			msg = msg + 'Please enter your phone.\n';
			if (focusElement.length == 0) { focusElement = 'Phone'; }
		}
	}

	
	thisState = primaryForm[primaryForm.name + "_State"];
	if (typeof(thisState) != "undefined" && ListFind(primaryForm.listRequiredFields.value, primaryForm.name + '_State') > 0) { 
		// State Check
		if (thisState.selectedIndex < 0 || thisState.value.length == 0) {
			flagError = true;
			if (thisState[0].text.search(/State/i) > 0) {
				msg = msg + 'Please select a state.\n';
			} else if (thisState.text.search(/Province/i) > 0) {
				msg = msg + 'Please select a province.\n';
			} 
			if (focusElement.length == 0) { focusElement = primaryForm.name + '_State'; }
		}
	}

//	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 (primaryForm.State[0].text.search(/State/i) > 0) {
//				msg = msg + 'Please select a state.\n';
//			} else if (primaryForm.State[0].text.search(/Province/i) > 0) {
//				msg = msg + 'Please select a province.\n';
//			} 
//			if (focusElement.length == 0) { focusElement = 'State'; }
//		}
//	}
	
	if (typeof(primaryForm.AreaCode) != "undefined" && ListFind(primaryForm.listRequiredFields.value, 'AreaCode') > 0) { 
		//Area Code check
		if (primaryForm.AreaCode.value == '') {
			flagError = true;
			msg = msg + 'Please select an area code.\n';
			if (focusElement.length == 0) { focusElement = 'AreaCode'; }
		}
	}
	

	if (typeof(primaryForm.DistrictID) != "undefined" && ListFind(primaryForm.listRequiredFields.value, 'DistrictID') > 0) { 
		//District check
		if (primaryForm.DistrictID.value == '') {
			flagError = true;
			msg = msg + 'Please select a school district.\n';
			if (focusElement.length == 0) { focusElement = 'DistrictID'; }
			if (typeof(primaryForm.DistrictName) != "undefined") { 
				primaryForm.DistrictName.value = 'unknown';
			}
		} else {
			if (typeof(primaryForm.DistrictName) != "undefined") { 
				primaryForm.DistrictName.value = primaryForm.DistrictID.options[primaryForm.DistrictID.selectedIndex].text;
			}
		}
	}

	if (typeof(primaryForm.SchoolID) != "undefined" && ListFind(primaryForm.listRequiredFields.value, 'SchoolID') > 0) { 
		//School check
		if (primaryForm.SchoolID.value == '') {
			flagError = true;
			msg = msg + 'Please select a school.\n';
			if (focusElement.length == 0) { focusElement = 'SchoolID'; }
			if (typeof(primaryForm.SchoolName) != "undefined") { 
				primaryForm.SchoolName.value = 'unknown';
			}
		} else {
			if (typeof(primaryForm.SchoolName) != "undefined") { 
				primaryForm.SchoolName.value = primaryForm.SchoolID.options[primaryForm.SchoolID.selectedIndex].text;
			}
		}
	}

	if (typeof(primaryForm.Comments) != "undefined" && ListFind(primaryForm.listRequiredFields.value, 'Comments') > 0) { 
		//Comments check
		if (primaryForm.Comments.value == '') {
			flagError = true;
			msg = msg + 'Please enter your Questions or Comments.\n';
			if (focusElement.length == 0) { focusElement = 'Comments'; }
		}
	}
	
	
	if (typeof(primaryForm.yourName) != "undefined" && ListFind(primaryForm.listRequiredFields.value, 'yourName') > 0) { 
		//yourName check
		if (primaryForm.yourName.value == '') {
			flagError = true;
			msg = msg + 'Please enter your name.\n';
			if (focusElement.length == 0) { focusElement = 'yourName'; }
		}
	}
	
	if (typeof(primaryForm.yourEmail) != "undefined" && ListFind(primaryForm.listRequiredFields.value, 'yourEmail') > 0) { 
		//yourEmail check
		if ((primaryForm.yourEmail.value == '') || (emailCheck(primaryForm.yourEmail.value) == false)) {
			flagError = true;
			msg = msg + 'Please enter a valid email address for your email address.\n';
			if (focusElement.length == 0) { focusElement = 'yourEmail'; }
		}
	}
	
	if (typeof(primaryForm.friendName) != "undefined" && ListFind(primaryForm.listRequiredFields.value, 'friendName') > 0) { 
		//friendName check
		if (primaryForm.friendName.value == '') {
			flagError = true;
			msg = msg + 'Please enter your friend\'s name.\n';
			if (focusElement.length == 0) { focusElement = 'friendName'; }
		}
	}
	
	if (typeof(primaryForm.friendEmail) != "undefined" && ListFind(primaryForm.listRequiredFields.value, 'friendEmail') > 0) { 
		//friendEmail check
		if ((primaryForm.friendEmail.value == '') || (emailCheck(primaryForm.friendEmail.value) == false)) {
			flagError = true;
			msg = msg + 'Please enter a valid email address for your friend\'s email address.\n';
			if (focusElement.length == 0) { focusElement = 'friendEmail'; }
		}
	}
	
	if (typeof(primaryForm.ContactMethodPreferrence) != "undefined" && ListFind(primaryForm.listRequiredFields.value, 'ContactMethodPreferrence') > 0) { 
		//ContactMethodPreferrence check
		if (primaryForm.ContactMethodPreferrence.selectedIndex < 0) {
			flagError = true;
			msg = msg + 'Please select a contact method.\n';
			if (focusElement.length == 0) { focusElement = 'ContactMethodPreferrence'; }
		}
	}

	if (flagError == true) {
		// errors -- alert & focus.
		alert(msg);
		document.getElementById(focusElement).focus();
		return false;
	} else {
		// no errors, store the lead

		try {
			newID = ColdFusion.Ajax.submitForm(primaryFormName, '/cfc/sales/leads.cfc?method=fnInitLeadRecord&returnFormat=json', callback, errorHandler, 'POST', false);
			secondaryForm.DemoLeadID.value = 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, thisState.value);
			}

		} catch(err) {
			 // alert('oh snap! the cfc failed to run: ' + err.description);
		}
			
		// open the dialog
		$j("#dialog_" + secondaryForm.name).dialog("open");
		
		// push values
		for (i=0; i < primaryForm.elements.length; i++) {
			thisName = primaryForm.elements[i].name;
			thisValue = primaryForm.elements[i].value;
			if (typeof(secondaryForm.elements[thisName]) == "undefined") {
				input = document.createElement("input");
				input.setAttribute("type", "hidden");
				input.setAttribute("name", thisName);
				input.setAttribute("value", thisValue);
				document.getElementById(secondaryFormName).appendChild(input);
			} else {
				if (thisName != 'btnSubmit') {
					secondaryForm.elements[thisName].value = thisValue;
				}
			}
		}
		
		return false;
	}
}


fnStoreSecondForm = function(f) {
	try {
		if (typeof(f.SchoolName) != "undefined") { 
			idx = f[f.name + "_SchoolID"].selectedIndex;
			f.SchoolName.value = f[f.name + "_SchoolID"].options[idx].text;
		}
		if (typeof(f.DistrictName) != "undefined") { 
			idx = f[f.name + "_DistrictID"].selectedIndex;
			f.DistrictName.value = f[f.name + "_DistrictID"].options[idx].text;
		}
		if (typeof(f.Title) != "undefined") { 
			idx = f[f.name + "_Title"].selectedIndex;
			f.Title.value = f[f.name + "_Title"].options[idx].text;
		}

		newID = ColdFusion.Ajax.submitForm(f.name, '/cfc/sales/leads.cfc?method=fnUpdateLeadRecord&returnFormat=json', callback, errorHandler, 'POST', false);

		// run Level 2 SEO exchange
		if (typeof(f.convNameL2) != "undefined") { 
			thisState = f[f.thisPrimaryFormName.value + "_State"];
			// because this is the second form submission, replace the term "Submit" with "2ndForm"
			f.convNameL2.value = f.convNameL2.value.replace(/Submit/i, "2ndForm");
			fnPostLevel2Data(f.convNameL2.value, f.FirstName.value + ' ' + f.LastName.value, f.Email.value, f.remoteIP.value, thisState.value);
		}

	} catch(err) {
		// alert('oh snap! the cfc failed to run: ' + err.description);
	}

	$j('#dialog_' + f.name).dialog('close');

}

fnOptOutSecondForm = function(f) {
	try {
		newID = ColdFusion.Ajax.submitForm(f.name, '/cfc/sales/leads.cfc?method=fnFinalizeLeadRecord&returnFormat=json', callback, errorHandler, 'POST', false);
	} catch(err) {
		// alert('oh snap! the cfc failed to run: ' + err.description);
	}

	$j('#dialog_' + f.name).dialog('close');

}

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);
} 