
/**
 *  Product detail page tab object
 */
function ProductDetailTab(productId) {
  
  /** All the tabs in the page*/
  this.tabs = new Array("description", "details", "teaches", "awards", "buzz");
  this.productId = productId;
  
  this.getTabTitle = function (tabId) {
    return dojo.byId(tabId + "Title_"+this.productId);
  };
  
  this.inactiveTile = function (tabId) {
    this.getTabTitle(tabId).className = "";
  };
  
  this.inactiveAllTiles = function () {
    for (var i = 0; i < this.tabs.length; i++) {
      this.inactiveTile(this.tabs[i]);
    }
  };
  
  this.activeTile = function (tabId) {
    this.getTabTitle(tabId).className = "active";
  };
  
  this.activeTab = function (tabId) {
    dojo.byId(tabId+"_"+this.productId).className = "tab_content active";
  };
  
  this.inactiveTab = function (tabId) {
    dojo.byId(tabId+"_"+this.productId).className = "tab_content";
  };
  
  this.inactiveAllTabs = function () {
    for (var i = 0; i < this.tabs.length; i++) {
      this.inactiveTab(this.tabs[i]);
    }
  };
  
  this.switchToTab = function (tabId) {
    this.inactiveAllTiles();
    this.inactiveAllTabs();
    this.activeTile(tabId);
    this.activeTab(tabId);
  };
}

/**
 *  Switch tabs in Product detail page 
 */
function switchProductDetailTab(nowTabId,productId) {
  var productDetailTab = new ProductDetailTab(productId);
  productDetailTab.switchToTab(nowTabId);
  return false;
}
/**
 *  control searchInput and signInput  in pageContainer
 */
  function inputGetFocus(inputId,inputText){
    
    var input=document.getElementsByName(inputId);
    for(var i=0;i<input.length;i++){
      if(input[i].value==inputText){
        input[i].value="";
        }
    }
  
  }
  function inputLoseFocus(inputId,inputText){
    var input=document.getElementsByName(inputId);
    for(var i=0;i<input.length;i++){
      if(trim(input[i].value)==""){
        input[i].value=inputText;
      }
    }
  }
  
  function trim(str)
  {
    return str.replace(/^\s*/,"").replace(/\s*$/,"");;  
  }  
  
/**
 *  Switches password input field to text input field to display 
 *  help note (defaultMessage). If user has already filled password input field then 
 *  do not switch.
 */   
function   changeToTextTypeIfEmpty(password, textId, defaultMessage)   
{ 
  if (password.value != '') {
    return;
  }  
  var text=document.getElementById(textId);
  text.value=defaultMessage;    
  text.style.disabled=false;       
  text.style.display="";   
  password.style.display="none";  
  password.style.disabled=true;
}   
  
/**
 *  Switches text input field to password input field 
 *  (should be called when text field is focused).  
 */     
function   changeToPasswordType(text, passwordId)   
{ 
  var password=document.getElementById(passwordId);    
  password.style.disabled=false;     
  password.style.display="";   
  text.style.display="none";   
  text.style.disabled=true;      
  password.focus();
}        

/**
 *  Reloads product image 
 */
function reloadProductImage(imageTrigger, contextPath, smallImageURL, largeImageURL, productId) {
  var contextPath;
  
  contextPath = contextPath + "/browse/gadgets/productAltImage.jsp";
     
  var url = contextPath;
  var contentParams = {"smallImageURL":smallImageURL,"largeImageURL":largeImageURL,"productId":productId}

  dojo.xhrGet( {
		url: url,
		content:contentParams,
		productId: productId,
		timeout: 5000, // Time in milliseconds

		// The LOAD function will be called on a successful response.
		load: function(response, ioArgs) {
		  dojo.byId("product_image_set_"+ioArgs.args.productId).innerHTML = response;
		  var srcNode = dojo.byId('product_image_set_'+ioArgs.args.productId);
      dojo.parser.parse(srcNode, true);		  
		  return response;
		},

		// The ERROR function will be called in an error case.
		error: function(response, ioArgs) {
		  console.error("HTTP status code: ", ioArgs.xhr.status);
		  return response;
		  }
		});
	var altContainer = dojo.byId('product_alternativeImages_'+productId);
	
	for (i = 0; i < altContainer.childNodes.length ;i++) {
	  var child = altContainer.childNodes[i];
	  if (child.tagName == 'IMG') {
		child.style.border = ''
	  }  
	}
   imageTrigger.className = "active";
}

function reloadProductVideo(imageTrigger, contextPath, smallImageURL, largeImageURL, productId) {
  var contextPath;
  
  contextPath = contextPath + "/browse/gadgets/productAltVideo.jsp";
   
  var smallImageURLParam = "?smallImageURL=" + smallImageURL;
  
  var largeImageURLParam = "&largeImageURL=" + largeImageURL;
  
  var url = contextPath + smallImageURLParam + largeImageURLParam;
  
  dojo.xhrGet( {
		url: url,
		productId: productId,
		timeout: 5000, // Time in milliseconds

		// The LOAD function will be called on a successful response.
		load: function(response, ioArgs) {
		  dojo.byId("product_image_set_"+ioArgs.args.productId).innerHTML = response;
		  return response;
		},

		// The ERROR function will be called in an error case.
		error: function(response, ioArgs) {
		  console.error("HTTP status code: ", ioArgs.xhr.status);
		  return response;
		  }
		});
	var altContainer = dojo.byId('product_alternativeImages_'+productId);
	
	for (i = 0; i < altContainer.childNodes.length ;i++) {
	  var child = altContainer.childNodes[i];
	  if (child.tagName == 'IMG') {
		child.style.border = ''
	  }  
	}
	
	if (imageTrigger.children[0].nodeName != "SPAN")
      imageTrigger.className = "active";
}

function clearBillingAddress()
{
  document.getElementById('atg_b2cblueprint_paymentInfoAddNewCardAddressNickname').value = '';
  document.getElementById('atg_store_streetAddressInput').value = '';
  document.getElementById('atg_store_streetAddressOptionalInput').value = '';
  document.getElementById('atg_store_localityInput').value = '';
  document.getElementById('atg_store_countryNameSelect').selectedIndex  = 0;
  document.getElementById('atg_store_stateSelect').selectedIndex  = 0;
  document.getElementById('atg_store_postalCodeInput').value = '';
  document.getElementById('atg_store_telephoneInput').value = '';
}