function imgClicked(imgId, imgElem){ 
 var end=imgId.lastIndexOf("_");
  imgId=imgId.substring(0,end);
 if (end!=-1)
    {
       unselectAll_iterator(imgId);     
    }
 else{ 
       unselectAll(imgId);
 }

 imgElem.name="selected";
 imgElem.className = "selected";
}

function changeColor(id, pColorCode, prodImg,skuId, pColorName, isLeaderProduct, pSizeName){
    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)
    {
        document["frm"+uniqueId].color.value=pColorCode;
        if (document["frm"+uniqueId].colorName != null)
        {
            document["frm"+uniqueId].colorName.value=pColorName;
        }
    }    
    
    if(getElement(uniqueId+"selectedColor")!=null && pColorName!=null)
    {    
    getElement(uniqueId+"selectedColor").innerHTML=pColorName.toUpperCase();
    }
    
    if(getElement(id+"sizeguide")!=null && pColorCode!=null && id!=null)
    {
        getElement(id+"sizeguide").href="javascript:openProductPopupWindow('"+contextPath+"/popups/popup_sizeguide.jsp?productid="+id+"&color="+pColorCode+"');";
    }
    
    if(getElement(id+"careguide")!=null && pColorCode!=null && id!=null)
    {
        getElement(id+"careguide").href="javascript:openProductPopupWindow('"+contextPath+"/popups/popup_careguide.jsp?productid="+id+"&color="+pColorCode+"');";
    }    
    
    if(getElement(id+"requestswatches")!=null && pColorCode!=null && id!=null)
    {
        getElement(id+"requestswatches").href="javascript:openProductPopupWindow('"+contextPath+"/popups/popup_requestswatches.jsp?productid="+id+"&color="+pColorCode+"');";
    }    
    
    if(getElement(id+"image")!=null && pColorCode!=null && id!=null)
    {
        getElement(id+"image").href="javascript:openProductPopupWindow('"+contextPath+"/popups/popup_zoomviewer.jsp?productid="+id+"&color="+pColorCode+"');";
    }    
    
    if(getElement(uniqueId+"zoomviewer")!=null && pColorCode!=null && id!=null)
    {
        getElement(uniqueId+"zoomviewer").href="javascript:openProductPopupWindow('"+contextPath+"/popups/popup_zoomviewer.jsp?productid="+id+"&color="+pColorCode+"');";
    }
    
        
    
    if(isLeaderProduct!=null & isLeaderProduct=="true"){
        if(getElement("scene7viewer")!=null && pColorCode!=null && uniqueId!=null)
        {
            getElement("scene7viewer").href="javascript:openProductPopupWindow('"+contextPath+"/popups/popup_zoomviewer.jsp?productid="+id+"&color="+pColorCode+"');";
        }
        if(getElement("openlargerimage")!=null && pColorCode!=null && uniqueId!=null)
        {
            getElement("openlargerimage").href="javascript:openProductPopupWindow('"+contextPath+"/popups/popup_zoomviewer.jsp?productid="+id+"&color="+pColorCode+"');";
        }
        if(getElement("tellafriend")!=null && pColorCode!=null && uniqueId!=null)
        {
            getElement("tellafriend").href=encodeURI(contextPath+'/user/login_check.jsp?productId='+id+'&cCode='+pColorCode);
        }
        setViewerWithColor(pColorCode)
    } else {
        changeImage("img"+uniqueId,prodImg);    
    }    
}

function changeImage(imageHandle,prodImg){  
    if(prodImg != null && document[imageHandle]!=null){
        document[imageHandle].src = prodImg;
    } 
}

function changeSelectedColorText(prodId, pColorName) {  
  if(getElement(prodId+"selectedColor")!=null && pColorName!=null) {    
    getElement(prodId+"selectedColor").innerHTML=pColorName.toUpperCase();
    }
}
    
function changeImageMixMatch(imgProdSelectorName, imgProdSelectorUrl, imgMixMatchName, imgMixMatchUrl ) {  
  changeImage(imgProdSelectorName, imgProdSelectorUrl); 
  changeImage(imgMixMatchName, imgMixMatchUrl);
}

function imgMouseOver(imgElem, color){
  
 imgElem.className = "mouseOver";
}

function imgMouseOut(imgElem, color){
 if(imgElem.name == "selected"){
  imgElem.className="selected";
 } else {
  imgElem.className="unselected";
 }
}
 function unselectAll_iterator(elementId){
 var i=0;
 while(!isNull(document.getElementById(elementId+"_"+i))) {
    var elem=document.getElementById(elementId+"_"+i)
    if(elem.name == "selected") {
        elem.name = "";
        elem.className="unselected";
   }
   i++;
 }
}

function isNull(elem) {
    if(elem==null) return true;
    return false;
}
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(){
    document.frmAddToCart.action.value = "addToBag";
    document.frmAddToCart.submit();
}    
function addItemToWishlist(){
    document.frmAddToCart.action.value = "addToWishlist";
    document.frmAddToCart.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=no";
    window.open(encodeURI(pURL), pName, features);
}

function openProductPopupWindow(pURL){
    openWindow(pURL, 'productpopup', 477, 600);
}

function openInformationalPopupWindow(pURL){
    openWindow(pURL, 'informationalpopup', 477, 600);
}

/*
openDetailsPopup - will display the quickview functionality if it's available. If not, it'll direct the customer to the product page.

@param- productId product id of product to display
@param- promoId *optional - used for cm tagging purposes, it's the promotion to associate the call with
@param- loc *optional - used for cm tagging to detail where the calll was thrown
*/
function openDetailsPopup(productId, promoId, loc) { 
     baseUrl = "/anthro/catalog/productdetail.jsp?_dyncharset=ISO-8859-1&navAction=jump&id=";
     var elemTag;
	 if (!arc){return false;}
	 if (arc.enabled ) 
     { if(promoId){
		    //format the element tag
		    if (loc){
		   		elemTag = "F-" + productId + "-"+ loc;
			}else{
				elemTag = "F-" + productId;}
		  //throw coremetrics element tag
		  cmCreatePageElementTag("'"+elemTag+"'","'"+promoId+"'");
		  //throw coremetrics link click tag
		  //cmCreateManualLinkClickTag("'"+promoId+"'","'"+elemTag+"'","'"+promoId+"'");
	  	 }
		 arcDetailsLaunch(productId);
	 }else { if(promoId){
		 	if (loc){
		   		elemTag = "S-" + productId + "-"+ loc;
			}else{
				elemTag = "S-" + productId;}
		   //throw coremetrics element tag
		   cmCreatePageElementTag("'"+elemTag+"'","'"+promoId+"'");
		   //throw coremetrics link click tag
		   //cmCreateManualLinkClickTag("'"+promoId+"'","'"+elemTag+"'","'"+promoId+"'");
		   }
	 	document.location.href =  baseUrl + productId;
	  }
}

function loginSubmit(){
    document.loginForm.action.value = "login";
    document.loginForm.submit();
}   

function submitenter(myfield,e)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;

if (keycode == 13)
   {
   myfield.form.submit();
   return false;
   }
else
   return true;
}

function formatPhoneNumber(phone){
	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;
}
