// JavaScript Document

window.onload = function() {
	contactSend();
	contactFocus();
	paySend();
	requestSend();
	printSizzlinCoupon();
	shippingFocus();
	shippingSend();
	totalSend();
	serviceContractFocus();
	serviceContractSend();
}

        
/* focus on contact form */

function contactFocus() {
	
	if(!document.contact_form) {
		return;
	}
	
	var name = document.contact_form.name;
	name.focus();
	
}

/* -- [ contact form validation ] -- */

function valContactForm() {
	
	if(!document.contact_form) {
		return;
	}
	
	var name_value = document.contact_form.name.value;
	var email_value = document.contact_form.email.value;
	var email_regex = /^([\w]+)(.[\w]+)*@([\w]+)(.[\w]{2,3}){1,2}$/;	
	var phone_value = document.contact_form.phone.value;
	var phone_regex = /^\([0-9]{3}\)\s?[0-9]{3}(-|\s)?[0-9]{4}$|^[0-9]{3}-?[0-9]{3}-?[0-9]{4}$/;
	
	var message_value = document.contact_form.msg.value;

	var errormsg = '';

	if (name_value == '') {
		errormsg = errormsg + '-You must enter your Name\n';
	}
	
	if (email_value == '' || email_regex.test(email_value) != true) {
		errormsg = errormsg + '-You must enter a valid Email Address\n';
	}

	if (phone_value && phone_regex.test(phone_value) != true) {
		errormsg = errormsg + '-You must enter a valid Phone Number\n';
	}

	if (message_value == '') {
		errormsg = errormsg + '-You must enter a Message\n';
	}
	
	if (errormsg != '') {
		alert('The following errors were found:\n'+errormsg);
		return false;
	}
	
	else { 
		return true; 
	}

}

function contactSend() {
	
	if(!document.contact_form) {
		return;
	}
	
	var contact_form = document.contact_form;
	
	contact_form.onsubmit = function() {
		return valContactForm();
	}
}


/* --  request service form validation -- */

function valRequestForm() {
	
	if(!document.request_form) {
		return;
	}
	
	var req_name_value = document.request_form.request_name.value;
	
	var req_address_value = document.request_form.request_add1.value;
	
	var req_city_value = document.request_form.request_city.value;	
	
	var req_state_value = document.request_form.request_state.value;	
	
	var req_email_value = document.request_form.request_email.value;
	var req_email_regex = /^([\w]+)(.[\w]+)*@([\w]+)(.[\w]{2,3}){1,2}$/;	
	
	var req_phone_value = document.request_form.request_phone.value;
	var req_phone_regex = /^\([0-9]{3}\)\s?[0-9]{3}(-|\s)?[0-9]{4}$|^[0-9]{3}-?[0-9]{3}-?[0-9]{4}$/;
	
	var req_message_value = document.request_form.request_message.value;

	var errormsg = '';

	if (req_name_value == '') {
		errormsg = errormsg + '-You must enter your Name\n';
	}
	
	if (req_address_value == '') {
		errormsg = errormsg + '-You must enter your Address\n';
	}

	if (req_city_value == '') {
		errormsg = errormsg + '-You must enter your City\n';
	}

	if (req_state_value == '' || req_state_value == "Select a State") {
		errormsg = errormsg + '-You must enter your State\n';
	}
	
	if (req_email_value == '' || email_regex.test(req_email_value) != true) {
		errormsg = errormsg + '-You must enter a valid Email Address\n';
	}

	if (req_phone_value && phone_regex.test(req_phone_value) != true) {
		errormsg = errormsg + '-You must enter a valid Phone Number\n';
	}

	if (req_message_value == '') {
		errormsg = errormsg + '-You must enter a Message\n';
	}
	
	if (errormsg != '') {
		alert('The following errors were found:\n'+errormsg);
		return false;
	}
	
	else { 
		return true; 
	}

}

function requestSend() {
	
	if(!document.request_form) {
		return;
	}
	
	var request_form = document.request_form;
	
	request_form.onsubmit = function() {
		return valRequestForm();
	}
}



/* -- checks and sends payment form -- */

function valPayForm() {
	
	if (!document.pay_form) {
	    return;
    }
						    
	var email_regex = /^([\w]+)(.[\w]+)*@([\w]+)(.[\w]{2,3}){1,2}$/;
	
	var email_value = document.pay_form.szemail.value;
	var acc_value = document.pay_form.account.value;
	var inv_value = document.pay_form.invoice.value;
	var amt_value = document.pay_form.chargetotal.value;
	
	var errormsg = '';


	if (email_value != "" && email_regex.test(email_value) != true) {
		errormsg = errormsg + '-Please enter a valid email address\n';
	}	
	
	if (acc_value == '') {
		errormsg = errormsg + '-You must enter your account number\n';
	}

	if (inv_value == '') {
		errormsg = errormsg + '-You must enter your invoice number\n';
	}

	if (amt_value == '') {
		errormsg = errormsg + '-You must enter the amount due\n';
	}

	if (errormsg != '') {
		alert('The following errors were found:\n'+errormsg);
		return false;
	}


	else { 
		return true; 
	}

}


