$(document).ready(function() {
	
	$('#frmContact').ajaxForm({ 
		beforeSubmit: validate,
        dataType:  'json', 
		success:   processJson
    }); 
		
});

function processJson(data) { 
    	if (data.message == 'success') {
			$("#divCommentSubmit").hide();
			$("#error").hide();	
			$("#thankyou").show();	
		}
		else if (data.error != '')
		{
			$("#error").show();	
			$("#error").html(data.error);
		}
	}
	
function validate(formData, jqForm, options) { 
    
	var EmailRegEx = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;
							
    for (var i=0; i < formData.length; i++) { 
        	if (formData[i].name == 'commentName' 
				&& (formData[i].value == 'Required...' || formData[i].value == '')) {
				$("#error").removeClass("hide");	
				$("#error").html("Please enter your Name");
				$("#commentName").removeClass("off");
				$("#commentName").val("");
				$("#commentName").focus();
				return false; 
			}
			else if (formData[i].name == 'commentEmail' 
				&& (formData[i].value == 'Required...' || formData[i].value == '' || !EmailRegEx.exec(formData[i].value))) {
				$("#error").removeClass("hide");	
				$("#error").html("Please enter a valid Email");
				$("#commentEmail").removeClass("off");
				$("#commentEmail").val("");
				$("#commentEmail").focus();				
				return false; 
			}        
			else if ($("#comment").val() == "" || formData[i].value == 'Click here to begin your comment...') {
				$("#error").removeClass("hide");
				$("#error").html("Please enter a comment");
				$("#comment").removeClass("off");
				$("#comment").val("");
				$("#comment").focus();	
				return false; 
			} 				
    }
    return true;
}

function ClearBox(element)
{
	if (element.name == 'comment' && element.value == 'Click here to begin your comment...') {
		element.value = '';
		$("#comment").removeClass("off");
		$("#input").removeClass("hide");		
	}
	else if (element.value == 'Required...') {
		if(element.name == 'commentName')
			$("#commentName").removeClass("off");
		else if(element.name == 'commentEmail')
			$("#commentEmail").removeClass("off");
			
			element.value = '';
	}
}
		
