// Include merchatn logo
// Include add to cart graphic


// First it call createForm
// Then it calls changeList to populate the country boxes
// Then it calls doUpdate to update the page with calculations

var lists = new Array();

// First set of text and values
lists['Canada']    = new Array();
lists['Canada'][0] = new Array(
'Alberta',
'British Columbia',
'Manitoba',
'New Brunswick',
'Newfoundland',
'NWT',
'Nova Scotia',
'Nunavut',
'Ontario',
'PEI',
'Québec',
'Saskatchewan',
'Yukon Territory'
);

// Second set of text and values
lists['USA']    = new Array();
lists['USA'][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;


// Copy the billing country value to the shipping country value on the orderform
document.forms['innerForm'].shipCountry.value=document.forms['innerForm'].billingCountry.value;


  if (document.forms['innerForm'].billingCountry.value=="Canada" || document.forms['innerForm'].billingCountry.value=="USA") {

  document.getElementById('province').innerHTML='<select name=\"M_province\" style=\"width:133px;\" onChange=\"document.forms[\'innerForm\'].shipProvince.value=document.forms[\'innerForm\'].M_province.value;doUpdate()\">';

	// 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 );

        // Copy the billing province value to the shipping province value on the orderform
        document.forms['innerForm'].shipProvince.value=document.forms['innerForm'].M_province.value;

//   doUpdate();


    } else {

  document.getElementById('province').innerHTML='<input type=text name=\"M_province\" style=\"width:130px;\" onChange=\"document.forms[\'innerForm\'].shipProvince.value=document.forms[\'innerForm\'].M_province.value;\"> ';
//  doUpdate();


  }


doUpdate();

}




/////////////////////////////////////////////////////////////////////////////////////////
// Begin createForm /////////////////////////////////////////////////////////////////////////

