/*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

    le.pants.js - functions specific to pants
 
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*/
function hem(num, thisSbg) {
	var thisPHS = this;
	var waistChooser 		   = new Object(); // used as associative array keyed by style number, example key "SN-255"
	var inseamChooser          = new Object(); // used as associative array keyed by style number, example key "SN-255"
	var cuffingDiv             = new Object(); // used as associative array keyed by style number, example key "SN-255"
	var sizeSelectorDiv        = new Object(); // used as associative array keyed by style number, example key "SN-255"
	var sizeSelectorCustomDiv  = new Object(); // used as associative array keyed by style number, example key "SN-255"
	var cuffingChooser         = new Object(); // used as associative array keyed by style number, example key "SN-255"
	    
    // popups
    var hemServiceInfoPopup = new Object(); // used as associative array keyed by style number, example key "SN-255";
    var inseamHemmingOptionsHelpPopup = new Object(); // used as associative array keyed by style number, example key "SN-255";
    
    this.waistChooser = waistChooser;
    
    this.buildUI = function() {

    	for (y = 0; y < thisSbg.snArray.length; y++) {
    		waistChooser["SN-"+thisSbg.snArray[y]] = document.getElementById("waistChooser_prd_" + num + "_" + thisSbg.snArray[y]).getElementsByTagName("select")[0];
    		inseamChooser["SN-"+thisSbg.snArray[y]] = document.getElementById("inseamChooser_prd_" + num + "_" + thisSbg.snArray[y]).getElementsByTagName("select")[0];
    		cuffingDiv["SN-"+thisSbg.snArray[y]] = document.getElementById("sizeCuffing_prd_" + num + "_" + thisSbg.snArray[y]);
    		sizeSelectorDiv["SN-"+thisSbg.snArray[y]] = document.getElementById("sizeSelector_prd_" + num + "_" + thisSbg.snArray[y]);
    		sizeSelectorCustomDiv["SN-"+thisSbg.snArray[y]] = document.getElementById("sizeSelectorCustom_prd_" + num + "_" + thisSbg.snArray[y]);
    		
            if (cuffingDiv["SN-"+thisSbg.snArray[y]] != null) {
                cuffingChooser["SN-"+thisSbg.snArray[y]] = document.getElementById("cuffingChooser_prd_" + num + "_" + thisSbg.snArray[y]).getElementsByTagName("select")[0];
            }    	

            if (document.getElementById("sizeChart_prd_" + num + "_" + thisSbg.snArray[y]).getElementsByTagName("a")[1] != null) {
                inseamHemmingOptionsHelpPopup["SN-"+thisSbg.snArray[y]] = document.getElementById("sizeChart_prd_" + num + "_" + thisSbg.snArray[y]).getElementsByTagName("a")[1];
            }  
          
            if (document.getElementById("hemmingInfo_prd_" + num + "_" + thisSbg.snArray[y]) != null) {
                hemServiceInfoPopup["SN-"+thisSbg.snArray[y]] = document.getElementById("hemmingInfo_prd_" + num + "_" + thisSbg.snArray[y]).getElementsByTagName("a")[0];
            }           
    	}
        
        doDebug("[buildUI] calling checkFeatureSelection()");
        thisSbg.getStyleNumber();
        thisSbg.checkFeatureSelection();
        thisPHS.selectHemmingType();
    };
    
    this.registerEvents = function() {
    	for (y = 0; y < thisSbg.snArray.length; y++) {
    		waistChooser["SN-"+thisSbg.snArray[y]].onchange = this.selectWaist;
    		inseamChooser["SN-"+thisSbg.snArray[y]].onchange = this.selectInseam;
    		
    		if (cuffingChooser["SN-"+thisSbg.snArray[y]] != null) {
    			cuffingChooser["SN-"+thisSbg.snArray[y]].onchange = this.selectCuffing;
    		}    		

            if (inseamHemmingOptionsHelpPopup["SN-"+thisSbg.snArray[y]] != null) {
                inseamHemmingOptionsHelpPopup["SN-"+thisSbg.snArray[y]].onclick = thisSbg.callLEPopup;
            }
            
            if (hemServiceInfoPopup["SN-"+thisSbg.snArray[y]] != null) {
                hemServiceInfoPopup["SN-"+thisSbg.snArray[y]].onclick = thisSbg.callLEPopup;
            }    	
    	}
    };
         
    this.selectWaist = function() { 	
    	doDebug("[selectWaist] selected: " + waistChooser["SN-"+thisSbg.currentStyle].options[waistChooser["SN-"+thisSbg.currentStyle].selectedIndex].value);
 		
		if (waistChooser["SN-"+thisSbg.currentStyle].selectedIndex > 0) {
			thisSbg.selection.hemmed = true;
			thisSbg.selection.sizeCode = waistChooser["SN-"+thisSbg.currentStyle].options[waistChooser["SN-"+thisSbg.currentStyle].selectedIndex].value;
						
	        thisSbg.compareSelectionToSku();
	        thisSbg.buildRecap();
	        
			if (cuffingChooser["SN-"+thisSbg.currentStyle] != null) {
				thisPHS.selectCuffing();
				cuffingChooser["SN-"+thisSbg.currentStyle].disabled = false;
			}
			thisPHS.selectInseam();
			inseamChooser["SN-"+thisSbg.currentStyle].disabled = false;
						
	        thisSbg.selection.updated = true;
		} else {
			thisSbg.selection.sizeCode = "";
			thisSbg.selection.hemming.length = "";
			inseamChooser["SN-"+thisSbg.currentStyle].disabled = true;
	
			if (cuffingChooser["SN-"+thisSbg.currentStyle] != null) {
				cuffingChooser["SN-"+thisSbg.currentStyle].disabled = true;
			}
	 	}
	 	thisPHS.buildPantsSizeRecap();
        thisSbg.checkColors();
    };
         
    this.selectInseam = function() {
    	doDebug("[selectInseam] selected: " + inseamChooser["SN-"+thisSbg.currentStyle].options[inseamChooser["SN-"+thisSbg.currentStyle].selectedIndex].value);
    	doDebug("[selectInseam] maxCuff = " + parseFloat(thisSbg.selection.sku.maxCuff));
    	
		if (inseamChooser["SN-"+thisSbg.currentStyle].selectedIndex == 0 && cuffingChooser["SN-"+thisSbg.currentStyle] != null) {
			// selected "please select"...try again
			show(cuffingDiv["SN-"+thisSbg.currentStyle]);
		} else if (inseamChooser["SN-"+thisSbg.currentStyle].selectedIndex == 1) {
			// selected unfinished
			thisSbg.selection.hemming.finished = false;
			thisSbg.selection.hemming.cuffed = false;
			
			if (cuffingChooser["SN-"+thisSbg.currentStyle] != null) {
				hide(cuffingDiv["SN-"+thisSbg.currentStyle]);	
			}
		} else {
			// selected an inseam value
			thisSbg.selection.hemming.finished = true;
			thisSbg.selection.hemming.length = inseamChooser["SN-"+thisSbg.currentStyle].options[inseamChooser["SN-"+thisSbg.currentStyle].selectedIndex].value;
			
			if (cuffingChooser["SN-"+thisSbg.currentStyle] != null) {
				if (parseFloat(thisSbg.selection.hemming.length) > parseFloat(thisSbg.selection.sku.maxCuff)) {
					thisSbg.selection.hemming.cuffed = false;
					hide(cuffingDiv["SN-"+thisSbg.currentStyle]);	
				} else {
					show(cuffingDiv["SN-"+thisSbg.currentStyle]);
					thisPHS.selectCuffing();
				}
			}
		}
		
		// cuffing could be previously selected when we show the div so let's make sure to do it now
		thisPHS.updateInseamDropdownForSku(thisSbg.selection.sku);
		thisPHS.buildPantsSizeRecap();
        document.getElementById("inseamChoice_prd_" + num + "_" + thisSbg.currentStyle).getElementsByTagName("span")[0].className = document.getElementById("inseamChoice_prd_" + num + "_" + thisSbg.currentStyle).getElementsByTagName("span")[0].className.replace(" errorHiLight", "");
    };         
    
    this.selectCuffing = function() {
    	doDebug("[selectCuffing] selected: " + cuffingChooser["SN-"+thisSbg.currentStyle].options[cuffingChooser["SN-"+thisSbg.currentStyle].selectedIndex].value);
    	
    	if (cuffingChooser["SN-"+thisSbg.currentStyle].options[cuffingChooser["SN-"+thisSbg.currentStyle].selectedIndex].value == "cuffed") {
    		thisSbg.selection.hemming.cuffed = true;
    	} else {
    		thisSbg.selection.hemming.cuffed = false;
    	}

    	thisPHS.updateInseamDropdownForSku(thisSbg.selection.sku);
    	thisPHS.buildPantsSizeRecap();
    }
    
    // return whether the selections are valid
    this.selectHemming = function() {
    	var isGood = false;
    	
    	if (thisSbg.selection.hemmed == false) {
    		return true;
    	}
    	
    	if (waistChooser["SN-"+thisSbg.currentStyle].selectedIndex > 0 && inseamChooser["SN-"+thisSbg.currentStyle].selectedIndex > 0) {
    		return true;
    	} else {
    		return false;
    	}
    }
         
    this.updateInseamDropdownForSku = function(sku) {
		var i;
		var min;
		var max;
		var hemmingError = false;
		
		if (cuffingChooser["SN-"+thisSbg.currentStyle] != null && cuffingChooser["SN-"+thisSbg.currentStyle].options[cuffingChooser["SN-"+thisSbg.currentStyle].selectedIndex].value == "cuffed" && cuffingChooser["SN-"+thisSbg.currentStyle].disabled == false) {
			doDebug("[updateInseamDropdownForSku] cuffing selected");
			min = parseFloat(sku.minCuff);
			max = parseFloat(sku.maxCuff);
		} else {
			doDebug("[updateInseamDropdownForSku] cuffing not selected");
			min = parseFloat(sku.minInseam);
			max = parseFloat(sku.maxInseam);
		}
		
		for (i = 2; i < inseamChooser["SN-"+thisSbg.currentStyle].options.length; i++) {
			if (parseFloat(inseamChooser["SN-"+thisSbg.currentStyle].options[i].value) < min || parseFloat(inseamChooser["SN-"+thisSbg.currentStyle].options[i].value) > max) {
				doDebug("hide(inseamChooser.options[" + i + "]) = " + inseamChooser["SN-"+thisSbg.currentStyle].options[i].value);
				if (inseamChooser["SN-"+thisSbg.currentStyle].selectedIndex == i) {
					hemmingError = true;
					inseamChooser["SN-"+thisSbg.currentStyle].selectedIndex = 0;
				}
				hide(inseamChooser["SN-"+thisSbg.currentStyle].options[i]);
			} else {
				doDebug("show(inseamChooser.options[" + i + "]) = " + inseamChooser["SN-"+thisSbg.currentStyle].options[i].value);
				show(inseamChooser["SN-"+thisSbg.currentStyle].options[i]);
			}
		}
		
		if (hemmingError == true) {
			show(thisSbg.invalidSizeColorMessage);
			thisSbg.invalidSizeColorMessage.innerHTML = "You have selected an inseam length and hemming option combination which is not offered.";		
		} else {
			hide(thisSbg.invalidSizeColorMessage);
		}
    };        
    
    this.buildPantsSizeRecap = function(){
       	var displaySizeCode = sizeAssoc[thisSbg.selection.sizeCode];
       	
       	if (displaySizeCode == undefined) {
       		displaySizeCode = "";
       	}
       	
       	if (thisSbg.selection.hemmed == true && thisPHS.selectHemming() == true) {
	       	doDebug("[buildPantsSizeRecap] thisPHS.selectHemming() = " + true);
       		if (thisSbg.selection.hemming.finished == false) {
       			displaySizeCode = displaySizeCode + " x unfinished";
       		} else if (thisSbg.selection.hemming.length != "" && thisSbg.selection.hemming.length != null) {
       			doDebug("[buildPantsSizeRecap] thisSbg.selection.hemming.length = " + thisSbg.selection.hemming.length);
       			displaySizeCode = displaySizeCode + " x " + thisSbg.selection.hemming.length;
       		}

         	if (thisSbg.selection.hemming.cuffed == true) {
        		displaySizeCode = displaySizeCode + " (cuffed)" 
        	}  
       	}
       	
       	doDebug("[buildPantsSizeRecap] displaySizeCode = " + displaySizeCode);
        thisSbg.recap.size.innerHTML = displaySizeCode + " ";
    };      
    
    this.selectHemmingType = function() {
        var i;
        var preHemmed = false;
        
        for (i = 0; i < thisSbg.selection.featureCodes.length; i++) {
            if (thisSbg.selection.featureCodes[i] == 250002) {
            	doDebug("[selectHemmingType] selected pre-hemmed");
            	preHemmed = true;    
            } else if (thisSbg.selection.featureCodes[i] == 250001) {
           		doDebug("[selectHemmingType] selected hemmed-to-order");
            }
        }
        
        if (preHemmed == true) {
        	if(sizeSelectorDiv["SN-"+thisSbg.currentStyle] != null){
        		show(sizeSelectorDiv);
        	}
        	if(sizeSelectorCustomDiv["SN-"+thisSbg.currentStyle] != null){
        		hide(sizeSelectorCustomDiv);
        	}
           	thisSbg.sizeSelectFromDropdown();
           	thisSbg.selection.hemmed = false;      
        } else {
        	if(sizeSelectorDiv["SN-"+thisSbg.currentStyle] != null){
        		hide(sizeSelectorDiv["SN-"+thisSbg.currentStyle]);
        	}
        	if(sizeSelectorCustomDiv["SN-"+thisSbg.currentStyle] != null){
        		show(sizeSelectorCustomDiv["SN-"+thisSbg.currentStyle]);  
        	}
           	thisPHS.selectWaist();
        }		
    };
 	
    /*/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
    
        Dynamic selectors for modify from bag
    
    /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/*/
        
    this.pickInseam = function(inseam) {
        var i;

        if(inseamChooser["SN-"+thisSbg.currentStyle] != null && inseamChooser["SN-"+thisSbg.currentStyle].options.length > 0){
        	inseamChooser["SN-"+thisSbg.currentStyle].options[0].selected = "selected";
        }else{
        	return;
        }
        	
        for (i = 0; i < inseamChooser["SN-"+thisSbg.currentStyle].options.length; i++) {
            if ((parseFloat(inseamChooser["SN-"+thisSbg.currentStyle].options[i].text) == parseFloat(inseam)) || (inseam == "UNF" && inseamChooser["SN-"+thisSbg.currentStyle].options[i].text == "unfinished")) {
                inseamChooser["SN-"+thisSbg.currentStyle].options[i].selected = "selected";
                thisPHS.selectInseam();
            }
        }
    };
    
    this.pickCuffing = function(cuffing) {
        var cuffOpt;
        if (cuffingChooser["SN-"+thisSbg.currentStyle] == null) {
        	return;
        }
        
        if (cuffing === true || cuffing === "true") {
            cuffOpt = "cuffed";
        } else if (cuffing === false || cuffing === "false") {
            cuffOpt = "uncuffed";
        }

        var i;
        
        for (i = 0; i < cuffingChooser["SN-"+thisSbg.currentStyle].options.length; i++) {
            if (cuffingChooser["SN-"+thisSbg.currentStyle].options[i].text == cuffOpt) {
                cuffingChooser["SN-"+thisSbg.currentStyle].options[i].selected = true;
                thisPHS.selectCuffing();
            }
        }      
    };
    
	this.buildUI();
	this.registerEvents();
};
