<!--
/*
  Allen Shymchuk
*/

var debug = new Boolean();
debug = false;
var thanksurl = 'http://www.personalizingpilates.com/orderMp3success.html';
var nothanksurl = 'http://www.personalizingpilates.com/orderMp3failure.html';

var mp3array = [];
var mp3items = new Number();
var pcarray = [];
var pcitems = new Number();

function Init() {
  document.entryform.Bname.focus();
  LoadMP3s();
//  LoadPromoCodes();
}
function LoadPromoCodes() {
  var ajaxGet = new net.ContentLoader( '/orderCDpromocodes.xml', LoaderPromoCodes );
}
function LoaderPromoCodes() {
  var xmlDoc = this.req.responseXML.documentElement;
  pcarray = xmlDoc.getElementsByTagName('promocode');
  pcitems = pcarray.length;
}
function LoadMP3s() {
  mp3array = [];
  mp3items = 0;
  var ajaxGet = new net.ContentLoader( '/orderCD.xml', LoaderMP3s );
}
function LoaderMP3s() {
  var xmlDoc = this.req.responseXML.documentElement;
  mp3array = xmlDoc.getElementsByTagName('mp3');
  mp3items = mp3array.length;
  DisplayItems();
}
function DisplayItems() {
  var el = document.getElementById( 'formitems' );
  RemoveElements( el );
  var divnode = document.createElement( 'div' );
  divnode.setAttribute( 'id', 'divnode' );
  divnode.setAttribute( 'name', 'divnode' );
  el.appendChild(divnode);
  divnode = document.getElementById( 'divnode' );

  var tablenode = document.createElement( 'table' );
  tablenode.setAttribute( 'id', 'tablenode' );
  tablenode.setAttribute( 'name', 'tablenode' );
  tablenode.setAttribute( 'class', 'text' ); // Firefox
  tablenode.className = 'text'; // IE
  tablenode.setAttribute( 'style', 'table-layout: fixed;' ); // Firefox
  tablenode.style.cssText = 'table-layout: fixed;'; // IE does not like setAttribute method
  tablenode.setAttribute( 'width', '605' );
  tablenode.setAttribute( 'border', '0' );
  tablenode.setAttribute( 'align', 'center' );
  tablenode.setAttribute( 'cellSpacing', '0' );
  tablenode.setAttribute( 'cellPadding', '0' );
  tablenode.setAttribute( 'bgColor', '#FFFFFF' );
  divnode.appendChild(tablenode);
  tablenode = document.getElementById( 'tablenode' );

  var colgroupnode = document.createElement( 'colgroup' );
  colgroupnode.setAttribute( 'id', 'colgroupnode' );
  colgroupnode.setAttribute( 'name', 'colgroupnode' );
  tablenode.appendChild(colgroupnode);
  colgroupnode = document.getElementById( 'colgroupnode' );

  var node = document.createElement( 'col' );
  node.setAttribute( 'width', '80' );
  colgroupnode.appendChild(node);
  node = document.createElement( 'col' );
  node.setAttribute( 'width', '*' );
  colgroupnode.appendChild(node);
  node = document.createElement( 'col' );
  node.setAttribute( 'width', '120' );
  colgroupnode.appendChild(node);

  var theadnode = document.createElement( 'thead' );
  theadnode.setAttribute( 'id', 'theadnode' );
  theadnode.setAttribute( 'name', 'theadnode' );
  tablenode.appendChild(theadnode);
  theadnode = document.getElementById( 'theadnode' );
  var theadrownode = document.createElement( 'tr' );
  theadrownode.setAttribute( 'id', 'theadrownode' );
  theadrownode.setAttribute( 'name', 'theadrownode' );
  theadnode.appendChild(theadrownode);
  theadrownode = document.getElementById( 'theadrownode' );
  node = document.createElement( 'th' );
  node.setAttribute( 'align', 'left' );
  node.setAttribute( 'vAlign', 'bottom' ); // another IE issue, must use vAlign
  node.setAttribute( 'class', 'handTitle' ); // Firefox
  node.className = 'handTitle'; // IE
  var txtnode = document.createTextNode( 'Quantity' );
  node.appendChild( txtnode );
  theadrownode.appendChild( node );
  node = document.createElement( 'th' );
  node.setAttribute( 'align', 'left' );
  node.setAttribute( 'vAlign', 'bottom' ); // another IE issue, must use vAlign
  node.setAttribute( 'class', 'handTitle' ); // Firefox
  node.className = 'handTitle'; // IE
  var txtnode = document.createTextNode( 'Item' );
  node.appendChild( txtnode );
  theadrownode.appendChild( node );
  node = document.createElement( 'th' );
  node.setAttribute( 'align', 'left' );
  node.setAttribute( 'vAlign', 'bottom' ); // another IE issue, must use vAlign
  node.setAttribute( 'class', 'handTitle' ); // Firefox
  node.className = 'handTitle'; // IE
  var txtnode = document.createTextNode( 'Price' );
  node.appendChild( txtnode );
  theadrownode.appendChild( node );

  var tbodynode = document.createElement( 'tbody' );
  tbodynode.setAttribute( 'id', 'tbodynode' );
  tbodynode.setAttribute( 'name', 'tbodynode' );
  tablenode.appendChild(tbodynode);
  tbodynode = document.getElementById( 'tbodynode' );

  for( i=0; i<mp3items; i++ ) {
    j = i + 1;
    var trnode = document.createElement( 'tr' );
    trnode.setAttribute( 'id', 'tblRow'+i );
    trnode.setAttribute( 'name', 'tblRow'+i );
    trnode.setAttribute( 'onmouseover', "style.background='#FFFFFF'" ); // Firefox only; technically this is event trigger
    trnode.setAttribute( 'onmouseout', "style.background='#FCFAD8'" ); // Firefox only; technically this is event trigger
    tbodynode.appendChild(trnode);
    trnode = document.getElementById( 'tblRow'+i );

    var tdnode = document.createElement( 'td' );
    tdnode.setAttribute( 'align', 'left' );
    tdnode.setAttribute( 'vAlign', 'top' );
    var inputnode = document.createElement('input');
    inputnode.setAttribute( 'type', 'text' );
    inputnode.setAttribute( 'name', 'Quantity' + j );
    inputnode.setAttribute( 'id', 'Quantity' + j );
    inputnode.setAttribute( 'size', '3' );
    tdnode.appendChild(inputnode);
    trnode.appendChild(tdnode);

    tdnode = document.createElement( 'td' );
    tdnode.setAttribute( 'align', 'left' );
    tdnode.setAttribute( 'vAlign', 'top' );
    var s = new String();
    s = getNodeValue(mp3array[i], 'item');
//    if( getNodeValue(mp3array[i], 'duration') != '' ) s = s + ' (' + getNodeValue(mp3array[i], 'duration') + ' minutes)';
    var txtnode = document.createTextNode( s );
    tdnode.appendChild( txtnode );
    inputnode = document.createElement( 'input' );
    inputnode.setAttribute( 'type', 'hidden' );
    inputnode.setAttribute('name', 'ItemID' + j );
    inputnode.setAttribute('id', 'ItemID' + j );
    inputnode.setAttribute( 'value', s );
    tdnode.appendChild( inputnode );
    trnode.appendChild(tdnode);

    tdnode = document.createElement( 'td' );
    tdnode.setAttribute( 'align', 'left' );
    tdnode.setAttribute( 'vAlign', 'top' );
    var price = new String();
    price = ( formatCurrency(getNodeValue(mp3array[i], 'price')) == '$0.00' ) ? 'N/A' : formatCurrency(getNodeValue(mp3array[i], 'price')) + ' CAN each' ;
    var txtnode = document.createTextNode( price );
    tdnode.appendChild( txtnode );
    inputnode = document.createElement( 'input' );
    inputnode.setAttribute( 'type', 'hidden' );
    inputnode.setAttribute('name', 'Price' + j );
    inputnode.setAttribute('id', 'Price' + j );
    inputnode.setAttribute( 'value', getNodeValue(mp3array[i], 'price') );
    tdnode.appendChild( inputnode );
    trnode.appendChild(tdnode);
  }
}
function DisplayForm(Form){j=Form.elements.length;var s=new String();for(i=0;i<j;i++)s+=i+', '+Form.elements[i].name+': '+Form.elements[i].value+' ___ ';s+='Action: '+Form.action+'\n';alert(s);}
function formatCurrency(num){num=num.toString().replace(/\$|\,/g,'');if(isNaN(num))num="0";sign=(num==(num=Math.abs(num)));num=Math.floor(num*100+0.50000000001);cents=num%100;num=Math.floor(num/100).toString();if(cents<10)cents="0"+cents;for(var i=0;i<Math.floor((num.length-(1+i))/3);i++)num=num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));return(((sign)?'':'-')+'$'+num+'.'+cents);}
function highlight(Form){Form.focus();Form.select();}
function isEmailValid(Form,error){if(isEmpty(Form,error))return false;badKeys=" /:;$%^&*()!|\~`=+'><";for(i=0;i<badKeys.length;i++){badChar=badKeys.charAt(i);if(Form.value.indexOf(badChar,0)>-1){alert("There is an invalid character in the email address. Please try again.");highlight(Form);return false}}atPos=Form.value.indexOf("@",0);if(atPos==-1){alert("There must be a @ with in your customer's email address. Please try again.");highlight(Form);return false}if(atPos<1){alert("The @ character can not be your first character within the email address. Please try again.");highlight(Form);return false}if(Form.value.indexOf("@",atPos+1)!=-1){alert("Only one @ within the email address is allowed. Please try again.");highlight(Form);return false}periodPos=Form.value.indexOf(".",atPos);if(periodPos<=atPos+2){alert("Invalid email address. There should be at least 2 characters after the period. Please try again.");highlight(Form);return false}if(periodPos<atPos+1){alert("There should be at least one '.' after the @ symbol in the email address. Please try again.");highlight(Form);return false}return true}
function isEmpty(Form,error){if(Form.value==''||Form.value==' '){alert(error);highlight(Form);return true}return false}
function ValidateForm( Form ) {
  var valid = new Boolean();
  valid = false;
  if( isEmpty( Form.Bname, 'You must provide a name') ) return valid;
  if( !isEmailValid( Form.Email,'You must provide an email address') ) { Form.Email.focus(); return valid; }
  if( isEmpty( Form.Baddress1, 'You must provide your address') ) return valid;
  if( isEmpty( Form.Bcity, 'You must provide your city') ) return valid;
  if( isEmpty( Form.Bpostalcode, 'You must provide a postal code') ) return valid;
  if( isEmpty( Form.Phone, 'You must provide a phone number') ) return valid;

  // have you selected any items
  for( i=1; i<=mp3items; i++ ) if( document.getElementById('Quantity'+i).value != '' && document.getElementById('Quantity'+i).value != 0 ) valid = true;
  if( !valid ) {
    alert( 'You must select a quantity' );
    Form.Quantity1.focus();
    return valid;
  }

  // determine promo codes
//  var pc = Form.Comments.value;
//  var amount = new Number();
//  var percent = new Number();
//  amount = 0;
//  percent = 0;
//  for( i=0; i<pcitems; i++ ) {
//    if( getNodeValue(pcarray[i], 'code') == pc ) {
//      // make sure amount and percent have values
//      if( pcarray[i].getElementsByTagName('amount')(0).hasChildNodes ) amount = parseFloat(getNodeValue(pcarray[i], 'amount'));
//      if( pcarray[i].getElementsByTagName('percent')(0).hasChildNodes ) percent = parseFloat(getNodeValue(pcarray[i], 'percent'));
//    }
//  }

  // adjust totals based on promo code
//  for( i=1; i<=mp3items; i++ ) document.getElementById('Price'+i).value = (document.getElementById('Price'+i).value - amount) * ((100-percent)/100);

  // display totals
  var s = new String();
  s = '--- Select OK to confirm order ---\n\n';
//  if( Form.Comments.value != '' ) {
//    s = s + 'Promo Code: ' + Form.Comments.value + '\n';
//    if( amount ) s = s + 'Entitles you to ' + formatCurrency(amount) + ' off each item.\n\n';
//    if( percent ) s = s + 'Entitles you to ' + percent + '% off each item.\n\n';
//  }
  var subtotal = new Number();
  subtotal = 0;
  for( i=1; i<=mp3items; i++ ) {
    if( document.getElementById('Quantity'+i).value != '' && document.getElementById('Quantity'+i).value != 0 ) {
      s = s + document.getElementById('Quantity'+i).value + ' pc @ ' + formatCurrency(document.getElementById('Price'+i).value) + 'ea for ' + document.getElementById('ItemID'+i).value + '\n';
      subtotal = subtotal + (parseFloat(document.getElementById('Quantity'+i).value) * parseFloat(document.getElementById('Price'+i).value));
    } else document.getElementById('Quantity'+i).value = 0;
  }
  s = s + '\nSubtotal:\t' + formatCurrency(subtotal) + '\n';
  if( document.getElementById('CountryID').value == 'Canada' ) {
    Form.Tax1.value = subtotal * 0.13;
//    Form.Tax1.value = subtotal * 0;
    s = s + 'Canadian taxes: ' + formatCurrency(Form.Tax1.value) + '\n';
  }
  Form.SubTotal.value = subtotal;
  s = s + 'Total: ' + formatCurrency(parseFloat(Form.SubTotal.value) + parseFloat(Form.Tax1.value));

  // submit order
  if ( confirm( s ) ) {
    alert( 'You are about to be redirected to the Payment section of the site. Please note: enter your credit card number with no spaces.');
    Form.ThanksURL.value = thanksurl;
    Form.NoThanksURL.value = nothanksurl;
    Form.action = "https://checkout.psigate.com/HTMLPost/HTMLMessenger";
    // Form.action = 'https://secure.psigate.com:7934/Messenger/HTMLMessenger';
    // Form.action = 'https://devcheckout.psigate.com/HTMLPost/HTMLMessenger';
    if( debug ) DisplayForm( Form );
//    return false;
    return true;
  } else {
    if( debug ) DisplayForm( Form );
    LoadMP3s();
    return false;
  }
}
window.onload = Init;
-->
