/**
 * This file contains Javascript utility functions used throughout the blueprint application.
 */

dojo.provide("atg.b2cblueprint.util");


atg.b2cblueprint.util={

  createNode: function(nodeName){
	return document.createElement(nodeName);
  },
  removeNode: function (targetNode){
	targetNode.parentNode.removeChild(targetNode);
  },
  insertBefore: function(newNode,targetNode){
	targetNode.parentNode.insertBefore(newNode,targetNode);
  },
  replaceNode: function(newNode,targetNode) {	
	targetNode.parentNode.replaceChild(newNode,targetNode);
	targetNode = null;
  },
  dropOffParentNode: function (srcNode){
	if(dojo.isString(srcNode)){
		srcNode = dojo.byId(srcNode);
	}
	
	for(var i=0; i<srcNode.childNodes.length; i++){
		var node = srcNode.childNodes[i];
		var _node =node.cloneNode(true);
		this.insertBefore(_node,srcNode);
	}

	this.removeNode(srcNode);

  },
  forceUpdateNodeContent: function(targetNode,content){

	for (i=0; i < targetNode.childNodes.length; i++) {
		this.removeNode(targetNode.childNodes[i]);
	}

	try
	{
		targetNode.innerHTML = dojo.trim(content);
	}
	catch (err)
	{

		var tmpdom = document.createElement("div");
		tmpdom.innerHTML = dojo.trim(content);
		for (i=0; i < tmpdom.childNodes.length; i++)
		{
			try
			{
				targetNode.appendChild(tmpdom.childNodes[i]);
			}
			catch (err)
			{
				console.debug("can't update"+err);
				return false;
			}
			
		}
		tmpdom = null;
	}
	
	return true;
  },

  getCompleteHTML: function (srcNode){
	var _div = this.createNode("div");
	var _displayNode = srcNode.cloneNode(true);
	_div.appendChild(_displayNode);
	var completeHTML = _div.innerHTML;
	_displayNode = null;
	_div = null;
	return completeHTML;
  },

  forceDisplayNewNodeInTargetNode:function (displayNode,targetNode){

	var div = this.createNode("div");

	div.innerHTML = this.getCompleteHTML(displayNode);
    this.replaceNode(div.firstChild,targetNode);
	


  },

  forceDisplayExitNodeInTargetNode:function (exitNode){
	 var tmpNode = this.createNode("div");
	 this.insertBefore(tmpNode,exitNode);
	 this.forceDisplayNewNodeInTargetNode(exitNode,tmpNode);
	 this.removeNode(exitNode);

  },
  addParentNodeToExitNode: function(parentNode,src,flag){
	 this.insertBefore(parentNode,src);
	 if (flag==="node"){

		 this.forceUpdateNodeContent(parentNode,src);
	 } else if(flag==="html"){

		 this.forceUpdateNodeContent(parentNode,this.getCompleteHTML(src));
		 this.removeNode(src);
	 }
	 
  },

  openwindow:function(url,name,iWidth,iHeight) {
	var url;
	var name;
	var iWidth;
	var iHeight;
	var iTop = (window.screen.availHeight-30-iHeight)/2;
	var iLeft = (window.screen.availWidth-10-iWidth)/2;
	var param = 'height='+iHeight+',,innerHeight='+iHeight+',width='+iWidth+',innerWidth='+iWidth+',top='+iTop+',left='+iLeft+',toolbar=no,menubar=no,scrollbars=yes,resizeable=no,location=no,status=no';
	window.open(url,name,param);
 },

  /** 
   * Used on the cart page to autoselect the giftnote when 
   * a user selects the gift wrap option
   */
  autoSelectGiftNote: function() {    
    if (document.cartform.atg_b2cblueprint_addWrap.checked &&
        !document.cartform.atg_b2cblueprint_addNote.checked) {       
      document.cartform.atg_b2cblueprint_addNote.click();
    }  
  },
  
  /**
   * The email campaign popup.  First check the form fields to 
   * make sure we can submit.
   */ 
  emailSignup: function(URL, theForm) {  
    // need to make sure the required fields are not null
    var openWindow = true;
    
    // check to see if we have an email address at all
    var addressEntered = theForm.atg_b2cblueprint_signUpInput.value;
    if (dojo.trim(addressEntered) === "") {
      openWindow = false;
    }
    
    //if email id is not valid don't open the window
    if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(addressEntered))){
      openWindow = false;
    }
  
    // don't open window if we had a form error
    if (openWindow) {
      document.open(URL,"","scrollbars=yes,toolbar=no,directories=no,menubar=no,resizable=yes,status=yes,width=480,height=500");
    }
  },
  
  /**
   * The product details popup 
   */
  detailsPopup: function(URL) {
    document.open(URL,"","scrollbars=yes,toolbar=no,directories=no,menubar=no,resizable=yes,status=yes,width=450,height=525");
  },
  
  /**
   * The notify me when in-stock popup 
   */
  notifyMePopup: function(URL) {
    document.open(URL,"","scrollbars=yes,toolbar=no,directories=no,menubar=no,resizable=yes,status=yes,width=480,height=500");
  },

  noScrollbarPopup: function(URL) {
    document.open(URL,"","scrollbars=no,toolbar=no,directories=no,menubar=no,resizable=yes,status=yes,width=480,height=500");
  },
  /**
   * Return false if the key pressed in the event object is the return key, true otherwise
   */
  killEnter: function(evt) {
    if(evt.keyCode == 13 || evt.which == 13) {
      
      return false;
    }
    return true;
  },

  
  
  checkExactBox: function(){
	    
	  var form = dojo.byId("exactSearchForm");
	  
	  if (!dojo.byId("isExactSearch").checked){	 
		//  dojo.byId("isExactSearch").value="false";
		  form.elements.isExactSearch.value ="false";
		  form.elements.isExactSearch.checked="false";
		  dojo.byId("isExactSearch").checked="";
	  } 
	  dojo.xhrPost({
	   form: "exactSearchForm",
	   handleAs: "text",
	   handle: function(data,args){
	   if(typeof data == "error"){
	      console.warn("error!",args);
	     }else{
	    }
	   }
	 
	  });	
	  
	
  },
  
  enableBox:function(){
	  dojo.byId("isExactSearch").disabled="";
  },
  
  disableBox:function(){
	  dojo.byId("isExactSearch").disabled="true";
  },
  
  /**
   * Update facet trail value
   */
  updateFacetTrail: function(xkeyword, actionPage, categoryFacetId, isSearchableFacetId){
  var trltxt = dojo.string.trim(document.facetSearch.trailtext.value);
   
   var searchCategoriesDropdown = dojo.byId("searchmenu");
   selectedCategoryId = searchCategoriesDropdown.value;

    if(trltxt === "" || trltxt === dojo.string.trim(xkeyword)){
      document.facetSearch.addFacet.value="";
      document.facetSearch.trailtext.value="";
      if(selectedCategoryId == 'allCategories'){
    	  document.facetSearch.categoryId.value="";
          document.facetSearch.trail.value=isSearchableFacetId + ":1";
          document.facetSearch.trailSize.value="0";
      } else {
        document.facetSearch.trail.value=isSearchableFacetId + ":1"+ ":"+categoryFacetId + ":" + selectedCategoryId;
        document.facetSearch.trailSize.value="1";
        document.facetSearch.categoryId.value=selectedCategoryId;
        document.facetSearch.action=actionPage;
      }
    }
    else{
      if(selectedCategoryId == 'allCategories'){
        document.facetSearch.categoryId.value="";
        document.facetSearch.removeAllFacets.value=true;
        document.facetSearch.trail.value="";
        document.facetSearch.trailSize.value="1";
        document.facetSearch.addFacet.value=isSearchableFacetId + ":1" + ":" + "SRCH:"+trltxt;
        
      } else {
        document.facetSearch.trail.value=categoryFacetId + ":" + selectedCategoryId;
        document.facetSearch.trailSize.value="1";
        document.facetSearch.addFacet.value=isSearchableFacetId + ":1" + ":" + "SRCH:"+trltxt;
        document.facetSearch.categoryId.value=selectedCategoryId;
        document.facetSearch.action=actionPage;
      }
    }
    return true;
  }, 
  /**
   * Display block if none 
   */
  toggleDiv: function(divName){
 
    var displayStyle = document.getElementById(divName).style.display;
    displayStyle=(displayStyle!="block")? "block" : "none";
    document.getElementById(divName).style.display=displayStyle; 
	
  },
  /**
   * Display blocks as per number of facetIndex
   */
  compressDiv: function(i){
    var displayStyleBlock = "block";
    var displayStyleNone = "none";
    if (document.getElementById("moreDiv"+i) != null) {
      document.getElementById("moreDiv"+i).style.display=displayStyleBlock;
      document.getElementById("lessDiv"+i).style.display=displayStyleNone;
    }
  },
  /**
   * Caller function for toggleDIv and compressDiv
   */
  toggleBothDiv: function(idx, totalFacet){
    atg.b2cblueprint.util.toggleDiv("moreDiv"+idx);
    atg.b2cblueprint.util.toggleDiv("lessDiv"+idx); 
	
    for (i=0;i<totalFacet;i++) {
      if (i != idx) {
        atg.b2cblueprint.util.compressDiv(i);
      }
      else {
        continue;
      }
    }
  },
  
    toggleBothDivFacet: function(idx, totalFacet){
    atg.b2cblueprint.util.toggleDiv("moreDiv"+idx);
    atg.b2cblueprint.util.toggleDiv("lessDiv"+idx); 
	
    for (i=0;i<totalFacet;i++) {
      if (i != idx) {
        atg.b2cblueprint.util.compressDiv(i);
      }
      else {
        continue;
      }
    }
  },
   /**
   * Caller function for toggleDIv and compressDiv
   */
  toggleBothDiv: function(idx, totalFacet,optionType){
    
	var animation;
	if(optionType==1){
	     atg.b2cblueprint.util.toggleDiv("moreDiv"+idx);
		 animation = dojo.fx.wipeIn({node: "lessDiv" + idx, duration: 500});
		 animation.play();
	}else if(optionType==2){
		 animation = dojo.fx.wipeOut({node: "lessDiv" + idx, duration: 500});
		 animation.play();
		 var moreDivId="moreDiv"+idx;
		 var _this=this;
		 window.setTimeout(function(){
							_this.toggleDiv("moreDiv"+idx); 
						  },500);
         
	}
	
    for (i=0;i<totalFacet;i++) {
      if (i != idx) {
        atg.b2cblueprint.util.compressDiv(i);
      }
      else {
        continue;
      }
    }
  },
 
  /**
   * Display the catalog flyout menus in IE6. All other browsers handle flyouts via CSS.
   */
  catalogNavIE: function(){
    if (!dojo.isIE){
      // Only apply to IE6
      return;
    }
    
    var catNav = dojo.byId('atg_b2cblueprint_catalogNav');
    if (!catNav){
      return;
    }
    
    var navItems = catNav.getElementsByTagName('ul');
  
    for(i=1; i<navItems.length; i++){    
      // Add the "over" class if the current list item doesn't have the class "active"
      dojo.event.connect(navItems[i].parentNode, "onmouseover", function(evt){
          dojo.html.addClass(evt.currentTarget, "over");
      });
    
      // Remove the "over" class   
      dojo.event.connect(navItems[i].parentNode, "onmouseout", function(evt){
        dojo.html.removeClass(evt.currentTarget, "over");
        evt.stopPropagation();
      });
    }
  },

  /**
   * Text Area character counter
   */
  textAreaCounter: function(htmlTextArea , currentCounter, maxCounter) {
    var maxLimit = document.getElementById(maxCounter).firstChild.nodeValue;
    var currentCount = document.getElementById(currentCounter);

    if (htmlTextArea.value.length > maxLimit){
      htmlTextArea.value = htmlTextArea.value.substring(0, maxLimit);
    }else{
      currentCount.innerHTML = htmlTextArea.value.length;
    }
  },
  
  /**
   * Disable nodes that have atg_behavior_disableOnClick CSS class applied to them when they are clicked.
   * 
   * Expects the following attributes passed in on the params object
   *   cssClass: CSS class denoting the behavior
   *   defaultDisabledValue: Text value that will be set on the node when clicked and disabled
   *   freezeWidth: boolean signifying whether the width of the nodes should be retained
   * 
   * Any node that the behavior is attached to will be disabled whenever a click event is intercepted. This
   * should help prevent any double submit errors on the server.
   * 
   * A 'disabledValue' attribute may be set on the node itself. This value will override any default 
   * value that is set on this function.
   */
  applyDisableOnClickBehavior: function(params){	  
    var elements=dojo.query(params.cssClass);
    console.debug("Applying DisableOnClick behavior to "+elements.length+" nodes with class ["+params.cssClass+"]");
    
    for (var i=0; i<elements.length; i++){
      var node=elements[i];
      console.debug(node);
      
      dojo.event.connect(node,"onclick",function(evt){  
        var node=evt.target;
        if (node.justClicked){
          console.debug("Ignoring click");
          // Node has already been clicked and is being handled, so ignore this click
          evt.preventDefault();
          evt.stopPropagation();
          return false;
        }
        
        console.debug("Disabling node before form submission");
        console.debug(node);
        
        // retain original node width - prevents node from resizing when disabled value text is set on it
        if (params.freezeWidth){
          node.style.width=dojo.html.getBorderBox(node).width+"px";
        }
        
        // Get disabled text from node if set, otherwise use default passed to this function. If default not
        // set, just use the existing value on the button.
        var disabledValue=node.getAttribute("disabledValue");
        var originalValue=(node.nodeName=="INPUT" ? node.value : node.innerHTML);
        if (!disabledValue) { 
          disabledValue=(params.defaultDisabledValue ? params.defaultDisabledValue : originalValue);
        }
        
        if (node.nodeName=="INPUT"){
          // Create hidden form element to copy submit button's value into. Need to do this as disabled elements
          // are not submitted from a form by the browser.
          var replacementNode=document.createElement("INPUT");
          replacementNode.type="hidden";
          replacementNode.name=node.name;
          replacementNode.value=node.value;
          
          // Append this to the parent form
          var formNode=dojo.html.getParentByType(node,"FORM");
          formNode.appendChild(replacementNode);
          
          // Disable the node
          node.value=disabledValue;
          node.name="";
          node.disabled=true;
          
          // Disabling the submit button prevents the form from being submitted in IE, so submit it here
          evt.preventDefault();
          formNode.submit();        
        } 
        else if (node.nodeName=="A"){
          node.innerHTML=disabledValue;
          node.justClicked=true; // Prevent further clicks from causing default behavior
        }
        
        // Continue with normal browser processing of click event
        return true;
      });
    }
  },
  noenter: function() {
    return !(window.event && window.event.keyCode == 13); 
  },
  
  noEmailEntered: function(){
    var emailText = dojo.byId("atg_b2cblueprint_registerEmailAddress");
    
    if (emailText.value == 'Example: name@domain.com'){
        emailText.value='';
    }

  },
  /**
   * Performs submitting the form if enter key was pressed. The method takes the submit button named as "Next". 
   * 
   */
   performEnterSubmit:function(evt, theForm){
	if (evt.keyCode == 13 || evt.which == 13){
		var formElements = theForm.elements;
		
		for( i = 0; i < formElements.length-1; i++ ){
			if(formElements[i].name == "Next"){
				formElements[i].click();
				break;
			}
		}
		return false;
	} 
   }
 };
