/******************************************************************************************************
  Author          : PA
  Date Created    : July 30, 2008

  Change history

  Task/Bug              Date(mm-dd-yyyy)  Author       Change Description
  
  trac 1274             10-23-2008        mlavinia     update to include cm Element Tags for each form submit & each add to bag
  
  Trac 1372             02-04-2009        SGopidinne   updateItems() function has been modified to disable 'editWishlistPage'
                                                       parameter in update mode.
                                                
  Unable to move item   06-02-2009        SGopidinne   setWishlistId() function added for adding an Item from Bag to wishlist.   
  from shopping bag to
  wishlist.
  
  Trac 1497             09-24-2009        NThota       removeAllGiftListItems() function added for clear items from wish list.
  ***********************************************************************************************************/ 

/* This method is called to select the first giftlist Id(first Event Name in the popup) when
 the popup is open in the Cart and Product detail pages */
  function selectWishlist(){
	var noOfGiftlists=selectWishlist.arguments[0];
	//Passing two Args from Cart page.
	if(selectWishlist.arguments.length == 2){

			var id = selectWishlist.arguments[1];
				//There is only one giftlist in the profile.
				if(noOfGiftlists == 1){
						document['gift'+id].giftlistgroup.checked=true;
						
				}else{//More than one Giftlist in the Profile.
						document['gift'+id].giftlistgroup[0].checked=true;
				}
	}else{//Product datail page.
		//There is only one giftlist in the profile.
		if(noOfGiftlists == 1){
			document['frmAddToCart'].giftlistgroup.checked=true;
		}else{//More than one Giftlist in the Profile.
			document['frmAddToCart'].giftlistgroup[0].checked=true;
		}	
	}

	}
	
  function selectWishlistFromSaveForLater(noOfGiftlists,item){
		if(noOfGiftlists == 1){//There is only one giftlist in the profile.
				document['movetogiftlist'+item].giftlistgroup.checked=true;
		}else{//More than one Giftlist in the Profile.
				document['movetogiftlist'+item].giftlistgroup[0].checked=true;
		}
	}	
  
  function wishlistLoginSubmitform(s){	 
     document.frmWishlistLoginForm1.formAction.value=s;	
     document.frmWishlistLoginForm1.loginsubmit.disabled=true; 
   	 document.frmWishlistLoginForm1.submit();
	 if (s=="forgotPassword") {cmCreatePageElementTag("Login Vwr: Forgot Password Button", "Wish List");}
  } 
  
   function changeSelectedUser(pUserId) {
    var formId = 'frmUserLineItem' + pUserId;
    document.getElementById(formId).submit();
  } 
  

   function addFriendsGiftlistItemToBagExtended(pGiftlistId) {	
	var formId = 'frmFriendsWishlistLineItem' + pGiftlistId;
	document.getElementById(formId).submit();
	cmCreatePageElementTag("WL Item: Add to Bag - Friends Wish List", "Wish List");
}

  function addGiftlistItemToBagExtended(pGiftlistId) {
    var formId = 'frmWishlistLineItem' + pGiftlistId;
    var formObj =  document.getElementById(formId);
	formObj["updateSubmit"].disabled=true;
    formObj.submit();
	cmCreatePageElementTag("WL Item: Add to Bag - Own Wish List", "Wish List");
  }

//find horizontal position of an object using dom
 function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

//find vertical position of an object using dom
  function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }
  
  function getBrowserHeight() {
          var intH = 0;
          var intW = 0;

          if(typeof window.innerWidth  == 'number') {
             intH = window.innerHeight;
             intW = window.innerWidth;
          } 
          else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
               intH = document.documentElement.clientHeight;
               intW = document.documentElement.clientWidth;
          }
          else if(document.body && (document.body.clientWidth || document.body.clientHeight)) {
            intH = document.body.clientHeight;
            intW = document.body.clientWidth;
          }

          return {width: parseInt(intW), height: parseInt(intH)};
      }

