/***************************************************************************************************************************************************
 	Author                  :           PA
	Version		     	    :	        1.0
	Date Created	        :
   
 	Change history 
 	
	Task/Bug 					Date(mm-dd-yyyy) 	    Author 		       Change Description
	
	Phone numbers in Account
    menu page are not 
    displaying properly  		04-01-2009			    HAlagesan		   formatPhoneNumber() function is added to display phone number with (xxx)xxx-xxxx format.
    
    Trac 1535                   04-17-2009              BTellajeera        function displayOrderNumber(pSubject) added.
    
    Trac 1598                   05-05-2009              HAlagesan          function formatPhoneNumber(phone)is modified and function ReplaceAll(Source,stringToFind,stringToReplace) is added.

****************************************************************************************************************************************************/

function imgClicked(imgId, imgElem){ 
 unselectAll(imgId);
 imgElem.name="selected";
 imgElem.className = "selected";
}

 function changeColorandLink(id, pColorCode, prodImg,skuId, pColorName, isLeaderProduct,itemdescription,itemCount,sortProperties,navAction,parentid,navCount ){
 
  var uniqueId = id;
 
 
  if(isLeaderProduct!=null & isLeaderProduct=="true"){
  uniqueId = "l"+uniqueId;
 }
 if(skuId!=null && skuId!="")
 {
  getElement(uniqueId+"catalogRefIds").value = skuId;
     getElement(uniqueId+"qty").name = skuId;
 }
 
 if(document["frm"+uniqueId]!=null && isLeaderProduct=="true")
 {  
 document["frm"+uniqueId].color.value=pColorCode;
 document["frm"+uniqueId].colorName.value=pColorName;
 }
 
 if(getElement(uniqueId+"altviews")!=null && pColorCode!=null && id!=null){
 
getElement(uniqueId+"altviews").href="javascript:openProductPopupWindow('"+contextPath+"/popups/popup_views.jsp?productid="+id+"&color="+pColorCode+"');";
 }
if(getElement(uniqueId+"selectedColor")!=null && pColorName!=null)
 {
 getElement(uniqueId+"selectedColor").innerHTML=pColorName.toUpperCase();
 }
 
 if(getElement(uniqueId+"popup_avail")!=null && pColorCode!=null && id!=null)
 {
  getElement(uniqueId+"popup_avail").href="javascript:openProductPopupWindow('"+contextPath+"/popups/popup_avail.jsp?productid="+id+"&color="+pColorCode+"');";
 }
 
 if(getElement(uniqueId+"popup_sizechart")!=null && pColorCode!=null && id!=null)
 {
  getElement(uniqueId+"popup_sizechart").href="javascript:openProductPopupWindow('"+contextPath+"/popups/popup_sizechart.jsp?productid="+id+"&color="+pColorCode+"');";
 } 
 
 if(getElement(uniqueId+"requestswatches")!=null && pColorCode!=null && id!=null)
 {
  getElement(uniqueId+"requestswatches").href="javascript:openProductPopupWindow('"+contextPath+"/popups/popup_requestswatches.jsp?productid="+id+"&color="+pColorCode+"');";
 } 
 
 if(getElement(uniqueId+"viewmorephotos")!=null && pColorCode!=null && id!=null)
 {
  getElement(uniqueId+"viewmorephotos").href="javascript:openProductPopupWindow('"+contextPath+"/popups/popup_views.jsp?productid="+id+"&color="+pColorCode+"');";
 }
 
  
 
 if(isLeaderProduct!=null & isLeaderProduct=="true"){
  if(getElement("tellafriend")!=null && pColorCode!=null && uniqueId!=null)
  {
   getElement("tellafriend").href=encodeURI(contextPath+'/user/login_check.jsp?productId='+id+'&cCode='+pColorCode);
  }
 } 
 changeImageandLink(uniqueId,prodImg,pColorCode,itemdescription,itemCount,sortProperties,navAction,parentid,navCount,id ); 
 
}
 
