function updateDropDown(ddName, ddValue)
{
	for(i = 0; i < document.getElementById(ddName).options.length; i++)
	{
		if(document.getElementById(ddName).options[i].value == ddValue && document.getElementById(ddName).options[i].value != "")
		{
			document.getElementById(ddName).selectedIndex = i;
		}
	}
}

function copyFields()
{
	document.getElementById('shipping_name').value    = document.getElementById('billing_name').value;
	document.getElementById('shipping_company').value = document.getElementById('billing_company').value;
	document.getElementById('shipping_address').value = document.getElementById('billing_address').value;
	document.getElementById('shipping_city').value    = document.getElementById('billing_city').value;
	document.getElementById('shipping_zip').value     = document.getElementById('billing_zip').value;
	document.getElementById('shipping_phone').value   = document.getElementById('billing_phone').value;
	
	return;
}

function checkOrderForm()
{
	if(document.getElementById("shipping_state_id").value == "")
	{
		alert("You must choose a shipping state in order to continue.");
		document.getElementById("shipping_state_id").focus();
		
		return false;
	}
	
	var qtyFound = 0;
	var inputs = document.getElementsByTagName("input");
		
	for(var i = 0; i < inputs.length; i++)
	{
		if(inputs[i].id.indexOf("product_") == 0 && inputs[i].value > 0)
		{
			qtyFound = 1;
		}
	}
	
	if(qtyFound == 0)
	{
		alert("You must order at least one product in order to continue.");
		
		return false;
	}
	
	return true;
}

function numbersOnly(e)
{
	var unicode = e.charCode? e.charCode : e.keyCode;
	
	//allow tab and backspace
	if(unicode != 8 && unicode != 9)
	{
		if(unicode < 48 || unicode > 57)
		{
			return false;
		}
	}
}

function limitLength(obj, length)
{
	var maxlength = length;
	
	if(obj.value.length > maxlength)
	{
		obj.value = obj.value.substring(0, maxlength);
	}
}

function showGiftMessage(toShow)
{
	if(toShow)
	{
		document.getElementById("gift_message_row").style.display = "";
	} else {
		document.getElementById("gift_message_row").style.display = "none";
	}
	
	document.getElementById("gift_message").focus();
	
	return;
}

function checkOrderFormConfirm()
{
	if(document.getElementById("billing_name").value == "")
	{
		alert("You must enter a billing name in order to continue.");
		document.getElementById("billing_name").focus();
		
		return false;
	}
	
	if(document.getElementById("billing_address").value == "")
	{
		alert("You must enter a billing address in order to continue.");
		document.getElementById("billing_address").focus();
		
		return false;
	}
	
	if(document.getElementById("billing_city").value == "")
	{
		alert("You must choose a billing city in order to continue.");
		document.getElementById("billing_city").focus();
		
		return false;
	}
	
	if(document.getElementById("billing_state").value == "")
	{
		alert("You must enter a billing state in order to continue.");
		document.getElementById("billing_state").focus();
		
		return false;
	}
	
	if(document.getElementById("billing_zip").value == "")
	{
		alert("You must enter a billing zip in order to continue.");
		document.getElementById("billing_zip").focus();
		
		return false;
	}
	
	if(document.getElementById("billing_phone").value == "")
	{
		alert("You must enter a billing phone number in order to continue.");
		document.getElementById("billing_phone").focus();
		
		return false;
	}
	
	if(document.getElementById("email").value == "")
	{
		alert("You must enter an e-mail address in order to continue.");
		document.getElementById("email").focus();
		
		return false;
	}
	
	if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.getElementById("email").value)))
	{
		alert("The e-mail address you entered does not seem to be valid.");
		document.getElementById("email").focus();
		
		return false;
	}
	
	if(document.getElementById("email").value != document.getElementById("email_confirm").value)
	{
		alert("Your e-mail and confirmaiton must match in order to continue.");
		document.getElementById("email").value = "";
		document.getElementById("email_confirm").value = "";
		document.getElementById("email").focus();
		
		return false;
	}
	
	if(document.getElementById("cc_type").value == "")
	{
		alert("You must choose a credit card type in order to continue.");
		document.getElementById("cc_type").focus();
		
		return false;
	}
	
	if(document.getElementById("cc_number").value == "")
	{
		alert("You must enter a credit card number in order to continue.");
		document.getElementById("cc_number").focus();
		
		return false;
	}
	
	if(!validateCard(document.getElementById("cc_number").value, document.getElementById("cc_type").value))
	{
		alert("The credit card you entered doesn't seem to be valid for the type that you chose.  Please try again.");
		document.getElementById("cc_number").focus();
		
		return false;
	}
	
	if(document.getElementById("cc_exp").value == "")
	{
		alert("You must enter a credit card expiration date in order to continue.");
		document.getElementById("cc_exp").focus();
		
		return false;
	}
	
	if(document.getElementById("cc_code").value == "")
	{
		alert("You must enter a 3-digit security code in order to continue.");
		document.getElementById("cc_code").focus();
		
		return false;
	}
	
	if(!document.getElementById("old_enough").checked)
	{
		alert("You must be 21 or older in order to continue.");
		document.getElementById("old_enough").focus();
		
		return false;
	}
	
	if(document.getElementById("birthdate_year").value == "" || document.getElementById("birthdate_month").value == "" || document.getElementById("birthdate_day").value == "")
	{
		alert("You must enter a valid birthdate in order to continue.");
		document.getElementById("birthdate_day").focus();
		
		return false;
	}
	
	if(document.getElementById("shipping_name").value == "")
	{
		alert("You must enter a shipping name in order to continue.");
		document.getElementById("shipping_name").focus();
		
		return false;
	}
	
	if(document.getElementById("shipping_address").value == "")
	{
		alert("You must enter a shipping address in order to continue.");
		document.getElementById("shipping_address").focus();
		
		return false;
	}
	
	if(document.getElementById("shipping_city").value == "")
	{
		alert("You must enter a shipping city in order to continue.");
		document.getElementById("shipping_city").focus();
		
		return false;
	}
	
	if(document.getElementById("shipping_zip").value == "")
	{
		alert("You must enter a shipping zip in order to continue.");
		document.getElementById("shipping_zip").focus();
		
		return false;
	}
	
	if(document.getElementById("shipping_phone").value == "")
	{
		alert("You must enter a shipping phone in order to continue.");
		document.getElementById("shipping_phone").focus();
		
		return false;
	}

	return true;
}

function checkDiscountCodeForm()
{
	if(document.getElementById("code").value == "")
	{
		alert("You must enter a code in order to continue");
		document.getElementById("code").focus();
		return false;
	}
	
	return true;
}

function checkAll(formElement)
{
	for (var i = 0; i < formElement.elements.length; i++)
	{
		var e = formElement.elements[i];
		if((e.name != 'check_all') && (e.type == 'checkbox'))
		{
			e.checked = formElement.check_all.checked;
		}
	}
}

function generateCode(formElement)
{
	var i = 0;
	var type = 0;
	var code = "";
	var new_char = "";
	
	for(i = 0; i < 15; i++)
	{
		type = Math.round(1 + (3 - 1) * Math.random());
		
		if(type == 1)
		{
			new_char = 	String.fromCharCode( Math.round(48 + (57 - 48) * Math.random()));
		} 
		else 
		{
			new_char = 	String.fromCharCode( Math.round(65 + (90 - 65) * Math.random()));
		}
		
		code += new_char;
	}
	
	formElement.value = code;
}