/*****************************/
/* Messaging Functions       */
/*****************************/
var TIMER = 5, SPEED = 10, WRAPPER = 'wrapper', TIMEOUTID, DIALOGACTIVE = 0, DIALOGFOCUSTARGET;

function pageWidth() {
	return window.innerWidth ? window.innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body ? document.body.clientWidth : null;
}

function pageHeight() {
	return window.innerHeight ? window.innerHeight : document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body ? document.body.clientHeight : null;
}

function topPosition() {
	return typeof window.pageYOffset !== 'undefined' ? window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;
}

function leftPosition() {
	return typeof window.pageXOffset !== 'undefined' ? window.pageXOffset : document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0;
}

function fadeDialog(flag) {
	var value, dialog = document.getElementById('dialog');

	if (flag === null || flag === 1) {
		value = dialog.alpha + SPEED;
	} else {
		value = dialog.alpha - SPEED;
	}

	dialog.alpha = value;
	dialog.style.opacity = (value / 100);
	dialog.style.filter = 'alpha(opacity=' + value + ')';
	if (value >= 99) {
		clearInterval(dialog.timer);
	} else if (value <= 1) {
		dialog.style.visibility = "hidden";
		document.getElementById('dialog-mask').style.visibility = "hidden";
		clearInterval(dialog.timer);
	}
}

function hideDialog() {
	var dialog = document.getElementById('dialog');

	clearInterval(dialog.timer);
	dialog.timer = setInterval(function() { fadeDialog(0); }, TIMER);
	if (TIMEOUTID) {
		window.clearTimeout(TIMEOUTID);
	}
	if (DIALOGFOCUSTARGET) {
		DIALOGFOCUSTARGET.focus();
	}
	DIALOGACTIVE = 0;
	DIALOGFOCUSTARGET = null;
}

function showDialog(title, message, type, autohide, focustarget) {
	var width = pageWidth(), height = pageHeight(), left = leftPosition(), top = topPosition(), 
	    dialog, dialogheader, dialogclose, dialogtitle, dialogcontent, dialogmask, 
	    dialogwidth, dialogheight, topposition, leftposition, content,
	    closeTxt = "<br/><br/><div id='dialog-cbdiv' style='text-align:center;'><input class='sbutton' type='button' id='dialog-cb' name='closeDiag' value='Close' onclick='javascript:hideDialog();' /></div>";

	if (!type) {
		type = 'error';
	}

	if (!document.getElementById('dialog')) {
		dialog = document.createElement('div');
		dialog.id = 'dialog';
		dialogheader = document.createElement('div');
		dialogheader.id = 'dialog-header';
		dialogtitle = document.createElement('div');
		dialogtitle.id = 'dialog-title';
		dialogclose = document.createElement('div');
		dialogclose.id = 'dialog-close';
		dialogcontent = document.createElement('div');
		dialogcontent.id = 'dialog-content';
		dialogmask = document.createElement('div');
		dialogmask.id = 'dialog-mask';
		document.body.appendChild(dialogmask);
		document.body.appendChild(dialog);
		dialog.appendChild(dialogheader);
		dialogheader.appendChild(dialogtitle);
		dialogheader.appendChild(dialogclose);
		dialog.appendChild(dialogcontent);
		dialogclose.setAttribute('onclick','hideDialog()');
		dialogclose.onclick = hideDialog;
	} else {
		dialog = document.getElementById('dialog');
		dialogheader = document.getElementById('dialog-header');
		dialogtitle = document.getElementById('dialog-title');
		dialogclose = document.getElementById('dialog-close');
		dialogcontent = document.getElementById('dialog-content');
		dialogmask = document.getElementById('dialog-mask');
		dialogmask.style.visibility = "visible";
		dialog.style.visibility = "visible";
	}

	dialog.style.opacity = 0.00;
	dialog.style.filter = 'alpha(opacity=0)';
	dialog.alpha = 0;
	if (autohide && autohide !== 0) {
		dialogmask.style.opacity = 0.00;
		dialogmask.style.filter = 'alpha(opacity=0)';
		dialogmask.alpha = 0;
	} else {
		dialogmask.style.opacity = 0.75;
		dialogmask.style.filter = 'alpha(opacity=75)';
		dialogmask.alpha = 75;
	}
	dialogwidth = dialog.offsetWidth;
	dialogheight = dialog.offsetHeight;
	topposition = top + (height / 2) - (dialogheight / 2);
	leftposition = left + (width / 2) - (dialogwidth / 2);
	dialog.style.top = topposition + "px";
	dialog.style.left = leftposition + "px";
	dialogheader.className = type + "header";
	dialogtitle.innerHTML = title;
	dialogcontent.className = type;
	dialogcontent.innerHTML = message + closeTxt;
	content = document.getElementById(WRAPPER);
	dialogmask.style.height = (document.body.scrollHeight+20) + 'px';
	document.getElementById('dialog-cb').focus();
	DIALOGACTIVE = 1;
	DIALOGFOCUSTARGET = focustarget;
	clearInterval(dialog.timer);
	dialog.timer = setInterval(function() { fadeDialog(1); }, TIMER);

	if (autohide && autohide !== 0) {
		dialogclose.style.visibility = "hidden";
		TIMEOUTID = window.setTimeout("hideDialog()", (autohide * 1000));
	} else {
		dialogclose.style.visibility = "visible";
	}
}


/*****************************/
/* General Use Functions     */
/*****************************/
function isNum(passed)
{
	var i;
	for (i = 0; i < passed.length; i += 1) {
		if (passed.charAt(i) < "0") {
			return false;
		}
		if (passed.charAt(i) > "9") {
			return false;
		}
	}
	return true;
}

function isNumDecimal(passed)
{
	var i;
	for (i = 0; i < passed.length; i += 1) {
		if (passed.charAt(i) === "." || passed.charAt(i) === "$") {
			continue;
		}
		if (passed.charAt(i) < "0") {
			return false;
		}
		if (passed.charAt(i) > "9") {
			return false;
		}
	}
	return true;
}