function changeImageandLink(uniqueId,prodImg,pColorCode,itemdescription,itemCount,sortProperties,navAction,parentid,navCount,id){
 var imageHandle = "img"+uniqueId;
 var linkHandle = "link"+uniqueId;
 var deslinkHandle = "deslink"+uniqueId;
 var dislinkHandle = "dislink"+uniqueId;
    var viewAllinkHandle = "viewAllink"+uniqueId; 
 if(prodImg != null && document[imageHandle]!=null){
  document[imageHandle].src = prodImg;
  
 } 
  
 var jsessionid = getElement("jsessionid").value;
  if(getElement(linkHandle) != null){
   
  getElement(linkHandle).href=encodeURI(contextPath+'/catalog/productdetail.jsp;jsessionid='+jsessionid+'?itemdescription='+itemdescription+'&itemCount='+itemCount+'&id='+id+'&parentid='+parentid+'&sortProperties='+sortProperties+'&navCount='+navCount+'&navAction='+navAction+'&color='+pColorCode);
  }
 
 
 if(getElement(deslinkHandle) != null){
  
 getElement(deslinkHandle).href=encodeURI(contextPath+'/catalog/productdetail.jsp;jsessionid='+jsessionid+'?itemdescription='+itemdescription+'&itemCount='+itemCount+'&id='+id+'&parentid='+parentid+'&sortProperties='+sortProperties+'&navCount='+navCount+'&navAction='+navAction+'&color='+pColorCode);
 }
 
 if(getElement(dislinkHandle) != null){
  
 getElement(dislinkHandle).href=encodeURI(contextPath+'/catalog/productdetail.jsp;jsessionid='+jsessionid+'?itemdescription='+itemdescription+'&itemCount='+itemCount+'&id='+id+'&parentid='+parentid+'&sortProperties='+sortProperties+'&navCount='+navCount+'&navAction='+navAction+'&color='+pColorCode);
 }
 
 if(getElement(viewAllinkHandle) != null){
  
 getElement(viewAllinkHandle).href=encodeURI(contextPath+'/catalog/productdetail.jsp;jsessionid='+jsessionid+'?itemdescription='+itemdescription+'&itemCount='+itemCount+'&id='+id+'&parentid='+parentid+'&sortProperties='+sortProperties+'&navCount='+navCount+'&navAction='+navAction+'&color='+pColorCode);
 }
}



function changeColor(id, pColorCode, prodImg,skuId, pColorName, isLeaderProduct, pSizeName,itemdescription,itemCount,sortProperties,navAction,parentid,navCount){
	var uniqueId = id;
	if(pSizeName!=null){
		uniqueId = pSizeName;
	}
	
	if(isLeaderProduct!=null & isLeaderProduct=="true"){
		uniqueId = "l"+uniqueId;
	}
	if(skuId!=null && skuId!="")
	{
		getElement(uniqueId+"catalogRefIds").value = skuId;
	    getElement(uniqueId+"qty").name = skuId;
	}
	
	if(document["frm"+uniqueId]!=null && isLeaderProduct=="true")
	{		
	document["frm"+uniqueId].color.value=pColorCode;
	}

	if(getElement(uniqueId+"altviews")!=null && pColorCode!=null && id!=null){

getElement(uniqueId+"altviews").href="javascript:openProductPopupWindow('"+contextPath+"/popups/popup_views.jsp?productid="+id+"&color="+pColorCode+"');";
	}
if(getElement(uniqueId+"selectedColor")!=null && pColorName!=null)
	{
	getElement(uniqueId+"selectedColor").innerHTML=pColorName.toUpperCase();
	}
	
	if(getElement(uniqueId+"popup_avail")!=null && pColorCode!=null && id!=null)
	{
		getElement(uniqueId+"popup_avail").href="javascript:openProductPopupWindow('"+contextPath+"/popups/popup_avail.jsp?productid="+id+"&color="+pColorCode+"');";
	}
	
	if(getElement(uniqueId+"popup_sizechart")!=null && pColorCode!=null && id!=null)
	{
		getElement(uniqueId+"popup_sizechart").href="javascript:openProductPopupWindow('"+contextPath+"/popups/popup_sizechart.jsp?productid="+id+"&color="+pColorCode+"');";
	}	
	
	if(getElement(uniqueId+"requestswatches")!=null && pColorCode!=null && id!=null)
	{
		getElement(uniqueId+"requestswatches").href="javascript:openProductPopupWindow('"+contextPath+"/popups/popup_requestswatches.jsp?productid="+id+"&color="+pColorCode+"');";
	}	
	
	if(getElement(uniqueId+"viewmorephotos")!=null && pColorCode!=null && id!=null)
	{
		getElement(uniqueId+"viewmorephotos").href="javascript:openProductPopupWindow('"+contextPath+"/popups/popup_views.jsp?productid="+id+"&color="+pColorCode+"');";
	}
	
		
	
	if(isLeaderProduct!=null & isLeaderProduct=="true"){
	
		var formObj = document.sendMessage;
    	var productIdsElement = formObj["/uo/commerce/SendEmailFriendFormHandler.colorCode"];
	    	if(productIdsElement!=null && pColorCode!=null && uniqueId!=null){
			 	productIdsElement.value = pColorCode;
			}
       		if(getElement("emailColorCode")!=null && pColorCode!=null && uniqueId!=null){
           		getElement("emailColorCode").value = pColorCode;
	   		}

	} 
	 changeImage(uniqueId,prodImg,pColorCode,itemdescription,itemCount,sortProperties,navAction,parentid,navCount,id); 
    }
 
    function changeImage(uniqueId,prodImg,pColorCode,itemdescription,itemCount,sortProperties,navAction,parentid,navCount,id){
     var imageHandle = "img"+uniqueId;
     var linkHandle = "link"+uniqueId;
 
     if(prodImg != null && document[imageHandle]!=null){
      document[imageHandle].src = prodImg;
  
     } 
   	 if(getElement(linkHandle) != null){
     	getElement(linkHandle).href=encodeURI(contextPath+'/catalog/productdetail.jsp?itemdescription='+itemdescription+'&itemCount='+itemCount+'&id='+id+'&parentid='+parentid+'&sortProperties='+sortProperties+'&navCount='+navCount+'&navAction='+navAction+'&color='+pColorCode);
	}
}

