/* Javascript functions for the new shopping bag and checkout layout */

function showQtyDropdown(prodid, position) {
 
 $$('.atg_b2cblueprint_qtyPicker').each(function(item){
  var curpos = parseInt(item.id.substring('thil_qtyPicker_'.length,item.id.length));
  if (item.id != 'thil_qtyPicker_' + position && curpos > position && curpos < position + 3) {
  	item.hide();
  }
 });

 $('thil_qtyPicker_1').show();
 $('thil_qtyDropdown_' + prodid).show();

}

function changeCountry(param, shippingFlag, shipCountry){
    var form_refresh = document.getElementById("thil_billing_refresh");
    if ( form_refresh ) {
      form_refresh.action = "/tommy/checkout/billing.jsp";
      form_refresh.elements['in'].value = param;
      form_refresh.ns.value = "false";
      form_refresh.cleanAll.value = "false";
      form_refresh.clean.value = "true";
      form_refresh.cardType.value = getRadioElementValue("thil_billing_cardType");
      form_refresh.cardNumber.value = getTextElementValue("thil_billing_cardNumber");
      form_refresh.cardSecurityCode.value = getTextElementValue("cc_VerNum");
      form_refresh.cardExpirationMonth.value = getSelectElementValue("thil_billing_cardExpirationMonth");
      form_refresh.cardExpirationYear.value = getSelectElementValue("thil_billing_cardExpirationYear");
      form_refresh.submit();
    }
}

function changeCountryShipping(param, shippingFlag, shipCountry){

/*  No longer needed with international shipping removed...
	
  if(shippingFlag=='true') {
	
	  var groundExists  = $('#atg_b2cblueprint_shippingOption1').val();
	  var interExists = $('#atg_b2cblueprint_shippingOption4').val();
	  var currentParam = $(".radio_new").attr('id');
	  if (typeof currentParam != 'undefined'){
	    var currentParam = $(".radio_new").attr('id');
	    $(".radio_new").attr('id', param); 
	    if((param == 'US'|| param == 'CA')&& currentParam != 'US' && currentParam != 'CA'){
			  $('#shipAddrBody').remove();
			  $("#shippingAddressTable").load('/tommy/checkout/shippingSingleFormContainer_new.jsp?in='+param+'&clean=true'+' #shipAddrBody');
			  $('#thil_shippingMethods_table').remove();
			  $("#thil_shippingMethods").load('/tommy/checkout/shippingSingleFormContainer_new.jsp?in='+param+'&clean=true'+' #thil_shippingMethods_table');
		  }else if ((currentParam == 'US'|| currentParam == 'CA')&& param != 'US' && param != 'CA'){
			  $('#shipAddrBody').remove();
			  $("#shippingAddressTable").load('/tommy/checkout/shippingSingleFormContainer_new.jsp?in='+param+'&clean=true'+' #shipAddrBody');
			  $('#thil_shippingMethods_table').remove();
			  $("#thil_shippingMethods").load('/tommy/checkout/shippingSingleFormContainer_new.jsp?in='+param+'&clean=true'+' #thil_shippingMethods_table');
		  }
	    
	  } else if ((typeof groundExists != 'undefined') || (typeof interExists != 'undefined')) {
		  if((param == 'US'|| param == 'CA')&& (typeof interExists != 'undefined')){
			$('#shipAddrBody').remove();
			  $("#shippingAddressTable").load('/tommy/checkout/shippingSingleFormContainer_new.jsp?in='+param+'&clean=true'+' #shipAddrBody');
			  $('#thil_shippingMethods_table').remove();
			  $("#thil_shippingMethods").load('/tommy/checkout/shippingSingleFormContainer_new.jsp?in='+param+'&clean=true'+' #thil_shippingMethods_table');
		  }else if ((typeof groundExists != 'undefined')&& param != 'US' && param != 'CA'){
			  $('#shipAddrBody').remove();
			  $("#shippingAddressTable").load('/tommy/checkout/shippingSingleFormContainer_new.jsp?in='+param+'&clean=true'+' #shipAddrBody');
			  $('#thil_shippingMethods_table').remove();
			  $("#thil_shippingMethods").load('/tommy/checkout/shippingSingleFormContainer_new.jsp?in='+param+'&clean=true'+' #thil_shippingMethods_table');
		  }
	  }
	
	  
	  
    //document.location.href='/tommy/checkout/shipping.jsp?in='+param+'&clean=true';
  } else {
    if(param!=shipCountry){
      document.location.href='/tommy/checkout/billing.jsp?in='+param+'&ns=false'+'&clean=true';
    } else {
      document.location.href='/tommy/checkout/billing.jsp?in='+param+'&clean=true';
    }
  }

	...We will now reload the address form every time the country chages (it will always change between US and CA)  */
	
		//  Reload the shipping address form.
	$('#shipAddrBody').remove();
	$("#shippingAddressTable").load('/tommy/checkout/shippingSingleFormContainer_new.jsp?in='+param+'&clean=false'+' #shipAddrBody');
		//  Hide expidited shipping for canada.
	hideRestrictedShippingOptions(param);
}