function addToCart(key, quantity, feedback)
{
	if (!isNum(quantity)) {
		showDialog('Shopping Cart', 'The quantity must be numeric', 'error', 0, document.getElementById('entry_' + key));
		return false;
	}
	
	var expireDate = new Date();
	expireDate.setHours(expireDate.getHours() + 24);
	document.cookie = key + "=" + quantity + ";expires=" + expireDate.toGMTString();
	if (feedback === 1) {
		showDialog('Shopping Cart', 'Item successfully added!', 'success', 3);
	}
	return true;
}

function addBadgeToCart(key, quantity, startnum, typ, feedback)
{
	if (!isNum(quantity)) {
		showDialog('Shopping Cart', 'The quantity must be numeric', 'error', 0, document.getElementById('entry_' + key));
		return false;
	}
	if (!isNum(startnum)) {
		showDialog('Shopping Cart', 'The starting number must be numeric', 'error', 0, document.getElementById('entrysn_' + key));
		return false;
	}
	if (typ === "pcez" && startnum.length > 3) {
		showDialog('Shopping Cart', 'PayClock EZ badges can only be 3 digits or less', 'error', 0, document.getElementById('entrysn_' + key));
		return false;
	}
	if (typ === "am" && startnum.length > 3) {
		showDialog('Shopping Cart', 'Amano badges can only be 3 digits or less', 'error', 0, document.getElementById('entrysn_' + key));
		return false;
	}
	if (typ === "quest" && startnum.length > 3) {
		showDialog('Shopping Cart', 'Quest ETC badges can only be 3 digits or less', 'error', 0, document.getElementById('entrysn_' + key));
		return false;
	}

	var expireDate = new Date();
	expireDate.setHours(expireDate.getHours() + 24);
	document.cookie = key + "=" + quantity + "?" + startnum + "|" + typ + ";expires=" + expireDate.toGMTString();
	if (feedback === 1) {
		showDialog('Shopping Cart', 'Item successfully added!', 'success', 3);
	}
	return true;
}


/*****************************/
/* Functions for cart.php    */
/*****************************/
function removeFromCart(key)
{
	var expireDate = new Date();
	expireDate.setDate(expireDate.getDate() - 1);
	document.cookie = key + "=;expires=" + expireDate.toGMTString();
	location.reload();
	return true;
}

function removeAll()
{
	if (window.confirm('Are you sure you want to clear your cart?') && document.cookie !== "") {
		var i, cookieName, thisCookie = document.cookie.split("; "), expireDate = new Date();

		expireDate.setDate(expireDate.getDate() - 1);
		for (i = 0; i < thisCookie.length; i += 1) {
			cookieName = thisCookie[i].split("=")[0];
			document.cookie = cookieName + "=;expires=" + expireDate.toGMTString();
		}
	}
	location.reload();
}

function sendCart()
{
	var expireDate = new Date();
	expireDate.setHours(expireDate.getHours() + 24);
	document.cookie = "subtotal=" + document.forms.myCart.subtotal.value + ";expires=" + expireDate.toGMTString();
	document.forms.myCart.submit();
}


/*****************************/
/* Functions for order.php   */
/*****************************/
function orderEnableShip(checkVal)
{
	document.forms.myOrderForm.sfname.disabled = !checkVal;
	document.forms.myOrderForm.slname.disabled = !checkVal;
	document.forms.myOrderForm.scname.disabled = !checkVal;
	document.forms.myOrderForm.saddress1.disabled = !checkVal;
	document.forms.myOrderForm.saddress2.disabled = !checkVal;
	document.forms.myOrderForm.scity.disabled = !checkVal;
	document.forms.myOrderForm.sstate.disabled = !checkVal;
	document.forms.myOrderForm.szipcode.disabled = !checkVal;
	document.forms.myOrderForm.sphone.disabled = !checkVal;
	document.forms.myOrderForm.sphe.disabled = !checkVal;

	document.forms.myOrderForm.sfname.style.background = checkVal?"white":"#D3D3D3";
	document.forms.myOrderForm.slname.style.background = checkVal?"white":"#D3D3D3";
	document.forms.myOrderForm.scname.style.background = checkVal?"white":"#D3D3D3";
	document.forms.myOrderForm.saddress1.style.background = checkVal?"white":"#D3D3D3";
	document.forms.myOrderForm.saddress2.style.background = checkVal?"white":"#D3D3D3";
	document.forms.myOrderForm.scity.style.background = checkVal?"white":"#D3D3D3";
	document.forms.myOrderForm.sstate.style.background = checkVal?"white":"#D3D3D3";
	document.forms.myOrderForm.szipcode.style.background = checkVal?"white":"#D3D3D3";
	document.forms.myOrderForm.sphone.style.background = checkVal?"white":"#D3D3D3";
	document.forms.myOrderForm.sphe.style.background = checkVal?"white":"#D3D3D3";

	if (checkVal === false) {
		document.forms.myOrderForm.sfname.value = "";
		document.forms.myOrderForm.slname.value = "";
		document.forms.myOrderForm.scname.value = "";
		document.forms.myOrderForm.saddress1.value = "";
		document.forms.myOrderForm.saddress2.value = "";
		document.forms.myOrderForm.scity.value = "";
		document.forms.myOrderForm.sstate.value = "ZZ";
		document.forms.myOrderForm.szipcode.value = "";
		document.forms.myOrderForm.sphone.value = "";
		document.forms.myOrderForm.sphe.value = "";
		document.forms.myOrderForm.diffShip.checked = false;
	}
}

function orderValidField(myField)
{
	var i, invalidChars = "?!$%^*|<>";
	for (i = 0; i < invalidChars.length; i += 1) {
		if (myField.value.indexOf(invalidChars.charAt(i), 0) > -1) {
			return false;
		}
	}
	return true;
}

