var osv5 = new Array();
osv5[0] = 59.95;	// upgrade price
osv5[1] = 89.95;
osv5[2] = 159.95;
osv5[3] = 209.95;

var bizcalc = new Array();
bizcalc[1] = 39.95;
bizcalc[2] = 49.95;
bizcalc[3] = 59.95;

var mbacalc = new Array();
mbacalc[1] = 39.95;
mbacalc[2] = 49.95;
mbacalc[3] = 59.95;

var products = new Array();
products[0] = 'Desktop';
products[1] = 'HPC';
products[2] = 'HPC2000';
products[3] = 'PocketPC';

var addease = 19.95;

var shippingCost = new Array();
shippingCost[1] = 12.0;
shippingCost[2] = 15.0;
shippingCost[3] = 23.0;
shippingCost[4] = 25.0;
shippingCost[5] = 16.0;
shippingCost[6] = 20.0;
shippingCost[7] = 25.0;
shippingCost[8] = 25.0;

function loadInfo()
{
	var doc = document.orderForm;
	var first = GetCookie("first");
	var last = GetCookie("last");
	var company = GetCookie("company");
	var address = GetCookie("address");
	var city = GetCookie("city");
	var state = GetCookie("state");
	var zipCode = GetCookie("zipcode");
	var country = GetCookie("country");
	var email = GetCookie("email");
	var phone = GetCookie("phone");
	var fax = GetCookie("fax");
	var deviceModel = GetCookie("model");
	var winCEVersion = GetCookie("version");
	
	if(first)
		doc.first.value = first;
	if(last)
		doc.last.value = last;
	if(company)
		doc.company.value = company;
	if(address)
		doc.address.value = address;
	if(city)
		doc.city.value = city;
	if(state)
		doc.state.value = state;
	if(zipCode)
		doc.zipcode.value = zipCode;
	if(country)
		doc.country.value = country;
	else
		doc.country.value = 'US';
	if(email)
		doc.email.value = email;
	if(phone)
		doc.phone.value = phone;
	if(fax)
		doc.fax.value = fax;
	if(deviceModel)
		doc.model.value = deviceModel;
	if(winCEVersion)
		doc.version.value = winCEVersion;
}


function sendForm()
{
	if(customerInfo() && productInfo() && paymentInfo())
	{
		document.orderForm.submit();
	}
}

function customerInfo()
{
	var doc = document.orderForm;
	return isValue(doc.name, 'Name') &&
		   isValue(doc.phone, 'Phone') &&
		   isEmail(doc.email, 'Email') &&	
		   isValue(doc.address, 'Address') &&
		   isValue(doc.city, 'City') &&
		   isValue(doc.state, 'State') &&
		   isValue(doc.zipcode, 'Zip Code') &&
		   isValue(doc.country, 'Country');
}

function paymentInfo()
{
	var doc = document.orderForm;
	var cardIndex = doc.cardType.selectedIndex;
	if(isValue(doc.cardNumber, 'Card Number'))
	{
		if( (cardIndex == 3 && !/^\d{15}$/.test(doc.cardNumber.value)) || 
			(cardIndex != 3 && !/^\d{16}$/.test(doc.cardNumber.value)))
		{
			alert('Incorrect card number');
			doc.cardNumber.focus();
			return false;
		}
	}
	else
		return false;
		
	if(isValue(doc.expireDate, 'Expiration Date'))
	{
		if(!/^\d{4}$/.test(doc.expireDate.value))
		{
			alert('Incorrect expiration date.');
			doc.expireDate.focus();
			return false;
		}
		else
			return true;
	}
	return false;
}

function productInfo()
{
	var doc = document.orderForm;
	var isOrder = false;
	
	// on-schedule
	if(doc.osv5Package.selectedIndex != 0)
	{
		if(checkQuantity(doc.osv5Quantity) && validateQuantity(doc.osv5Quantity) && checkOptions(doc.osv5Package, 'osv5'))
			isOrder=true;
		else 
			return false;
	}
	
	// upgrade
	//if(doc.osv5UpgradeQuantity.value != '')
	//{
	//	isOrder=true;
	//}
	
	// bizcalc
	if(doc.bizcalcPackage.selectedIndex != 0)
	{
		if(checkQuantity(doc.bizcalcQuantity) && validateQuantity(doc.bizcalcQuantity) && checkOptions(doc.bizcalcPackage, 'bizcalc'))
			isOrder=true;
		else
			return false;
	}
	
	// mbacalc
	if(doc.mbacalcPackage.selectedIndex != 0)
	{
		if(checkQuantity(doc.mbacalcQuantity) && validateQuantity(doc.mbacalcQuantity) && checkOptions(doc.mbacalcPackage, 'mbacalc'))
			isOrder=true;
		else
			return false;
	}
	
	// addease 
	if(doc.addeaseQuantity.value != '')
	{
		if(isDigit(doc.addeaseQuantity, 'Quantity'))
		{
			if(validateQuantity(doc.addeaseQuantity))
				isOrder = true;
			else 
				return false;
		}
		else
			return false;
	}
	
	if(!isOrder)
		alert('No product selected');
	return isOrder;
}

