var onloadCue= new Array();
function onloadController() {
  for( f=0; f < onloadCue.length; f++ ) {
    //alert( onloadCue[f] + " (" + (f+1) +" of "+ onloadCue.length +" )" );
    try {
    eval(onloadCue[f]);
    } catch(e) { /*alert(e)*/ }
  }
}
window.onload= onloadController;

function centerPopup(srcDoc, w, h, features) {
  leftPosition = (screen.width) ? (screen.width-w)/2 : 0;
  topPosition = (screen.height) ? (screen.height-h)/2 : 0;
  windowHandler = window.open(srcDoc,'popupWin','width='+ w +',height='+ h +',top='+ topPosition +',left='+ leftPosition +','+ features);
  windowHandler.focus();
}

function getCurrency(amount, language) {
  amount= Math.round(amount*100)/100;
  // free version: url= "http://www.xe.com/ucc/convert.cgi?&From=CAD&To=USD&Amount=" + amount;
  template= (language.toLowerCase() == 'fr') ? "11B-FR" : "11B";
  if (language.toLowerCase() == 'fr') {
    amount= amount.toString().replace( /\./g, "," );
  }
  url= "http://www.xe.com/ucc/convert.cgi?template=" + template + "&From=CAD&To=USD&Amount=" + amount;
  w= window.open( url, "currencyConverter", "width=550,height=300,status=no,address=no,scrollbars=yes,resizeable=yes,toolbar=no,menubar=no" );
}


function addtowishlist(){
  var form = document.getElementsByName('addToCartForm')[0];
  form.action="addToWishList.htm";
  form.submit();
}

function fixIE(divId) {
  if (navigator.appName == "Microsoft Internet Explorer" && navigator.userAgent.indexOf("Opera") < 0) {
    var navbarHt = 28;
    var leftOffset = 4;
    var ieEls = document.getElementById(divId).getElementsByTagName("LI");
    var shimobject = document.getElementById("iframeshim");
    for (var i=0; i<ieEls.length; i++) {
      ieEls[i].onmouseover = function() {
      this.className += " iehover";
//        alert(this.childNodes.length + " " + this.childNodes[0].nodeName + " " + this.childNodes[1].nodeName);
        if (this.childNodes[1].nodeName == 'UL') {
          shimobject.style.top = eval(this.offsetParent.offsetTop + navbarHt) + "px";
          shimobject.style.left = eval(this.offsetLeft + leftOffset) + "px";
          shimobject.style.width = this.childNodes[1].offsetWidth + "px";
          shimobject.style.height = this.childNodes[1].offsetHeight + "px";
          shimobject.style.display = "block";
        }
      }
      ieEls[i].onmouseout = function() {
        this.className = this.className.replace(new RegExp(" iehover\\b"), "");
        shimobject.style.display = "none";
      }
    }
  }  
}


function setZeroQuantity(chkObj,idx) {

  var qtyTxtObj = chkObj.form["lineItem("+ idx +")"];
  qtyTxtObj.value = chkObj.checked ? 0 : qtyTxtObj.defaultValue;
  
//  var qtyTxtObj = chkObj.form.lineItems;

//  if(qtyTxtObj.length > 0){
//    qtyTxtObj[idx].value = chkObj.checked ? 0 : qtyTxtObj[idx].defaultValue;
//  } else {
//    qtyTxtObj.value = chkObj.checked ? 0 : qtyTxtObjdefaultValue;
//  }

//  if(document.updateCartForm.quantity.length > 0){
//    document.updateCartForm.quantity[idx].value = chkObj.checked ? 0 : document.updateCartForm.quantity[idx].defaultValue;
//  } else {
//    document.updateCartForm.quantity.value = chkObj.checked ? 0 : document.updateCartForm.quantity.defaultValue;
//  }
  
}


function setCheckbox(txtObj,idx) {
  if(txtObj.value == '') txtObj.value = 0;
  if(document.updateCartForm.removeitem.length > 0){
    if(txtObj.value == 0){
      document.updateCartForm.removeitem[idx].checked = true;
    } else {
      document.updateCartForm.removeitem[idx].checked = false;
    }
  } else {
    if(txtObj.value == 0){
      document.updateCartForm.removeitem.checked = true;
    } else {
      document.updateCartForm.removeitem.checked = false;
    }
  }
}

function enableSizeList(selObj) {
  var sizeSelObj = selObj.form.size;
  if (selObj.selectedIndex != 0) {
    sizeSelObj.selectedIndex = 0;
    sizeSelObj.disabled = false; // size enabled
  } else {
    sizeSelObj.selectedIndex = 0;
    sizeSelObj.disabled = true; // size disabled
  }
}

function enableMultiaddSizeList(selObj, idx) {
  var sizeSelObj = selObj.form["product["+ idx +"].size"];
  if (selObj.selectedIndex != 0) {
    sizeSelObj.selectedIndex = 0;
    sizeSelObj.disabled = false; // size enabled
  } else {
    sizeSelObj.selectedIndex = 0;
    sizeSelObj.disabled = true; // size disabled
  }
}

function setMultiaddOneQuantity(chkObj, idx) {
	var selObj = chkObj.form["product["+ idx +"].quantity"];

  if (chkObj.checked == true){
	  // quantity 1
	  selObj.selectedIndex = 1;
	} else {
	  // quantity 0
	  selObj.selectedIndex = 0;
	}
}

