
var prdSelArr = new Array();
var g_color = "";

function prdSel(pProductId, isMainProduct){
	this.productId = pProductId;	
	this.prodSelector = new ProdSelector();
	this.prodList = new Array();
	this.sizeList = new Array();
	this.colorList = new Array();
	this.isMainProduct = isMainProduct;
	
 
	this.addProduct = function(pPrd){
		this.prodList.push(pPrd);
		this.addColor(pPrd.colorCode, pPrd.colorName);
	}
 
	this.addSize = function(pSize){
		this.sizeList.push(pSize);
	}
	 
	this.addColor = function(pColorCode, pColorName){
		if(!this.containsColorCode(pColorCode)){
			this.colorList.push(new Color(pColorCode, pColorName));
		}
	}
 
	this.containsColorCode = function(pColorCode){
		for (var i = 0; i < this.colorList.length; i++) {
			if(this.colorList[i].colorCode == pColorCode){
				return true;
			}
		}
		return false;
	}
 
	this.getColorName = function(pColorCode){
		if(pColorCode != null){
			for (var i = 0; i < this.colorList.length; i++) {
				if(this.colorList[i].colorCode == pColorCode){
					return this.colorList[i].colorName;
				}
			}
		}	
		return null;
	}
	
	this.initialize = function(){
		this.prodSelector.initialize(this.prodList, this.sizeList, "");	
	}	
	
	this.setSelectedColorCode = function(pSelectedColor) { 
		if(isEmpty(pSelectedColor)){
		this.prodSelector.mSelectedColorCode =this.prodSelector.mAvailableColorCodes[0];
		this.prodSelector.initializeAvailableSizes(this.prodSelector.mSelectedColorCode, this.prodSelector.mSelectedSizeType);
		this.prodSelector.initializeDefaultSelectedSize();		
		}
		else{
		this.prodSelector.mSelectedColorCode = pSelectedColor;
		this.prodSelector.initializeAvailableSizes(this.prodSelector.mSelectedColorCode, this.prodSelector.mSelectedSizeType);
		this.prodSelector.initializeDefaultSelectedSize();

		}
	}
 
	this.setSelectedSizeType = function(pSelectedSizeType) {
		this.prodSelector.mSelectedSizeType = pSelectedSizeType;
		this.prodSelector.initializeAvailableColors(this.prodSelector.mSelectedSizeType);
		this.prodSelector.initializeDefaultSelectedColor();
		this.prodSelector.initializeAvailableSizes(this.prodSelector.mSelectedColorCode, this.prodSelector.mSelectedSizeType);
		this.prodSelector.initializeDefaultSelectedSize();
		
	}
 
	this.setSelectedSizeName = function(pSelectedSizeName) {
		this.prodSelector.mSelectedSizeName = pSelectedSizeName;
		
	}
 
	this.repopulateSizeTypes = function(){		
			for(var i=0;i<this.prodSelector.mAvailableSizeTypes.length;i++){
				
				if(this.prodSelector.mAvailableSizeTypes[i] != ""){
					if(this.prodSelector.mAvailableSizeTypes[i] == this.prodSelector.mSelectedSizeType){	
						if(getElement("sizetype"+this.productId+this.prodSelector.mAvailableSizeTypes[i])!=null){			
							getElement("sizetype"+this.productId+this.prodSelector.mAvailableSizeTypes[i]).className="active";
						}	

					} else {
						if(getElement("sizetype"+this.productId+this.prodSelector.mAvailableSizeTypes[i])!=null){
							getElement("sizetype"+this.productId+this.prodSelector.mAvailableSizeTypes[i]).className="";
						}	
					}
				}	
			}
	}
	
 
	this.repopulateSizes = function(isMainProduct){	
		var modifiedProductId = this.productId;
		if(isMainProduct!=null & isMainProduct=="true"){
			modifiedProductId = "l"+this.productId;
		}
	
		for(var i=0;i<this.prodSelector.mAllSizes.length;i++){			
			var currentPrd = this.prodSelector.getProd(this.prodSelector.mAllSizes[i]);			
			if(getElement("size"+modifiedProductId+this.prodSelector.mAllSizes[i]) != null){
				if(this.prodSelector.isAvailableSize(this.prodSelector.mAllSizes[i]) && currentPrd != null){	
					if(this.prodSelector.mAllSizes[i] == this.prodSelector.mSelectedSizeName){						
						getElement("size"+modifiedProductId+this.prodSelector.mAllSizes[i]).innerHTML = "<a class=\"sizeButton selectedSizeButton\" href=\"javascript:putSelectedSize(\'" + (i+1) + "\',\'" +this.productId+"\',\'"+replaceAll(this.prodSelector.mAllSizes[i])+"\',\'"+isMainProduct+"\')\">"+this.prodSelector.mAllSizes[i]+"</a>";					
					} 					
					else if(currentPrd.availabilityStatus == this.prodSelector.AVAILABILITY_STATUS_DISCONTINUED || currentPrd.availabilityStatus == this.prodSelector.STATUS_OUT_OF_STOCK){				
						getElement("size"+modifiedProductId+this.prodSelector.mAllSizes[i]).innerHTML = "<span class=\"sizeButton notavailableSizeButton\">"+this.prodSelector.mAllSizes[i]+"</span>";									
					}
					 else {
					     getElement("size" + modifiedProductId + this.prodSelector.mAllSizes[i]).innerHTML = "<a class=\"sizeButton availableSizeButton\" href=\"javascript:putSelectedSize(\'" + (i+1) + "\',\'" + this.productId + "\',\'" + replaceAll(this.prodSelector.mAllSizes[i]) + "\',\'" + isMainProduct + "\')\">" + this.prodSelector.mAllSizes[i] + "</a>";
					}				
				} 
				else{
					getElement("size"+modifiedProductId+this.prodSelector.mAllSizes[i]).innerHTML="";
				}
			}
		}
	} 

function replaceAll(oldStr) {
  var findStr="'";
  var repStr="\\'";

  var srchNdx = 0;  
  var newStr = "";  
  while (oldStr.indexOf(findStr,srchNdx) != -1)  
  {
    newStr += oldStr.substring(srchNdx,oldStr.indexOf(findStr,srchNdx));
    newStr += repStr;
    srchNdx = (oldStr.indexOf(findStr,srchNdx) + findStr.length);
  }
  newStr += oldStr.substring(srchNdx,oldStr.length);
  return newStr;
}

	this.repopulateAll = function(isMainProduct){
		var modifiedProductId = this.productId;
		if(isMainProduct!=null & isMainProduct=="true"){
			modifiedProductId = "l"+this.productId;
		}
		
		this.repopulateSizeTypes();
		
		this.repopulateSizes(isMainProduct);
		var colorName = this.getColorName(this.prodSelector.mSelectedColorCode);
		if(colorName != null){
			if(getElement(modifiedProductId+"selectedColor") != null){				
				getElement(modifiedProductId+"selectedColor").innerHTML=colorName.toUpperCase();
			}
			if (getElement("ctl00_ContentPlaceHolder1_" + modifiedProductId + "selectedColor") != null) {
			    getElement("ctl00_ContentPlaceHolder1_" + modifiedProductId + "selectedColor").innerHTML = colorName.toUpperCase();
			}
			
			if(getElement(this.productId+"selColor") != null){
				getElement(this.productId+"selColor").innerHTML=colorName.toUpperCase();
			}	
		}	
		else {
			if(getElement(modifiedProductId+"selectedColor") != null){	
				getElement(modifiedProductId+"selectedColor").innerHTML="";
            }
            if (getElement("ctl00_ContentPlaceHolder1_" + modifiedProductId + "selectedColor") != null) {
                getElement("ctl00_ContentPlaceHolder1_" + modifiedProductId + "selectedColor").innerHTML = colorName.toUpperCase();
            }
			
			if(getElement(this.productId+"selColor") != null){
				getElement(this.productId+"selColor").innerHTML="";
			}
		}
		
		if(this.prodSelector.mSelectedSizeName != null){
			if(getElement(this.productId+"selectedSize") != null){
				getElement(this.productId+"selectedSize").innerHTML= ReplaceIfNeeded(this.prodSelector.mSelectedSizeName.toUpperCase());
			}

			if (getElement("ctl00_ContentPlaceHolder1_" + this.productId + "selectedSize") != null) {
			    getElement("ctl00_ContentPlaceHolder1_" + this.productId + "selectedSize").innerHTML = ReplaceIfNeeded(this.prodSelector.mSelectedSizeName.toUpperCase());
			}
			
			if(getElement(this.productId+"selSize")){
				getElement(this.productId+"selSize").innerHTML="SIZE"+" "+this.prodSelector.mSelectedSizeName.toUpperCase();
			}
		} else {
			if(getElement(this.productId+"selectedSize") != null){
				getElement(this.productId+"selectedSize").innerHTML="NO SIZE SELECTED";
			}

			if (getElement("ctl00_ContentPlaceHolder1_" + this.productId + "selectedSize") != null) {
			    getElement("ctl00_ContentPlaceHolder1_" + this.productId + "selectedSize").innerHTML = "NO SIZE SELECTED";
			}
			
			if(getElement(this.productId+"selSize")){
				getElement(this.productId+"selSize").innerHTML="NO SIZE SELECTED";
			}
		}
		

		if(getElement(this.productId+"selType") != null){ 
			if(this.prodSelector.mSelectedSizeType != null){
				if(this.prodSelector.mSelectedSizeName != null){
					if(this.prodSelector.mAvailableSizeTypes.length>1){
				getElement(this.productId+"selType").innerHTML=","+" "+this.prodSelector.mSelectedSizeType.toUpperCase();	
					
					}
					else if(this.prodSelector.mAvailableSizeTypes.length==1){
						if(this.prodSelector.mSelectedSizeType=="regular"){
						getElement(this.productId+"selType").innerHTML="";
						}
                        else{
						getElement(this.productId+"selType").innerHTML=","+" "+this.prodSelector.mSelectedSizeType.toUpperCase();
						}
					}
					else{
					getElement(this.productId+"selType").innerHTML="";
					}
				}
				else
				{
        getElement(this.productId+"selType").innerHTML= "";
				}
			} else {
			
				getElement(this.productId+"selType").innerHTML= "";
			}
		} 

		if(this.prodSelector.getSelectedProd() != null 
			&& (getElement(this.productId+"selectBox")==null 
				|| getElement(this.productId+"selectBox").checked)){

			var selectedSku = this.prodSelector.getSelectedProd();
			 
		}
	}
}	