function orderValidEmail(myEm)
{
	var i, invalidChars = " /:,;!$%?^*|<>", atPos = myEm.indexOf("@", 1), perPos = myEm.indexOf(".", atPos + 2);

	for (i = 0; i < invalidChars.length; i += 1) {
		if (myEm.indexOf(invalidChars.charAt(i), 0) > -1) {
			return false;
		}
	}

	if (atPos === -1) {
		return false;
	}
	if (myEm.indexOf("@", atPos + 1) > -1) {
		return false;
	}
	if (perPos === -1) {
		return false;
	}
	if (perPos + 3 > myEm.length) {
		return false;
	}

	return true;
}

function orderValidShipping(mf)
{
	if (mf.diffShip.checked === false) {
		return true;
	}

	if (mf.sfname.value === "") {
		//mf.sfname.focus();
		showDialog('Order Form', 'You must enter a First Name', 'error', 0, mf.sfname);
		return false;
	}
	if (!orderValidField(mf.sfname)) {
		//mf.sfname.focus();
		showDialog('Order Form', 'Invalid characters in First Name', 'error', 0, mf.sfname);
		return false;
	}
	if (mf.slname.value === "") {
		//mf.slname.focus();
		showDialog('Order Form', 'You must enter a Last Name', 'error', 0, mf.slname);
		return false;
	}
	if (!orderValidField(mf.slname)) {
		//mf.slname.focus();
		showDialog('Order Form', 'Invalid characters in Last Name', 'error', 0, mf.slname);
		return false;
	}
	if (mf.scname.value === "") {
		//mf.scname.focus();
		showDialog('Order Form', 'You must enter a Company Name', 'error', 0, mf.scname);
		return false;
	}
	if (!orderValidField(mf.scname)) {
		//mf.scname.focus();
		showDialog('Order Form', 'Invalid characters in Company Name', 'error', 0, mf.scname);
		return false;
	}
	if (mf.saddress1.value === "") {
		//mf.saddress1.focus();
		showDialog('Order Form', 'You must enter an Address', 'error', 0, mf.saddress1);
		return false;
	}
	if (!orderValidField(mf.saddress1)) {
		//mf.saddress1.focus();
		showDialog('Order Form', 'Invalid characters in Address', 'error', 0, mf.saddress1);
		return false;
	}
	if (!orderValidField(mf.saddress2)) {
		//mf.saddress2.focus();
		showDialog('Order Form', 'Invalid characters in Address', 'error', 0, mf.saddress2);
		return false;
	}
	if (mf.scity.value === "") {
		//mf.scity.focus();
		showDialog('Order Form', 'You must enter a City', 'error', 0, mf.scity);
		return false;
	}
	if (!orderValidField(mf.scity)) {
		//mf.scity.focus();
		showDialog('Order Form', 'Invalid characters in City', 'error', 0, mf.scity);
		return false;
	}
	if (mf.sstate.value === "ZZ") {
		//mf.sstate.focus();
		showDialog('Order Form', 'You must enter a State', 'error', 0, mf.sstate);
		return false;
	}
	if (mf.szipcode.value === "") {
		//mf.szipcode.focus();
		showDialog('Order Form', 'You must enter a Zip Code', 'error', 0, mf.szipcode);
		return false;
	}
	if (!isNum(mf.szipcode.value)) {
		//mf.szipcode.focus();
		showDialog('Order Form', 'Zip Code must be numeric', 'error', 0, mf.szipcode);
		return false;
	}
	if (mf.szipcode.value.length < 5) {
		//mf.szipcode.focus();
		showDialog('Order Form', 'Zip Code must be 5 digits', 'error', 0, mf.szipcode);
		return false;
	}
	if (mf.sphone.value === "") {
		//mf.sphone.focus();
		showDialog('Order Form', 'You must enter a Phone Number', 'error', 0, mf.sphone);
		return false;
	}
	if (!isNum(mf.sphone.value)) {
		//mf.sphone.focus();
		showDialog('Order Form', 'Phone Number must be numeric', 'error', 0, mf.sphone);
		return false;
	}
	if (mf.sphone.value.length < 10) {
		//mf.sphone.focus();
		showDialog('Order Form', 'Phone Number must be 10 digits', 'error', 0, mf.sphone);
		return false;
	}
	if (!isNum(mf.sphe.value)) {
		//mf.sphe.focus();
		showDialog('Order Form', 'Phone Extension must be numeric', 'error', 0, mf.sphe);
		return false;
	}

	return true;
}

