// Lambert contact form
$(document).ready(function(){

	var defaultname = $("#textname").val();
	var defaultemail = $("#textemail").val();
	var defaultphone = $("#textphone").val();
	var defaultcomment = $("#textcomment").val();

	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);

	$("#textname").focusin(function() {
		var fieldvalue = $(this).val();
		$(this).css('color','black');
		if( fieldvalue == defaultname ){
			$(this).val('')
		}
	});
	$("#textname").focusout(function(){
		var fieldvalue = $(this).val();
		if( fieldvalue == '' ){
			$(this).css('color','#999');
			$(this).val(defaultname);
		}
	});
	$("#textemail").focusin(function() {
		var fieldvalue = $(this).val();
		$(this).css('color','black');
		if( fieldvalue == defaultemail ){
			$(this).val('')
		}
	});
	$("#textemail").focusout(function(){
		var fieldvalue = $(this).val();
		if( fieldvalue == '' ){
			$(this).css('color','#999');
			$(this).val(defaultemail);
		}
	});
	$("#textphone").focusin(function() {
		var fieldvalue = $(this).val();
		$(this).css('color','black');
		if( fieldvalue == defaultphone ){
			$(this).val('')
		}
	});
	$("#textphone").focusout(function(){
		var fieldvalue = $(this).val();
		if( fieldvalue == '' ){
			$(this).css('color','#999');
			$(this).val(defaultphone);
		}
	});
	$("#textcomment").focusin(function() {
		var fieldvalue = $(this).val();
		$(this).css('color','black');
		if( fieldvalue == defaultcomment ){
			$(this).val('')
		}
	});
	$("#textcomment").focusout(function(){
		var fieldvalue = $(this).val();
		if( fieldvalue == '' ){
			$(this).css('color','#999');
			$(this).val(defaultcomment);
		}
	});
	$("#contactFormForm").submit(function(){
		var flag;
		var textname = $("#textname").val();
		var textemail = $("#textemail").val();
		var textphone = $("#textphone").val();
		var textcomment = $("#textcomment").val();
		var textemail2 = $("#textemail2").val();
		$("#messageContainer > div").css('display','none');
		if( textemail2 != '' ){
			return false;
		}
		if( textname == defaultname ){
			$("#errorname").css('display','block');
			flag = true;
		}
		if( !pattern.test(textemail) ){
			$("#erroremail").css('display','block');
			flag = true;
		}
		if( textphone == defaultphone ){
			$("#errorphone").css('display','block');
			flag = true;
		}
		if( textcomment == defaultcomment ){
			textcomment = '';
		}
		if( flag == true ){
			$("#messageContainer").css('display','block');
			var new_position = $("#contactForm").offset();
			window.scrollTo(new_position.left,new_position.top); 
			return false;
		}
		$("#textname").css('color','#999');
		$("#textname").val(defaultname);
		$("#textemail").css('color','#999');
		$("#textemail").val(defaultemail);
		$("#textphone").css('color','#999');
		$("#textphone").val(defaultphone);
		$("#textcomment").css('color','#999');
		$("#textcomment").val(defaultcomment);
		$.ajax({ 
			type: "GET", 
			url: "procform.aspx", 
			data: "name="+textname+"&email="+textemail+"&phone="+textphone+"&comment="+textcomment+"&ajaxsubmit=yes", 
			success: function(msg){
				if(msg == "We have received your inquiry and will contact you soon."){
					window.location.href = 'http://lambertpalletizers.com/contact-success.aspx';	
				}
				else{
					$("#ajaxmessage").html(msg);
					$("#ajaxmessage").css('display','block');
					$("#messageContainer").css('display','block');
				} 
				//$("#ajaxmessage").html(msg);
				//$("#ajaxmessage").css('display','block');
				//$("#messageContainer").css('display','block');
			} 
		});
		return false;
	});
});

