//Contact form
function doContact() {		
	
	//set error to false, will be set to true if we encounter any errors
	var err = false;

	//check form variables, if they don't validate then add the error class to the inputs and set the error variable to true 
	if ($('#fname').val() == '') { $('#fname').addClass('error'); err = true; } 
	else { $('#fname').removeClass('error'); }
	
	if ($('#email').val() == '' || echeck($('#email').val()) == false) { $('#email').addClass('error'); err = true; } 
	else { $('#email').removeClass('error'); }
	
	if ($('#comments').val() == '') { $('#comments').addClass('error'); err = true; } 
	else { $('#comments').removeClass('error'); }

	//check to see if we have an error, if not we can send the form but if we do then well show the error message
	if (err == false) {	
	$('.formmsg').hide();
	    //get variables to send
		var data = $("#cform").serialize();		
		//hide contact form
		$('#cform').hide();
		//teplace html form with loading image
		$('.formmsg').html("<img src='images/ajax-loader.gif' />").show();
		//make the ajax call to the form handler
		$.post('actions/contact.action.php', data, function() {
			//if ajax call is successful then show success message												
			$('.formmsg').html('Thank you for taking the time to contact us, please stay tuned for a response.'),show();
		});			
	} else {
		//form did not validate, show error message
		$('.formmsg').show();	
	}	
	
}	

//Order form
function doOrder() {		
	
	//set error to false, will be set to true if we encounter any errors
	var err = false;

	//check form variables, if they don't validate then add the error class to the inputs and set the error variable to true 
	if ($('#fname').val() == '') { $('#fname').addClass('error'); err = true; } 
	else { $('#fname').removeClass('error'); }

	if ($('#lname').val() == '') { $('#lname').addClass('error'); err = true; } 
	else { $('#lname').removeClass('error'); }

	if ($('#company').val() == '') { $('#company').addClass('error'); err = true; } 
	else { $('#company').removeClass('error'); }

	if ($('#street').val() == '') { $('#street').addClass('error'); err = true; } 
	else { $('#street').removeClass('error'); }

	if ($('#city').val() == '') { $('#city').addClass('error'); err = true; } 
	else { $('#city').removeClass('error'); }

	if ($('#state').val() == '') { $('#state').addClass('error'); err = true; } 
	else { $('#state').removeClass('error'); }

	if ($('#zip').val() == '') { $('#zip').addClass('error'); err = true; } 
	else { $('#zip').removeClass('error'); }

	if ($('#phone').val() == '') { $('#phone').addClass('error'); err = true; } 
	else { $('#phone').removeClass('error'); }

	if ($('#vname').val() == '') { $('#vname').addClass('error'); err = true; } 
	else { $('#vname').removeClass('error'); }

	if ($('#vphone').val() == '') { $('#vphone').addClass('error'); err = true; } 
	else { $('#vphone').removeClass('error'); }
	
	if ($('#email').val() == '' || echeck($('#email').val()) == false) { $('#email').addClass('error'); err = true; } 
	else { $('#email').removeClass('error'); }
	
	if ($('#comments').val() == '') { $('#comments').addClass('error'); err = true; } 
	else { $('#comments').removeClass('error'); }

	//check to see if we have an error, if not we can send the form but if we do then well show the error message
	if (err == false) {	
	$('.formmsg').hide();
	    //get variables to send
		var data = $("#oform").serialize();		
		//hide contact form
		$('#oform').hide();
		//teplace html form with loading image
		$('.ordermsg').html("<img src='../images/ajax-loader.gif' />").show();
		//make the ajax call to the form handler
		$.post('../actions/order.action.php', data, function() {
			//if ajax call is successful then show success message												
			$('.ordermsg').html('Thank you for your order, we will give you a call to confirm your order shortly.'),show();
		});			
	} else {
		//form did not validate, show error message
		$('.ordermsg').show();
		window.location = '#oformtop';
	}	
	
}	

//Validate email function
function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1) {
		   return false
		}
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){

		    return false
		 }
		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }
		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }
 		 return true;	
}