function validProductForm()
{
	var value = $("#product_list").val();
	if (value.length < 2)
	{
		alert("Please select a product.");
		return false;
	}
	var value = $("#order_type_list").val();
	if (value.length < 2)
	{
		alert("Please select type of printing.");
		return false;
	}
	var value = $("#product_name_list").val();
	if (value.length < 2)
	{
		alert("Please select a size.");
		return false;
	}
	var value = $("#stock_list").val();
	if (value.length < 2)
	{
		alert("Please select a paper stock.");
		return false;
	}
	var value = $("#quantity_list").val();
	if (value.length < 2)
	{
		alert("Please select a quantity.");
		return false;
	}
	var value = $("#proof_list").val();
	if (value.indexOf('Select') >= 0)
	{
		alert("Please select a proofing method.");
		return false;
	}
}


function requireShippingField()
{
	return (($("#shipping").val() != 'no' && !$("#same_as_above").is(':checked')) ? true : false);
}


function requireContactField()
{
	return (($("#shipping").val() != 'no' && $("#same_as_above").is(':checked')) ? true : false);
}


$(function(){
		
	var name_regex  = "([a-zA-Z '-]+)";
	var email_regex  = /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/ ;
	var phone_regex  = /(\+)?([-\._\(\) ]?[\d]{3,20}[-\._\(\) ]?){2,10}/;
	var num_regex  = "[0-9]";
	var alpha_regex  = "[a-zA-Z ]";
	var alphnum_regex  = "[0-9a-zA-Z ]";

	$("#name").constrain({
		allow:	{ regex: name_regex} 
	});
	
	$("#email").constrain({
		allow:	{ regex: email_regex} 
	});
	
	$("#phone").constrain({
		allow:	{ regex: phone_regex} 
	});
	
	$("#city").constrain({
		allow:	{ regex: name_regex} 
	});
	
	$("#province").constrain({
		allow:	{ regex: alpha_regex} 
	});
	
	$("#postal_code").constrain({
		allow:	{ regex: alphnum_regex} 
	});
	
	$("#country").constrain({
		allow:	{ regex: alpha_regex} 
	});
	
	$("#shipping_name").constrain({
		allow:	{ regex: name_regex} 
	});
	
	$("#shipping_phone").constrain({
		allow:	{ regex: phone_regex} 
	});
	
	$("#shipping_city").constrain({
		allow:	{ regex: name_regex} 
	});
	
	$("#shipping_province").constrain({
		allow:	{ regex: alpha_regex} 
	});
	
	$("#shipping_country").constrain({
		allow:	{ regex: alpha_regex} 
	});
	
	$("#phone").mask("(999) 999 - 9999");
	$("#shipping_phone").mask("(999) 999 - 9999");

	$("#cc_firstname").constrain({
		allow:	{ regex: name_regex} 
	});
	
	$("#cc_lastname").constrain({
		allow:	{ regex: name_regex} 
	});
		
	var creditcard = $("#cc_cardnum").mask("9999 9999 9999 9999");
	$("#cc_type").change(
	function() {
		switch ($(this).val()){
			case 'AmEx':
			creditcard.unmask().mask("9999 999999 99999");
			break;
			default:
			creditcard.unmask().mask("9999 9999 9999 9999");
			break;
		}
	 }); 

	$("#cc_code").constrain({
		allow:	{ regex: num_regex} 
	});
	
	$("#captcha").constrain({
		allow:	{ regex: alphnum_regex} 
	});


	$.validator.messages.required = false;

	var checkBoxError;
	
	var contactValidation = $("#payment_info_form").validate({
	
		onfocusout: false,
		onkeyup: false,
		onclick: false,
		
		highlight: function(element, errorClass) {
		
			var fetch_object = document.getElementById($(element).attr('id') + '_error');
			if (fetch_object != null) fetch_object.className = 'fieldErrorShow';
		},	
		unhighlight: function(element, validClass) {
			var fetch_object = document.getElementById($(element).attr('id') + '_error');
			if (fetch_object != null) fetch_object.className = 'fieldErrorHide';
		},

		rules: {
			/*
			name: {
				required: true
			},
			email: {
				required: true
			},
			phone: {
				required: true
			},
			address: {
				required: function(){ return requireContactField(); }
			},
			city: {
				required: function(){ return requireContactField(); }
			},
			province_list: {
				required: function(){
					if($("#country").val() != 'US')
					{
						return requireContactField();
					} else {
						return false;
					}
				}
			},
			state_list: {
				required: function(){
					if ($("#country").val() == 'US')
					{
						return requireContactField();
					} else {
						return false;
					}
				}
			},
			postal_code: {
				required: true// function(){ return requireContactField(); }
			},
			country: {
				required: function(){ return requireContactField(); }
			},
			shipping: {
				required: true
			},
			shipping_name: {
				required: function(){ return requireShippingField(); }
			},
			shipping_phone: {
				required: function(){ return requireShippingField(); }
			},
			shipping_address: {
				required: function(){ return requireShippingField(); }
			},
			shipping_city: {
				required: function(){ return requireShippingField(); }
			},
			shipping_province_list: {
				required: function(){
					if($("#shipping_country").val() != 'US')
					{
						return requireShippingField();
					} else {
						return false;
					}
				}
			},
			shipping_state_list: {
				required: function(){
					if ($("#shipping_country").val() == 'US')
					{
						return requireShippingField();
					} else {
						return false;
					}
				}
			},
			shipping_postal_code: {
				required: function(){ return requireShippingField(); }
			},
			shipping_country: {
				required: function(){ return requireShippingField(); }
			},
			*/
			cc_firstname: {
				required: true
			},
			cc_lastname: {
				required: true
			},
			cc_street_number: {
				required: true
			},
			cc_street_name: {
				required: true
			},
			cc_postal_code: {
				required: true
			},
			cc_cardnum: {
				//creditcard: true
				creditcard2: function(){ return $('#cc_type').val(); }
			},
			cc_year: {
				required: true
			},
			cc_month: {
				required: true
			},
			/*
			cc_authorize:
			{
				required: function(){
							if ($("#cc_authorize").is(':checked')) 
							{ 						  		
								return false;
								checkBoxError = null;
							} else { 
								checkBoxError = "We cannot process your order without your payment authorization. Please make sure you have check the box.";
								return true;
							}}
			},
			*/
			cc_code: {
				required: true,
				rangelength: [3, 4]
			},
			/*
			captcha: {
				required: true
			},
			*/
			tnc_agreement:
			{
				required: function(){
							if ($("#tnc_agreement").is(':checked')) 
							{ 						  		
								return false;
								checkBoxError = null;
							} else { 
								checkBoxError = "You must agree to our terms and conditions to proceed with your order.";
								return true;
							}}
			}
		},
		invalidHandler: function(e, validator) {
			var new_position = $('#orderPaymentInfoWrapper').offset();
			window.scrollTo(new_position.left,new_position.top);
			 var errors = validator.numberOfInvalids();
			 if (errors) {
				var message = errors == 1
				? '1 field below is empty or invalid, please correct and try again'
				: errors + ' fields below are empty or invalid, please correct and try again.<br/>';
				$("div.jsError").html(message);
				$("div.jsError").show();
				if (errors == 1 && checkBoxError.length > 4)
				{
					alert(checkBoxError);
					$("#cc_authorize").focus;
				}/* else {
					alert("Please fill in and correct all error fields highlighted in red.");
				}*/
			 } else {
				 $("div.jsError").hide();
			 }
		}
	});
	
	
	var billingValidation = $("#billing_info_form").validate({
	
		onfocusout: false,
		onkeyup: false,
		onclick: false,
		

		highlight: function(element, errorClass) {
		
			var fetch_object = document.getElementById($(element).attr('id') + '_error');
			if (fetch_object != null) fetch_object.className = 'fieldErrorShow';
		},	
		unhighlight: function(element, validClass) {
			var fetch_object = document.getElementById($(element).attr('id') + '_error');
			if (fetch_object != null) fetch_object.className = 'fieldErrorHide';
		},

		rules: {
			name: {
				required: true
			},
			email: {
				required: true
			},
			phone: {
				required: true
			},
			address: {
				required: true
			},
			city: {
				required: true
			},
			province_list: {
				required: function(){
					if($("#country").val() != 'US')
					{
						return true;
					} else {
						return false;
					}
				}
			},
			state_list: {
				required: function(){
					if ($("#country").val() == 'US')
					{
						return true;
					} else {
						return false;
					}
				}
			},
			postal_code: {
				required: true
			},
			country: {
				required: true
			},
			shipping: {
				required: true
			},
			shipping_name: {
				required: function(){ return requireShippingField(); }
			},
			shipping_phone: {
				required: function(){ return requireShippingField(); }
			},
			shipping_address: {
				required: function(){ return requireShippingField(); }
			},
			shipping_city: {
				required: function(){ return requireShippingField(); }
			},
			shipping_province_list: {
				required: function(){
					if($("#shipping_country").val() != 'US')
					{
						return requireShippingField();
					} else {
						return false;
					}
				}
			},
			shipping_state_list: {
				required: function(){
					if ($("#shipping_country").val() == 'US')
					{
						return requireShippingField();
					} else {
						return false;
					}
				}
			},
			shipping_postal_code: {
				required: function(){ return requireShippingField(); }
			},
			shipping_country: {
				required: function(){ return requireShippingField(); }
			},
			job_name: {
				required: true
			}
		},
		invalidHandler: function(e, validator) {
			var new_position = $('#orderBillingInfoWrapper').offset();
			var shipping_position = $('#orderShippingDetailWrapper').offset();
			if (requireShippingField() && $("#postal_code").valid())
			{
				window.scrollTo(shipping_position.left,shipping_position.top);	
			}
			else
			{
				window.scrollTo(new_position.left,new_position.top);	
			}
			var errors = validator.numberOfInvalids();
			 if (errors) {
				var message = errors == 1
				? '1 field below is empty or invalid, please correct and try again'
				: errors + ' fields below are empty or invalid, please correct and try again.<br/>';
				$("div.jsError").html(message);
				$("div.jsError").show();
			 } else {
				 $("div.jsError").hide();
			 }
		}
	});
	
});

