$(".error").hide();

var validation_input=new Array("name","email","phone");

function validate_error(index){
	$("#error"+index).show();
}

function validate_check(index){
	element = document.getElementById(validation_input[index]);
	
	if(index == 0){ 
		var expression = /^\s{1,}$/;
		if(expression.test(element.value) || element.value.length == 0){validate_error(index); return false;}
		return true;
	}
	else if(index == 1){
		var expression = /^[^@]+@[^@]+.[a-z]{2,}$/;
		if (!expression.test(element.value)){validate_error(index); return false;}
		return true;
	}
	else if(index == 2){
		var expression = /^\d{3}-\d{3}-\d{4}$/;
		var expressionAlt = /^\d{10}$/
		if (!expression.test(element.value) && !expressionAlt.test(element.value)){return true;}
		return true;
	}
	
}

function validate_generic(){
	
	var validation=true;
	$(".error").hide();
	// Check Fields For Correct Input
	for(x=0;validation_input[x];x++){
		if(!validate_check(x)){validation=false;}
	}
	
	if(validation==false){return false;}
	
	$.ajax({
		url: $("#stdForm").attr("action"),
		data:$("#stdForm").serialize(),
		error: function(){
				$("#stdFormWrapper").html("<br/><h3>Error</h3>There is a problem with submitting your request. If the problem persists you can contact us directly at support@jpautomotivemarketing.com.<br/><br/>");
		},
		success: function(data){

				if(data==" "){
					$("#stdFormWrapper").html("<br/><h3>Thank You!</h3>Your information has been submitted and we will respond shortly.<br/><br/>");
				}
				else{
					$("#stdFormWrapper").html(data);
				}
				
		}
	});
	
	return false;
	
}