function paySend() {
	
	if(!document.pay_form) {
		return;
	}
	
	var pay_form = document.getElementById("pay_form");
	
	pay_form.onsubmit = function() {
		return valPayForm();
	}
}


/*  print honeywell coupon */

function printSizzlinCoupon() {
	
	if(!document.getElementById("sizzlin-coupon")) {
		return;
	}
	
	var coupon = document.getElementById("sizzlin-coupon");
	
	coupon.onclick = function () {
		pageTracker._trackPageview('/sizzlin-cash-coupon');
		window.print();
		return false;
	}
		
}

/* focus on shipping form */

function shippingFocus() {
	
	if(!document.shipping_form) {
		return;
	}
	
	var name = document.shipping_form.name;
	name.focus();
	
}

/* -- [ shipping form validation ] -- */

function valShippingForm() {
	
	if(!document.shipping_form) {
		return;
	}
	
	var name_value = document.shipping_form.name.value;
	var email_value = document.shipping_form.email.value;
	var email_regex = /^([\w]+)(.[\w]+)*@([\w]+)(.[\w]{2,3}){1,2}$/;	
	var add1_value = document.shipping_form.add1.value;
	var city_value = document.shipping_form.city.value;
	var state_value = document.shipping_form.state.value;
	var zip_value = document.shipping_form.zip.value;
	var zip_regex = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
	
	var errormsg = '';

	if (name_value == '') {
		errormsg = errormsg + '-You must enter your Name\n';
	}
	
	if (email_value == '' || email_regex.test(email_value) != true) {
		errormsg = errormsg + '-You must enter a valid Email Address\n';
	}
	
	if (add1_value == '') {
		errormsg = errormsg + '-You must enter your Address\n';
	}

	if (city_value == '') {
		errormsg = errormsg + '-You must enter your City\n';
	}

	if (state_value == '') {
		errormsg = errormsg + '-You must enter your State\n';
	}

	if (zip_value = '' || zip_regex.test(zip_value) != true) {
		errormsg = errormsg + '-You must enter a valid Zip Code\n';
	}
	
	if (errormsg != '') {
		alert('The following errors were found:\n'+errormsg);
		return false;
	}
	
	else { 
		return true; 
	}

}

function shippingSend() {
	
	if(!document.shipping_form) {
		return;
	}
	
	var shipping_form = document.shipping_form;
	
	shipping_form.onsubmit = function() {
		return valShippingForm();
	}
}


/* -- [ shipping form validation ] -- */

function valTotalForm() {
	
	if(!document.total_form) {
		return;
	}
	
	var shipping_value = document.total_form.shipping.value;
	
	var errormsg = '';

	if (shipping_value == '') {
		errormsg = errormsg + '-Your shipping information has not processed correctly. Please make sure you have JavaScript enabled and try again. \n';
	}
	
	if (errormsg != '') {
		alert('The following errors were found:\n'+errormsg);
		return false;
	}
	
	else { 
		return true; 
	}

}

function totalSend() {
	
	if(!document.total_form) {
		return;
	}
	
	var total_form = document.total_form;
	
	total_form.onsubmit = function() {
		return valTotalForm();
	}
}

/* focus on service contract form */

function serviceContractFocus() {
	
	if(!document.contract_form) {
		return;
	}
	
	var name = document.contract_form.name;
	name.focus();
	
}

/* -- service contract validation  -- */

function valServiceContractForm() {
	
	if(!document.contract_form) {
		return;
	}
	
	var name_value = document.contract_form.name.value;
	var city_value = document.contract_form.city.value;
	var zip_value = document.contract_form.zip.value;
	var zip_regex = /(^\d{5}$)|(^\d{5}-\d{4}$)/;	
	var phone_value = document.contract_form.phone.value;
	var phone_regex = /^\([0-9]{3}\)\s?[0-9]{3}(-|\s)?[0-9]{4}$|^[0-9]{3}-?[0-9]{3}-?[0-9]{4}$/;
	var email_value = document.contract_form.email.value;
	var email_regex = /^([\w]+)(.[\w]+)*@([\w]+)(.[\w]{2,3}){1,2}$/;	
	

	var errormsg = '';

	if (name_value == '') {
		errormsg = errormsg + '-You must enter your Name\n';
	}

	if (zip_value == '' || zip_regex.test(zip_value) != true) {
		errormsg = errormsg + '-You must enter a valid Zip Code\n';
	}
	
	if (phone_value == '' || phone_regex.test(phone_value) != true) {
		errormsg = errormsg + '-You must enter a valid Phone Number\n';
	}

	if (email_value == '' || email_regex.test(email_value) != true) {
		errormsg = errormsg + '-You must enter a valid Email Address\n';
	}
	
	if (errormsg != '') {
		alert('The following errors were found:\n'+errormsg);
		return false;
	}
	
	else { 
		return true; 
	}

}

function serviceContractSend() {
	
	if(!document.contract_form) {
		return;
	}
	
	var contract_form = document.contract_form;
	
	contract_form.onsubmit = function() {
		return valServiceContractForm();
	}
}