function orderContinue()
{
	var mf = document.forms.myOrderForm;

	if (mf.fname.value === "") {
		//mf.fname.focus();
		showDialog('Order Form', 'You must enter a First Name', 'error', 0, mf.fname);
		return false;
	}
	if (!orderValidField(mf.fname)) {
		//mf.fname.focus();
		showDialog('Order Form', 'Invalid characters in First Name', 'error', 0, mf.fname);
		return false;
	}
	if (mf.lname.value === "") {
		//mf.lname.focus();
		showDialog('Order Form', 'You must enter a Last Name', 'error', 0, mf.lname);
		return false;
	}
	if (!orderValidField(mf.lname)) {
		//mf.lname.focus();
		showDialog('Order Form', 'Invalid characters in Last Name', 'error', 0, mf.lname);
		return false;
	}
	if (mf.cname.value === "") {
		//mf.cname.focus();
		showDialog('Order Form', 'You must enter a Company Name', 'error', 0, mf.cname);
		return false;
	}
	if (!orderValidField(mf.cname)) {
		//mf.cname.focus();
		showDialog('Order Form', 'Invalid characters in Company Name', 'error', 0, mf.cname);
		return false;
	}
	if (mf.address1.value === "") {
		//mf.address1.focus();
		showDialog('Order Form', 'You must enter an Address', 'error', 0, mf.address1);
		return false;
	}
	if (!orderValidField(mf.address1)) {
		//mf.address1.focus();
		showDialog('Order Form', 'Invalid characters in Address', 'error', 0, mf.address1);
		return false;
	}
	if (!orderValidField(mf.address2)) {
		//mf.address2.focus();
		showDialog('Order Form', 'Invalid characters in Address', 'error', 0, mf.address2);
		return false;
	}
	if (mf.city.value === "") {
		//mf.city.focus();
		showDialog('Order Form', 'You must enter a City', 'error', 0, mf.city);
		return false;
	}
	if (!orderValidField(mf.city)) {
		//mf.city.focus();
		showDialog('Order Form', 'Invalid characters in City', 'error', 0, mf.city);
		return false;
	}
	if (mf.state.value === "ZZ") {
		//mf.state.focus();
		showDialog('Order Form', 'You must enter a State', 'error', 0, mf.state);
		return false;
	}
	if (mf.zipcode.value === "") {
		//mf.zipcode.focus();
		showDialog('Order Form', 'You must enter a Zip Code', 'error', 0, mf.zipcode);
		return false;
	}
	if (!isNum(mf.zipcode.value)) {
		//mf.zipcode.focus();
		showDialog('Order Form', 'Zip Code must be numeric', 'error', 0, mf.zipcode);
		return false;
	}
	if (mf.zipcode.value.length < 5) {
		//mf.zipcode.focus();
		showDialog('Order Form', 'Zip Code must be 5 digits', 'error', 0, mf.zipcode);
		return false;
	}
	if (mf.phone.value === "") {
		//mf.phone.focus();
		showDialog('Order Form', 'You must enter a Phone Number', 'error', 0, mf.phone);
		return false;
	}
	if (!isNum(mf.phone.value)) {
		//mf.phone.focus();
		showDialog('Order Form', 'Phone Number must be numeric', 'error', 0, mf.phone);
		return false;
	}
	if (mf.phone.value.length < 10) {
		//mf.phone.focus();
		showDialog('Order Form', 'Phone Number must be 10 digits', 'error', 0, mf.phone);
		return false;
	}
	if (!isNum(mf.phe.value)) {
		//mf.phe.focus();
		showDialog('Order Form', 'Phone Extension must be numeric', 'error', 0, mf.phe);
		return false;
	}
	if (!isNum(mf.fax.value)) {
		//mf.fax.focus();
		showDialog('Order Form', 'Fax Number must be numeric', 'error', 0, mf.fax);
		return false;
	}
	if (mf.fax.value.length > 0 && mf.fax.value.length < 10) {
		//mf.fax.focus();
		showDialog('Order Form', 'Fax Number must be 10 digits', 'error', 0, mf.fax);
		return false;
	}
	if (mf.email.value === "") {
		//mf.email.focus();
		showDialog('Order Form', 'You must enter an E-mail address', 'error', 0, mf.email);
		return false;
	}
	if (!orderValidEmail(mf.email.value)) {
		//mf.email.focus();
		showDialog('Order Form', 'Invalid E-mail address', 'error', 0, mf.email);
		return false;
	}
	if (mf.email.value.toLowerCase() !== mf.email2.value.toLowerCase()) {
		//mf.email.focus();
		showDialog('Order Form', 'E-mail addresses do not match', 'error', 0, mf.email);
		return false;
	}

	// Check the shipping stuff
	if (orderValidShipping(mf)) {
		mf.submit();
		return true;
	}

	return false;
}


/*****************************/
/* Functions for ordero.php  */
/*****************************/
function resetOnlineOrder()
{
	var mf = document.forms.myOrderForm;
	mf.cctype.value = "ZZ";
	mf.ccnum.value = "";
	mf.ccmon.value = "  ";
	mf.ccyear.value = "  ";
	mf.ccccv.value = "";
	mf.terms.checked = false;
	return true;
}

function mod10(cardNumber)	// LUHN Formula for validation of credit card numbers.
{
	var ar = [], i, sum = 0;

	for (i = 0; i < cardNumber.length; i += 1) {
		ar[i] = parseInt(cardNumber.charAt(i), 10);
	}
	for (i = ar.length - 2; i >= 0; i -= 2)	{	// you have to start from the right, and work back.
		ar[i] *= 2;				// every second digit starting with the right most (check digit)
		if (ar[i] > 9) {			// will be doubled, and summed with the skipped digits.
			ar[i] -= 9;			// if the double digit is > 9, ADD those individual digits together
		}					// if the sum is divisible by 10, mod10 succeeds
	}
	for (i = 0; i < ar.length; i += 1) {
		sum += ar[i];
	}

	return (((sum % 10) === 0) ? true : false);
}

