/*
	NOTES
		<storeComments> Place comments in hidden text field as cookie loop does not store textAreas and is therefore not passed
	DEBUG	
		document.write('<a href=\"' + dataToSend + '\">Test submission directly</a>');
		document.getElementById('ajax1').innerHTML = dataToSend;
		document.getElementById('ajax2').innerHTML = dataToSend;
	FILES
		Xsl_FSEcho/RequestForm.xslt
		o2Fsecho/www/JavascriptShell/Requestform.js	
		o2Fsecho/www/include/writeLeadToFS.asp
		fedsig/www/infoReqFromIntegratedSystems.asp
*/
var debugDisplay = false;	//Show debug display data: transaction still occurs
var testSubmissionDirectly = false;

function storeComments(){
	document.RequestForm.commentsTextBox.value = document.RequestForm.comments.value;
}
function submitDataToDatabase(){
	var dataToSend = document.RequestForm.AJAX_VAL.value;
	document.RequestForm.style.display = 'none';
	window.scrollTo(0,0);
	xmlhttpPost('/include/writeLeadToFS.asp','ajax1',dataToSend,'dataSubmittedReturnResults');
}
function dataSubmittedReturnResults(httpResults){
	var dataToSend = document.RequestForm.AJAX_VAL.value
	
	httpResults = unescape(httpResults);
	
	if(debugDisplay == true){
		document.getElementById('ajax1').innerHTML = httpResults; 
	}
	if((httpResults.indexOf('SENDING ERROR') != -1)){
		document.getElementById('errors').style.display = 'block';
	}else if(httpResults.indexOf('Insert Complete') != -1){
		var searchString = 'CONTACT_ID_FOR_EMAIL';
		var FedSigAWProfileIDStart = httpResults.indexOf(searchString);
		var FedSigAWProfileIDEnd = httpResults.indexOf('/' + searchString);
		var FedSigAWProfileID = httpResults.substring((FedSigAWProfileIDStart + (searchString.length + 1)),FedSigAWProfileIDEnd-1);
		var AWLink = '';

		if(siteUrl().indexOf('orbisdesign.net') != -1){
			AWLink = 'http://fedsigaw.orbisdesign.net/AWWeb/admin/AdminStart.asp?Page=4&View=5&ContactID=' + FedSigAWProfileID;
		}else{
			//AWLink = 'http://www.federalsignal-indust.com/AWWeb/admin/AdminStart.asp?Page=4&View=5&ContactID=' + FedSigAWProfileID;
			AWLink = 'http://aw.federalsignal-indust.com/AWWeb/admin/default_admin.asp?Page=4&View=5&ContactID=' + FedSigAWProfileID;
		}
		
		dataToSend = dataToSend.replace(/ReplaceWithDynamicLink/,escape(AWLink));
		
		if(testSubmissionDirectly){
			document.write('<a href=\"' + dataToSend + '\">Test submission directly</a>');
		}else{	
			document['ProcessForm'].src = dataToSend;
		}	
		document.getElementById('formThanks').style.display = 'block';
	}else{
		document.getElementById('errors').style.display = 'block';
	}
}
function setProductInterest(){
	if(getQueryVariable('productRfi') != '999'){
		document.RequestForm.product.value = unescape(getQueryVariable('productRfi'));
	}
}



function formvalidation_sf(thisform){
	var string = "";
	var els = document.forms[thisform].elements;
	for(var no=0;no<els.length;no++){
	    string+="Name: "+els[no].name + ", Value: "+els[no].value + "\n";
	}
	var ValidateVarsArray = ValidateVars.split('|');
	
	if(document.requestInfo.elements[2].value == ''){
		alert('Product Interest is required');
		document.requestInfo.elements[2].focus();
		return false;
	}else{	
	
		with (thisform)
		{
			for (var i=0;i<ValidateVarsArray.length-1;i++) {
				
				var ValidateVarPieces = ValidateVarsArray[i].split('=');
				if(typeof(eval('document.' + thisform + '.' + ValidateVarPieces[1])) != 'undefined'){			
					if(eval(ValidateVarPieces[0] + '(document.' + thisform + '.' + ValidateVarPieces[1] + ',"' + ValidateVarPieces[2] + '")')==false){
						eval('document.' + thisform + '.' + ValidateVarPieces[1]).focus();
						return false;
					}else{
						//Everything is good 
					}
				}else{
					alert('FormValidationVars ERROR: \r Trying to validate >>>' + ValidateVarPieces[1] + '<<< which is not in this form \r \r Check that the validation var is spelled correctly  \r Or \r Remove the validation check \r Or \r Add the form element that is missing');
					return false;
				}
			}	
		}
		
		GetAllInputs(thisform);
		document.forms[thisform].submit();
	}	
}

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];
		}
  	}
	return "999";
}
function checkSuccess(){
	if(getQueryVariable('result') == 'success'){
		document.getElementById('formThanks').style.display = 'block';
		document.getElementById('fillInForm').style.display = 'none';
	}
}