var lists = new Array();

// First set of text and values
lists['ca']    = new Array();
lists['ca'][0] = new Array(
'Alberta',
'British Columbia', 
'Manitoba',
'New Brunswick', 
'Newfoundland', 
'Northwest Territories', 
'Nova Scotia', 
'Nunavut', 
'Ontario',
'Prince Edward Island',
'Québec', 
'Saskatchewan',
'Yukon Territory'
);

// Second set of text and values
lists['us']    = new Array();
lists['us'][0] = new Array(
'Alabama',
'Arizona',
'Arkansas',
'California',
'Colorado',
'Connecticut',
'Delaware',
'District of Columbia',
'Florida',
'Georgia',
'Hawaii',
'Idaho',
'Illinois',
'Indiana',
'Iowa',
'Kansas',
'Kentucky',
'Louisiana',
'Maine',
'Maryland',
'Massachusetts',
'Michigan',
'Minnesota',
'Mississippi',
'Missouri',
'Montana',
'Nebraska',
'Nevada',
'New Hampshire',
'New Jersey',
'New Mexico',
'New York',
'North Carolina',
'North Dakota',
'Ohio',
'Oklahoma',
'Oregon',
'Pennsylvania',
'Puerto Rico',
'Rhode Island',
'South Carolina',
'South Dakota',
'Tennessee',
'Texas',
'Utah',
'Vermont',
'Virginia',
'Virgin Islands',
'Washington',
'West Virginia',
'Wisconsin',
'Wyoming'
);

// Third set of text and values
lists['in']    = new Array();
lists['in'][0] = new Array(
'International'
);

function emptyList( box ) {
	// Set each option to null thus removing it
	while ( box.options.length ) box.options[0] = null;
}

// This function assigns new drop down options to the given
// drop down box from the list of lists specified

function fillList( box, arr ) {
	// arr[0] holds the display text
	// arr[1] are the values

	for ( i = 0; i < arr[0].length; i++ ) {

		// Create a new drop down option with the
		// display text and value from arr

		option = new Option( arr[0][i], arr[0][i]);

		// Add to the end of the existing options

		box.options[box.length] = option;
	}

	// Preselect option 0

	box.selectedIndex=0;
}

// This function performs a drop down list option change by first
// emptying the existing option list and then assigning a new set

function changeList( box ) {

// Flush variable used to calculate international shipping used later on in priceonPage 
intShipping=0;

  if (document.forms['drops'].country.value=="ca" || document.forms['drops'].country.value=="us") { 

  document.getElementById('province').innerHTML='<select name=\"M_province\" size=1 onChange=\"priceonPage()\">';

	// Isolate the appropriate list by using the value
	// of the currently selected option

	list = lists[box.options[box.selectedIndex].value];

	// Next empty the fill list

	emptyList( box.form.M_province );

	// Then assign the new list values

	fillList( box.form.M_province, list );

    } else {
  
  document.getElementById('province').innerHTML='<input type=text name=\"M_province\" size=18>';

  }

  // Determine if the country is one we will ship to
  if (document.forms['drops'].country.value!="ca" && document.forms['drops'].country.value!="us" && document.forms['drops'].country.value!="in") { 
  intShipping=130;
  alert('Shipping for orders to countries outside North America is $130.\n\nFilling material can not be offered for international orders as it is too large to ship, and is much more cost effective for you to purchase locally. \(See filling notes at bottom of orderform\)   \n \n Your package will be sent via Canada Post Xpresspost. Xpresspost offers delivery within 7 business days.');
  }

  if (document.forms['drops'].country.value=="in") { alert('For orders to countries not listed above, email us for a shipping quote before ordering. \n \n sales@bean-bag-chairs.ca'); }

priceonPage();

}



// This function changes the price, and the order description, based upon the current
// selection in the drop down boxes on the orderform