function setMultiaddCheckbox(selObj, idx) {
	var chkObj = selObj.form["product["+ idx +"].include"];
  if (selObj.selectedIndex > 0){
	  //check box
	  chkObj.checked = true;
	} else {
	  // uncheck box
	  chkObj.checked = false;
	}
}


/* #### PRODUCT OPTIONS #### */
function ProductOptions () {
  this.optionsArr= new Array();
  this.labelsArr= new Array();
  this.isLoaded= false;
  this.labelPrefix="";

  this.load= function () {
    // load(): assign form, select fields, default selected options
    if( this.isLoaded ) return;
    if( typeof this.field0 == "undefined" ) {
      alert( "ProductOptions: Could not find field0. Please reload the page." );
      return;
    }
    this.form= document.getElementById(this.form);
    if( typeof this.field0 != "undefined" ) this.field0= this.form[this.field0];
    if( typeof this.field1 != "undefined" ) this.field1= this.form[this.field1];
    if( typeof this.field2 != "undefined" ) this.field2= this.form[this.field2];
    // FINALIZE OPTIONS (concat options into one hidden field)
    if( typeof this.finalField != "undefined" ) this.finalField= this.form[this.finalField];
    this.loadField(0);
    // OPTION DEFAULTS
    if( typeof this.default0 != "undefined" ) this.findDefault( 0 );
    if( typeof this.default1 != "undefined" ) this.findDefault( 1 );
    if( typeof this.default2 != "undefined" ) this.findDefault( 2 );
    this.isLoaded= true;
  }


  this.setLabels= function (labelList, delimiter) {
    // setLabels(): make array of labels (names of each option) from a delimited list
    this.labelsArr= labelList.toLowerCase().split(delimiter);
  }


  this.add= function (optionList, delimiter) {
    // add(): make array of option values from a delimted list
    var thisOptionArr= optionList.split(delimiter);
    var option0= thisOptionArr[0]
    if( ! this.optionsArr[option0] ) this.optionsArr[option0]= new Array();
    if( thisOptionArr.length > 1 ) {
      var option1= thisOptionArr[1];
      if( ! this.optionsArr[option0][option1] ) this.optionsArr[option0][option1]= new Array();
    }
    if( thisOptionArr.length > 2 ) {
      var option2= thisOptionArr[2];
      if( ! this.optionsArr[option0][option1][option2] ) this.optionsArr[option0][option1][option2]= new Array();
    }
  }


  this.emptyField= function (n) {
    this["field"+n].options.length= 0;
    this["field"+n].options[0]= new Option( this.labelPrefix + this.labelsArr[n] );
    this["field"+n].disabled= true;
    if( typeof this["field"+(n+1)] != "undefined" ) {
      this.emptyField(n+1);
    }
  }


  this.loadField= function (n) {
    var field= this["field"+n];
    if( typeof field != "undefined" ) {
      this.emptyField(n);
      var array= this.optionsArr;
      if( n >= 1 ) array= array[ this.field0.value ];
      if( n >= 2 ) array= array[ this.field1.value ];
      for( var o in array ) field.options[ field.options.length ]= new Option(o, o);
      field.disabled= false;
    }
    this.finalizeOptions();
  }


  this.finalizeOptions= function () {
    if( typeof this.finalField == "undefined" ) return;
    if( typeof this.finalFieldDelimeter == "undefined" ) this.finalFieldDelimeter= "-";
    var arr= new Array();
    arr[0]= this.field0.options.selectedIndex > 0 ? this.field0.value : "";
    if( typeof this.field1 != "undefined" ) {
      arr.push(this.field1.options.selectedIndex > 0 ? this.field1.value : "");
    }
    if( typeof this.field2 != "undefined" ) {
      arr.push(this.field2.options.selectedIndex > 0 ? this.field2.value : "");
    }
    //for zero length string: this.finalField.value= arr.join("").length ? arr.join("-") : "";
    this.finalField.value= arr.join(this.finalFieldDelimeter)
  }
  

  this.findDefault= function (n) {
    if( typeof this["field"+n] == "undefined" ) return;
    for( var o=0; o < this["field"+n].options.length; o++ ) {
      if( this["field"+n].options[o].value == this["default"+n] ) {
        this["field"+n].options.selectedIndex= o;
        this.loadField(n+1);
        break;
      }
    }
  }

}
/* #### / PRODUCT OPTIONS #### */

function setOptins(optinObj) {
  var thisForm = optinObj.form;
  hideLookupForm();
  if (optinObj.value == 'aetrue') {
    thisForm.optInAeLoyalty.value = 'true';
    thisForm.optInPennLoyalty.value = 'false';
  } else if (optinObj.value == 'penntrue') {
    thisForm.optInAeLoyalty.value = 'false';
    thisForm.optInPennLoyalty.value = 'true';
  } else {
    thisForm.optInAeLoyalty.value = 'false';
    thisForm.optInPennLoyalty.value = 'false';
  }
}

function showLookupForm() {
  document.getElementById('lookupForm').style.display='block';
}

function hideLookupForm() {
  document.getElementById('lookupForm').style.display='none';
}

function submitCustomerForm(action) {
  document.forms[0].action = action;
  document.forms[0].submit();
}

function popResponse() {
  window.open('/newsletteradd.htm?email=' + document.getElementById("emailID").value,'Newsletter','toolbar=no,scrollbars=yes,screenX=50,screenY=200,top=50,left=200,width=400,height=250');
  return false;
}

function cartValidateOrderLimits(orderTotal, msg) {
  if(orderTotal > 200 ) {
    alert(msg);
    return false;
  } else {
    return true;
  }
}