function sendOnlineOrder()
{
	var mf = document.forms.myOrderForm, length, prefix;

	if (mf.cctype.value === "ZZ") {
		//mf.cctype.focus();
		showDialog('Order Form', 'You must select a Credit Card Type', 'error', 0, mf.cctype);
		return false;
	}
	if (mf.ccnum.value === "") {
		//mf.ccnum.focus();
		showDialog('Order Form', 'You must enter a Credit Card Number', 'error', 0, mf.ccnum);
		return false;
	}
	if (!isNum(mf.ccnum.value)) {
		//mf.ccnum.focus();
		showDialog('Order Form', 'Credit Card Number must be numeric', 'error', 0, mf.ccnum);
		return false;
	}
	if (mf.ccmon.value === "  ") {
		//mf.ccmon.focus();
		showDialog('Order Form', 'You must select an Expiration Month', 'error', 0, mf.ccmon);
		return false;
	}
	if (mf.ccyear.value === "  ") {
		//mf.ccyear.focus();
		showDialog('Order Form', 'You must select an Expiration Year', 'error', 0, mf.ccyear);
		return false;
	}
	if (mf.ccccv.value === "") {
		//mf.ccccv.focus();
		showDialog('Order Form', 'You must enter a Card Code (CCV)', 'error', 0, mf.ccccv);
		return false;
	}
	if (!isNum(mf.ccccv.value)) {
		//mf.ccccv.focus();
		showDialog('Order Form', 'Card Code (CCV) must be numeric', 'error', 0, mf.ccccv);
		return false;
	}

	length = mf.ccnum.value.length;	// perform num checks

	if (mf.cctype.value === "AM") {
		if (length !== 15) {
			//mf.ccnum.focus();
			showDialog('Order Form', 'Please enter a valid American Express Card number', 'error', 0, mf.ccnum);
			return false;
		}
		prefix = parseInt(mf.ccnum.value.substring(0, 2), 10);
		if (prefix !== 34 && prefix !== 37) {
			//mf.ccnum.focus();
			showDialog('Order Form', 'Please enter a valid American Express Card number', 'error', 0, mf.ccnum);
			return false;
		}
	} else if (mf.cctype.value === "DC") {
		if (length !== 16) {
			//mf.ccnum.focus();
			showDialog('Order Form', 'Please enter a valid Discover Card number', 'error', 0, mf.ccnum);
			return false;
		}
		prefix = parseInt(mf.ccnum.value.substring(0, 4), 10);
		if (prefix !== 6011) {
			//mf.ccnum.focus();
			showDialog('Order Form', 'Please enter a valid Discover Card number', 'error', 0, mf.ccnum);
			return false;
		}
	} else if (mf.cctype.value === "MC") {
		if (length !== 16) {
			//mf.ccnum.focus();
			showDialog('Order Form', 'Please enter a valid MasterCard number', 'error', 0, mf.ccnum);
			return false;
		}
		prefix = parseInt(mf.ccnum.value.substring(0, 2), 10);
		if (prefix < 51 || prefix > 55) {
			//mf.ccnum.focus();
			showDialog('Order Form', 'Please enter a valid MasterCard number', 'error', 0, mf.ccnum);
			return false;
		}
	} else if (mf.cctype.value === "VS") {
		if (length !== 16 && length !== 13) {
			//mf.ccnum.focus();
			showDialog('Order Form', 'Please enter a valid Visa Card number', 'error', 0, mf.ccnum);
			return false;
		}
		prefix = parseInt(mf.ccnum.value.substring(0, 1), 10);
		if (prefix !== 4) {
			//mf.ccnum.focus();
			showDialog('Order Form', 'Please enter a valid Visa Card number', 'error', 0, mf.ccnum);
			return false;
		}
	}

	if (!mod10(mf.ccnum.value)) {
		//mf.ccnum.focus();
		showDialog('Order Form', 'Not a valid credit card number', 'error', 0, mf.ccnum);
		return false;
	}

	if (!isNumDecimal(mf.subtot.value) || !isNumDecimal(mf.tax.value) || !isNumDecimal(mf.shipping.value)) {
		alert("Cookie Error! Make sure cookies are enabled.");
		return false;
	}
	if (mf.terms.checked === false) {
		showDialog('Order Form', 'You must agree to the Terms and Conditions to proceed', 'error');
		return false;
	}

	document.forms.myOrderForm.submit();
	return true;
}



/*****************************/
/* Disable Enter Key         */
/*****************************/
function stopRKey(e) 
{
	var evt = e ? e : (event ? event : null),
	    node = evt.target ? evt.target : (evt.srcElement ? evt.srcElement : null);

	if ((DIALOGACTIVE === 1) && (evt.keyCode === 27)) {
		hideDialog();
		return false;
	}

	if ((evt.keyCode === 13) && (node.type === "text")) {
		return false;
	}

	return true;
} 

document.onkeypress = stopRKey; 


/*****************************/
/* Add to Bookmark           */
/*****************************/

function addToBookmark() {
        var title = "TimeClockSupply.com - ID Badges Unlimited Inc.",
            url = "http://www.timeclocksupply.com",
            elem;
                        
        if (window.sidebar) { // Firefox
                window.sidebar.addPanel(title, url, "");
        } else if (window.opera && window.print) { // Opera
                elem = document.createElement('a');
                elem.setAttribute('href',url);
                elem.setAttribute('title',title);
                elem.setAttribute('rel','sidebar');
                elem.click();
        } else if (document.all) { // Internet Explorer
                window.external.AddFavorite(url, title);
        }
}


/*****************************/
/* Lots of stuff for MD5     */
/*****************************/

/*
 * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
 * Digest Algorithm, as defined in RFC 1321.
 * Version 2.1 Copyright (C) Paul Johnston 1999 - 2002.
 * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
 * Distributed under the BSD License
 * See http://pajhome.org.uk/crypt/md5 for more info.
 */

/*
 * Configurable variables. You may need to tweak these to be compatible with
 * the server-side, but the defaults work in most cases.
 */
var hexcase = 0,  /* hex output format. 0 - lowercase; 1 - uppercase        */
    b64pad  = "", /* base-64 pad character. "=" for strict RFC compliance   */
    chrsz   = 8;  /* bits per input character. 8 - ASCII; 16 - Unicode      */

/*
 * Bitwise rotate a 32-bit number to the left.
 */
function bit_rol(num, cnt)
{
	return (num << cnt) | (num >>> (32 - cnt));
}

/*
 * Add integers, wrapping at 2^32. This uses 16-bit operations internally
 * to work around bugs in some JS interpreters.
 */
function safe_add(x, y)
{
	var lsw = (x & 0xFFFF) + (y & 0xFFFF), msw = (x >> 16) + (y >> 16) + (lsw >> 16);
	return (msw << 16) | (lsw & 0xFFFF);
}

/*
 * These functions implement the four basic operations the algorithm uses.
 */
function md5_cmn(q, a, b, x, s, t)
{
	return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s), b);
}
function md5_ff(a, b, c, d, x, s, t)
{
	return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
}
function md5_gg(a, b, c, d, x, s, t)
{
	return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
}
function md5_hh(a, b, c, d, x, s, t)
{
	return md5_cmn(b ^ c ^ d, a, b, x, s, t);
}
function md5_ii(a, b, c, d, x, s, t)
{
	return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
}