function priceonPage() { 
var chairprice=319;
var subtotal=0;
var blacktotal=0;
var browntotal=0;
var tantotal=0;
var numchairs = 0;
shippingcost = 0;
var chargePST=false;
PSTamount=0;
PSTamountText=0;
var chargeGST=false;
GSTamount=0;
GSTamountText=0;
document.forms['drops'].M_PST.value = "";
document.forms['drops'].M_GST.value = "";
document.forms['drops'].M_shipping.value = "";

numchairs = parseInt(document.forms['drops'].M_colour_black.value) + parseInt(document.forms['drops'].M_colour_brown.value) + parseInt(document.forms['drops'].M_colour_tan.value);
blacktotal = ((document.forms['drops'].M_colour_black.value) * chairprice);
browntotal = ((document.forms['drops'].M_colour_brown.value) * chairprice);
tantotal = ((document.forms['drops'].M_colour_tan.value) * chairprice);
subtotal = (tantotal + browntotal + blacktotal);

  // Purchase with filling
  if (document.forms['drops'].M_filling[1].checked && intShipping == 0) {
  shippingcost = (115 * numchairs);
  subtotal = (subtotal + shippingcost);
  document.forms['drops'].M_shipping.value = shippingcost;
  }

  // International shipping
  if (intShipping > 0 && numchairs > 0) {
  shippingcost = (intShipping * numchairs);
  subtotal = (subtotal + shippingcost);
  document.forms['drops'].M_shipping.value = shippingcost;
  }


  if (document.forms['drops'].country.value=="ca" && numchairs > 0)  { 
  chargeGST = true; 
  GSTamount = subtotal * .05
  GSTamount = Math.round(((GSTamount/1))*100)/100;
  if (GSTamount.toFixed) { GSTamountText = GSTamount.toFixed(2); } else { GSTamountText = GSTamount; }
  document.forms['drops'].M_GST.value = GSTamount;
  }

  if (document.forms['drops'].M_province.value=="Ontario" && numchairs > 0)  { 
  chargePST = true; 
  PSTamount = (subtotal * .08);
  PSTamount = Math.round(((PSTamount/1))*100)/100;
  if (PSTamount.toFixed) { PSTamountText = PSTamount.toFixed(2); } else { PSTamountText = PSTamount; }
  document.forms['drops'].M_PST.value = PSTamount;
  }  

  subtotal = (subtotal + GSTamount + PSTamount)
  subtotal = Math.round(((subtotal/1))*100)/100;
  // subtotal.tofixed(2) sets it to two decimal places.  The if statement just checks to make sure the browser supports the twoFixed command.  (Javascript 1.5 and above)
  if (subtotal.toFixed) { subtotal = subtotal.toFixed(2); }
 


document.forms['drops'].amount.value = subtotal;
document.forms['drops'].desc.value = "Leather Bean Bag Chairs -  Black Chairs: " + document.forms['drops'].M_colour_black.value + " |  Brown Chairs: " + document.forms['drops'].M_colour_brown.value + " |  Tan Chairs: " + document.forms['drops'].M_colour_tan.value;
document.getElementById('theamount').innerHTML=subtotal;
if (chargeGST) { document.getElementById('gst').innerHTML=('<b class="btext">Includes $' + GSTamountText + ' GST </b>'); } else { document.getElementById('gst').innerHTML= ""; }
if (chargePST) { document.getElementById('pst').innerHTML=('<b class="btext">and $' + PSTamountText + ' PST</b>'); } else { document.getElementById('pst').innerHTML= ""; }
if (intShipping > 0 && numchairs > 0) { document.getElementById('shipping').innerHTML=('<b class="btext">Includes $' + shippingcost + ' for international shipping </b>'); } else { document.getElementById('shipping').innerHTML= ""; }


}


function checkForm() { 
var chairprice=319;
leaving=false;
var subtotal=0;

var $temp = new Array(7)

$temp[0]='name'
$temp[1]='country'
$temp[2]='address'
$temp[3]='M_city'
$temp[4]='M_province'
$temp[5]='postcode'
$temp[6]='tel'
$temp[7]='email'

 var missing=0
 var nochairs=0

if (document.drops.email.value=='') { (missing=1); }
if (document.forms['drops'].amount.value==0) { (nochairs=1); }

// for (var loop_index=0; loop_index < 8; loop_index++) {
//    $temp2=$temp[loop_index]
//    if (document.drops[$temp2].value=='') { (missing=1); break;  }
// }

// radiovar=0

// if (document.drops.M_filling.checked==true) { (radiovar=1) }


// if (document.forms['drops'].M_province.value=="Ontario")  {
 // var temp=(document.forms['drops'].amount.value);
 // taxes = (temp * .15);
 // var taxtotal = Math.round(((temp/1) + (taxes/1))*100)/100;
 // document.forms['drops'].amount.value = (taxtotal);
// }


 if (missing==1) { 
// alert ('Your contact information is required.  \n\n If you dont include your contact information\, we cant ship your chair!');
alert ('Your email address is required.  \n\n If you dont include your contact information\, we cant ship your chair!');
 document.drops.email.focus();
 return false
 }

 if (nochairs==1) {
 alert ('You have not selected the colour or quantity of the chairs you wish to order!');
 document.drops.M_colour_black.focus();
 return false
 }

// if (radiovar==0)  { 
// alert ('Please select your desired shipping option.  \n \n  You can choose to have your chair shipped to you empty\, \n or aleady filled with polystryene beads.');
// document.forms['drops'].amount.value = 319;
// return false;
// }

// alert('Your total is going to be');
// alert(document.forms['drops'].amount.value);

}