function checkOptions(packageObj, optionNames, obj)
{
	var doc = document.orderForm;
	var selected = totalChecked(optionNames);
	var index = packageObj.selectedIndex;
				
	if(index == 0)
	{
		if(obj)
		{
			obj.checked = false;
			alert('Please select package first');
			packageObj.focus();
		}
		else
		{
			for(var i=0;i<products.length;i++)
			{
				doc.elements[optionNames + products[i]].checked = false;
			}
		}
		return;
	}
	
	
	if(selected != index )
	{
		var productName = '';
		if(optionNames == "osv5")
			productName = "On-Schedule";
		else if(optionNames == "bizcalc")
			productName = "BizCalc";
		else if(optionNames == "mbacalc")
			productName = "MBA Calc";
		alert('Please select ' + packageObj.selectedIndex + ' ' + productName + ' options(s).');
		doc.elements[optionNames + products[0]].focus();
		return false;
	}
	return true;
}

// round up to two decimal point
function formatCurrency(number)
{
	var str = '' + (Math.round(number * 100) / 100);
	if(!/.\d{2}$/.test(str))
		str += '0';
	return str;
}

function isExcess(packageName, optionName, obj)
{
	var doc = document.orderForm;
	var selected = totalChecked(optionName);
	var packageObj = doc.elements[packageName];
	if(selected > packageObj.selectedIndex)
	{
		alert('Only ' + packageObj.selectedIndex + ' options(s) could be selected');
		obj.checked = false;
		return false;
	}
	return true;
}

function totalChecked(optionNames)
{
	var selected = 0;
	var doc = document.orderForm;
	for(var i=0;i<products.length;i++)
	{
		if(doc.elements[optionNames + products[i]] && doc.elements[optionNames + products[i]].checked)
			selected++;
	}
	return selected;
}

function uncheckedAll(optionNames)
{
	var doc = document.orderForm;
	for(var i=0;i < products.length;i++)
	{
		doc.elements[optionNames + products[i]].checked = false;
	}
}

function updatePurchasePrice(optionNames, quantityName, packageName, totalName)
{
	//alert('updatePurchasePrice()');
	var doc = document.orderForm;
	var prices = eval('window.' + optionNames);
	var quantity = doc.elements[quantityName];
	var total = doc.elements[totalName];
	var packageObj = doc.elements[packageName];
	var index = packageObj.selectedIndex;
	
	if(index == 0)
	{
		// select none, clear options, quantity, total
		if(total.value != '')
		{
			quantity.value = '';
			total.value = '';
			uncheckedAll(optionNames);
		}
		// enter quantity before select package
		else
		{
			alert('Please select package first');
			quantity.value = '';
			packageObj.focus();
		}
	}
	// package selected 
	else
	{
		quantity.focus();
		// need to get quantity
		if(quantity.value == '')
		{
			if(total.value == '')
			{
				quantity.value = '1';
				total.value = prices[index];
			}
			else
			{
				total.value = '';
			}
		}
		// update price and options
		else if(isDigit(quantity, 'Quantity') && validateQuantity(quantity))
		{
			total.value = formatCurrency(parseInt(quantity.value) * prices[index]);
			if(totalChecked(optionNames) > index)
				uncheckedAll(optionNames);
		}
		else
			total.value = '';
	}
	updateAll();
}

function updateUpgradePrice()
{
	var doc = document.orderForm;
	var quantity = doc.osv5UpgradeQuantity;
	var total = doc.osv5UpgradeTotal;
	var selected = totalChecked('osv5Upgrade');
	if(quantity.value == '')
	{
		quantity.focus();
	}
	else if(selected == 0)
	{
		quantity.value = '';
		if(total.value == '')
		{
			alert('Please select upgrade option first');
			doc.osv5UpgradeDesktop.focus();
		}
		else
			total.value = '';
	}
	else if(isDigit(quantity, 'Quantity') && validateQuantity(quantity))
		total.value = formatCurrency(parseInt(quantity.value) * osv5[0] * selected);
	updateAll();
}

function updateAddEasePrice()
{
	var doc = document.orderForm;
	var quantity = doc.addeaseQuantity;
	var total = doc.addeaseTotal;
	if(quantity.value == '')
		total.value='';
	else if(isDigit(quantity, 'Quantity') && validateQuantity(quantity))
		total.value = formatCurrency(parseInt(quantity.value) * addease);
	updateAll();
}