function createForm() {



// We only want to create the form 1 time, so wrap it in an IF statement that should only run once.with a variable called newForm
if(typeof newForm == "undefined") {


// Do we post to the Canadian or US Currency account ID?
if (document.forms['theFormer'].transactionMode.value=="100") { theFormAction = "https://select-test.wp3.rbsworldpay.com/wcc/purchase"; } else { theFormAction="https://secure.wp3.rbsworldpay.com/wcc/purchase"; }

theFormAction = 'https://devcheckout.psigate.com/HTMLPost/HTMLMessenger';


// Create and populat the content of the hidden DIV layer
createDiv='<div style="background:#ffffff;color:#222222;border:1px solid #222222; padding: 0 10px;height:500px;font-family:arial;">' +
'<form name="innerForm" onSubmit="return checkForm()" action="' + theFormAction + '" method="post">' +
'<input type="hidden" name="MerchantID" value="">' +
'<input type=hidden name"PaymentType" value="cc">' +


'<input type="hidden" name="Description01" value="">' +
'<input type="hidden" name="CustomerRefNo" value="">' +
'<input type="hidden" name="currency" value="">' +
'<input type="hidden" name="testMode" value="">' +
'<input type="hidden" name="amount" value="">' +
'<input type="hidden" name="address" value="">' +
'<input type="hidden" name="MC_ShippingAddress" value="">' +
'<input type="hidden" name="country" value="">' +
'<table border=0 cellpadding=3 cellspacing=0>' +
'<tr><Td></td><td><b style="font-size:14px";>Billing Address</b></td><td width=5></td><td><b style="font-size:14px;">Shipping Address</b></td></tr>' +
'<tr><Td>Customer:</td> <td><input type="text" name="name" style="width:130px;" onChange="document.forms[\'innerForm\'].shipName.value=document.forms[\'innerForm\'].name.value;"></td><td width=5></td><td><input type="text" name="shipName" style="width:130px;"></td></tr>' +
'<tr><Td>Country:</td><td>' +
'      <select name="billingCountry" style="width:133px;" onchange="changeList(this);">' +
'      <option value="Canada">Canada' +
'      <option value="USA">United States' +
'      <option value="United Kingdom">United Kingdom' +
'      <option value="Australia">Australia' +
'      <option value="New Zealand">New Zealand' +
'      <option value="International">None of the Above' +
'      </select>' +
'</td><td width=5></td><td><input type="text" name="shipCountry" style="width:130px;"></td></tr>' +
'<tr><Td>Street Address: </td><td><input type="text" name="street" style="width:130px;" onChange="document.forms[\'innerForm\'].shipStreet.value=document.forms[\'innerForm\'].street.value;"></td><td width=5></td><td><input type="text" name="shipStreet" style="width:130px;"></td></tr>' +
'<tr><Td>City:</td><td> <input type="text" name="M_city" style="width:130px;" onChange="document.forms[\'innerForm\'].shipCity.value=document.forms[\'innerForm\'].M_city.value;"></td><td width=5></td><td><input type="text" name="shipCity" style="width:130px;"></td></tr>' +
'<tr><Td>Prov/State</td><td>' +
'      <span id="province">' +
'      <select name="M_province" style="width:133px;" onChange="doUpdate()">' +
'      <option>Choose A Value' +
'      </select>' +
'      </span>' +
'</td><td width=5></td><td><input type="text" name="shipProvince" style="width:130px;"></td></tr>' +
'<tr><Td>Postal/Zip: </td><td><input type="text" name="postcode" style="width:130px;" onChange="document.forms[\'innerForm\'].shipPostal.value=document.forms[\'innerForm\'].postcode.value;"></td><td width=5></td><td><input type="text" name="shipPostal" style="width:130px;"></td></tr>' +
'<tr><Td>Telephone: </td><td><input type="text" name="tel" style="width:130px;" onChange="document.forms[\'innerForm\'].shipTelephone.value=document.forms[\'innerForm\'].tel.value;"></td><td width=5></td><td><input type="text" name="shipTelephone" style="width:130px;"></td></tr>' +
'<tr><Td>E-mail:</td><td> <input type="text" name="email" style="width:130px;"></td><td width=5></td><td></td></tr>' +
'</table>' +
'<table border=0 width=420 cellpadding=3 cellspacing=0>' +
'<tr><td width=210>' +
'<br><br>' +
'<b>Your Order Details:</b><br>' +
'<span id="description"></span> <br><br>' +
'Subtotal:   $<span id="subTotal">0.00</span> <br>' +
'Shipping: $<span id="shippingTotal"></span> <br>' +
'Federal Tax: $<span id="federalTaxTotal"></span> <br>' +
'Prov / State Tax: $<span id="provincialTaxTotal"></span> <br><br>' +
'<img src="tinyPayment.gif">' +
'</td>' +
'<td width=210 valign=top align=right>' +
'<br><br><br>' +
'<table border=0 cellpadding=0 cellspacing=0>' +
'<tr><td height=25 valign=bottom><img src="tinyMerchantAccountsLogo.png"></td><td align=right valign=top style="font-family:arial;font-size:9px;line-height: 13px;">EasyCheckout Powered By<br><a href="http://www.merchant-accounts.ca" target="_blank" style="font-size:11px;color:#6bbae6;font-family:arial;font-weight:bold;text-decoration:none;">Merchant Accounts.ca</a></td></tr>' +
'</table>'+
'<br><br><br><br>'+
'<h2 style="font-size:15px;">Order Total: $<span id="orderTotal" style="font-size:20px;"></span>&nbsp;  </h2>' +
'<input type="submit" value="Submit Your Order" style="width:200px;">' +
'<!-- Call (800) 898-4243 with any<br>' +
'questions regarding your order. -->' +
'</td>' +
'</tr>' +
'</table>' +
'</form>' +
'</div>';



document.getElementById('easyCheckout_content').innerHTML=createDiv;

newForm="created";  // One this variable is created it won't crete the form again.

} // End wrapping the creat form inan if statement



changeList(document.forms['innerForm'].billingCountry);





}

//  End createForm
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////