function hideRestrictedShippingOptions(countryObj) {
    if ( countryObj == 'US' ) {
		$('#atg_b2cblueprint_shippingOption1').attr('disabled', '').parent('td').parent('tr').show();
		$('#atg_b2cblueprint_shippingOption2').attr('disabled', '').parent('td').parent('tr').show();
		$('#atg_b2cblueprint_shippingOption3').attr('disabled', '').parent('td').parent('tr').show();
		$('#atg_b2cblueprint_shippingOption4').attr('disabled', '').attr('checked', '').parent('td').parent('tr').hide();	
	}
	else if ( countryObj == 'CA' ) {
		$('#atg_b2cblueprint_shippingOption1').attr('disabled', '').attr('checked', 'checked').parent('td').parent('tr').show();
		$('#atg_b2cblueprint_shippingOption2').attr('disabled', 'disabled').parent('td').parent('tr').hide();
		$('#atg_b2cblueprint_shippingOption3').attr('disabled', 'disabled').parent('td').parent('tr').hide();
		$('#atg_b2cblueprint_shippingOption4').attr('disabled', '').attr('checked', '').parent('td').parent('tr').hide();
	}
    else  {
		$('#atg_b2cblueprint_shippingOption1').attr('disabled', 'disabled').parent('td').parent('tr').hide();
		$('#atg_b2cblueprint_shippingOption2').attr('disabled', 'disabled').parent('td').parent('tr').hide();
		$('#atg_b2cblueprint_shippingOption3').attr('disabled', 'disabled').parent('td').parent('tr').hide();
		$('#atg_b2cblueprint_shippingOption4').attr('disabled', '').attr('checked', 'checked').parent('td').parent('tr').show();	
	}
}

function changeCountryBill(isChecked, country){
    var form_refresh = document.getElementById("thil_billing_refresh");
    if ( form_refresh ) {
      form_refresh.action = "/tommy/checkout/billing.jsp";
      form_refresh.elements['in'].value = country;
      form_refresh.ns.value = (isChecked) ? "true" : "false";
      form_refresh.cleanAll.value = (isChecked) ? "false" : "true";
      form_refresh.clean.value = "false";
      form_refresh.cardType.value = getRadioElementValue("thil_billing_cardType");
      form_refresh.cardNumber.value = getTextElementValue("thil_billing_cardNumber");
      form_refresh.cardSecurityCode.value = getTextElementValue("cc_VerNum");
      form_refresh.cardExpirationMonth.value = getSelectElementValue("thil_billing_cardExpirationMonth");
      form_refresh.cardExpirationYear.value = getSelectElementValue("thil_billing_cardExpirationYear");
      form_refresh.submit();
    }
}

function getTextElementValue(elementId) {
  var elementObj = document.getElementById(elementId);
  return (elementObj) ? elementObj.value : "";
}

function getRadioElementValue(elementId) {
  var result = "";
  var next = true;
  for (var index = 1; next; index++) {
    var elementObj = document.getElementById(elementId + "_" + index);
    if (elementObj) {
      if(elementObj.checked) {
        result = elementObj.value;
        next = false;
      }
    } else {
      next = false;
    }
  }
  return result;
}

function getSelectElementValue(elementId) {
  var result = "";
  var elementObj = document.getElementById(elementId);
  if (elementObj) {
    result = elementObj[elementObj.selectedIndex].value;
  }
  return result;
}

function hideQtyDropdown(prodid,position) {
 $$('.atg_b2cblueprint_qtyPicker').each(function(item){item.show();});
 $('thil_qtyDropdown_' + prodid).hide();
}

function setQuantity(prodid,qty) {
	
 // Do some stuff to a hidden field maybe?
 
  document.getElementById('input_qty_'+prodid).value = qty;
  // Set the html to reflect the value in the hidden field too
 $('thil_qtyValue_' + prodid).update(qty);
 $('thil_qtyDropdown_' + prodid).hide();
	
}

function setModQuantity(prodid,qty) {
	
 // Do some stuff to a hidden field maybe?
 document.getElementById('input_qty_mod_'+prodid).value = qty;
  // Set the html to reflect the value in the hidden field too
 document.getElementById('thil_qtyValue_' + prodid).firstChild.nodeValue = qty;
 hideEl('thil_qtyDropdown_' + prodid);
}

function toggleQViewBtn(id) {
  getDetailDiv = document.getElementById('qViewBtn_' + id);
  var getDetailDivClass = getDetailDiv.className;

  if (getDetailDivClass.match('qViewNone')) {
    var findOpenDetail = getElementsByClassName('qViewBlock', 'div');
    if (findOpenDetail.length > 0) {
      for (var i = 0, j = findOpenDetail.length; i < j; i++) {
        var getOpenDetailById = document.getElementById(findOpenDetail[i].getAttribute('id'));
        getOpenDetailById.className = getOpenDetailById.className.replace('qViewBlock', 'qViewNone');
      }
    }
    getDetailDiv.className = getDetailDivClass.replace('qViewNone', 'qViewBlock');
  } else {
    getDetailDiv.className = getDetailDivClass.replace('qViewBlock', 'qViewNone');
  }
}