function imgMouseOver(imgElem, color){
	imgElem.className = "mouseOver";
}

function imgMouseOut(imgElem, color){
 if(imgElem.name == "selected"){
  imgElem.className="selected";
 } else {
  imgElem.className="unselected";
 }
}
 
function unselectAll(elementId){
 if(document.all){
  var length = document.all(elementId).length;
  for(var i=0;i<length;i++){
   var elem = document.all(elementId, i);
   if(elem.name == "selected"){
    elem.name = "";
    elem.className="unselected";
   }
  }
 } else {
  var length = document.getElementById(elementId).length;
  for(var i=0;i<length;i++){
   var elem = document.getElementById(elementId, i);
   if(elem.name = "selected"){
	   elem.name = "";
        elem.className="unselected";
   }
  }
 }
}

var scriptfunctionsarray = new Array();

function addScriptFunction(pProductId, pSizeName, pProductColor, pProdImg,pSkuId, pColorName){
	scriptfunctionsarray.push(new scriptfunction(pProductId, pSizeName, pProductColor, pProdImg,pSkuId, pColorName));
}

function callscriptfunctions(){
	for(var i=0;i<scriptfunctionsarray.length;i++){		
		changeColor(scriptfunctionsarray[i].productId,scriptfunctionsarray[i].productColor,scriptfunctionsarray[i].prodImg,scriptfunctionsarray[i].skuId, scriptfunctionsarray[i].colorName, '', scriptfunctionsarray[i].sizeName);
	}
}

function scriptfunction(productId, pSizeName, pProductColor, pProdImg, pSkuId, pColorName){
	this.productId = productId;
	this.sizeName = pSizeName;
	this.productColor = pProductColor;
	this.prodImg = pProdImg;
	this.skuId = pSkuId;
	this.colorName = pColorName;	
}

function addItemToBag(frmName){
		document[frmName].action.value = "addToBag";		
		document[frmName].submit();
		}	
	function addItemToWishlist(frmName){
		document[frmName].itemsadded.value='';
		document[frmName].action.value = "addToWishlist";
		document[frmName].submit();
	}	
function getElement(elementId){
	return document.all ? document.all[elementId] : document.getElementById(elementId);
}	

function View(pViewName, pViewURL){
	this.viewName = pViewName;
	this.viewURL = pViewURL;
}

function Color(pColorCode, pColorName, pProductImageURL){
	this.colorCode = pColorCode;
	this.colorName = pColorName;
	this.productImageURL = pProductImageURL;
	this.viewList = new Array();
	
	this.addView = function(pViewName, pViewURL){
		this.viewList.push(new View(pViewName, pViewURL));
	}
}

function openWindow(pURL, pName, pWidth, pHeight){
    var features = ["width=", pWidth, ",height=", pHeight, ",status=no,toolbar=no,menubar=no,location=no,scrollbars=yes,titlebar=no,resizable=yes"];
    window.open(encodeURI(pURL), pName, features.join(''));
}

function openProductPopupWindow(pURL){
	openWindow(pURL, 'productpopup', 452, 599);
}