/*
 * Convert a string to an array of little-endian words
 * If chrsz is ASCII, characters >255 have their hi-byte silently ignored.
 */
function str2binl(str)
{
	var bin = [], mask = (1 << chrsz) - 1, i;
	for (i = 0; i < str.length * chrsz; i += chrsz) {
		bin[i >> 5] |= (str.charCodeAt(i / chrsz) & mask) << (i % 32);
	}
	return bin;
}

/*
 * Convert an array of little-endian words to a string
 */
function binl2str(bin)
{
	var str = "", mask = (1 << chrsz) - 1, i;
	for (i = 0; i < bin.length * 32; i += chrsz) {
		str += String.fromCharCode((bin[i >> 5] >>> (i % 32)) & mask);
	}
	return str;
}

/*
 * Convert an array of little-endian words to a hex string.
 */
function binl2hex(binarray)
{
	var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef", str = "", i;
	for (i = 0; i < binarray.length * 4; i += 1) {
		str += hex_tab.charAt((binarray[i >> 2] >> ((i % 4) * 8 + 4)) & 0xF) + hex_tab.charAt((binarray[i >> 2] >> ((i % 4) * 8)) & 0xF);
	}
	return str;
}

/*
 * Convert an array of little-endian words to a base-64 string
 */
function binl2b64(binarray)
{
	var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", str = "", i, j, triplet;
	for (i = 0; i < binarray.length * 4; i += 3) {
		triplet = (((binarray[i >> 2] >> 8 * (i % 4)) & 0xFF) << 16) | (((binarray[i + 1 >> 2] >> 8 * ((i + 1) % 4)) & 0xFF) << 8) | ((binarray[i + 2 >> 2] >> 8 * ((i + 2) % 4)) & 0xFF);
		for (j = 0; j < 4; j += 1) {
			if (i * 8 + j * 6 > binarray.length * 32) {
				str += b64pad;
			} else {
				str += tab.charAt((triplet >> 6 * (3 - j)) & 0x3F);
			}
		}
	}
	return str;
}

/*
 * Calculate the MD5 of an array of little-endian words, and a bit length
 */
function core_md5(x, len)
{
	/* append padding */
	x[len >> 5] |= 0x80 << ((len) % 32);
	x[(((len + 64) >>> 9) << 4) + 14] = len;

	var a =  1732584193, b = -271733879, c = -1732584194, d =  271733878, i, olda, oldb, oldc, oldd, ret = [];

	for (i = 0; i < x.length; i += 16) {
		olda = a;
		oldb = b;
		oldc = c;
		oldd = d;

		a = md5_ff(a, b, c, d, x[i + 0], 7 , -680876936);
		d = md5_ff(d, a, b, c, x[i + 1], 12, -389564586);
		c = md5_ff(c, d, a, b, x[i + 2], 17,  606105819);
		b = md5_ff(b, c, d, a, x[i + 3], 22, -1044525330);
		a = md5_ff(a, b, c, d, x[i + 4], 7 , -176418897);
		d = md5_ff(d, a, b, c, x[i + 5], 12,  1200080426);
		c = md5_ff(c, d, a, b, x[i + 6], 17, -1473231341);
		b = md5_ff(b, c, d, a, x[i + 7], 22, -45705983);
		a = md5_ff(a, b, c, d, x[i + 8], 7 ,  1770035416);
		d = md5_ff(d, a, b, c, x[i + 9], 12, -1958414417);
		c = md5_ff(c, d, a, b, x[i + 10], 17, -42063);
		b = md5_ff(b, c, d, a, x[i + 11], 22, -1990404162);
		a = md5_ff(a, b, c, d, x[i + 12], 7 ,  1804603682);
		d = md5_ff(d, a, b, c, x[i + 13], 12, -40341101);
		c = md5_ff(c, d, a, b, x[i + 14], 17, -1502002290);
		b = md5_ff(b, c, d, a, x[i + 15], 22,  1236535329);

		a = md5_gg(a, b, c, d, x[i + 1], 5 , -165796510);
		d = md5_gg(d, a, b, c, x[i + 6], 9 , -1069501632);
		c = md5_gg(c, d, a, b, x[i + 11], 14,  643717713);
		b = md5_gg(b, c, d, a, x[i + 0], 20, -373897302);
		a = md5_gg(a, b, c, d, x[i + 5], 5 , -701558691);
		d = md5_gg(d, a, b, c, x[i + 10], 9 ,  38016083);
		c = md5_gg(c, d, a, b, x[i + 15], 14, -660478335);
		b = md5_gg(b, c, d, a, x[i + 4], 20, -405537848);
		a = md5_gg(a, b, c, d, x[i + 9], 5 ,  568446438);
		d = md5_gg(d, a, b, c, x[i + 14], 9 , -1019803690);
		c = md5_gg(c, d, a, b, x[i + 3], 14, -187363961);
		b = md5_gg(b, c, d, a, x[i + 8], 20,  1163531501);
		a = md5_gg(a, b, c, d, x[i + 13], 5 , -1444681467);
		d = md5_gg(d, a, b, c, x[i + 2], 9 , -51403784);
		c = md5_gg(c, d, a, b, x[i + 7], 14,  1735328473);
		b = md5_gg(b, c, d, a, x[i + 12], 20, -1926607734);

		a = md5_hh(a, b, c, d, x[i + 5], 4 , -378558);
		d = md5_hh(d, a, b, c, x[i + 8], 11, -2022574463);
		c = md5_hh(c, d, a, b, x[i + 11], 16,  1839030562);
		b = md5_hh(b, c, d, a, x[i + 14], 23, -35309556);
		a = md5_hh(a, b, c, d, x[i + 1], 4 , -1530992060);
		d = md5_hh(d, a, b, c, x[i + 4], 11,  1272893353);
		c = md5_hh(c, d, a, b, x[i + 7], 16, -155497632);
		b = md5_hh(b, c, d, a, x[i + 10], 23, -1094730640);
		a = md5_hh(a, b, c, d, x[i + 13], 4 ,  681279174);
		d = md5_hh(d, a, b, c, x[i + 0], 11, -358537222);
		c = md5_hh(c, d, a, b, x[i + 3], 16, -722521979);
		b = md5_hh(b, c, d, a, x[i + 6], 23,  76029189);
		a = md5_hh(a, b, c, d, x[i + 9], 4 , -640364487);
		d = md5_hh(d, a, b, c, x[i + 12], 11, -421815835);
		c = md5_hh(c, d, a, b, x[i + 15], 16,  530742520);
		b = md5_hh(b, c, d, a, x[i + 2], 23, -995338651);

		a = md5_ii(a, b, c, d, x[i + 0], 6 , -198630844);
		d = md5_ii(d, a, b, c, x[i + 7], 10,  1126891415);
		c = md5_ii(c, d, a, b, x[i + 14], 15, -1416354905);
		b = md5_ii(b, c, d, a, x[i + 5], 21, -57434055);
		a = md5_ii(a, b, c, d, x[i + 12], 6 ,  1700485571);
		d = md5_ii(d, a, b, c, x[i + 3], 10, -1894986606);
		c = md5_ii(c, d, a, b, x[i + 10], 15, -1051523);
		b = md5_ii(b, c, d, a, x[i + 1], 21, -2054922799);
		a = md5_ii(a, b, c, d, x[i + 8], 6 ,  1873313359);
		d = md5_ii(d, a, b, c, x[i + 15], 10, -30611744);
		c = md5_ii(c, d, a, b, x[i + 6], 15, -1560198380);
		b = md5_ii(b, c, d, a, x[i + 13], 21,  1309151649);
		a = md5_ii(a, b, c, d, x[i + 4], 6 , -145523070);
		d = md5_ii(d, a, b, c, x[i + 11], 10, -1120210379);
		c = md5_ii(c, d, a, b, x[i + 2], 15,  718787259);
		b = md5_ii(b, c, d, a, x[i + 9], 21, -343485551);

		a = safe_add(a, olda);
		b = safe_add(b, oldb);
		c = safe_add(c, oldc);
		d = safe_add(d, oldd);
	}

	ret[0] = a;
	ret[1] = b;
	ret[2] = c;
	ret[3] = d;
	return ret;
}


