/*
	2010.02.03	gricke
				<blankRadio> remove duplicates
				<typeof(emailHeader)> optional email header image
				See also
					O_ProcessFormSubmit.asp
					Templates_Common.asp
					FormProcess.js
					
	2007.07.09	gricke
				<formvalidation_2>
					Separate out validation so it can be run as separate function
					<submitMethAjaxFunc>
						If set within requestVars - run this function after processing
						
					<formvalidationVars>
						the function that was separated out
						
	2007.05.16	gricke
				<oProcessString>
				Create a common variable for the process string
				<copySender>
				Capture and pass
	2007.05.03	gricke
				<pass checkbox nulls>
				Added to pass a space for check boxes (was supressed) so as to write csv files accurately
				
	2007.04.07	gricke
				<submitMeth == 'ajax'>
				Passes the O_ProcessFormSubmit.asp data into a hidden field
				<input type="hidden" name="AJAX_VAL" value="" />
				A second javascript file is required (in addition to this one) to use AJAX to process
				
				Must replace
					onClick="formvalidation('RequestForm');return false;"
					width
					onClick="newFunction();return false;"
						newFunction: Grab the data from the hidden field and pass with Ajax
				
	2006.10.31	gricke
				<SERVER_EMAIL> add condition for
	2006.07.20	gricke
				<typeof(eval('document.' + thisform + '.' + ValidateVarPieces[1]> added check to see if validation var actually exists in the form
				This corrects the error an alerts of the problem
				
				<ValidateVarsArray.length-1>  added the "-1" it was checking for too many
				
	2006.07.12	gricke
				<MAIN_FORM).method >  Set the method for submit option (i.e. submitDebug) for those forms that did not have it
				Add error checking if required hidden fields are missing
				<writeFile> Add to end of ProcessForm to pass 
	2006.06.30	gricke
				<submitDebug> Updated the submision types to get better debuggin options
				
	2006.06.22	gricke
				<submitMeth=submitForm>: New RequestVars added
				submitMeth
					submitForm
					javaScript
					debugSend
					
	2006.06.15	gricke
				<ProcessInputs>: Re code to get all form elements to get proper order
				
	2006.06.06	gricke
				<URLEncode(HoldVals)> fixed problems when sending values with special characters
	2006.04.14	gricke
				<o2Layout>: replaces updates.orbisdesign.net (virtual directory required)
	2006.03.28	gricke
				<turnOffNulls>: currently have nulls turned off
				
				<InPutBx[i].type == 'radio'>: add condition to <ProcessInputs> as regardless of selection, both options were showing
				
	2006.03.01	gricke
				<ProcessInputs>: Rebuild this (based on GetAllInputs) - erros corrected in FireFox (and Mac)
				<debugSendData>: added option to send debugging data to text field defined in the form
				Disable <setTimeout> to see within the form box
				<GetForPreSet>
				<GetForPreSetSelect>
				<GetForPreSetCheck>: 	No longer using the cookie name passed within the image onload
										from the form.  Using what's set in RequestVars structure - form
										No longer has to be customized at the cookie end
										
	NOTES
		<o2Layout> Virtual Directory Required to process		
		<document['ProcessForm']>  The Image that loads		

		"ProcessForm" name of transparent image
		Variables set from custom file: Client/JavascriptShell/FormValidationVars.js	
		
		TEST
		/o2Layout/O_ProcessFormSubmit.asp?Vars=|first_name=Gary|last_name=Ricke|email=grickesecret@gmail.com|&FROM_ADDR=service@elginsweeper.com&ML_RECIPIENT_EMAIL=gricke@orbisdesign.com&ML_SUBJECT=Elgin Sweeper Service Request
				
*/
function GetAllInputs(FormName){
	/* pass to cookie */
	var InPutBx = eval('document.' + FormName).getElementsByTagName('input');
	var SelectBx = eval('document.' + FormName).getElementsByTagName('select');
	var TextAreaBx = eval('document.' + FormName).getElementsByTagName('textarea');
   
	var HoldVals = '';
	for (i=0; i<InPutBx.length;i++) {
		if(InPutBx[i].type != 'hidden' && InPutBx[i].type != 'button'){
			if(InPutBx[i].type == 'checkbox'){
				if(InPutBx[i].checked == true){
					HoldVals = HoldVals + "|" + InPutBx[i].name + '=' + InPutBx[i].value;
				}else{
					HoldVals = HoldVals + "|" + InPutBx[i].name + '=' + '';
				}
			}else{
				HoldVals = HoldVals + "|" + InPutBx[i].name + '=' + InPutBx[i].value; 
			}	
		}
	}
   
	for (i=0; i<SelectBx.length;i++) {
		HoldVals = HoldVals + "|" + SelectBx[i].name + '=' + SelectBx[i].value; 
	}   

	for (i=0; i<TextAreaBx.length;i++) {
		if(TextAreaBx[i].name != 'comments'){
			HoldVals = HoldVals + "|" + TextAreaBx[i].name + '=' + TextAreaBx[i].value; 
		}	
	}   

	HoldVals = HoldVals + "|" //Add final delimiter for parsing
	SetGetMemberKookie(FORM_KOOKIE,HoldVals);
}
function GetForPreSet(FormName,CookieName,FieldName){
	var CookieInfo = getCookie(FORM_KOOKIE);
	if(CookieInfo != null){
		CookieInfo2 = CookieInfo.substring(CookieInfo.indexOf(FieldName));
		CookieInfo3 = CookieInfo2.substring(0,CookieInfo2.indexOf('|'));
		CookieInfo4 = CookieInfo3.substring((CookieInfo3.indexOf('=')+1));
		FieldFill = eval('document.' + MAIN_FORM + '.' + FieldName);
		FieldFill.value = CookieInfo4;
	}	
}
function GetForPreSetSelect(FormName,CookieName,FieldName){
	var CookieInfo = getCookie(FORM_KOOKIE);
	if(CookieInfo != null){
		CookieInfo2 = CookieInfo.substring(CookieInfo.indexOf(FieldName));
		CookieInfo3 = CookieInfo2.substring(0,CookieInfo2.indexOf('|'));
		CookieInfo4 = CookieInfo3.substring((CookieInfo3.indexOf('=')+1));
		FieldFill = eval('document.' + MAIN_FORM + '.' + FieldName);
		
		var optionCounter;
		for (optionCounter = 0; optionCounter < FieldFill.length; optionCounter++) {
			if(FieldFill.options[optionCounter].value == CookieInfo4){
				FieldFill.options[optionCounter].selected = true;
			}
		}
	}	
}
function GetForPreSetCheck(FormName,CookieName,FieldName){
	var CookieInfo = getCookie(FORM_KOOKIE);
	if(CookieInfo != null){
		CookieInfo2 = CookieInfo.substring(CookieInfo.indexOf(FieldName));
		CookieInfo3 = CookieInfo2.substring(0,CookieInfo2.indexOf('|'));
		CookieInfo4 = CookieInfo3.substring((CookieInfo3.indexOf('=')+1));
		FieldFill = eval('document.' + MAIN_FORM + '.' + FieldName);
		if(CookieInfo4 != ''){
			FieldFill.checked = true;
		}
	}	
}
function ProcessInputs(){
	var ReqForm = document.getElementById(MAIN_FORM);
	var InPutBx = eval('document.' + MAIN_FORM).getElementsByTagName('input');
	var SelectBx = eval('document.' + MAIN_FORM).getElementsByTagName('select');
	var TextAreaBx = eval('document.' + MAIN_FORM).getElementsByTagName('textarea');
	var HoldVals = '';
	var InPutBx = eval('document.' + MAIN_FORM).elements;
	var blankRadio = '';
	
	for(i=0; i<InPutBx.length; i++){ 
		if(InPutBx[i].type != 'hidden' && InPutBx[i].type != 'button' && InPutBx[i].type != undefined && InPutBx[i].type != ''){
			if(InPutBx[i].type == 'checkbox' || InPutBx[i].type == 'radio'){
				if(InPutBx[i].checked == true){
					HoldVals = HoldVals + "|" + InPutBx[i].name + '=' + InPutBx[i].value;
					//Removed previous blank version - had not found a checked on yet
					blankRadio = InPutBx[i].name + '=' + ' |';
					if(HoldVals.indexOf(blankRadio) != -1){
						HoldVals = HoldVals.replace(blankRadio, "");
					}
				}else{
					if(InPutBx[i].type == 'checkbox'){
						HoldVals = HoldVals + "|" + InPutBx[i].name + '=' + ' ';	//pass checkbox nulls
					}else{
						/* radio */
						if(HoldVals.indexOf(InPutBx[i].name) == -1){
							//Pass at least one in
							HoldVals = HoldVals + "|" + InPutBx[i].name + '=' + ' ';
						}
					}
				}			
			}else{
				HoldVals = HoldVals + "|" + InPutBx[i].name + '=' + InPutBx[i].value;
			}	
		}
	}

	if(typeof(FROM_ADDR) != 'string'){
		FROM_ADDR = 'support@orbisdesign.com';
	}
	if(typeof(ML_RECIPIENT_EMAIL) == 'undefined'){
		ML_RECIPIENT_EMAIL = 'gary@orbisdesign.com';
	}
	if(typeof(SERVER_EMAIL) == 'undefined'){
		SERVER_EMAIL = '';
	}
	if(typeof(ML_SUBJECT) == 'undefined'){
		ML_SUBJECT = 'Form Request Not Set';
	}
	if(typeof(writeFile) == 'undefined'){
		writeFile = '';
	}
	if(typeof(copySender) == 'undefined'){
		copySender = '';
	}
	if(typeof(emailHeader) == 'undefined'){
		emailHeader = '';
	}else{
		//add the base URL
		var url = document.location.toString() ; //url
		var e_url = '';
		var p = 0;
		var p2 = 0;
		p = url.indexOf("//");
		e_url = url.substring(p+2);
		p2 = e_url.indexOf("/") ;
		var root_url = url.substring(0,p + p2 + 3);		
		emailHeader = root_url + emailHeader;
	}
	
	var oProcessString = '/o2New/www/include/Layout/O_ProcessFormSubmit.asp?Vars=' + URLEncode(HoldVals) + '&FROM_ADDR=' + FROM_ADDR + '&ML_RECIPIENT_EMAIL=' + ML_RECIPIENT_EMAIL + '&ML_SUBJECT=' + ML_SUBJECT + '&ML_WRITEFILE=' + writeFile + '&SERVER_EMAIL=' + SERVER_EMAIL + '&copySender=' + copySender + '&emailHeader=' + emailHeader; 
	//document.write(oProcessString);
	if(typeof(submitMeth) == 'undefined'){
		document['ProcessForm'].src = oProcessString;
	}else if(submitMeth == 'submitForm' || submitMeth == 'submitDebug' || submitMeth == 'submitFormDebug'){
		if(typeof(eval('document.' + MAIN_FORM).FROM_ADDR) == 'undefined'){
			alert('FORM ERROR: Missing hidden field: FROM_ADDR \rForm not submitting');
		}else if(typeof(eval('document.' + MAIN_FORM).ML_RECIPIENT_EMAIL) == 'undefined'){
			alert('FORM ERROR: Missing hidden field: ML_RECIPIENT_EMAIL \rForm not submitting');
		}else if(typeof(eval('document.' + MAIN_FORM).ML_SUBJECT) == 'undefined'){
			alert('FORM ERROR: Missing hidden field: ML_SUBJECT \rForm not submitting');		
		}else if(typeof(eval('document.' + MAIN_FORM).ML_SENDTYPE) == 'undefined'){
			alert('FORM ERROR: Missing hidden field: ML_SENDTYPE \rForm not submitting');
		}else if(typeof(eval('document.' + MAIN_FORM).ML_WRITEFILE) == 'undefined'){
			alert('FORM ERROR: Missing hidden field: ML_WRITEFILE \rForm not submitting');	
		}
		eval('document.' + MAIN_FORM).action = '/o2New/www/include/Layout/O_ProcessFormSubmit.asp';
		eval('document.' + MAIN_FORM).method = 'Post';
		eval('document.' + MAIN_FORM).FROM_ADDR.value = FROM_ADDR;
		eval('document.' + MAIN_FORM).ML_RECIPIENT_EMAIL.value = ML_RECIPIENT_EMAIL;
		eval('document.' + MAIN_FORM).ML_SUBJECT.value = ML_SUBJECT;
		eval('document.' + MAIN_FORM).ML_SENDTYPE.value = submitMeth;
		eval('document.' + MAIN_FORM).ML_WRITEFILE.value = writeFile;
		eval('document.' + MAIN_FORM).submit();
		
	}else if(submitMeth == 'debug'){
		document.write('<a href=\"' + oProcessString + '\">Test submission directly</a>');
	}else if(submitMeth == 'ajax'){
		var ajaxDataStr = oProcessString; 
		eval('document.' + MAIN_FORM + '.AJAX_VAL').value = ajaxDataStr;
		if(typeof(submitMethAjaxFunc) == 'undefined'){
			submitMethAjaxFunc = '';
		}
	}else{
		document['ProcessForm'].src = oProcessString; 
	}
}
function formvalidation(thisform)
{
	//Variables set from custom file: FormValidationVars.js
	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('|');
	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);
	ProcessInputs();
	eval('document.' + thisform).style.display = 'none';
	window.scrollTo(0,0);
	setTimeout('pauseDisplay()',1000);
}
function formvalidation_2(thisform){
	if(formvalidationVars(thisform) == true){
		GetAllInputs(thisform);
		ProcessInputs();
		if((submitMeth == 'ajax') && (submitMethAjaxFunc.length > 3)){
			eval(submitMethAjaxFunc + '();');
		}
	}
}
function formvalidationVars(thisform){
	// separated out from formvalidation function
	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('|');
	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 
					return true;
				}
			}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;
			}
		}	
	}	
}
function pauseDisplay(){
	var ThankyouMsg = document.getElementById('formThanks');
	ThankyouMsg.style.display = 'block';
}