/////////////////////////////////////////////////////////////////////////////////////////
// Begin doUpdate /////////////////////////////////////////////////////////////////////////

function doUpdate() {


var subTotal="";
shipCost="";
provTaxCost="";
countryTaxCost="";
var finalTotal="";
var productDescription="";



subTotal = (document.forms['theFormer'].quantity.value * document.forms['theFormer'].itemCost.value)

// Shipping Cost in Canada
if (document.forms['innerForm'].billingCountry.value == "Canada") {  if (document.forms['theFormer'].shippingCalculation.value == "flatRate") {  shipCost = document.forms['theFormer'].shippingCostCanada.value; } else { shipCost = (document.forms['theFormer'].shippingCostCanada.value * document.forms['theFormer'].quantity.value); }  }

// Shipping Cost in USA
if (document.forms['innerForm'].billingCountry.value == "USA") { if (document.forms['theFormer'].shippingCalculation.value == "flatRate") {  shipCost = document.forms['theFormer'].shippingCostUSA.value; } else { shipCost = (document.forms['theFormer'].shippingCostUSA.value * document.forms['theFormer'].quantity.value); } }



// Shipping Cost International
if (document.forms['innerForm'].billingCountry.value != "Canada" && document.forms['innerForm'].billingCountry.value != "USA") { if (document.forms['theFormer'].shippingCalculation.value == "flatRate") {  shipCost = document.forms['theFormer'].shippingCostInternational.value; } else { shipCost = (document.forms['theFormer'].shippingCostInternational.value * document.forms['theFormer'].quantity.value); } }


shipCost = Math.round(((shipCost/1))*100)/100;
if (shipCost.toFixed) { shipCost = shipCost.toFixed(2); }    // sets it to two decimal places.


// Federal Tax Cost
if (document.forms['theFormer'].taxCountry.value == "all") { countryTaxCost = (subTotal * (document.forms['theFormer'].countryRate.value * .01)); }
if (document.forms['theFormer'].taxCountry.value == "CanadaUSA") {  if (document.forms['innerForm'].billingCountry.value=="USA" || document.forms['innerForm'].billingCountry.value=="Canada") { countryTaxCost = (subTotal * (document.forms['theFormer'].countryRate.value * .01)); } }
if (document.forms['innerForm'].billingCountry.value == document.forms['theFormer'].taxCountry.value) { countryTaxCost = (subTotal * (document.forms['theFormer'].countryRate.value * .01)); }

// Format the Country Tax number to display properly
countryTaxCost = Math.round(((countryTaxCost/1))*100)/100;
if (countryTaxCost.toFixed) { countryTaxCost = countryTaxCost.toFixed(2); }    // sets it to two decimal places.




//  Provincial tax anywhere in Canada
if (document.forms['theFormer'].taxProvince.value == "anyProv" && document.forms['innerForm'].billingCountry.value=="Canada") {  provTaxCost = (subTotal * (document.forms['theFormer'].provinceRate.value * .01)); }

//  Provincial tax anywhere in USA
if (document.forms['theFormer'].taxProvince.value == "anyState" && document.forms['innerForm'].billingCountry.value=="USA") {  provTaxCost = (subTotal * (document.forms['theFormer'].provinceRate.value * .01)); }

//  Provincial tax to any State or Province
if (document.forms['theFormer'].taxProvince.value == "anyProvOrState") {  if (document.forms['innerForm'].billingCountry.value=="USA" || document.forms['innerForm'].billingCountry.value=="Canada") {  provTaxCost = (subTotal * (document.forms['theFormer'].provinceRate.value * .01)); } }


// First make sure the province isn't blank or it will apply tax to all transactions when the customer hasn't typed in a province.
if (document.forms['innerForm'].M_province.value!="") {

    // Provincial tax must match province
   if  (document.forms['innerForm'].M_province.value == document.forms['theFormer'].taxProvince.value) { provTaxCost = (subTotal * (document.forms['theFormer'].provinceRate.value * .01)); }
}


// Format the provincial tax number to display propeprly
provTaxCost = Math.round(((provTaxCost/1))*100)/100;
if (provTaxCost.toFixed) { provTaxCost = provTaxCost.toFixed(2); }    // sets it to two decimal places.


finalTotal = Number(subTotal) + Number(shipCost) + Number(provTaxCost) + Number(countryTaxCost);
finalTotal = Math.round(((finalTotal/1))*100)/100;

// 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 (finalTotal.toFixed) { finalTotal = finalTotal.toFixed(2); }


productDescription = document.forms['theFormer'].quantity.value + ' - ' + document.forms['theFormer'].productDescription.value;




// Display the calculated totals on the orderform
document.getElementById('subTotal').innerHTML=subTotal;
document.getElementById('shippingTotal').innerHTML=shipCost;
document.getElementById('provincialTaxTotal').innerHTML=provTaxCost;
document.getElementById('federalTaxTotal').innerHTML=countryTaxCost;
document.getElementById('description').innerHTML=productDescription;
document.getElementById('orderTotal').innerHTML=finalTotal;




// Update the form data for sending transaction to gateway
document.forms['innerForm'].amount.value = finalTotal;
document.forms['innerForm'].currency.value = document.forms['theFormer'].transactionCurrency.value
document.forms['innerForm'].testMode.value = document.forms['theFormer'].transactionMode.value
document.forms['innerForm'].instId.value = document.forms['theFormer'].installation.value
document.forms['innerForm'].desc.value = productDescription;
document.forms['innerForm'].address.value = document.forms['innerForm'].street.value + ' \n' + document.forms['innerForm'].M_city.value + ', ' + document.forms['innerForm'].M_province.value + '\n' + document.forms['innerForm'].postcode.value;
document.forms['innerForm'].MC_ShippingAddress.value = 'Shipping Address: \n' + document.forms['innerForm'].shipName.value + ' \n' + document.forms['innerForm'].shipStreet.value + '\n' + document.forms['innerForm'].shipCity.value + ', ' + document.forms['innerForm'].shipProvince.value + '  ' + document.forms['innerForm'].shipPostal.value + '\n' +document.forms['innerForm'].shipTelephone.value;


if (document.forms['theFormer'].shippingCalculation.value == "flatRate") {  shipCost = document.forms['theFormer'].shippingCostCanada.value; } else { shipCost = (document.forms['theFormer'].shippingCostCanada.value * document.forms['theFormer'].quantity.value); }



}