function getPrdSel(pProductId, createNew, pIsLeaderProduct){ 
	for(var i=0;i<prdSelArr.length;i++){		
		if(prdSelArr[i].productId == pProductId && prdSelArr[i].isMainProduct == pIsLeaderProduct){		
			return prdSelArr[i];
		}
	}
	
	if(createNew){	
		prdSelArr.push(new prdSel(pProductId, pIsLeaderProduct));	
		return prdSelArr[prdSelArr.length - 1];
	} else {
		 
		return null;
	}
}

function addProduct(pProductId, prd, pIsLeaderProduct){
	var prdSel = getPrdSel(pProductId, true, pIsLeaderProduct);
	prdSel.addProduct(prd);
}

function addSize(pProductId, size, pIsLeaderProduct){
	var prdSel = getPrdSel(pProductId, true, pIsLeaderProduct);
	prdSel.addSize(size);
}

function initializeProduct(productId, colorCode, pIsLeaderProduct) {    
	var prdSel = getPrdSel(productId, true, pIsLeaderProduct);
	prdSel.prodSelector.mSelectedColorCode = colorCode;
	prdSel.initialize();
} 

function loadSelSize(imgElem, pProductId, pColorCode, prodImg, pPrdId, pColorName, isMainProduct){
if (getElement("ProductSizes")!= null && getElement("ProductSizes").length == 2) {
    getElement("ProductSizes").selectedIndex = 1; 
  } 
}

