
function setTraining(product)
{
	var selbox = document.frmdemo.training;
 
	selbox.options.length = 0;
	selbox.options[selbox.options.length] = new Option('Select Training','Select Training');
	switch(product)
	{
	case '0':
		document.frmdemo.linetotal.value = "0";
  		break;
	case '1':
  		selbox.options[selbox.options.length] = new Option('Jan. 12-13: Foothill Ranch, Calif','Jan. 12-13: Foothill Ranch, Calif');	
  		selbox.options[selbox.options.length] = new Option('Jan. 27-28: Atlanta','Jan. 27-28: Atlanta');
  		selbox.options[selbox.options.length] = new Option('Feb. 10-11: Houston','Feb. 10-11: Houston');
  		selbox.options[selbox.options.length] = new Option('Mar. 17-18: Denver','Mar. 17-18: Denver');
  		selbox.options[selbox.options.length] = new Option('Apr. 12-13: Foothill Ranch, Calif','Apr. 12-13: Foothill Ranch, Calif');
  		selbox.options[selbox.options.length] = new Option('Apr. 28-29: Minneapolis','Apr. 28-29: Minneapolis');
  		selbox.options[selbox.options.length] = new Option('May 19-20: New York','May 19-20: New York');
  		selbox.options[selbox.options.length] = new Option('Jun. 2-3: Portland, Ore','Jun. 2-3: Portland, Ore');
  		selbox.options[selbox.options.length] = new Option('Aug. 2-3: Foothill Ranch, Calif','Aug. 2-3: Foothill Ranch, Calif');
  		selbox.options[selbox.options.length] = new Option('Sep. 15-16: Philadelphia','Sep. 15-16: Philadelphia');
  		selbox.options[selbox.options.length] = new Option('Oct. 13-14: Columbus, Ohio','Oct. 13-14: Columbus, Ohio');
  		selbox.options[selbox.options.length] = new Option('Oct. 19-20: Foothill Ranch, Calif','Oct. 19-20: Foothill Ranch, Calif');
  		selbox.options[selbox.options.length] = new Option('Nov. 10-11: Houston','Nov. 10-11: Houston');
  		document.frmdemo.linetotal.value = "$3,200";
  		break;    
	case '2':
  		selbox.options[selbox.options.length] = new Option('Apr. 15-16: Foothill Ranch, Calif','Apr. 15-16: Foothill Ranch, Calif');
  		selbox.options[selbox.options.length] = new Option('Aug. 5-6: Foothill Ranch, Calif','Aug. 5-6: Foothill Ranch, Calif');
  		document.frmdemo.linetotal.value = "$3,200";
  		break;
  	case '3':
  		selbox.options[selbox.options.length] = new Option('Jan. 12-13: Foothill Ranch, Calif','Jan. 12-13: Foothill Ranch, Calif');	
  		selbox.options[selbox.options.length] = new Option('Jan. 27-28: Atlanta','Jan. 27-28: Atlanta');
  		selbox.options[selbox.options.length] = new Option('Feb. 10-11: Houston','Feb. 10-11: Houston');
  		selbox.options[selbox.options.length] = new Option('Mar. 17-18: Denver','Mar. 17-18: Denver');
  		selbox.options[selbox.options.length] = new Option('Apr. 12-13: Foothill Ranch, Calif','Apr. 12-13: Foothill Ranch, Calif');
  		selbox.options[selbox.options.length] = new Option('Apr. 28-29: Minneapolis','Apr. 28-29: Minneapolis');
  		selbox.options[selbox.options.length] = new Option('May 19-20: New York','May 19-20: New York');
  		selbox.options[selbox.options.length] = new Option('Jun. 2-3: Portland, Ore','Jun. 2-3: Portland, Ore');
  		selbox.options[selbox.options.length] = new Option('Aug. 2-3: Foothill Ranch, Calif','Aug. 2-3: Foothill Ranch, Calif');
  		selbox.options[selbox.options.length] = new Option('Sep. 15-16: Philadelphia','Sep. 15-16: Philadelphia');
  		selbox.options[selbox.options.length] = new Option('Oct. 13-14: Columbus, Ohio','Oct. 13-14: Columbus, Ohio');
  		selbox.options[selbox.options.length] = new Option('Oct. 19-20: Foothill Ranch, Calif','Oct. 19-20: Foothill Ranch, Calif');
  		selbox.options[selbox.options.length] = new Option('Nov. 10-11: Houston','Nov. 10-11: Houston');
  		selbox.options[selbox.options.length] = new Option('Apr. 15-16: Foothill Ranch, Calif','Apr. 15-16: Foothill Ranch, Calif');
  		selbox.options[selbox.options.length] = new Option('Aug. 5-6: Foothill Ranch, Calif','Aug. 5-6: Foothill Ranch, Calif');
  		document.frmdemo.linetotal.value = "$4,500";
  		break;
	}
}

