
//Fix blank in Nescafe Authoring site.
if (_spBodyOnLoadFunctionNames) {
   _spBodyOnLoadFunctionNames.push("setBackgroundForNescafeSite");
}


function doSearchTextFieldClick(buttonName, e)
{
   var key;
   
   if (window.event)
      key = window.event.keyCode;     // IE
   else
      key = e.which;     // Firefox
    
   if (key == 13)
   {
      // Get the button the user wants to have clicked
      var btn = document.getElementById(buttonName);
      if (btn != null)
      { //If we find the button click it
         btn.click();
         event.keyCode = 0
      }
   }
      
   return false;
}


// Java script on Recipes page
function UpdateFacet(groupFacetName, divName,changeGroupFacet){
	
	// groupFacet change class block <-> opened
	// divFacet colapse <-> expand
	// if changeGroupFacet == false, skip update groupFacet's class.
	
  var groupFacet=document.getElementById(groupFacetName);
  
  if(!groupFacet)return true;

  var divFacet=document.getElementById(divName);
  if(!divFacet)return true;
  if(divFacet.style.display=="none"){
    divFacet.style.display="block"
    if(changeGroupFacet==true){
    	groupFacet.className="opened"
    }
  } else {
    divFacet.style.display="none"
    if(changeGroupFacet==true){
    	groupFacet.className="closed"
    }
  }
  return true;
}

function UpdateFacetAndChangeImage(groupFacetName, divName, iconID, iconMinusUrl, iconPlusUrl, 
	changeGroupFacet){
	
	// Change Image. 
  var imgElement =document.getElementById(iconID);
  
  if(!imgElement)return true;
  
  var divFacet=document.getElementById(divName);
  if(!divFacet)return true;
  
  if(divFacet.style.display=="none"){
  	imgElement.src = iconMinusUrl;
  } else {
  	imgElement.src = iconPlusUrl;
  }
 
  // Colapse / Expand all facet items.
  UpdateFacet(groupFacetName, divName, changeGroupFacet);
  
  return true;
}

// Java script on Recipes details page
function expandFacetOnRecipesDetail(groupFacetName, divName, iconID, iconMinusUrl, iconPlusUrl){
	UpdateFacetAndChangeImage(groupFacetName, divName, iconID, iconMinusUrl, iconPlusUrl,true);
}

// Java script on Recipes finder page
// Add  js to image : use custom style sheet, so skip update groupFacet's class.
function expandFacetOnRecipesFinder(groupFacetName, divName, iconID, iconMinusUrl, iconPlusUrl){
	UpdateFacetAndChangeImage(groupFacetName, divName, iconID, iconMinusUrl, iconPlusUrl,false);
}

function ChangeDataOfDropDownList(lstBusinessCatID, lstBusinessLineID, businessLineHidID, NullValue, isMandatory){
   var lstBusinessCat = document.getElementById(lstBusinessCatID);
   var lstBusinessLine = document.getElementById(lstBusinessLineID);
   var hidbusinessLine = document.getElementById(businessLineHidID);
   var currentBusinessCatItem = lstBusinessCat.options[lstBusinessCat.selectedIndex].value;
   // Remove items in line of business
   if(lstBusinessLine != null){
      for(var i = lstBusinessLine.length-1; i >= 0; i--){
         lstBusinessLine.remove(i);
      }
	   if(lstBusinessCat != null){
	      if(typeof(currentBusinessCatItem) != "undefined" && currentBusinessCatItem != "NULLKEY"){
	      	  // Line of business is not mandatory, add null value first
	      	 if(isMandatory == "false"){
	            var oOption = document.createElement("OPTION");
	            oOption.value = "NULLKEY";
	            oOption.text = NullValue;
	            lstBusinessLine.options.add(oOption);
	      	 }
	      	 // Add all new value of line of business
	         for(var key in BP_arrBusinessCat[currentBusinessCatItem]){
	            var oOption = document.createElement("OPTION");
	            oOption.value = key;
	            oOption.text = BP_arrBusinessCat[currentBusinessCatItem][key];
	            lstBusinessLine.options.add(oOption);
	         }
	         var lstSelectedValue = lstBusinessLine.options[0];
	         // Set value to hidden field
	         if(hidbusinessLine != null){
	         	if(typeof(lstSelectedValue) != "undefined"){
	         	   hidbusinessLine.value = lstBusinessLine.options[0].value;
	         	}
	         }
	      }else{
	            var oOption = document.createElement("OPTION");
	            oOption.value = "NULLKEY";
	            oOption.text = NullValue;
	            lstBusinessLine.options.add(oOption);
	         if(hidbusinessLine != null){
				hidbusinessLine.value = "NULLKEY";
	         }
	      }
	   }
   }
}

function SetBusinessLineValue(businessLineHidID, lstBusinessLineID){
   var hidbusinessLine = document.getElementById(businessLineHidID);
   var lstBusinessLine = document.getElementById(lstBusinessLineID);
   if(typeof(hidbusinessLine) != "undefined"){
      if(typeof(lstBusinessLine) != "undefined"){
         var currentValue = lstBusinessLine.options[lstBusinessLine.selectedIndex].value;
         if(typeof(currentValue) != "undefined"){
            hidbusinessLine.value = currentValue;
         }
      }
   }
}

