//FORM VALIDATION
//---------------------------------------------------------------------------------------------
//EMAIL FIELD
jQuery.fn.validateEmail = function(){
	var email = jQuery(".contactForm #email").val();
	if(email != 0){
		if(jQuery().isValidEmailAddress(email)){
			jQuery(".contactForm #email").removeClass("invalid").addClass("valid");
		} else {
			jQuery(".contactForm #email").removeClass("valid").addClass("invalid");
		}
	}else{
		jQuery(".contactForm #email").removeClass("valid").removeClass("invalid");
	}
}
jQuery.fn.isValidEmailAddress = function(email){
	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(email);
}

//NAME FIELD								   
jQuery.fn.validateName = function(){
	var name = jQuery(".contactForm #name").val();
	if(name != 0){
		if(jQuery().isValidName(name)){
			jQuery(".contactForm #name").removeClass("invalid").addClass("valid");
		} else {
			jQuery(".contactForm #name").removeClass("valid").addClass("invalid");
		}
	}else{
		jQuery(".contactForm #name").removeClass("valid").removeClass("invalid");
	}
}
jQuery.fn.isValidName = function(name){
	var pattern = new RegExp("[a-zA-Z]");
	return pattern.test(name);
}

//REFRESH FORM
jQuery.fn.refreshForm = function(){
	jQuery(".results").animate({opacity:'1.0'}, 1500 ,function(){		
		jQuery(".results .error").fadeOut(500);
		jQuery(".results .success").fadeOut(500, function(){
			jQuery(".results .button").fadeIn(500);
		});
	});	
}


jQuery(document).ready(function() {

	//SIGNUP FIELD EVENT HANDLERS
	jQuery(".contactForm #email").bind("keyup keydown change focus blur load keypress mousedown mouseout mouseover select", function(){
		jQuery.fn.validateEmail()
	});															 
	jQuery(".contactForm #name").bind("keyup keydown change focus blur load keypress mousedown mouseout mouseover select", function(){
		jQuery.fn.validateName()
	});
	

//CONSUME MAIL FORM WEBSERVICE 
//---------------------------------------------------------------------------------------------

	//SIGNUP FORM SUBMIT						
	jQuery("#submitInfo").click(function(){
		
		var i = 0;
		jQuery('.contactForm input').each(function(){
			if(jQuery(this).val() == 0 || jQuery(this).val() == null){
				i++;
			}
		});
		if(jQuery(".invalid").length == 0 & i == 0){

			var name = jQuery(".contactForm #name").val();	
			var email = jQuery(".contactForm #email").val();
			var comments = jQuery(".contactForm #comments").val();
			jQuery(".results .button").fadeOut(500,function(){
				jQuery(".results .animation").fadeIn(800, function(){
						
					jQuery.ajax({
						type: "POST",
						url: "/emailForm.asmx/SendEmail",
						data: "{'name':'" + name + "','email':'" + email + "','comments':'" + comments + "'}",
						contentType: "application/json; charset=utf-8",
						dataType: "json",
						success: function(msg) {
							jQuery(".results .animation").fadeOut(500, function(){
								if (msg.d != "+"){												
									jQuery(".results .error").fadeIn(500, function(){
										jQuery().refreshForm();
										alert(msg.d);
									});
								}else{
									jQuery(".results .success").fadeIn(500, function(){
										jQuery().refreshForm();												
									});
								}
							});
						},
						error: function(error, element) {
							jQuery(".results .animation").fadeOut(500, function(){											
								jQuery(".results .error").fadeIn(500, function(){
									jQuery().refreshForm();												
								});
							});
						}
					});												   
				});										
			});
			
		}else{
			jQuery('.contactForm input').each(function(){
				if(jQuery(this).val() == 0 || jQuery(this).val() == null){
					jQuery(this).addClass("invalid");	
				}
			});		
		}
		
	});
	

});