function Calc(prod,quan,menu,price) 
{
	var prod;
	var quan;
	var menu;
	var price;
	var sub;
	var x = document.getElementById(menu);
	var y = x.selectedIndex;
	var form = document.order2;
	
	quan.value = (y);
	price.value = table[prod][y];
	
	sub = parseInt(form.PriceFL.value);
	sub += parseInt(form.Price3D.value); 
	sub += parseInt(form.Price2D.value);
	sub += parseInt(form.PriceFD.value);
//	sub += parseInt(form.PriceMS.value);
//	sub += parseInt(form.PriceTW.value);
	sub += parseInt(form.PriceRF.value);
	sub += parseInt(form.PriceRB.value);
	sub += parseInt(form.PriceSE.value);
	sub += parseInt(form.PriceCN.value);
	
	form.ProdSubtotal.value = commas(sub);
}

function round(number,x) {
  return Math.round(number * Math.pow(10,x)) / Math.pow(10,x);
}

function cents(amount) {
  
  amount -= 0;
  amount = round(amount, 2);
  
  if (amount == 0)
    amount = '.00';
  else if (amount == Math.floor(amount))
    amount += '.00';
  else if (amount*10 == Math.floor(amount*10))
    amount += '0';
  
  // had to add 2 lines below because browsers were adding leading zero
  amount += '';
  amount = amount.replace('0', '');

  return '' + amount;
}

function commas(integer) {
  integer = '' + Math.round(integer);

  if (integer.length > 3) {
    var mod = integer.length%3;

    var output = (mod > 0 ? (integer.substring(0,mod)) : '');

    for (i=0 ; i < Math.floor(integer.length/3) ; i++) {
      if ((mod ==0) && (i ==0))
        output+= integer.substring(mod+3*i,mod+3*i+3);
      else
        output+= ',' + integer.substring(mod+3*i,mod+3*i+3);
    }
    return output;
  }
  return integer;
}

function monetary(amount) {
  var integer = Math.floor(amount);
  var floating = amount - integer;
  return commas(integer) + cents(floating);
}


function CalcShip(ship)
{
	var ship;
	var form = document.frmreceipt;
	sub = (parseFloat(form.SubTax.value)- 0) + parseInt(ship);
	form.BalanceDue.value = '$' + monetary(sub);
}

function hide(state) {
		
       var e = document.getElementById("salestax");
       if(state == 'CA')
          e.style.display = 'block';
       else
          e.style.display = 'none';
}

function validateEmail(s)
{
	var error = "";
	isEmail1 = /^\w+([\.\-]\w+)*\@\w+([\.\-]\w+)*\.\w+$/;
	isEmail2 = /^.*@[^_]*$/;

  	if (!isEmail1.test(s) || !isEmail2.test(s))
  		{  	
  		error = "\tPlease enter a valid Email Address.\n\n";
  		}
  	return error;
}