function concatenate(){
    return [].join.call(arguments, "");
}

function putSelColor(imgElem, pProductId, pColorCode, prodImg, pPrdId, pColorName, isMainProduct){ 
	var prdSel = getPrdSel(pProductId, false, isMainProduct);
	g_color = prodImg; 
	var prImage = document.images['ProductImage'];
	 
	if (prImage != null) {
	    document.images['ProductImage'].src = "/store/productimages/regular/" + eval('aColor' + pProductId)[prodImg];
	} else {
	document.images['ctl00_ContentPlaceHolder1_ProductImage'].src = "/store/productimages/regular/" + eval('aColor' + pProductId)[prodImg];
	}

	reload_sizes_aj(pProductId);
	var pc = document.getElementById("ProductColors");
	if (pc != null) {
	    document.getElementById("ProductColors").selectedIndex = prodImg;
	} else {
	    document.getElementById("ctl00_ContentPlaceHolder1_ProductColors").selectedIndex = prodImg;
	} 
	change_upc_aj(pProductId);
	change_extragroup_aj(pProductId, prodImg);
	change_unitcost_aj(pProductId);
	change_previouscost_aj();
	change_costspans();

	try { ClearElement("message", ""); }
	catch(err){}
	
	if(prdSel != null){
		prdSel.setSelectedColorCode(pColorCode);
		initializeAllProducts(isMainProduct);			
	}
	changeColor(pProductId, pColorCode, prodImg, pPrdId, pColorName, isMainProduct);	
	imgClicked(imgElem.id, imgElem);
}

function change_extrabutton_aj(hyperlinkname, imgname, elementindex, pProductId, prodImg) {
    //    alert(g_color + " " + hyperlinkname + " " + imgname + " " + elementindex);
    if (eval('g_useextragroup_colors' + pProductId) == 0)
        return;

    var e_img = getElementByID_Master(imgname);
    if (e_img == null)
        return;
   
    var e_hl = getElementByID_Master(hyperlinkname);
    if (e_hl == null)
        return;
        
    try {
        if (eval('aExtraGroupColors' + pProductId)[prodImg] == null || eval('aExtraGroupColors' + pProductId)[prodImg][elementindex] == null) {
            e_hl.style.visibility = "hidden";
            e_img.src = '';
            return;
        }

        e_hl.style.visibility = "visible";
        e_img.src = eval('aExtraGroupColors' + pProductId)[prodImg][elementindex];
    }
    catch (err) {
    } 
}

