$(document).ready(function() {
	$('#errorMessage').hide();
	$('#cerrorMessage').hide();
	
	$('#inquiriesForm').submit(function() {
		var contactName = trim($('#name').val());
		var company 	= trim($('#company').val());
		var email 		= trim($('#email').val());
		var pnumber 	= trim($('#pnumber').val());
		var product 	= trim($('#product').val());
		var amount 		= trim($('#amount').val());
		
		if(contactName.length == 0) {
			$('#errorMessage').html("Contact Name is a mandatory field");
			$('#errorMessage').show();
			$('#name').focus();
			return false;
		}
		else if(company.length == 0) {
			$('#errorMessage').html("Company is a mandatory field");
			$('#errorMessage').show();
			$('#company').focus();
			return false;
		}
		else if(email.length == 0) {
			$('#errorMessage').html("Email is a mandatory field");
			$('#errorMessage').show();
			$('#email').focus();
			return false;
		}
		else if(!(new RegExp(/^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/).test(email))) {
			$('#errorMessage').html("Invalid email address");
			$('#errorMessage').show();
			$('#email').focus();
			return false;
		}
		else if(pnumber.length == 0) {
			$('#errorMessage').html("Phone Number is a mandatory field");
			$('#errorMessage').show();
			$('#pnumber').focus();
			return false;
		}
		else if(product.length == 0 || product == 'Select Product') {
			$('#errorMessage').html("Product is a mandatory field");
			$('#errorMessage').show();
			$('#product').focus();
			return false;
		}
		else if(amount.length == 0 || isNaN(amount)) {
			$('#errorMessage').html("Invalid number entered");
			$('#errorMessage').show();
			$('#amount').focus();
			return false;
		}
		$('#errorMessage').hide();
		return true;
	});
	
	$('#contactForm').submit(function() {
		var name 	= trim($('#cname').val());
		var email 	= trim($('#cemail').val());
		var subject = trim($('#subject').val());
		var message = trim($('#message').val());
		
		if(name.length == 0) {
			$('#cerrorMessage').html("Your Name is a mandatory field");
			$('#cerrorMessage').show();
			$('#cname').focus();
			return false;
		}
		else if(email.length == 0) {
			$('#cerrorMessage').html("Your Email is a mandatory field");
			$('#cerrorMessage').show();
			$('#cemail').focus();
			return false;
		}
		else if(!(new RegExp(/^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/).test(email))) {
			$('#cerrorMessage').html("Invalid email address");
			$('#cerrorMessage').show();
			$('#cemail').focus();
			return false;
		}
		else if(subject.length == 0) {
			$('#cerrorMessage').html("Subject is a mandatory field");
			$('#cerrorMessage').show();
			$('#subject').focus();
			return false;
		}
		else if(message.length == 0) {
			$('#cerrorMessage').html("Message is a mandatory field");
			$('#cerrorMessage').show();
			$('#message').focus();
			return false;
		}
		
		$('#cerrorMessage').hide();
		return true;
	});
});

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
