//FORM VALIDATION
//---------------------------------------------------------------------------------------------
//EMAIL FIELDS
jQuery.fn.validateEmail = function(id){
	var email = jQuery(".contactForm #"+id).val();
	if(email != 0){
		if(jQuery().isValidEmailAddress(email)){
			jQuery(".contactForm #"+id).removeClass("invalid").addClass("valid");
		} else {
			jQuery(".contactForm #"+id).removeClass("valid").addClass("invalid");
		}
	}else{
		jQuery(".contactForm #"+id).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);
}

//TEXT FIELDS								   
jQuery.fn.validateText = function(id){
	var text = jQuery(".contactForm #"+id).val();
	if(text != 0){
		if(jQuery().isValidText(text)){
			jQuery(".contactForm #"+id).removeClass("invalid").addClass("valid");
		} else {
			jQuery(".contactForm #"+id).removeClass("valid").addClass("invalid");
		}
	}else{
		jQuery(".contactForm #"+id).removeClass("valid").removeClass("invalid");
	}
}
jQuery.fn.isValidText = function(text){
	var pattern = new RegExp("[a-zA-Z]");
	return pattern.test(text);
}


//REFRESH FORM
jQuery.fn.refreshForm = function(){
	jQuery(".contactForm .results").animate({opacity:'1.0'}, 1500 ,function(){		
		jQuery(".contactForm .results .error").fadeOut(500);
		jQuery(".contactForm .results .success").fadeOut(500, function(){
			jQuery(".contactForm .results .button").fadeIn(500);
			jQuery(".contactForm .field").val("").change();
		});
	});	
}


jQuery(document).ready(function() {

    //BIND VALIDATORS -- Fields must have an ID assigned!
    jQuery(".contactForm #email").bind("keyup keydown change focus blur load keypress mousedown mouseout mouseover select", function() {
        var id = jQuery(this).attr("id");
		jQuery.fn.validateEmail(id)
    });
    jQuery(".contactForm #name").bind("keyup keydown change focus blur load keypress mousedown mouseout mouseover select", function(){
		var id = jQuery(this).attr("id");																																																																																																				
        jQuery.fn.validateText(id)
    });
	jQuery(".contactForm #comments").bind("keyup keydown change focus blur load keypress mousedown mouseout mouseover select", function() {																																	
		var id = jQuery(this).attr("id");																																		
        jQuery.fn.validateText(id)
    });
	
	
	//COMMENTS ANIMATION
	jQuery(".contactForm #comments").bind("focus", function() {
		if (jQuery(this).height() < 99){
			jQuery(this).animate({height:'100px'});
		}
    });
	jQuery(".contactForm #comments").bind("blur", function() {
		if (jQuery(this).height() > 19){
			jQuery(this).animate({height:'19px'});
		}
    });


    //CONSUME MAIL FORM WEBSERVICE 
    //---------------------------------------------------------------------------------------------

    //SIGNUP FORM SUBMIT						
    jQuery("#submitInfo").click(function() {

        var i = 0;
        jQuery('.contactForm .field').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();

            name = name.replace("'", "\\'");
            comments = comments.replace("'", "\\'");
            name = name.replace('"', '\\"');
            comments = comments.replace('"', '\\"');

            
            jQuery(".results .button").fadeOut(500, function() {
                jQuery(".results .animation").fadeIn(800, function() {

                    jQuery.ajax({
                        type: "POST",
                        url: "/WebServices/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();
                                    //alert(error.statusText);
                                });
                            });
                        }
                    });
                });
            });

        } else {
            jQuery('.contactForm input').each(function() {
                if (jQuery(this).val() == 0 || jQuery(this).val() == null) {
                    jQuery(this).addClass("invalid");
                }
            });
        }

    });


});

