function windowPopup(html, window_name, attributes)
{
	window.open (html, window_name, attributes)
}
	
function return_Number (dollar_number)
{
	var where_dollar_sign = dollar_number.indexOf('$')
	var retValue = 0
	
	if (where_dollar_sign != -1)
	{
		retValue = parseFloat(dollar_number.substring(where_dollar_sign + 1))
	}
	else
	{
		if (isNaN(dollar_number) || (dollar_number.length == 0))
		{
			retValue = 0
		}
		else
		{
			retValue = parseFloat(dollar_number)
		}
	}
	
	return retValue
}
	
function calcTotal (quantity, price, total)
{
	if (quantity > 0)
	{
		var cost = quantity * price
		total.value = "$" + cost.toFixed(2)
	}
	else
	{
		total.value = ''
	}
	
	var seed_total = 0

	for (i=1; i<= document.orderForm.rows.value; i++)
	{
		var elem = "total_" + i
		var elemID = document.getElementById(elem)
		// Add it to the total...
			if (return_Number(elemID.value) > 0)
		{
			seed_total = seed_total + return_Number(elemID.value)
		}
	}

	document.orderForm.seed_total.value = "$" + seed_total.toFixed(2)
	calc_shipping_handle()
	calc_subtotal()
	CT_tax()
	calc_total()
}
	
function calc_shipping_handle()
{
	var total = 0
	for (i=1; i<= document.orderForm.rows.value; i++)
	{
		var elem = "quantity_" + i
		var elemID = document.getElementById(elem)

		// Add it to the total...
		if (return_Number(elemID.value) > 0)
		{
			total = total + return_Number(elemID.value)
		}
	}

	var charge = 0
	if (total > 0)
	{
		if (total >= 1 && total <= 5)
		{
			charge = 3
		}
		else if (total >= 6 && total <= 11)
		{
			charge = 3.5
		}
		else if (total >= 12 && total <= 17)
		{
			charge = 4
		}
		else if (total >= 18 && total <= 23)
		{
			charge = 4.5
		}
		else if (total >= 24 && total <= 29)
		{
			charge = 5
		}
		else if (total >= 30 && total <= 35)
		{
			charge = 5.5
		}
		else
		{
//			alert ("Please contact us for shipping charges")
			charge = 0
		}
	}
		
	document.orderForm.shipping_total.value = "$" + charge.toFixed(2)
}
	
function calc_subtotal()
{
	var total_so_far = return_Number(document.orderForm.seed_total.value) + return_Number(document.orderForm.shipping_total.value)

	document.orderForm.sub_total.value = "$" + total_so_far.toFixed(2)
}
	
function CT_tax()
{
	var ct_tax_rate = 0.06
		
	var ct_tax = ct_tax_rate * return_Number(document.orderForm.sub_total.value)
	
	var state = document.getElementById("customerState")
	
	if (state.value == "CT")
	{
		document.orderForm.CT_tax.value = "$" + ct_tax.toFixed(2)
	}
	else
	{
		document.orderForm.CT_tax.value = "$0.00"
	}
}
	
function calc_total()
{
	var total_owed = return_Number(document.orderForm.sub_total.value) + return_Number(document.orderForm.CT_tax.value)
			
	document.orderForm.final_total.value = "$" + total_owed.toFixed(2)
}

function state_changed()
{
	CT_tax()
	calc_total()
}

function calc_load()
{
	calc_shipping_handle()
	calc_subtotal()
	CT_tax()
	calc_total()
}

function zipOnly (obj)
{
	reg = /[^0-9\-]/g;
	obj.value = obj.value.replace(reg,"");
}

function numOnly (obj)
{
	reg = /[^0-9]/g;
	obj.value = obj.value.replace(reg,"");
}

function validZip(zip)
{
	var reZip = new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/)
	var retVal = true
		
	if (!reZip.test(zip)) 
	{
		retVal = false
	}
	else
	{
		retVal = true
	}
	
	return retVal
}

function validPhone(phone, th_phone)
{
	var regPhone = new RegExp(/\(\d{3}\)\d{3}\-\d{4}$/)
	var retVal = true

	if (!isEmpty(phone))
	{
		if (!regPhone.test(phone.value))
		{
			retVal = false
			alert ("Please enter a correct phone number")
			th_phone.style.color = "red"
			phone.style.background = "#FE9701"
			phone.focus()
		}
		else
		{
			th_phone.style.color = "#696969"
			phone.style.background = "white"
			retVal = true
		}
	}
	
	return retVal
}