// End doUpdate /////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////







function checkForm() {



doUpdate();

var $temp = new Array(7)

$temp[0]='name'
$temp[1]='billingCountry'
$temp[2]='address'
$temp[3]='M_city'
$temp[4]='M_province'
$temp[5]='postcode'
$temp[6]='tel'
$temp[7]='email'

 var missing=0


if (document.forms['innerForm'].email.value=='') { (missing=1); }

 if (missing==1) {
 alert ('Your email address is required.  \n\n If you dont include your contact information\, we cant ship your order!');
 document.forms['innerForm'].email.focus();
 return false
 }


// Convert the country from the orderform to it's 2 digit code for WorldPay submissions
if (document.forms['innerForm'].billingCountry.value == 'Canada') { document.forms['innerForm'].country.value = 'ca'; }
if (document.forms['innerForm'].billingCountry.value == 'USA') { document.forms['innerForm'].country.value = 'us'; }
if (document.forms['innerForm'].billingCountry.value == 'Australia') { document.forms['innerForm'].country.value = 'au'; }
if (document.forms['innerForm'].billingCountry.value == 'United Kingdom') { document.forms['innerForm'].country.value = 'gb'; }
if (document.forms['innerForm'].billingCountry.value == 'New Zealand') { document.forms['innerForm'].country.value = 'nz'; }



document.forms['innerForm'].cartId.value = '\n' + document.forms['innerForm'].MC_ShippingAddress.value +'\n' +
'Fed Tax: ' + countryTaxCost + ' | Prov Tax: ' + provTaxCost + ' | Shipping: ' + shipCost;

}