//shipping page on load methods
function showCorrectShippingOptions(){
   var checkedAddrCountry = $("input:radio").filter(":checked").attr('class'); 
   //loading getting correct methods on page
   if (checkedAddrCountry == 'radio_new'){
	   checkedAddrCountry = $("#atg_b2cblueprint_countryNameSelect").val(); 
	   //checkedAddrCountry = $(".radio_new").attr('id');
	    $(".radio_new").attr('id', checkedAddrCountry);
   }else if (checkedAddrCountry == ''){
	   checkedAddrCountry = $("#atg_b2cblueprint_countryNameSelect").val();
	   
//	      $('#shipAddrBody').remove();
//		  $("#shippingAddressTable").load('/tommy/checkout/shippingSingleFormContainer_new.jsp?in='+checkedAddrCountry+'&clean=true'+' #shipAddrBody');
//		  $('#thil_shippingMethods_table').remove();
//		  $("#thil_shippingMethods").load('/tommy/checkout/shippingSingleFormContainer_new.jsp?in='+checkedAddrCountry+'&clean=true'+' #thil_shippingMethods_table');
//		  $("#thil_shippingMethods").show(); 
//		  return;
   }  
   
   $('#thil_shippingMethods_table').remove();
   var url = "/tommy/checkout/shippingSingleFormContainer_new.jsp?in="+checkedAddrCountry + " #thil_shippingMethods_table";   
   //$("#thil_shippingMethods").load(url);	 
   //$("#thil_shippingMethods").show(); 
	$("#thil_shippingMethods").load(url, function() {
		$("#thil_shippingMethods").show();
		hideRestrictedShippingOptions(checkedAddrCountry);
	});
}

function showCorrectShippingOptionsForNew(){
	   var param = $("#atg_b2cblueprint_countryNameSelect").val(); 
	   //loading getting correct methods on page
	   $('#thil_shippingMethods_table').remove();
	   var url = "/tommy/checkout/shippingSingleFormContainer_new.jsp?in="+param+"&clean=true" + " #thil_shippingMethods_table";
	  // $("#thil_shippingMethods").load(url);	 
	  // $("#thil_shippingMethods").show();
		$("#thil_shippingMethods").load(url, function() {
			$("#thil_shippingMethods").show();
			hideRestrictedShippingOptions(param);
		});
}



/*
v1.03 Copyright (c) 2006 Stuart Colville
http://muffinresearch.co.uk/archives/2006/04/29/getelementsbyclassname-deluxe-edition/

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, 
and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial 
portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 
IN THE SOFTWARE.
*/
  
function getElementsByClassName(strClass, strTag, objContElm) {
  strTag = strTag || "*";
  objContElm = objContElm || document;    
  var objColl = objContElm.getElementsByTagName(strTag);
  if (!objColl.length &&  strTag == "*" &&  objContElm.all) objColl = objContElm.all;
  var arr = new Array();                              
  var delim = strClass.indexOf('|') != -1  ? '|' : ' ';   
  var arrClass = strClass.split(delim);    
  for (var i = 0, j = objColl.length; i < j; i++) {                         
    var arrObjClass = objColl[i].className.split(' ');   
    if (delim == ' ' && arrClass.length > arrObjClass.length) continue;
    var c = 0;
    comparisonLoop:
    for (var k = 0, l = arrObjClass.length; k < l; k++) {
      for (var m = 0, n = arrClass.length; m < n; m++) {
        if (arrClass[m] == arrObjClass[k]) c++;
        if ((delim == '|' && c == 1) || (delim == ' ' && c == arrClass.length)) {
          arr.push(objColl[i]);   
          break comparisonLoop;
        }
      }
    }
  }
  return arr; 
}

function showEl(ele) {
  obj = document.getElementById(ele).style;
  obj.visibility='visible';
}

function hideEl(ele) {
  obj = document.getElementById(ele).style;
  obj.visibility='hidden';
}

function showQuickViewSizePicker(ele) {
  ZoomReset();
  showEl(ele);
}

function hideQuickViewSizePicker(ele) {
  hideEl(ele);
}

function showElement(ele) {
  obj = document.getElementById(ele).style;
  obj.display='block';
}

function hideElement(ele) {
  obj = document.getElementById(ele).style;
  obj.display='none';
}
function toggleDisplay(obj) {  
  if (getDisplay(obj) == 'block') {
    hideElement(obj);
  } else {
    showElement(obj);
  }
}


function toggleGiftMsgForm(ele) {

 if(ele && ele.checked) {
  showElement('giftBoxPrice');
  showElement('thil_giftMessageForm');
 }
 else {
  hideElement('giftBoxPrice');
  hideElement('thil_giftMessageForm');
 }
 
function showQtyDropdownPicker(){
	$('thil_qtyPicker_1').show();
	 $('thil_qtyDropdown_' + prodid).show();
	
}

function hideQtyDropdownPicker(){
	$('thil_qtyDropdown_' + prodid).hide();
	
}

}