function updateShipping()
{
	var doc = document.orderForm;
	var shipping = doc.shipping;
	var units = getTotalUnits();
	if(shipping.selectedIndex != 0 && units != 0)
	{
		//doc.shippingTotal.value = shippingCost[shipping.selectedIndex] + (units-1) + '.00';
		if(shipping.selectedIndex == 8)
			doc.shippingTotal.value = shippingCost[shipping.selectedIndex] + ((units-1)*3) + '.00';
		else
			doc.shippingTotal.value = shippingCost[shipping.selectedIndex] + (units-1) + '.00';
	}
	else
		doc.shippingTotal.value = '';
}

function updateTax()
{
	var doc = document.orderForm;
	var shipping = doc.shipping;
	var total = getSubTotal();
	if( total != 0 && shipping.selectedIndex != 0 && (doc.state.value.search(/^ca/i) != -1 && 
	    (doc.country.value.search(/^us/i) != -1 || doc.country.value.search(/united states/i) != -1)))
		doc.tax.value = formatCurrency(total * 0.0775);
	else
		doc.tax.value = "0.00";
}

function updateAll()
{
	updateShipping();
	updateTax();
	
	var doc = document.orderForm;
	var subTotal = doc.subTotal;
	var tax = doc.tax;
	var total = doc.total;
	
	var grandTotal = getSubTotal();
	if(grandTotal != 0)
	{
		subTotal.value = formatCurrency(grandTotal);
		if(tax.value != '')
			grandTotal += parseFloat(tax.value);
		total.value = formatCurrency(grandTotal);
	}
	else
	{
		subTotal.value = '';
		total.value = '';
	}		
}

function getSubTotal()
{
	var subtotal = 0;
	var doc = document.orderForm;
	subtotal += getTotal('osv5');
	subtotal += getTotal('bizcalc');
	subtotal += getTotal('mbacalc');

	// addease total
	var addeaseTotal = doc.addeaseTotal;
	if(addeaseTotal.value != '')
		subtotal += parseFloat(addeaseTotal.value);
	
	// upgrade total
	//var osv5UpgradeTotal = doc.osv5UpgradeTotal;
	//if(osv5UpgradeTotal.value != '')
	//	subtotal += parseFloat(osv5UpgradeTotal.value);
	
	// shippping total
	var shippingTotal = doc.shippingTotal;
	if(shippingTotal.value != '')
		subtotal += parseFloat(shippingTotal.value);
	return subtotal;
}

function getTotal(prefix)
{
	var doc = document.orderForm;
	var total = doc.elements[prefix + 'Total'];
	if(total.value != '')
		return parseFloat(total.value);
	else
		return 0;
}

function getTotalUnits()
{
	var units = 0;
	var doc = document.orderForm;
	units += getUnits('osv5');
	units += getUnits('bizcalc');
	units += getUnits('mbacalc');

	var addeaseQuantity = doc.addeaseQuantity
	if(addeaseQuantity.value != '')
		units += parseInt(addeaseQuantity.value);
		
	//var osv5UpgradeQuantity = doc.osv5UpgradeQuantity;
	//if(osv5UpgradeQuantity.value != '')
	//	units += parseInt(osv5UpgradeQuantity.value) * totalChecked('osv5Upgrade');
	return units;
}

function getUnits(prefix)
{
	var unit = 0;
	var doc = document.orderForm;
	var packageObj = doc.elements[prefix + 'Package'];
	if(packageObj.selectedIndex != 0)
	{
		if(doc.elements[prefix + 'Quantity'].value != '')
		{
			//unit = (packageObj.selectedIndex * parseInt(doc.elements[prefix + 'Quantity'].value));
			unit = parseInt(doc.elements[prefix + 'Quantity'].value);
		}
	}
	return unit;
}

function validateQuantity(obj)
{
	if(parseInt(obj.value) > 0)
		return true;
	else
	{
		alert('Quantity must be greater than 0');
		obj.value = '';
		obj.focus();
		return false;
	}
}

function isEmail(obj, field)
{
	if(isValue(obj, field))
	{
		if(obj.value.indexOf('@') != -1)
			return true;
		else
		{
			alert(field + ' is not in correct format. (i.e xxx@xxx.com)');
			obj.focus();
		}
	}
	return false;
}

function checkQuantity(obj)
{
	return (isValue(obj, 'Quantity') && isDigit(obj, 'Quantity'));
}

function isDigit(obj, field)
{
	if(obj.value.search(/\D/g) != -1)
	{
		alert(field + ' must be a number.');
		obj.value = '';
		obj.focus();
		return false;
	}
	return true;
}

function isValue(obj, field)
{
	if(obj.value == '')
	{
		alert(field + ' cannot be empty.');
		obj.focus();
		return false;
	}
	return true;
}