
$(document).ready(function(){	
	/**
	* remove the error and message boxes
	* by default
	*/
	
	$("#errorBox").hide();
	$("#messageBox").hide();	
	

		
	$("form#request_form").submit(function() {
		
		var error = "";
		
		var fullname = trim($("#fullname").val());
		var email = trim($("#email").val());
		var location = trim($("#location").val());
		var message = trim($("#message").val());
							
		result = name_validation(fullname);
		if (result) 
		 error += result;				
				
		result = email_validation(email);
		if (result) 
			error += result;			
				
		result = location_validation(location);
		if (result) 
			error += result;						
			
		result = message_validation(message);
		if (result) 
			error += result;					
				
		if (error)
		{
			$("#errorBox").html("<b>Please check errors:</b><br>"+ error +"").show();
			//remove_notice();
			return false;
		}else{
			return true;
		}
			
	});

});



//Requester Name Validation
function name_validation(string) {	

    var error="";
	var catRegExp = new RegExp("^[a-zA-Z.' -]*$"); //Avoid Numerical and Special Charters in Category name
	var name = catRegExp.test(string);	

	if (!string){	//Check if Category Field have Value or not
		
		error += "   - Please Fill Name Field<br>";		
	} else if (!name) { //Check Category Filed value is not true, after Regular Exp. Test
		error += "   - Please Fill valid Name<br>"; 		
	}	
	
	if (error){ //If any error occur it return the value to Called Function
		return error;	
	} 
}

function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value ) {
	return LTrim(RTrim(value));	
}


function email_validation(string) {
	
	var error="";	
	var string = jQuery.trim(string);	

	if (!string){	//Check if Category Field have Value or not
		error += "   - Please Fill E-Mail ID Field<br>";		
	} else if (!(isValidEmailAddress(string))) {
		error += " - Please Fill a Valid Email ID<br>"; 
	}	  
		
	if (error){	
		return error;
	}
}

function isValidEmailAddress(emailAddress) {
var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
return pattern.test(emailAddress);
}

function location_validation(string) {	

    var error="";
	var catRegExp = new RegExp("^[a-zA-Z.' -]*$"); //Avoid Numerical and Special Charters in Category name
	var name = catRegExp.test(string);	

	if (!string){	//Check if Category Field have Value or not
		
		error += "   - Please Fill Location Field<br>";		
	} else if (!name) { //Check Category Filed value is not true, after Regular Exp. Test
		error += "   - Please Fill valid Location<br>"; 		
	}	
	
	if (error) //If any error occur it return the value to Called Function
		return error;	
}



function message_validation(string) {
	
	 var error="";
	var strRegExp = new RegExp("^[a-zA-Z0-9.' -,\\n]*$"); //Avoid Numerical and Special Charters in Category name
	var name = strRegExp.test(string);	

	if (!string){	//Check if Category Field have Value or not
		error += "   - Please Fill Message Field<br>";		
	} else if (!name) { //Check Category Filed value is not true, after Regular Exp. Test
		error += "   - Please Fill valid Message Text<br>"; 		
	}	
	
	if (error) //If any error occur it return the value to Called Function
		return error;	
	
}

// Contact Us form validation
function validateContactUs(){
	var name = $("#fullname").val();
	var email = $("#email").val();
	var phone = $("#phone").val();
	var message = $("#message").val();
	var mm_action = $("#mm_action").val();
	var error = '';
	
	if(!name){
		error += " -Enter your full name.<br />";
		} else if(!validateName(name)) {
			error += " -Enter a valid name.<br />";
	}		
	if(!email){
		error += " -Enter your email address.<br />";
		} else if (!isValidEmailAddress(email)) {
		error += " -Enter valid email address.<br>"; 
	}	
	if(!phone){
		error += " -Enter your phone number.<br />";
		} else if(!validatePhone(phone)){
			error += " -Enter a valid phone number.<br />";
	}
	if(!message){
		error += " -Enter your message.<br />";
	}
	if(error){
		$("#errorBox").html("<b>Please check the following error(s).</b><br />"+error+"");
		$("#errorBox").show();
		return false;
		} else {
			$("#errorBox").hide();
				var dataStr = $("form#contactUsForm").serialize();
				$.post("contact-us.php",{fullname : name, email:email, phone:phone, message:message, mm_action:mm_action, data:dataStr},function(data){
				var resp = jQuery.trim(data);
				if(resp == "success"){
					$("#messageBox").html("<b>Thank you for your message. We will get back to you soon.</b>");
					$("#messageBox").show();
					$("#fullname").val("");
					$("#email").val("");
					$("#phone").val("");
					$("#message").val("");
					} else if(resp == "error"){
						$("#errorBox").html("<b>Could not send the contact message. Please try again.</b>");
						$("#errorBox").show();
				}
			});
	}
	return false;
}

// Phone number validation
function validatePhone(phone){
	return phoneRegX = (/^[0-9- ]{10,15}$/).test(phone);
}
// Phone name validation
function validateName(name){
	return nameRegX = (/^[a-zA-Z-' ]{4,15}$/).test(name);
}