function inputfieldValidation(object, type)
{
	var error_detected = true;
	var name_regex  = "([a-zA-Z '-]+)";
	var email_regex  = /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/ ;
	var phone_regex  = /(\+)?([-\._\(\) ]?[\d]{3,20}[-\._\(\) ]?){2,10}/;
	var num_regex  = "[0-9]";
	var alpha_regex  = "[a-zA-Z ]";
	var alphnum_regex  = "[0-9a-zA-Z ]";
	var skipValidation = false;
	var object_id = object.id;

	switch(type)
	{
		case "name":
			myregexp = new RegExp(name_regex);
			break;
		case "email":
			myregexp = new RegExp(email_regex);
			break;
		case "phone":
			myregexp = new RegExp(phone_regex);
			break;
		case "number":
		case "creditcard":
			myregexp = new RegExp(num_regex);
			break;
		case "alpha":
			myregexp = new RegExp(alpha_regex);
			break;
		case "alphanumeric":
			myregexp = new RegExp(alphnum_regex);
			break;
	}
	if (object_id == 'address' || object_id == 'postal_code' || 
			object_id == 'country' || object_id == 'city' ||
			object_id == 'province_list' || object_id == 'state_list')
	{
		if (!requireContactField())
		{
			skipValidation = true;
		}
	}
		
	if (!skipValidation)
	{
		if (type == 'length')
		{
			if (object.value.length >= 4)
			{
				error_detected = false;
			}
		} else if (type == 'creditcard')
		{
			var tmp_value = (object.value).replace(/ /g,'');
			if (tmp_value.indexOf('_') < 0)
			{
				if (tmp_value.length == 15 || tmp_value.length == 16)
				{
					if (tmp_value.match(myregexp)) {
						error_detected = false;
					}
				}
			}
		} else {
			if (object.value.match(myregexp)) {
				error_detected = false;
			}
		}
		
		var error_object = document.getElementById(object_id + '_error');
		if (error_detected)
		{
			error_object.className = 'fieldErrorShow';
		} else {
			error_object.className = 'fieldErrorHide';
		}
	}
}