function expandCollapse() {
        var element = document.getElementById(expandCollapse.arguments[0]);
		
		//if this is send to friend link from product detail page
		if (expandCollapse.arguments[0] == "sendToFriendPopup")
		{
			var leftPos = (findPosX(document.getElementById('sendToFriendBtn'))-150)+"px";
			if (leftPos != null){element.style.left=leftPos;}
			
			var topPos = (findPosY(document.getElementById('sendToFriendBtn'))-150)+"px";
			if (topPos != null){element.style.top=topPos;}
		}
		else
		{
			//search for the add to wish list button - if the id is defined: used for product detail & outfit pages
			if (document.getElementById('addToWishlistBtn')){
				var leftPos = findPosX(document.getElementById('addToWishlistBtn'))+"px";
				if (leftPos != null){element.style.left=leftPos;}
				
				var topPos = findPosY(document.getElementById('addToWishlistBtn')) - 50;
				topPos = topPos +"px"
				if (topPos != null){element.style.top=topPos;}
			}
			if (document.getElementById('searchBox')){  // used for wishlist page, search button
				var wishlist=1;
			}
		}
		//if not, look to see if the id is defined in the arguments - used in html cart & saved for later.
		if (document.getElementById(expandCollapse.arguments[1])){
			var leftPos = findPosX(document.getElementById(expandCollapse.arguments[1]))+"px";
			if (leftPos != null){element.style.left=leftPos;}
			var topPos = findPosY(document.getElementById(expandCollapse.arguments[1])) - 30;
			topPos = topPos +"px"
			if (topPos != null){element.style.top=topPos;}
		}
		
		if (element.style.display=="none"){
			element.style.display="block";
			if (typeof hideshowSelectTags == 'function'){ //check to see if this fnctn exists; in quickshop.js
				hideshowSelectTags(0,wishlist); //pass boolean for hide or show, and 1 for search button on wishlist
			}
		}
		else{
			element.style.display="none";
			if (typeof hideshowSelectTags == 'function'){ //check to see if this fnctn exists; in quickshop.js
				hideshowSelectTags(1,wishlist); //pass boolean for hide or show, and 1 for search button on wishlist
			}
		}
		//element.style.display = (element.style.display == "none") ? "block" : "none";
    }
 
function updateItems(funcname,giftlistItemId) {
   var formId = 'frmWishlistLineItem' + giftlistItemId;
   var formObj =  document.getElementById(formId);
   formObj["addSubmit"].disabled=true;
   //Code added for Trac 1372 --%> 
   formObj["editWishlistPage"].disabled=true;
   //End of Code added for Trac 1372  
   
   if(funcname=='updatechanges'){
      changeQuantityDesired(giftlistItemId,formObj);
	  formObj["giftlistItemId"].value=giftlistItemId;
	  formObj["oldSkuId"].value=formObj[giftlistItemId+"oldSkuId"].value;
      formObj["newSkuId"].value=formObj[giftlistItemId+"newSkuId"].value;
	  if((formObj["newSkuId"].value == 'null') || (formObj["newSkuId"].value == "" )){
	    formObj["newSkuId"].value= formObj[giftlistItemId+"oldSkuId"].value;
	  }
	  formObj.submit();
   } 
   cmCreatePageElementTag("WL Item: Update Wish List Item", "Wish List");
  }
  
  function changeQuantityDesired(giftlistItemId,formObj ){
   var textboxname = giftlistItemId+"quantity";
   formObj["qtDesired"+giftlistItemId ].value = formObj[textboxname].value;
  }
  
  function submitWishlistForm(formObj){
	   var cm_info;
	   if (formObj =="frmEditWishlist"){cm_info = "WL Header Vwr: Update";}
	   else if (formObj == "frmSaveWishlist") {cm_info = "Create Vwr: Submit Button";}
	   else if (formObj == "frmSearchForUsers") {cm_info = "Find Popup: Submit Button";}
	   else if (formObj == "frmSendWishlist") {cm_info = "Share Popup: Submit Button";}
	   else if (formObj == "frmWishlistLoginForm1") {cm_info = "Login Vwr: Login Button";}
	   else if (formObj == "frmValidateCreateGiftlist") {cm_info = "List Vwr: Create Button";}
	   else if (formObj == "frmDeleteGiftlistsForm") {cm_info = "List Vwr: Delete Button";}
	   document[formObj].submit();
	   cmCreatePageElementTag( cm_info, "Wish List");  
  }
  
  //Sets selected GiftlistId.
  function selectGiftlistId(id){
	document.getElementById('giftlistId').value=id;
}