/*
 * Calculate the HMAC-MD5, of a key and some data
 */
function core_hmac_md5(key, data)
{
	var bkey = str2binl(key), ipad = [], opad = [], hash, i;
	if (bkey.length > 16) {
		bkey = core_md5(bkey, key.length * chrsz);
	}

	for (i = 0; i < 16; i += 1) {
		ipad[i] = bkey[i] ^ 0x36363636;
		opad[i] = bkey[i] ^ 0x5C5C5C5C;
	}

	hash = core_md5(ipad.concat(str2binl(data)), 512 + data.length * chrsz);
	return core_md5(opad.concat(hash), 512 + 128);
}

/*
 * These are the functions you'll usually want to call
 * They take string arguments and return either hex or base-64 encoded strings
 */
function hex_md5(s)
{
	return binl2hex(core_md5(str2binl(s), s.length * chrsz));
}
function b64_md5(s)
{
	return binl2b64(core_md5(str2binl(s), s.length * chrsz));
}
function str_md5(s)
{
	return binl2str(core_md5(str2binl(s), s.length * chrsz));
}
function hex_hmac_md5(key, data)
{
	return binl2hex(core_hmac_md5(key, data));
}
function b64_hmac_md5(key, data)
{
	return binl2b64(core_hmac_md5(key, data));
}
function str_hmac_md5(key, data)
{
	return binl2str(core_hmac_md5(key, data));
}

/*
 * Perform a simple self-test to see if the VM is working
 */
function md5_vm_test()
{
	return hex_md5("abc") === "900150983cd24fb0d6963f7d28e17f72";
}


