String.prototype.trim = function() {
    return this.replace(/^\s+/,"").replace(/\s+$/,"").replace(/[ ]{2,}/gi," ").replace(/\n/," ");
}
	
var options = { 
	beforeSubmit:  validate,  // pre-submit callback 
	success:       showResponse,  // post-submit callback 
	resetForm: true        // reset the form after successful submit 
}; 
				
$('#mssg').css({"resize": "none"});

$('#fcn').ajaxForm(options); 
				
function showResponse(responseText, statusText){
	$('p.success').animate({ opacity: "show" }, "fast")
	$("#loading").remove();
	$("#button").show();
}
				
function validate(formData, jqForm, options) {
	$("p.err").animate({ opacity: "hide" }, 0);
	$("span.err").animate({ opacity: "hide" }, 0);
	$("input#button").blur();
			 
	var nameValue = new String($('input[name=name]').fieldValue()); 
	var emailValue = new String($('input[name=email]').fieldValue());
	var phoneValue = new String($('input[name=phone]').fieldValue());
	
	nameValue = nameValue.trim();
	emailValue = emailValue.trim();
	phoneValue = phoneValue.trim();
	
	var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	var phoneReg = /^[\d-+() ]{2,40}$/;
	var correct = true;
	var setFc = false;
	
	if (!nameValue) {
		$("span.err.wrong_name").animate({ opacity: "show" }, "slow");
		correct = false;
	} 
	if (!correct) {
		$("input#fname").focus();
		setFc = true;
	}
	
	if (!emailValue) {
		$("span.err.wrong1_email").animate({ opacity: "show" }, "slow");
		correct = false;
	} else if(!emailReg.test(emailValue)) {
		$("span.err.wrong2_email").animate({ opacity: "show" }, "slow");
		correct = false;
	}
	if (!setFc && !correct) {
		$("input#email").focus();
		setFc = true;
	}
	
	if (phoneValue && !phoneReg.test(phoneValue)) {
		$("span.err.wrong_phone").animate({ opacity: "show" }, "slow");
		correct = false;
	} 
	if (!setFc && !correct) {
		$("input#phone").focus();
	}
	
	if (!correct) {
		$("p.err").animate({ opacity: "show" }, "slow");
		return false;
	} else {
		$("#button").hide();
		$("#cnsubmit").append('<img src="images/load_sml.gif" id="loading" />');
	}
} 	
				
$("p.success").click( function () { 
	$(this).animate({ opacity: "hide" }, "slow"); 
});
				
$(".fbg").focus( function () { 
	$(this).css({"background": "#FFF"});
});
$(".fbg").blur( function () { 
	$(this).css({"background": "#DBDBCA"});
});
$(".button").focus( function () { 
	$(this).css({"background": "#FFF"});
});
$(".button").mousedown( function () { 
	$(this).css({"background": "#FFF"});
});
$(".button").blur( function () { 
	$(this).css({"background": "#DBDBCA"});
});