function validateName(s,field)
{
	var error = "";
	isLastName = /^[\w'\-\s\(\)\,\.\+\^\&\\\/]+$/;
	
	if (!isLastName.test(s) || s.length<2)
		error = "\tPlease enter a valid " + field + ".\n\n";
	return error;
}

function validateLength(s,field)
{
	var error = "";
	
	if (s.length<5)
		error = "\tPlease enter a valid " + field + ".\n\n";
	return error;
}

function checkpo(s,field)
{
	var error = "";
	var str = s.toUpperCase();
	
	if (str.length >= 5)
		{
		if (str.substring(0,2) == "PO" || str.substring(0,4) == "P.O.")
			error = "\t" + field + " can not be a PO Box.\n\n";
		}
	return error;
}

function validateShipState(thefrm)
{
	var error = "";
	var cindex = thefrm.country.selectedIndex;
    var cvalue = thefrm.country.options[cindex].value;
    var sindex = thefrm.state.selectedIndex;
    var svalue = thefrm.state.options[sindex].value;
    var zvalue = thefrm.zipcode.value;
	
	if (cvalue == 'United States' && svalue == '_')
		{
		error = "\tPlease enter a valid Ship To US State.\n\n";
		}
	else if (cvalue == 'Canada' && svalue == '_')
		{
		error = "\tPlease enter a valid Ship To Canadian Province.\n\n";
		}
	if ((cvalue == 'United States' || cvalue == 'Canada') && zvalue.length < 5)
		error += "\tPlease enter a valid Ship To Zip-Postal code.\n\n";
	return error;
}

function validateBillState(thefrm)
{
	var error = "";
	var cindex = thefrm.cccountry.selectedIndex;
    var cvalue = thefrm.cccountry.options[cindex].value;
    var sindex = thefrm.ccstate.selectedIndex;
    var svalue = thefrm.ccstate.options[sindex].value;
    var zvalue = thefrm.cczipcode.value;
	
	if (cvalue == 'United States' && svalue == '_')
		{
		error = "\tPlease enter a valid Credit Card US State.\n\n";
		}
	else if (cvalue == 'Canada' && svalue == '_')
		{
		error = "\tPlease enter a valid Credit Card Canadian Province.\n\n";
		}
	if ((cvalue == 'United States' || cvalue == 'Canada') && zvalue.length < 5)
		error += "\tPlease enter a valid Credit Card Zip-Postal code.\n\n";
	return error;
}

function CopyShipTo(frm)
{ 
	if (frm.copyship.checked==true)
		{
    
		frm.cccountry.selectedIndex = frm.country.selectedIndex;
		frm.ccfirstname.value = frm.firstname.value;
		frm.cclastname.value = frm.lastname.value;
		frm.cccompany.value = frm.company.value;
		frm.ccposition.value = frm.position.value;
		frm.ccaddress1.value = frm.address1.value;
		frm.ccaddress2.value = frm.address2.value;
		frm.cccity.value = frm.city.value;
		frm.cczipcode.value = frm.zipcode.value;
		
		setBillState(frm.cccountry.options[frm.cccountry.selectedIndex].value)
		frm.ccstate.selectedIndex = frm.state.selectedIndex;
		
		}
	else
		{
		frm.cccountry.selectedIndex = 0;
		frm.ccfirstname.value = '';
		frm.cclastname.value = '';
		frm.cccompany.value = '';
		frm.ccposition.value = '';
		frm.ccaddress1.value = '';
		frm.ccaddress2.value = '';
		frm.cccity.value = '';
		frm.cczipcode.value = '';
		
		setBillState('United States')
		frm.ccstate.selectedIndex = 0;
		}
}

function validatecc(thefrm)
{
	var error = "";
	var cardno = thefrm.cardno.value;
	//remove any whitespace character or hyphen
	cardno = cardno.replace(/\s|-/g,'');

		switch(thefrm.card.selectedIndex)
		{
		case 0:
			error = "\tPlease select a credit card.\n\n";
			break;    
		case 1:
			isCCvisa       = /^4\d{14,15}$/;
			if (!isCCvisa.test(cardno))
  			{  	
  			error = "\tPlease enter a valid Visa number.\n\n";
  			}
			break;
		case 2:
			isCCmastercard = /^5[1-5]\d{14}$/;
			if (!isCCmastercard.test(cardno))
  			{  	
  			error = "\tPlease enter a valid Master Card number.\n\n";
  			}
			break;
		case 3:
			isCCamex       = /^3[47]\d{13}$/;
			if (!isCCamex.test(cardno))
  			{  	
  			error = "\tPlease enter a valid American Express number.\n\n";
  			}
			break;
		case 4:
			isCCdiscover   = /^6011\d{12}$/;
			if (!isCCdiscover.test(cardno))
  			{  	
  			error = "\tPlease enter a valid Discover Card number.\n\n";
  			}
			break;
		}
	return error;
}

function validatedate(frm)
{
	var error = "";
	var date = new Date();
	var mindex = frm.ddlmonth.selectedIndex;
	var mvalue = frm.ddlmonth.options[mindex].value;
    var curmonth = date.getMonth() + 1;
	var yindex = frm.ddlyear.selectedIndex;
    var yvalue = frm.ddlyear.options[yindex].value;
	var curyear = date.getFullYear();
	
	//alert("date:" + Date() + "\nselMonth:" + mvalue + "\ncurMonth:" + curmonth + "\nselYear:" + yvalue + "\ncurYear:" + curyear);
	
		if (frm.ddlmonth.selectedIndex == 0 || frm.ddlyear.selectedIndex == 0)
		{
			error = "\tPlease enter the correct month and year.\n\n";
		}
		else if (curyear == yvalue)
		{
			if (mvalue < curmonth)
			{
				error = "\tPlease enter the correct month.\n\n";
			}
		}
	return error;
}

function validateProduct(frm)
{
	var error = "";
	
	if (frm.product.selectedIndex == 0)
	{
		error = "\tPlease select a Program.\n\n";

	}
	return error;
}

function validateTraining(frm)
{
	var error = "";
	
	if (frm.training.selectedIndex == 0)
	{
		error = "\tPlease select a Training Date.\n\n";

	}
	return error;
}