/* This code is used to restrict the message box(in Email your list page) to 200 characters  */
function onmyinput(o)
{
 if(o.value.length>=o.getAttribute("maxlength"))
 {
  if(o.value.length>o.getAttribute("maxlength"))
   o.value = o.value.substring(0,o.getAttribute("maxlength"));
  return false;
 }
 return true;
}
function mygetclipdata()
{
 if(!document.all)
 {
  netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
  var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
  var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
  trans.addDataFlavor('text/unicode');
  var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
  clip.getData(trans,clip.kGlobalClipboard);
  var str=new Object();
  var strLength=new Object();
  trans.getTransferData("text/unicode",str,strLength); 
  if (str)
  str=str.value.QueryInterface(Components.interfaces.nsISupportsString);
  var pastetext;
  if (str)
  pastetext=str.data.substring(0,strLength.value / 2);
  return pastetext;
 }
 else
 {
  return window.clipboardData.getData("Text");
 }
}
function mysetclipdata(o)
{
 if(!document.all)
 {
  netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
  var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
  var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
  trans.addDataFlavor("text/unicode");
  var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
  str.data=o;
  trans.setTransferData("text/unicode",str,o.length*2);
  var clipid=Components.interfaces.nsIClipboard;
  clip.setData(trans,null,clipid.kGlobalClipboard);
 }
 else
 {
  window.clipboardData.setData("Text",o);
 }
}
function onmypaste(o)
{
 var nMaxLen=o.getAttribute? parseInt(o.getAttribute("maxlength")):"";
 if(document.all)
 {
  if(document.selection.createRange().text.length>0)
  {
   var ovalueandclipboarddata = o.value +window.clipboardData.getData("Text");
   if(o.getAttribute && ovalueandclipboarddata.length-document.selection.createRange().text.length>nMaxLen)
   {
    if(window.clipboardData.getData("Text").substring(0,document.selection.createRange().text.length+nMaxLen-o.value.length)!="")
     window.clipboardData.setData("Text",window.clipboardData.getData("Text").substring(0,document.selection.createRange().text.length+nMaxLen-o.value.length));
    else
     return false;
   }
  }
  else
  {
   var ovalueandclipboarddata = o.value +window.clipboardData.getData("Text");
   if(o.getAttribute && ovalueandclipboarddata.length>nMaxLen)
   {
    if(ovalueandclipboarddata.substring(0,nMaxLen-o.value.length)!="")
     window.clipboardData.setData("Text",ovalueandclipboarddata.substring(0,nMaxLen-o.value.length));
    else
     return false;
   }
  }
  return true;
 }
}
function onmykeypress(o)
{
 if(!document.all)
 {
  var nMaxLen=o.getAttribute? parseInt(o.getAttribute("maxlength")):"";

  if(onmykeypress.caller.arguments[0].ctrlKey == true)
  {
   if(onmykeypress.caller.arguments[0].which==118)
   {

    if(o.selectionStart<o.selectionEnd)
    {
     var ovalueandclipboarddata = o.value + mygetclipdata();
     if(o.getAttribute && (ovalueandclipboarddata.length-o.selectionEnd + o.selectionStart>nMaxLen))
     {
      if(mygetclipdata().substring(0,o.selectionEnd - o.selectionStart+nMaxLen-o.value.length)!="")
       mysetclipdata(mygetclipdata().substring(0,o.selectionEnd - o.selectionStart+nMaxLen-o.value.length));
      else
       return false;
     }
    }
    else
    {
     var ovalueandclipboarddata = o.value +mygetclipdata();
     if(o.getAttribute && ovalueandclipboarddata.length>nMaxLen)
     {
      if(ovalueandclipboarddata.substring(0,nMaxLen-o.value.length)!="")
       mysetclipdata(ovalueandclipboarddata.substring(0,nMaxLen-o.value.length));
      else
       return false;
     }
    }
    return true;

   }
  }


  if(onmykeypress.caller.arguments[0].which==0 || onmykeypress.caller.arguments[0].which==8)
   return true;
  if(o.value.length>=o.getAttribute("maxlength"))
  {
   if(o.selectionStart<o.selectionEnd)
    return true;
   if(o.value.length>o.getAttribute("maxlength"))
    o.value = o.value.substring(0,o.getAttribute("maxlength"));
   return false;
  }
  else
   return true;
 
 }
 else
 {
  if(document.selection.createRange().text.length>0)
   return true;
  if(o.value.length>=o.getAttribute("maxlength"))
   return false;
  else
   return true;
 }
}

function setWishlistMessage(messageId1,messageId2){
	document.getElementById(messageId1).value=document.getElementById(messageId2).value;
}

/* End of Code is used to restrict the message box(in Email your list page) to 200 characters  */

function setWishlistId(giftlistForm,selectedgiftlistId){
  var formObj=document.getElementById(giftlistForm);
  formObj["SelectedGiftlistId"].value=selectedgiftlistId;
  }
// code modified for the Trac 1497 
function removeAllGiftListItems(){
    var  selectCheckBoxMsg="please select the checkbox";
    if(document.frmRemoveWishlistItems.chkbox.checked==true){
      document.frmRemoveWishlistItems.submit();
      }
      else{
      document.getElementById('selectCheckBoxMessage').innerHTML = selectCheckBoxMsg;
      }
}
// end of code modified for the Trac 1497