function change_extragroup_aj(pProductId, prodImg) {
    change_extrabutton_aj("ExtraMediaButton1", "ExtraMediaButton1_img", 1, pProductId, prodImg);
    change_extrabutton_aj("ExtraMediaButton2", "ExtraMediaButton2_img", 2, pProductId, prodImg);
    change_extrabutton_aj("ExtraMediaButton3", "ExtraMediaButton3_img", 3, pProductId, prodImg);
    change_extrabutton_aj("ExtraMediaButton4", "ExtraMediaButton4_img", 4, pProductId, prodImg);
}

function change_size_aj(i, pProductId) {
    try { eval('g_size' + pProductId) = i; }
    catch (err) { }
    
    ClearElement("message", "");
    if (eval('g_size_priority' + pProductId) == 1)
        reload_colors_aj(pProductId);
    change_upc();
    change_unitcost_aj(pProductId);
    change_previouscost();
    change_costspans();

    try { ClearElement("message", ""); }
    catch (err) { }
}

function change_extra2_aj(i, pProductId) {
    
    if (color_count > 1) {
        getElementByID_Master("ProductColors").selectedIndex = 0;
        set_color(0);
    }
    try {
        get_product_image().src = eval('aExtra' + pProductId)[i];
    }
    catch (err) {}
}

function change_extra(i, pProductId) {
    document.images['theimage'].src = eval('aExtra' + pProductId)[i];
}

function change_extra3_aj(i, pProductId) {
    
    if (eval('g_useextragroup_colors' + pProductId) == 0)
        return;
    
    if (eval('aExtraGroupColors' + pProductId)[g_color] == null || eval('aExtraGroupColors' + pProductId)[g_color][i + 1] == null)
        return;
     
    var img = eval('aExtraGroupColors' + pProductId)[g_color][i + 1];
    img = img.replace("_t.jpg", ".jpg");
    try {
        get_product_image().src = img;
    }
    catch (err) {
    }    
}

function reload_colors_aj() {
    if (color_count == 0)
        return;

    var ps = getElementByID_Master("ProductColors");
    if (ps == null)
        return;

    var curr_value = ps.options[ps.selectedIndex].value;
    var curr_index = 0;

    ps.length = 0;
    ps.options[0] = new Option("Select Color", "-1");
    curr_index++;

    for (var i = 1; i <= color_count; i++) {
        if (eval('g_size' + pProductId) == 0 || eval('aSCSA' + pProductId)[i][eval('g_size' + pProductId)] == true || eval('g_showwhenoos' + pProductId) == 1) {
            ps.options[curr_index] = new Option(eval('color_names' + pProductId)[i], eval('color_ids' + pProductId)[i]);
            if (curr_value == eval('color_ids' + pProductId)[i])
                ps.selectedIndex = curr_index;

            curr_index++;
        }
    }
    if (ps.length == 2) {
        ps.selectedIndex = 1;
    }
}

function change_color_aj(i, pProductId, prodImg) {
    set_color_aj(i, pProductId, prodImg);
    
    try { ClearElement("message", ""); }
    catch (err) { }
}

function set_def_color_aj(i, pProductId, prodImg) {
    g_color = i;

    if (getElementByID_Master("ProductColors") != null)
        getElementByID_Master("ProductColors").selectedIndex = i;

    if (getElementByID_Master("ProductImage") != null)
        get_product_image().src = "/store/productimages/regular/" + eval('aColor' + pProductId)[i];
  
    change_extragroup_aj(pProductId, prodImg); 
}

function set_color_aj(i, pProductId, prodImg) {
    g_color = i;
    
    try {
        get_product_image().src = "/store/productimages/regular/" + g_color[i];
    }
    catch (err) {
    }
    
    ClearElement("message", "");
    
    if (eval('g_size_priority' + pProductId) == 0)
        reload_sizes_aj(pProductId);
        
    change_extragroup_aj(pProductId, prodImg);
    change_upc();
    change_unitcost_aj(pProductId);s
    change_previouscost();
    change_costspans();    
}

