var errs;

function processForm(){
	
	// clean out any errors in the page
	errs = document.getElementById('errs');
	errs.innerHTML = '';

	processFormGeneral();
	errs.innerHTML = '<font class="star">' + errs.innerHTML + '<br/></font>';
	return false;

}

function processForm2(){
	
	errs = document.getElementById('errs');
	errs.innerHTML = '';

	processFormGeneral();
	processFormEmail();
	
	errs.innerHTML = '<font class="star">' + errs.innerHTML + '<br/></font>';
	return false;

}


// handles dojo ValidationTextBox fields
function processFormGeneral(){
			
	// loop through form elements
	var custForm = dijit.byId("custForm");			
	dojo.every(custForm.getDescendants(), function(widget){
										
		try{
				
			// check if field is valid, based on dojo markup on each ValidationTextBox
			if(!widget.validate()){
				var errMsg = widget.getErrorMessage(false);
				if(errMsg != null && errMsg.length > 0){
					errs.innerHTML = errs.innerHTML + '* ' + errMsg + '<br/>' ; 
				}
			}						
		}
		catch(err){ }				
			
		return true;				
	});
	
}

function onFormSubmit(){
	
    var o = document.getElementById('popup');
    if (o){ return onPopupFormSubmit(); }
	

   	//alert("onFormSubmit called");
	var custForm = dijit.byId("custForm");			
    //check for the errors
	if(	custForm.isValid() ) {
     //alert("submitting form");
	 dojo.xhrPost({
			 form: 'custForm',
			 error: function(resp) {
				 dijit.byId('sales_inquiry_form').setContent('<font class="star">There were some errors on server. Please try again later. <br/>Thank you for your co-operation.<br/></font>');
				 },
			 load: function(data,resp){
 				dijit.byId('sales_inquiry_form').setContent('<font >Thank you for submitting your message. Someone will get back to you soon.<br/></font>');
				 }
		  });
	}

	return false;
}

function onPopupFormSubmit(){

   	//alert("onFormSubmit called");
	var custForm = dijit.byId("custForm");			
    //check for the errors
	if(	custForm.isValid() ) {
     //alert("submitting form");
	 dojo.xhrPost({
			 form: 'custForm',
			 error: function(resp) {
				 dijit.byId('sales_inquiry_form').setContent('<div id="page_content_container" class="T9 clearfix" style="width: 520px;"><div id="page_content" class="clearfix" ><div id="content_middle" class="contact_us clearfix"><div class="clearfix" id="left_content" style="margin-top: -33px;"><h2>Thank You</h2><div class="content_section" style="margin-top: -10px;"><div class="form_info">There were some errors on server. Please try again later. Thank you for your co-operation.</div><div class="form_info" onClick="closeIt();" style="text-align: right; margin-right: 20px; margin-top: 10px;"><img src="/sys/images/buttons/close.gif" alt="[Close]" onClick="closeIt();" class="submit" style="margin-right: 2px;" /></div></div></div></div></div>');   
				 },
			 load: function(data,resp){
 				dijit.byId('sales_inquiry_form').setContent('<div id="page_content_container" class="T9 clearfix" style="width: 520px; "><div id="page_content" class="clearfix"><div id="content_middle" class="contact_us clearfix"><div class="clearfix" id="left_content" style="margin-top: -33px;"><h2>Thank You</h2><div class="content_section" style="margin-top: -10px;"><div class="form_info">Thank you for your submission. Someone will get back to you soon.</div><div class="form_info" onClick="closeIt();" style="text-align: right; margin-right: 20px; margin-top: 10px;"><img src="/sys/images/buttons/close.gif" alt="[Close]" onClick="closeIt();" class="submit" style="margin-right: 2px;" /></div></div></div></div></div>');
				 }
		  });
	}

	return false;
}

// Hnadles fields that depend on each other, i.e.
// email and confirm email fields. 
function processFormEmail(){
					
	var i = 0;
	var friendEmail = '';
	var friendConfirm = '';
	var needMsg = false;
	
	for (i=0; i<4;i=i+1) {
		
		if(i == 0){
			friendEmail = document.getElementById('email');
			friendConfirm = document.getElementById('confirmEmail');
		}
		else{
			friendEmail = document.getElementById('friendEmail' + i);
			friendConfirm = document.getElementById('confirmEmail' + i);
		}
			
		needMsg = false;
		if(friendEmail == null && friendConfirm == null){
			needMsg = false;
		}
		else{
			friendConfirm = friendConfirm.value;
			friendEmail = friendEmail.value;
			if(friendEmail == null && friendConfirm == null){ needMsg = false; }
			else if(friendEmail == friendConfirm ){ needMsg = false; }
			else needMsg = true;
		}
		
		if(needMsg){
			switch(i){

				case 0:												
					errs.innerHTML =  errs.innerHTML +  "* Your email and confirmation does not match.<br/>";						
					break;    

				case 1:						
					errs.innerHTML =  errs.innerHTML +  "* Your first friend's email and confirmation does not match.<br/>";							
					break;  

				case 2:						
					errs.innerHTML =  errs.innerHTML +  "* Your second friend's email and confirmation does not match.<br/>";							
					break;  

				case 3:						
					errs.innerHTML =  errs.innerHTML +  "* Your third friend's email and confirmation does not match.<br/>";							
					break;  
			}
			
		}
		
	}
	
}