function isEmpty(aTextField) 
{
	var retValue = true
		
	if ((aTextField.value.length==0) || (aTextField.value==null)) 
	{
		retValue = true;
	}
	else 
	{ 
		retValue = false; 
	}
	
	return retValue
} 

function checkEmail(email) 
{
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/
	var retVal

	if (!filter.test(email.value)) 
	{
		retVal = false
	}
	else
	{
		retVal = true
	}

	return retVal
}

function validate()
{
	var valid = 0
		var firstField = ""
		
	if (isEmpty(document.getElementById("customerName")))
	{
		document.getElementById("th_customerName").style.color = "red"
		document.getElementById("customerName").style.background = "#FE9701"
		if (valid == 0)
		{
			document.getElementById("customerName").focus()
		}
		valid++
	}
	else
	{
		document.getElementById("th_customerName").style.color = "#696969"
		document.getElementById("customerName").style.background = "white"
	}

	if (isEmpty(document.getElementById("customerAddress1")))
	{
		document.getElementById("th_customerAddress1").style.color = "red"
		document.getElementById("customerAddress1").style.background = "#FE9701"
		if (valid == 0)
		{
			document.getElementById("customerAddress1").focus()
		}
		valid++
	}
	else
	{
		document.getElementById("th_customerAddress1").style.color = "#696969"
		document.getElementById("customerAddress1").style.background = "white"
	}

	if (isEmpty(document.getElementById("customerCity")))
	{
		document.getElementById("th_customerCity").style.color = "red"
		document.getElementById("customerCity").style.background = "#FE9701"
		if (valid == 0)
		{
			document.getElementById("customerCity").focus()
		}
		valid++
	}
	else
	{
		document.getElementById("th_customerCity").style.color = "#696969"
		document.getElementById("customerCity").style.background = "white"
	}

	if (document.getElementById("customerState").selectedIndex == 0)
	{
		document.getElementById("th_customerState").style.color = "red"
		document.getElementById("customerState").style.background = "#FE9701"
		if (valid == 0)
		{
			document.getElementById("customerState").focus()
		}
		valid++
	}
	else
	{
		document.getElementById("th_customerState").style.color = "#696969"
		document.getElementById("customerState").style.background = "white"
	}

	if (isEmpty(document.getElementById("customerZip")) || document.getElementById("customerZip").value.length < 5 || (!validZip (document.getElementById("customerZip").value)))
	{
		document.getElementById("th_customerZip").style.color = "red"
		document.getElementById("customerZip").style.background = "#FE9701"
		if (valid == 0)
		{
			document.getElementById("customerZip").focus()
		}
		valid++
	}
	else
	{
		document.getElementById("th_customerZip").style.color = "#696969"
		document.getElementById("customerZip").style.background = "white"
	}

	if (isEmpty(document.getElementById("customerEmail")))
	{
		document.getElementById("th_customerEmail").style.color = "red"
		document.getElementById("customerEmail").style.background = "#FE9701"
		if (valid == 0)
		{
			document.getElementById("customerEmail").focus()
		}
		valid++
	}
	else
	{
		if (checkEmail(document.getElementById("customerEmail")))
		{
			document.getElementById("th_customerEmail").style.color = "#696969"
			document.getElementById("customerEmail").style.background = "white"
		}
		else
		{
			document.getElementById("th_customerEmail").style.color = "red"
			document.getElementById("customerEmail").style.background = "#FE9701"
			if (valid == 0)
			{
				document.getElementById("customerEmail").focus()
			}
			valid++
		}
	}
	
	if (!isEmpty (document.getElementById("customerPhone")))
	{

		var regPhone = new RegExp(/\(\d{3}\)\d{3}\-\d{4}$/)
		
		if (regPhone.test(document.getElementById("customerPhone").value))
		{
			document.getElementById("th_customerPhone").style.color = "#696969"
			document.getElementById("customerPhone").style.background = "white"
		}
		else 
		{
			document.getElementById("th_customerPhone").style.color = "red"
			document.getElementById("customerPhone").style.background = "#FE9701"
			document.getElementById("customerPhone").focus()
			valid++
		}
	}

	if (valid > 0)
	{
		if (valid == 1)
		{
			alert ("Please enter the item requested.")
		}
		else
		{
			alert ("Please enter the items requested.")
		}
		
	}
	else
	{
		if (return_Number(document.getElementById("final_total").value) != 0)
		{
			if (return_Number(document.getElementById("shipping_total").value) != 0)
			{
				alert ("OK!!!")
			}
			else
			{
				alert ("Please contact us for the shipping charge")
			}
		}
		else
		{
			alert ("You have not ordered anything")
		}
	}
}