function change_previouscost_aj(pProductId) {
    var pc = getElementByID_Master("PreviousCostSpan");
    if (pc == null)
        return;

    if (eval('aSKUPreviousCost' + pProductId)[eval('g_color' + pProductId)] == null) {
        reset_previouscost(pc);
        return;
    }

    var val = eval('aSKUPreviousCost' + pProductId)[eval('g_color' + pProductId)][eval('g_size' + pProductId)];
    if (val == null || val == '') {
        reset_previouscost(pc);
        return;
    }

    ShowElem("PreviousCost");
    ShowElem("PreviousCostCustom");
    ShowElem("PreviousCostCustom2");

    pc.innerHTML = val;
}

function reset_unitcost_aj(uc, flag, pProductId) { 
    if (eval('g_unitcost' + pProductId) == '$0.00' || eval('g_mincost' + pProductId) != eval('g_maxcost' + pProductId)) {
        if (eval('g_mincost' + pProductId) == eval('g_maxcost' + pProductId) || flag == 1) {
            if (eval('g_previouscost' + pProductId) == '$0.00')
                uc.innerHTML = eval('g_maxcost' + pProductId);
            else
                uc.innerHTML = eval('g_unitcost' + pProductId);
        }
        else
            uc.innerHTML = eval('g_mincost' + pProductId) + ' - ' + eval('g_maxcost' + pProductId);
    }
    else {
        uc.innerHTML = eval('g_unitcost' + pProductId);
    }
}

function change_unitcost_aj(pProductId) {
   
    var uc = getElementByID_Master("UnitCostSpan");
    var ucv2 = getElementByID_Master("RegularPrice2LB");
    HideElem("RegularPrice2LB");
   
    if (uc == null)
        return;

    if (eval('aSKUUnitCost' + pProductId)[eval('g_color' + pProductId)] == null) {
        reset_unitcost_aj(uc, 0, pProductId);
        return;
    }

    try {
        var val = eval('aSKUUnitCost' + pProductId)[g_color][eval('g_size' + pProductId)];
    }
        catch (err) {
    }
    
    if (val == null || val == '') {
        reset_unitcost_aj(uc, 1, pProductId);
        return;
    }

    if (ucv2 != null) {
        if (val == eval('g_unitcost' + pProductId))
            HideElem("RegularPrice2LB");
        else
            ShowElem("RegularPrice2LB");
    }

    uc.innerHTML = val;
}

function change_upc_aj(pProductId) {
    var upc = getElementByID_Master("upc_text");
    if (upc == null)
        return;

    if (eval('aUPC' + pProductId)[eval('g_color' + pProductId)] == null) {
        upc.innerHTML = '';
        return;
    }

    var val = eval('aUPC' + pProductId)[eval('g_color' + pProductId)][eval('g_size' + pProductId)];
    if (val == null || val == '') {
        upc.innerHTML = '';
        return;
    }

    upc.innerHTML = 'UPC: ' + val;
}

function reload_sizes_aj(pProductId) {
    try {
        if (eval('size_count' + pProductId) == 0)
            return;
    }
    catch (err) {
    }
    
    var ps = getElementByID_Master("ProductSizes");
    if (ps == null)
        return;

    var curr_value = ps.options[ps.selectedIndex].value;
    var curr_index = 0;

    ps.length = 0;
    ps.options[0] = new Option("Select " + eval('g_sizealias'+pProductId), "-1");
    curr_index++;

    for (var i = 1; i <= eval('size_count' + pProductId); i++) {
        if (eval('g_color' + pProductId) == 0 || eval('aSCSA' + pProductId)[eval('g_color' + pProductId)][i] == true || eval('g_showwhenoos' + pProductId) == 1) {
            ps.options[curr_index] = new Option(eval('size_names' + pProductId)[i], eval('size_ids' + pProductId)[i]);
            if (curr_value == eval('size_ids' + pProductId)[i])
                ps.selectedIndex = curr_index;

            curr_index++;
        }
    }
    if (ps.length == 2) {
        ps.selectedIndex = 1;
    }
}

function setSelectedType(pProductId, pSizeType, isMainProduct){
	var prdSel = getPrdSel(pProductId, false, isMainProduct);
	if(prdSel != null){
		prdSel.setSelectedSizeType(pSizeType);
		initializeAllProducts(isMainProduct);
	}	
}

function ClearElement(id, val) {
    if (getElementByID_Master(id))
        getElementByID_Master(id).innerHTML = val;
    try {
        getElementByID_Master(id).className = "";

        if (getElementByID_Master(id).style.background != "")
            getElementByID_Master(id).style.background = "";
    }
    catch (err) { }
}

