/**********************************************
**  Browser Kings Contact Form Error Processing
**	
**  Created By: Ken Dunlap
**
**
**
**		Make a container with id="reciever" in the html file -- it will hold the main messages (welcome, error, success)
**		Add <p id="err_*****"></p> where ***** id of each form element -- this will hold the specific field error messages
**		
**		### Style these accordingly ### -- applied to #reciever text
**		.message = default display message
**		.error = general you have an error message
**		.success = successful submission message
**
**
**		Add additional checks to fields not included in the validate.php file
**********************************************/



function handleForm(form){
	lockScreen();
	
	jQuery.ajax({
		type: "POST",
   		url: "scripts/validate.php",			//**** Path to the php validation file ****//
	    data: jQuery('#' + form).serialize(),
		dataType: "html",
		success: function(html){
			processErrors(html)
		},
		error: function(){
			alert("There was an error in your request");
		}
			
	});
	
}


function processErrors(html){

	// split error messages to their own line
	var errorInLines = html.split("\n");
	
	// Remove initial blue formatting
	if (jQuery('#reciever').hasClass("message")){
		jQuery('#reciever').removeClass("message");
	}
	jQuery('#reciever').html("");
	
	var hasErrors = false;
	

	for ( var i in errorInLines )
	{
		var explode_again = errorInLines[i].split("|");
		if (explode_again[0]=='error'){
		    hasErrors = true;
			jQuery('#err_' + explode_again[1]).html(explode_again[2]);
			jQuery('#err_' + explode_again[1]).show('slow');
			
		}
		else if (explode_again[0]=='ok') {
			jQuery('#err_' + explode_again[1]).hide('slow');
			jQuery('#err_' + explode_again[1]).html("");
		}
	}
		
	if ( !hasErrors ){		// No errors in form, process form
			

		add_remove_class('error', 'success', 'reciever');
		
		setTimeout('unlockScreen()',500);
		jQuery('#reciever').html("<p>The form has been submitted successfully. Thanks for taking the time to contact us, we will be getting back to you shortly.</p>");		
		
		//clear form, message sent
		clearContactForm();	
		
	}
	else {
		add_remove_class('success', 'error', 'reciever');
		jQuery('#reciever').html('<p><b>There are errors in your form, please try again.</b></p>');	
		
		unlockScreen();
	}
			
	req = null;
			
			
}





// remove class "search" if exists and add "replace"
function add_remove_class(search,replace,element_id)
{
	if (jQuery('#' + element_id).hasClass(search)){
		jQuery('#' + element_id).removeClass(search);
	}
	jQuery('#' + element_id).addClass(replace);
}


// Clear all fields of the form meaning successful submission
function clearContactForm(){
	
	document.getElementById("name").value="";
	document.getElementById("zip").value="";
	document.getElementById("phone").value="";
	document.getElementById("email").value="";
	document.getElementById("comment").value="";
	
}



// overlay screen with loading image
function lockScreen(){
	add_remove_class("LockOff", "LockOn", "lockPane");
	jQuery('#reciever').html("");
	
	//scroll(0,0);	 	    
} 


// take away loading image, display successful message, clear the form
function unlockScreen(str){
	add_remove_class("LockOn", "LockOff", "lockPane");			
} 