//
// Fire the redirection of the carousel
// @param radRotator: the instance of the carousel
//
function fireCarouselRedirection(radRotator) {
   var leftRight = 1;
   if(radRotator.ScrollDirection.toLowerCase()=="right"){
      leftRight = -1;
   }
   radRotator.FrameContainer.style.left=(radRotator.NumberOfFrames-radRotator.FramesToShow)*radRotator.FrameWidth*leftRight+"px";
   radRotator.FireTickers();
}

function isInEditMode() {
   var element = document.getElementById( "MSOLayout_InDesignMode" );
   if (element) {
      if (element.value == "1") {
         return true;
      }
   }
   return false;
}

//
//return true if current browser is IE
//
function isIE() {
   var agt=navigator.userAgent.toLowerCase();	
	var isIE=(agt.indexOf("msie")!=-1);
   return isIE;
}

//
//Fix Article Column Borders in Nescafe Homepage and Landing page.
//
function ncfFixArticleColumnBorders(){
   var leftColumn = document.getElementById("_leftColumn");
   var rightColumn = document.getElementById("_rightColumn");
   rightColumn.className = rightColumn.className + " " + "ncfArticles-borderLeft";
   rightColumn.style.borderLeftWidth= "1px";
   
   // Fix Hide the horizontal separator if there are no article on the left and right column
   // When there are no article on a column, its offsetHeight is 17
   if(rightColumn.offsetHeight < 20 && leftColumn.offsetHeight < 20){
		leftColumn.style.display = 'none';
		rightColumn.style.display = 'none';
	} else if (rightColumn.offsetHeight > leftColumn.offsetHeight){
      leftColumn.style.height = rightColumn.offsetHeight + "px";
      rightColumn.style.height = rightColumn.offsetHeight + "px";
   } else {
      rightColumn.style.height = leftColumn.offsetHeight + "px";
      leftColumn.style.height = leftColumn.offsetHeight + "px";
   }
}

//
//Fix Intro box height in Nescafe Content Landing page.
//
function ncfFixLandingIntroBoxHeight() {

   var IE6_MIN_OFFSET_TOP = 8;
   var BLACKBOX_MARGIN_LEFT = 5;
      
   var divIntroBox = document.getElementById('_divIntroBox');
   var divTextBoxTitle;
   var divTextBoxContent;
   var divBlackbox = document.getElementById('_divBlackbox');
   var arrDiv = divIntroBox.getElementsByTagName("div");
   var textBoxOffsetTop = -1;     
   var contentHeight = 0;
   
   for (var i = 0; i < arrDiv.length; i++){
      if (arrDiv[i].className.indexOf("contentLeftTitle") != -1) {
         divTextBoxTitle = arrDiv[i];
         contentHeight += divTextBoxTitle.offsetHeight;
         
         if (textBoxOffsetTop < 0) {
            textBoxOffsetTop = divTextBoxTitle.offsetTop;            
         }         
      }
      if (arrDiv[i].className.indexOf("ncfLanding-introText") != -1) {
         divTextBoxContent = arrDiv[i];
         contentHeight += divTextBoxContent.offsetHeight;
         
         if (textBoxOffsetTop < 0) {
            textBoxOffsetTop = divTextBoxContent.offsetTop;
         }
      }
      if (divTextBoxTitle && divTextBoxContent) {
         break;
      }
   }

   if (textBoxOffsetTop < 0) {
      textBoxOffsetTop = IE6_MIN_OFFSET_TOP;
   }
   
   if (browseris.ie && !isInEditMode()) {
      
      divIntroBox.style.overflow = "hidden";

      //IE6_MIN_OFFSET_TOP is a workaround for Evo 15 (r. 6001)
      if ((textBoxOffsetTop <= IE6_MIN_OFFSET_TOP) && (contentHeight < divBlackbox.offsetHeight)) {//The case when text's height is smaller than blackbox's height
         
         if (divBlackbox.offsetHeight > 50) {
            divIntroBox.style.height = (divBlackbox.offsetHeight - 3) + "px";
         }
      } else {
      
         //The case when blackbox's width is 100%
         if (divBlackbox.offsetWidth +  BLACKBOX_MARGIN_LEFT >= divIntroBox.offsetWidth) {
            divBlackbox.style.marginLeft = "0";
            divBlackbox.style.marginBottom = "10px";
         }
      }
   } else if (!isInEditMode()) {
   
      var divIntroBox = document.getElementById('_divIntroBox');
      var divBlackbox = document.getElementById('_divBlackbox');

      //The case when blackbox's width is 100%
      if ((contentHeight > 0) && (divBlackbox.offsetWidth +  BLACKBOX_MARGIN_LEFT >= divIntroBox.offsetWidth)) {
         divBlackbox.style.marginBottom = "10px";
      }
   }
}

function isInAuthoring() {
   var ele = document.getElementById("GlobalTitleAreaImage");
   return ele ? true : false;
}

function setBackgroundForNescafeSite() {
   
   if (isInAuthoring()) {
      var mainDiv = document.getElementById("mainDiv");
      if (mainDiv) {         
         mainDiv.className = "mainDiv";
         document.body.style.background = "none";
      }     
   }
}