function putSelectedSize(pInd, pProductId, pSizeName, isMainProduct) {
    try {
        ClearElement("message", "");
    } catch (err) { }   
       
    var ps = document.getElementById("ProductSizes");
    
    if (ps != null) {
        ps = document.getElementById("ProductSizes");
    } else {
        ps= document.getElementById("ctl00_ContentPlaceHolder1_ProductSizes");
    }

    for (var i = 0; i <= ps.length - 1; i = i + 1)
    {
        var ddlText = ps.options[i].text;   
        if(ddlText==pSizeName)
        {
            ps.selectedIndex = i;
            change_size_aj(pInd, pProductId)
        break;
    } 
} 
 
var prdSel = getPrdSel(pProductId, false, isMainProduct);

if(prdSel != null){
prdSel.setSelectedSizeName(pSizeName);
initializeAllProducts(isMainProduct);	
}

}

function setSelectedSizeForShopBySize(pProductId, pSizeName,pSizeName1,isMainProduct){	
	var prdSel = getPrdSel(pProductId, false, isMainProduct);
	if(prdSel != null){
	if(prdSel.prodSelector.isAvailableSize(pSizeName)){
	prdSel.setSelectedSizeName(pSizeName);
			initializeAllProducts(isMainProduct);	
	}
	else if(prdSel.prodSelector.isAvailableSize(pSizeName1)){
	prdSel.setSelectedSizeName(pSizeName1);
			initializeAllProducts(isMainProduct);	
	}
	else{	}
	}
}

function initializeAllProducts(isMainProduct){ 
	var prdSelected = false; 
	var sizeSelect = false; 
	var boxSelected = false; 
	var checkBox = true;

	for(var i=0;i<prdSelArr.length;i++){ 
		prdSelArr[i].repopulateAll(isMainProduct);

		if(prdSelArr[i].prodSelector.getSelectedProd() != null){			
			if(getElement(prdSelArr[i].productId+"selectBox")!=null){
				if(getElement(prdSelArr[i].productId+"selectBox").checked){
					prdSelected = true;
				}
			} else {
              sizeSelect = true;
              prdSelected = true;
			}	
		} else{
		 	if(getElement(prdSelArr[i].productId+"selectBox")!=null){
		    	if(getElement(prdSelArr[i].productId+"selectBox").checked){
                     boxSelected = true;
                     checkBox = false;
				}
			} else{ 
				sizeSelect = true;
			}
		}
	} 
}