/*****************************/
/* Service Pages (uses MD5)  */
/*****************************/
function serviceVerify()
{
	var mf, mycookie, ind;
	mf = document.forms.myServiceForm;

	// Check for present of service cookie
	mycookie = ""+document.cookie;
	ind = mycookie.indexOf("service");
	if (ind === -1) {
		alert("Session expired! Reload page and try again.");
		return false;
	}

	if (mf.cname.value === "") {
		//mf.cname.focus();
		showDialog('Service Form', 'You must enter a Company Name', 'error', 0, mf.cname);
		return false;
	}
	if (!orderValidField(mf.cname)) {
		//mf.cname.focus();
		showDialog('Service Form', 'Invalid characters in Company Name', 'error', 0, mf.cname);
		return false;
	}
	if (mf.name.value === "") {
		//mf.name.focus();
		showDialog('Service Form', 'You must enter a Name', 'error', 0, mf.name);
		return false;
	}
	if (!orderValidField(mf.name)) {
		//mf.name.focus();
		showDialog('Service Form', 'Invalid characters in Name', 'error', 0, mf.name);
		return false;
	}
	if (mf.add1.value === "") {
		//mf.add1.focus();
		showDialog('Service Form', 'You must enter an Address', 'error', 0, mf.add1);
		return false;
	}
	if (!orderValidField(mf.add1)) {
		//mf.add1.focus();
		showDialog('Service Form', 'Invalid characters in Address', 'error', 0, mf.add1);
		return false;
	}
	if (!orderValidField(mf.add2)) {
		//mf.add2.focus();
		showDialog('Service Form', 'Invalid characters in Address', 'error', 0, mf.add2);
		return false;
	}
	if (mf.city.value === "") {
		//mf.city.focus();
		showDialog('Service Form', 'You must enter a City', 'error', 0, mf.city);
		return false;
	}
	if (!orderValidField(mf.city)) {
		//mf.city.focus();
		showDialog('Service Form', 'Invalid characters in City', 'error', 0, mf.city);
		return false;
	}
	if (mf.state.value === "ZZ") {
		//mf.state.focus();
		showDialog('Service Form', 'You must enter a State', 'error', 0, mf.state);
		return false;
	}
	if (mf.zip.value === "") {
		//mf.zip.focus();
		showDialog('Service Form', 'You must enter a Zip Code', 'error', 0, mf.zip);
		return false;
	}
	if (!isNum(mf.zip.value)) {
		//mf.zip.focus();
		showDialog('Service Form', 'Zip Code must be numeric', 'error', 0, mf.zip);
		return false;
	}
	if (mf.zip.value.length < 5) {
		//mf.zip.focus();
		showDialog('Service Form', 'Zip Code must be 5 digits', 'error', 0, mf.zip);
		return false;
	}
	if (mf.servemail.value === "") {
		//mf.servemail.focus();
		showDialog('Service Form', 'You must enter an E-mail address', 'error', 0, mf.servemail);
		return false;
	}
	if (!orderValidEmail(mf.servemail.value)) {
		//mf.servemail.focus();
		showDialog('Service Form', 'Invalid E-mail address', 'error', 0, mf.servemail);
		return false;
	}
	if (mf.phone.value === "") {
		//mf.phone.focus();
		showDialog('Service Form', 'You must enter a Phone Number', 'error', 0, mf.phone);
		return false;
	}
	if (mf.fax.value === "") {
		//mf.fax.focus();
		showDialog('Service Form', 'You must enter a Fax Number', 'error', 0, mf.fax);
		return false;
	}
	if (mf.manufacturer.value === "") {
		//mf.manufacturer.focus();
		showDialog('Service Form', 'You must enter a Manufacturer', 'error', 0, mf.manufacturer);
		return false;
	}
	if (!orderValidField(mf.manufacturer)) {
		//mf.manufacturer.focus();
		showDialog('Service Form', 'Invalid characters in Manufacturer', 'error', 0, mf.manufacturer);
		return false;
	}
	if (mf.model.value === "") {
		//mf.model.focus();
		showDialog('Service Form', 'You must enter a Model Number', 'error', 0, mf.model);
		return false;
	}
	if (!orderValidField(mf.model)) {
		//mf.model.focus();
		showDialog('Service Form', 'Invalid characters in Model Number', 'error', 0, mf.model);
		return false;
	}
	if (mf.problem.value === "") {
		//mf.problem.focus();
		showDialog('Service Form', 'You must enter a description of the problem', 'error', 0, mf.problem);
		return false;
	}
	if (mf.problem.value.length > 2000) {
		//mf.problem.focus();
		showDialog('Service Form', 'The description of the problem cannot exceed 2000 characters', 'error', 0, mf.problem);
		return false;
	}
	if (mf.captcha.value === "" || hex_md5(mf.captcha.value.toUpperCase()) !== mf.kdata.value) {
		//mf.captcha.focus();
		showDialog('Service Form', 'Security Phrase failed', 'error', 0, mf.captcha);
		return false;
	}

	document.forms.myServiceForm.submit();
	return true;
}


/*****************************/
/* RMA Page (uses MD5)       */
/*****************************/
function rmaVerify()
{
	var mf, mycookie, ind;
	mf = document.forms.rmaform;

	// Check for present of rma cookie
	mycookie = ""+document.cookie;
	ind = mycookie.indexOf("rma");
	if (ind === -1) {
		alert("Session expired! Reload page and try again.");
		return false;
	}

	if (mf.name.value === "") {
		//mf.name.focus();
		showDialog('RMA Form', 'You must enter a Name', 'error', 0, mf.name);
		return false;
	}
	if (!orderValidField(mf.name)) {
		//mf.name.focus();
		showDialog('RMA Form', 'Invalid characters in Name', 'error', 0, mf.name);
		return false;
	}
	if (mf.company.value === "") {
		//mf.company.focus();
		showDialog('RMA Form', 'You must enter a Company Name', 'error', 0, mf.company);
		return false;
	}
	if (!orderValidField(mf.company)) {
		//mf.company.focus();
		showDialog('RMA Form', 'Invalid characters in Company Name', 'error', 0, mf.company);
		return false;
	}
	if (mf.phone.value === "") {
		//mf.phone.focus();
		showDialog('RMA Form', 'You must enter a Phone Number', 'error', 0, mf.phone);
		return false;
	}
	if (mf.email.value === "") {
		//mf.email.focus();
		showDialog('RMA Form', 'You must enter an E-mail address', 'error', 0, mf.email);
		return false;
	}
	if (!orderValidEmail(mf.email.value)) {
		//mf.email.focus();
		showDialog('RMA Form', 'Invalid E-mail address', 'error', 0, mf.email);
		return false;
	}
	if (mf.invoice.value === "") {
		//mf.invoice.focus();
		showDialog('RMA Form', 'You must enter an Invoice Number', 'error', 0, mf.invoice);
		return false;
	}
	if (!isNum(mf.invoice.value)) {
		//mf.invoice.focus();
		showDialog('RMA Form', 'Invoice Number must be numeric', 'error', 0, mf.invoice);
		return false;
	}
	if (mf.reason.value === "") {
		//mf.reason.focus();
		showDialog('RMA Form', 'You must enter a reason for return', 'error', 0, mf.reason);
		return false;
	}
	if (mf.reason.value.length > 5000) {
		//mf.reason.focus();
		showDialog('RMA Form', 'The reason for return cannot exceed 5000 characters', 'error', 0, mf.reason);
		return false;
	}
	if (mf.captcha.value === "" || hex_md5(mf.captcha.value.toUpperCase()) !== mf.kdata.value) {
		//mf.captcha.focus();
		showDialog('RMA Form', 'Security Phrase failed', 'error', 0, mf.captcha);
		return false;
	}

	document.forms.rmaform.submit();
	return true;
}