function openInformationalPopupWindow(pURL)
{
    openWindow(encodeURI(pURL), "dayphoneinfopopup", 635, 200);
}
//Functions for AddresBook 
function getElement(elementId){
		return document.all ? document.all[elementId] : document.getElementById(elementId);
	}
	function deleteAddress(){
		var formObj = document.savedAddressFrm;
		formObj["/atg/userprofiling/ProfileFormHandler.removeAddress"].value = getSelectedAddress(formObj);
		 
		formObj["/atg/userprofiling/ProfileFormHandler.editAddress"].name='';
		formObj["/atg/userprofiling/ProfileFormHandler.editAddress"].value='';
		formObj.newAddress.value="true";
		formObj.submit();
	}
	function editAddress(){		
		var formObj = document.savedAddressFrm;		
		formObj.newAddress.value="";
		 formObj["/atg/userprofiling/ProfileFormHandler.editAddress"].value = getSelectedAddress(formObj); 
		
		formObj["/atg/userprofiling/ProfileFormHandler.removeAddress"].name='';
		formObj["/atg/userprofiling/ProfileFormHandler.removeAddress"].value='';		
		
		formObj.submit();
	}
	
	function getSelectedAddress(formObj){
		var radioObj= formObj.edit;
		
		if(!radioObj)
		   	return "";
		  var radioLength = radioObj.length;
		  if(radioLength == undefined)
		   if(radioObj.checked)
		    return radioObj.value;
		   else
		    return null;
		  for(var i = 0; i < radioLength; i++) {
		   if(radioObj[i].checked) {			   
		    return radioObj[i].value;
		   }
		  }
		  return null;		
		}
	 
	function disableDelete(){	
		getElement("deletebutton").style.display = 'none';
		getElement("key1").value="billing";		
	}
	function enableDelete(){		
		getElement("deletebutton").style.display = 'inline';
		getElement("key1").value="true";		
	}
//Functions end here for AddressBook
var blanks = []
function blank(input) {
    var id = input.form.name + '.' + input.name
    for (var i = 0, l = blanks.length; i < l; i++) {
        if (blanks[i] === id)
            return;
    }
    blanks.push(id)
    input.value = "";
}

//Functions added for Phone numbers in Account menu page are not displaying properly .
function formatPhoneNumber(phone){
	// Code modified for Trac 1598.
	var phoneNumberLength = phone.length;
	if(!phoneNumberLength < 11){
		phone = ReplaceAll (phone, " " ,"");
		phone = ReplaceAll (phone, "-" ,"");
		phoneNumberLength = phone.length;
		if(phoneNumberLength == 11){
			var isOne = phone.substring(0,1);
			if(isOne == 1){
				phone= phone.replace("1","");
			}
		}
	}
	// End of code modified for Trac 1598.
	var sAreaCode = phone.substring(0,3);
	var s3dig = phone.substring(3,6);
	var s4dig = phone.substring(6,10);
	var newPhone = "("+sAreaCode+")"+s3dig+"-"+s4dig;
	return newPhone;
}
//End of Functions added for Phone numbers in Account menu page are not displaying properly .

//Function added for Trac 1535.
function displayOrderNumber(pSubject){

      if(getElement("subject").value == ""){
        document.getElementById( "orderNumber" ).style.display = "none";
        document.getElementById( "orderLabel" ).style.display = "none";
        document.getElementById( "orderNumber" ).value = "";
      }
      else{
        if(pSubject.options[pSubject.selectedIndex].text == "Orders" || pSubject.options[pSubject.selectedIndex].text == "Customer Service"){ 
          document.getElementById( "orderNumber" ).style.display = "";
          document.getElementById( "orderNumber" ).style.width = "250px";
          document.getElementById( "orderLabel" ).style.display = "";
        }
        else{       
          document.getElementById( "orderNumber" ).style.display = "none";
          document.getElementById( "orderLabel" ).style.display = "none";
          document.getElementById( "orderNumber" ).value = "";
        }
      }
}
//End of Functions added for 1535.
//Function added for Trac 1598.
function ReplaceAll(Source,stringToFind,stringToReplace){
	  var temp = Source;
	  var index = temp.indexOf(stringToFind);
	  while(index != -1){
	  	temp = temp.replace(stringToFind,stringToReplace);
	    index = temp.indexOf(stringToFind);
	  }
	  return temp;
}
//End of Function added for 1598.