function ProdSelector() {

	this.STATUS_IN_STOCK = 1;
	this.STATUS_OUT_OF_STOCK = 0;
	
	this.contextPath = "";
	this.prds = null; 
	this.mSelectedColorCode = null; 
	this.mSelectedSizeType = null; 
	this.mSelectedSizeName = null; 
	this.mAvailableColorCodes = null; 
	this.mAvailableSizeTypes = null; 
	this.mAvailableSizes = null;	
	this.mAllSizes = null;

	this.initialize = function(prodList, sizeList, path){
		this.prds = prodList;
		this.mAllSizes = sizeList;
		this.contextPath = path;
		
		this.initializeAvailableSizeTypes();
		this.initializeDefaultSelectedSizeType();
		this.initializeAvailableColors(this.mSelectedSizeType);
		this.initializeDefaultSelectedColor();
		this.initializeAvailableSizes(this.mSelectedColorCode,
				this.mSelectedSizeType);
		this.initializeDefaultSelectedSize();
	}	
	
	 
	this.initializeAvailableSizeTypes =  function(){ 
		this.mAvailableSizeTypes = new Array();
		var index = 0;
		for (var i = 0; i < this.prds.length; i++) {
			var currentPrd = this.prds[i];
			if (this.mAvailableSizeTypes,this.prds[i].type!="" && !contains(this.mAvailableSizeTypes,this.prds[i].type)) {
				this.mAvailableSizeTypes[index] = this.prds[i].type;
				index++;
			}
		}
           this.mAvailableSizeTypes.sort();
          this.sortSizeTypes(this.mAvailableSizeTypes);
	}
	
	 this.sortSizeTypes = function(array){
	 	if(contains(array,"regular") && contains(array,"petite")){
	 		var temp = array[0];
	 		array[0] = array[1];
	 		array[1] = temp;
	 	}
    }
	 
	this.initializeDefaultSelectedSizeType = function () { 
		
		if (isEmpty(this.mSelectedSizeType)) { 
			this.mSelectedSizeType = this.mAvailableSizeTypes[0]; 
		} else { 
			var currentSelectedSizeTypeIsAvaiable = false;
			for (var i = 0; i < this.mAvailableSizeTypes.length; i++) {
				var sizeType = this.mAvailableSizeTypes[i];
				if (sizeType == this.mSelectedSizeType) {
					currentSelectedSizeTypeIsAvaiable = true;
					break;
				}
			}
			if (!currentSelectedSizeTypeIsAvaiable) { 
				this.mSelectedSizeType = this.mAvailableSizeTypes[0];
			}
		}
	}		
 
	this.initializeAvailableColors = function (pSizeType) {
		 
		this.mAvailableColorCodes = new Array();
		var index = 0;
		for (var i = 0; i < this.prds.length; i++) {
			var currentPrd = this.prds[i];
			//if (currentPrd.type == pSizeType) {
				if (this.mAvailableColorCodes, currentPrd.colorCode!="" && !contains(this.mAvailableColorCodes, currentPrd.colorCode)) {
					this.mAvailableColorCodes[index] = currentPrd.colorCode;
					index++;
				}
			//}
		}
	}
	 
	this.initializeDefaultSelectedColor = function() {
		 
		if (isEmpty(this.mSelectedColorCode)) {
			this.mSelectedColorCode = this.mAvailableColorCodes[0];
		} else {
			var selectedColorIsPresentInAvailableColors = false;
			 
			for (var i = 0; i < this.mAvailableColorCodes.length; i++) {
				var color = this.mAvailableColorCodes[i];
				if (color == this.mSelectedColorCode) {
					selectedColorIsPresentInAvailableColors = true;
					break;
				}
			}
			if (!selectedColorIsPresentInAvailableColors) {
			 
				this.mSelectedColorCode = this.mAvailableColorCodes[0];
			}
		}	
	}
	
	this.initializeAvailableSizes = function(pSelectedColor, pSelectedSizeType) {
	 
		this.mAvailableSizes = new Array();
		var index = 0;
		
		for (var i = 0; i < this.prds.length; i++) {
			var currentPrd = this.prds[i];
			//if ((currentPrd.type == pSelectedSizeType) && (currentPrd.colorCode == pSelectedColor)) {
				if (this.mAvailableSizes, currentPrd.sizeName!="" && !contains(this.mAvailableSizes, currentPrd.sizeName)) {
					this.mAvailableSizes[index] = currentPrd.sizeName;
					index++;
				}
			//}
		}		
	
	}
 
	this.initializeDefaultSelectedSize = function () {
  		availableSizesinSizeType = new Array();
		for (var i = 0; i < this.mAvailableSizes.length; i++) {
 			var sizeName1 = this.mAvailableSizes[i];
 			if(this.getProd(this.mAvailableSizes[i]) != null && (this.getProd(this.mAvailableSizes[i]).availabilityStatus == this.STATUS_IN_STOCK || this.getProd(this.mAvailableSizes[i]).availabilityStatus == this.AVAILABILITY_STATUS_BACKORDERABLE))
 			{
 				availableSizesinSizeType.push(sizeName1);
 			} 
		}
 
		if(availableSizesinSizeType.length==1){ 
			this.mSelectedSizeName =availableSizesinSizeType[0] ; 			
			 
			
		}
		else{
  			for (var i = 0; i < this.mAvailableSizes.length; i++) {
    			var sizeName = this.mAvailableSizes[i];
    			if (sizeName == this.mSelectedSizeName) {
     				if(this.getProd(this.mAvailableSizes[i]) != null && (this.getProd(this.mAvailableSizes[i]).availabilityStatus == this.STATUS_IN_STOCK || this.getProd(this.mAvailableSizes[i]).availabilityStatus == this.AVAILABILITY_STATUS_BACKORDERABLE))
     				{
      					return;
     				}
    			}
   			}
  			this.mSelectedSizeName = null;
  			
  			
		}
 	}
  
	this.getSelectedProd = function() { 
		for (var i = 0; i < this.prds.length; i++) {
			var prd = this.prds[i];
			if(prd.colorCode!='' && prd.sizeName!=''){
				if ((prd.colorCode == this.mSelectedColorCode) 
					&& (prd.sizeName == this.mSelectedSizeName)
						&& (prd.type == this.mSelectedSizeType)) {
					return prd;
				}
			} else if(prd.colorCode=='' && prd.sizeName!=''){
				if ((prd.sizeName == this.mSelectedSizeName)
						&& (prd.type == this.mSelectedSizeType)) {
					return prd;
				}
			} else if(prd.colorCode!='' && prd.sizeName==''){
				if (prd.colorCode == this.mSelectedColorCode) {
					return prd;
				}
			}else {
				return prd;
			}
		}
		return null;
	}
	
	this.getProd = function(pSizeName) {

		for (var i = 0; i < this.prds.length; i++) {
			var prd = this.prds[i];
			if(this.mSelectedColorCode != null){
				if ((prd.colorCode == this.mSelectedColorCode) 
					&& (prd.sizeName == pSizeName)
						&& (prd.type == this.mSelectedSizeType)) {

					return prd;
				}
			} else {
				if ((prd.sizeName == pSizeName)
						&& (prd.type == this.mSelectedSizeType)) {

					return prd;
				}	
			}
		}
		return null;
	}
	
	this.getColorName = function(pColorCode){
		for (var i = 0; i < this.prds.length; i++) {
			var prd = this.prds[i];
			if (prd.colorCode == pColorCode) {
				return prd.colorName;
			}
		}
		return null;
	}
	
	this.isAvailableSize = function(pSizeName){
		for (var i = 0; i < this.mAvailableSizes.length; i++) {			
			if (this.mAvailableSizes[i] == pSizeName) {
				return true;
			}
		}
		return false;
	}
}

