function addItem(itemnum)
{
	if(! editMode)
	{
		editMode = true;
		document.getElementById('cartdiv').style.display='';
		eval('document.getElementById("msg'+itemnum+'").innerHTML="Adding...&nbsp;&nbsp;&nbsp;&nbsp;"');
		doAJAX('ajax.inc.php', 'action=add_item&itemnum='+itemnum, eval, 'post', false);
	}
	//else
	//	alert('Error adding item! Please reload the page.');
}
function updateItem(itemnum, quantity)
{
	clearInterval(gtimer);
	gtimer = setTimeout("updateItemWait("+itemnum+", "+quantity+")", 1000);
}
function updateItemWait(itemnum, quantity)
{
	if(! editMode)
	{
		if(quantity != "" && quantity >= 0)
		{
			editMode = true;
			document.search.query.focus();
			eval('document.getElementById("q'+itemnum+'").disabled=true');
			doAJAX('ajax.inc.php', 'action=update_item&itemnum='+itemnum+'&quantity='+quantity, eval, 'post', false);
		}
	}
	//else
	//	alert('Error updating quantity! Please reload the page.');
}
function removeItem(itemnum)
{
	if(! editMode)
	{
		editMode = true;
		eval("document.getElementById('tr"+itemnum+"').style.background='#FF0000'");
		doAJAX('ajax.inc.php', 'action=remove_item&itemnum='+itemnum, eval, 'post', false);
	}
	//else
	//	alert('Error removing item! Please reload the page.');
}
function updateCart(subtotal, checkout)
{
	if(checkout)
	{
		document.getElementById('btn_checkout').disabled = false;
		document.getElementById('btn_checkout').style.color = '#009900';
	}
	else
	{
		document.getElementById('btn_checkout').disabled = true;
		document.getElementById('btn_checkout').style.color = '';
	}
	document.getElementById('cart_sub').innerHTML = '$'+subtotal;
}
function closeCart()
{
	document.getElementById('cart_sub').innerHTML='Closing...';
	doAJAX('ajax.inc.php', 'action=close_cart', eval, 'post', false);
}
function deleteCart()
{
	if(confirm("Are you sure you wish to delete this cart?"))
	{
		document.getElementById('cart_sub').innerHTML='Closing...';
		doAJAX('ajax.inc.php', 'action=delete_cart', eval, 'post', false);
	}
}
function validateRegister()
{
	var df = document.form_register;
	if(df.fname.value == "" || df.lname.value == "" || df.email.value == "" || df.rpass.value == "" || df.rcpass.value == "")
	{
		alert('Please fill in all required fields.');
		return false;
	}
	else if(df.rpass.value != df.rcpass.value)
	{
		alert('Please reconfirm your password.');
		return false;
	}
	return true;
}
function state_changed(val)
{
	if(val)
		doAJAX('ajax.inc.php', 'action=get_tax&state_id='+val, eval, 'post', false);
}
function checkCC()
{
	if(checkCreditCard(document.formaddress.cc_number.value, document.formaddress.cc_type.value) == true)
	{
		document.getElementById('checkmark').style.display = '';
		document.formaddress.cc_month.focus();
	}
	else
		document.getElementById('checkmark').style.display = 'none';
}
function checkCCSubmit()
{
	var missingTop="Please Update The Following:\n";
	var missing='';
	var frm=document.formaddress;
	if (frm['b_name'].value.length<4)
		missing += "--Invalid Billing Name\n";
	if (frm['b_address1'].value.length<4)
		missing += "--Invalid Billing Address\n";
	if (frm['b_city'].value.length<2)
		missing += "--Invalid Billing City\n";
	if (frm['b_state'].value.length<1)
		missing += "--Invalid Billing State\n";
	if (frm['b_zip'].value.length<5)
		missing += "--Invalid Billing Zip Code\n";
	if (frm['s_name'].value.length<4)
		missing += "--Invalid Shipping Name\n";
	if (frm['s_address1'].value.length<4)
		missing += "--Invalid Shipping Address\n";
	if (frm['s_city'].value.length<2)
		missing += "--Invalid Shipping City\n";
	if (frm['s_state'].value.length<1)
		missing += "--Invalid Shipping State\n";
	if (frm['s_zip'].value.length<5)
		missing += "--Invalid Shipping Zip Code\n";
	if (frm['cc_number'].value.length<10)
		missing += "--Invalid Credit Card Number\n";
	if (frm['cc_cvv2'].value.length != 3 &&  frm['cc_cvv2'].value.length != 4 )
		missing += "--Invalid Credit Card CVV2 Code\n";
	if (! frm['agree'].checked)
		missing += "--You must agree to our policy\n";			
	if (missing.length>0)
	{
		alert(missingTop+missing);
		return false;
	} 
	else 
	{
		return true;
	}
}
function checkCreditCard(cc_number, cc_type)
{
	/*
	This routine checks the credit card number. The following checks are made:
	
	1. A number has been provided
	2. The number is a right length for the card
	3. The number has an appropriate prefix for the card
	4. The number has a valid modulus 10 number check digit if required
	
	If the validation fails an error is reported.
	*/
	// Errors
	var ccErrors = new Array ()
	ccErrors[0] = "Unknown card type";
	ccErrors[1] = "No card number provided";
	ccErrors[2] = "Credit card number is in invalid format";
	ccErrors[3] = "Credit card number is invalid";
	ccErrors[4] = "Credit card number has an inappropriate number of digits";   
	// Array to hold the permitted card characteristics
	var cards = new Array();
	//  Name:      As in the selection box of the form - must be same as user's
	//  Length:    List of possible valid lengths of the card number for the card
	//  prefixes:  List of possible prefixes for the card
	//  checkdigit Boolean to say whether there is a check digit
	
	cards [0] = {name: "Visa", 
			   length: "13,16", 
			   prefixes: "4",
			   checkdigit: true};
	cards [1] = {name: "Mastercard", 
			   length: "16", 
			   prefixes: "51,52,53,54,55",
			   checkdigit: true};
	cards [3] = {name: "Amex", 
			   length: "15", 
			   prefixes: "34,37",
			   checkdigit: true};
	cards [4] = {name: "Discover", 
			   length: "16", 
			   prefixes: "6011,650",
			   checkdigit: true};
			   
	// Establish card type
	var cardType = -1;
	for (var i=0; i<cards.length; i++)
	{
	
	// See if it is this card (ignoring the case of the string)
	if (cc_type.toLowerCase() == cards[i].name.toLowerCase())
	{
		cardType = i;
		break;
	}
	}
	// If card type not found, report an error
	if (cardType == -1)
	{
	 	return ccErrors[0];
	}
	
	// Ensure that the user has provided a credit card number
	if (cc_number.length == 0) 
	{
	 	return ccErrors[1]; 
	}
	// Now remove any spaces from the credit card number
	cc_number = cc_number.replace(/\s/g, "");
	// Check that the number is numeric
	var cardNo = cc_number
	var cardexp = /^[0-9]{13,19}$/;
	if (!cardexp.exec(cardNo)) 
	{
	 	return ccErrors[2]; 
	}
	// Now check the modulus 10 check digit - if required
	if (cards[cardType].checkdigit)
	{
		var checksum = 0;                                  // running checksum total
		var mychar = "";                                   // next char to process
		var j = 1;                                         // takes value of 1 or 2
		// Process each digit one by one starting at the right
		var calc;
		for (i = cardNo.length - 1; i >= 0; i--)
		{
			// Extract the next digit and multiply by 1 or 2 on alternative digits.
			calc = Number(cardNo.charAt(i)) * j;
			
			// If the result is in two digits add 1 to the checksum total
			if (calc > 9)
			{
				checksum = checksum + 1;
				calc = calc - 10;
			}
			// Add the units element to the checksum total
			checksum = checksum + calc;
			// Switch the value of j
			if (j ==1)
				j = 2;
			else
				j = 1;
		} 
		// All done - if checksum is divisible by 10, it is a valid modulus 10.
		// If not, report an error.
		if (checksum % 10 != 0)
		{
			return ccErrors[3]; 
		}
	}
	// The following are the card-specific checks we undertake.
	var LengthValid = false;
	var PrefixValid = false; 
	var undefined; 
	
	// We use these for holding the valid lengths and prefixes of a card type
	var prefix = new Array ();
	var lengths = new Array ();
	
	// Load an array with the valid prefixes for this card
	prefix = cards[cardType].prefixes.split(",");
	// Now see if any of them match what we have in the card number
	for (i=0; i<prefix.length; i++)
	{
		var exp = new RegExp ("^" + prefix[i]);
		if (exp.test (cardNo))
			PrefixValid = true;
	}
	// If it isn't a valid prefix there's no point at looking at the length
	if (!PrefixValid)
	{
	 	return ccErrors[3]; 
	}
	// See if the length is valid for this card
	lengths = cards[cardType].length.split(",");
	for (j=0; j<lengths.length; j++)
	{
		if (cardNo.length == lengths[j]) 
			LengthValid = true;
	}
	// See if all is OK by seeing if the length was valid. We only check the 
	// length if all else was hunky dory.
	if (!LengthValid)
	{
	 	return ccErrors[4]; 
	}
	// The credit card is in the required format.
	return true;
}
var editMode = false;
var gtimer;