function Prd(pPrdId, pSizeName, pType, pColorCode, pColorName, pAvailabilityStatus, pAvailabilityDate, pImagePath) {
		this.prdId = pPrdId;
		this.sizeName = pSizeName;		
		this.type = pType;
		this.colorCode = pColorCode;
		this.colorName = pColorName;		
		this.availabilityStatus = pAvailabilityStatus;
		this.availabilityDate = pAvailabilityDate;
		this.imagePath = pImagePath;
}
 
function isEmpty (pValue) {
	if ((pValue == null) || (trim(pValue) == "")) {
		return true;
	} else {
		return false;
	}
}
function trim(str)
{
  return str.replace(/^\s*|\s*$/g,"");
}
 
function contains (array, value){
	for(var i=0;i<array.length;i++){
		if(array[i] == value){
			return true;
		}
	}
	return false;
}
 
function getElement(elementId){
	return document.all ? document.all[elementId] : document.getElementById(elementId);
}

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, prdId, pColorName, isMainProduct, pSizeName){
	var uniqueId = id;
	
	if(pSizeName!=null){
		uniqueId = pSizeName;
	}
		
	if(isMainProduct!=null & isMainProduct=="true"){
		uniqueId = "l"+uniqueId; 
	}
	if(prdId!=null && prdId!="")
	{
		getElement(uniqueId+"catalogRefIds").value = prdId;
	    getElement(uniqueId+"qty").name = prdId; 
	}
	
	if(document["frm"+uniqueId]!=null)
	{
	document["frm"+uniqueId].color.value=pColorCode; 
	}
if(getElement(uniqueId+"selectedColor")!=null && pColorName!=null)
	{
	getElement(uniqueId+"selectedColor").innerHTML=pColorName.toUpperCase(); 
	}
	 

	if(isMainProduct!=null & isMainProduct=="true"){	} else {
		changeImage("img"+uniqueId,prodImg);	
	}	
}

function changeImage(imageHandle,prodImg){
	if(prodImg != null && document[imageHandle]!=null){
		document[imageHandle].src = prodImg;
	} 
}

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 scrptFunArr = new Array();

function addScriptFunction(pProductId, pSizeName, pProductColor, pProdImg,pPrdId, pColorName){
	scrptFunArr.push(new scrptF(pProductId, pSizeName, pProductColor, pProdImg,pPrdId, pColorName));
}

function callscriptfunctions(){
	for(var i=0;i<scrptFunArr.length;i++){		
		changeColor(scrptFunArr[i].productId,scrptFunArr[i].productColor,scrptFunArr[i].prodImg,scrptFunArr[i].prdId, scrptFunArr[i].colorName, '', scrptFunArr[i].sizeName);
	}
}

function scrptF(productId, pSizeName, pProductColor, pProdImg, pPrdId, pColorName){
	this.productId = productId;
	this.sizeName = pSizeName;
	this.productColor = pProductColor;
	this.prodImg = pProdImg;
	this.prdId = pPrdId;
	this.colorName = pColorName;	
}
 
	
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));
	}
} 

var colorList = new Array();

function addColor(pColorCode, pColorName, pProductImageURL){
    colorList.push(new Color(pColorCode, pColorName, pProductImageURL));
}

function getColor(pColorCode){
    for(var i=0;i<colorList.length;i++){
        if(colorList[i].colorCode == pColorCode){
            return colorList[i];
        }
    }
    return null;
} 

function ReplaceIfNeeded(pSizeName){ 

var sizes=new Array();
sizes[0]="PETITE";
sizes[1]="SMALL";
sizes[2]="MEDIUM";
sizes[3]="LARGE";
sizes[4]="PETITE/SMALL"; 
sizes[5]="MEDIUM/LARGE";  

  for (var i = 0; i < sizes.length; i++) {	  
	  if(pSizeName=='P')
	    return sizes[0];	  
	  if(pSizeName=='S')
	    return sizes[1];
	  if(pSizeName=='M')
	    return sizes[2];
	  if(pSizeName=='L')
	    return sizes[3];
	  if(pSizeName=='P/S')
	    return sizes[4];
	  if(pSizeName=='M/L')
	    return sizes[5];
	  
	  else
	    return pSizeName;
  } 
}
  