/*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
    le.pants.js - functions specific to pants
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*/
function hem(num, thisSbg) {
	var thisPHS = this;
	var waistChooser           = document.getElementById("waistChooser_prd_" + num).getElementsByTagName("select")[0];
	var inseamChooser          = document.getElementById("inseamChooser_prd_" + num).getElementsByTagName("select")[0];
	var cuffingDiv             = document.getElementById("sizeCuffing_prd_" + num);
	var sizeSelectorDiv        = document.getElementById("sizeSelector_prd_" + num);
	var sizeSelectorCustomDiv  = document.getElementById("sizeSelectorCustom_prd_" + num);
	var cuffingChooser         = null;
    // popups
    var hemServiceInfoPopup;
    var inseamHemmingOptionsHelpPopup;
    this.waistChooser = waistChooser;
    this.buildUI = function() {
        if (cuffingDiv != null) {
            cuffingChooser = document.getElementById("cuffingChooser_prd_" + num).getElementsByTagName("select")[0];
        }
        if (document.getElementById("sizeChart_prd_" + num).getElementsByTagName("a")[1] != null) {
            inseamHemmingOptionsHelpPopup = document.getElementById("sizeChart_prd_" + num).getElementsByTagName("a")[1];
        }
        if (document.getElementById("hemmingInfo_prd_" + num) != null) {
            hemServiceInfoPopup = document.getElementById("hemmingInfo_prd_" + num).getElementsByTagName("a")[0];
        }
        doDebug("[buildUI] calling checkFeatureSelection()");
        thisSbg.getStyleNumber();
        thisSbg.checkFeatureSelection();
        thisPHS.selectHemmingType();
    };
    this.registerEvents = function() {
		waistChooser.onchange = this.selectWaist;
		inseamChooser.onchange = this.selectInseam;
		if (cuffingChooser != null) {
			cuffingChooser.onchange = this.selectCuffing;
		}
        if (inseamHemmingOptionsHelpPopup != null) {
            inseamHemmingOptionsHelpPopup.onclick = thisSbg.callLEPopup;
        }
        if (hemServiceInfoPopup != null) {
            hemServiceInfoPopup.onclick = thisSbg.callLEPopup;
        }
    };
    this.selectWaist = function() {
    	doDebug("[selectWaist] selected: " + waistChooser.options[waistChooser.selectedIndex].value);
		if (waistChooser.selectedIndex > 0) {
			thisSbg.selection.hemmed = true;
			thisSbg.selection.sizeCode = waistChooser.options[waistChooser.selectedIndex].value;
	        thisSbg.compareSelectionToSku();
	        thisSbg.buildRecap();
			if (cuffingChooser != null) {
				thisPHS.selectCuffing();
				cuffingChooser.disabled = false;
			}
			thisPHS.selectInseam();
			inseamChooser.disabled = false;
	        thisSbg.selection.updated = true;
		} else {
			thisSbg.selection.sizeCode = "";
			thisSbg.selection.hemming.length = "";
			inseamChooser.disabled = true;
			if (cuffingChooser != null) {
				cuffingChooser.disabled = true;
			}
	 	}
	 	thisPHS.buildPantsSizeRecap();
        thisSbg.checkColors();
    };
    this.selectInseam = function() {
    	doDebug("[selectInseam] selected: " + inseamChooser.options[inseamChooser.selectedIndex].value);
    	doDebug("[selectInseam] maxCuff = " + parseFloat(thisSbg.selection.sku.maxCuff));
		if (inseamChooser.selectedIndex == 0 && cuffingChooser != null) {
			// selected "please select"...try again
			show(cuffingDiv);
		} else if (inseamChooser.selectedIndex == 1) {
			// selected unfinished
			thisSbg.selection.hemming.finished = false;
			thisSbg.selection.hemming.cuffed = false;
			if (cuffingChooser != null) {
				hide(cuffingDiv);	
			}
		} else {
			// selected an inseam value
			thisSbg.selection.hemming.finished = true;
			thisSbg.selection.hemming.length = inseamChooser.options[inseamChooser.selectedIndex].value;
			if (cuffingChooser != null) {
				if (parseFloat(thisSbg.selection.hemming.length) > parseFloat(thisSbg.selection.sku.maxCuff)) {
					thisSbg.selection.hemming.cuffed = false;
					hide(cuffingDiv);	
				} else {
					show(cuffingDiv);
					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).getElementsByTagName("span")[0].className = document.getElementById("inseamChoice_prd_" + num).getElementsByTagName("span")[0].className.replace(" errorHiLight", "");
    };         
    this.selectCuffing = function() {
    	doDebug("[selectCuffing] selected: " + cuffingChooser.options[cuffingChooser.selectedIndex].value);
    	if (cuffingChooser.options[cuffingChooser.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.selectedIndex > 0 && inseamChooser.selectedIndex > 0) {
    		return true;
    	} else {
    		return false;
    	}
    }
    this.updateInseamDropdownForSku = function(sku) {
		var i;
		var min;
		var max;
		var hemmingError = false;
		if (cuffingChooser != null && cuffingChooser.options[cuffingChooser.selectedIndex].value == "cuffed" && cuffingChooser.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.options.length; i++) {
			if (parseFloat(inseamChooser.options[i].value) < min || parseFloat(inseamChooser.options[i].value) > max) {
				doDebug("hide(inseamChooser.options[" + i + "]) = " + inseamChooser.options[i].value);
				if (inseamChooser.selectedIndex == i) {
					hemmingError = true;
					inseamChooser.selectedIndex = 0;
				}
				hide(inseamChooser.options[i]);
			} else {
				doDebug("show(inseamChooser.options[" + i + "]) = " + inseamChooser.options[i].value);
				show(inseamChooser.options[i]);
			}
		}
		if (hemmingError == true) {
			show(thisSbg.invalidSizeColorMessage);
			thisSbg.invalidSizeColorMessage.innerHTML = pantsNotOffered;		
		} 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) {
            show(sizeSelectorDiv);
           	hide(sizeSelectorCustomDiv);
           	thisSbg.sizeSelectFromDropdown();
           	thisSbg.selection.hemmed = false;      
        } else {
 			hide(sizeSelectorDiv);
           	show(sizeSelectorCustomDiv);  
           	thisPHS.selectWaist();
        }		
    };
    /*/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
        Dynamic selectors for modify from bag
    /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/*/
    this.pickInseam = function(inseam) {
        var i;
        for (i = 0; i < inseamChooser.options.length; i++) {
            if ((parseFloat(inseamChooser.options[i].text) == parseFloat(inseam)) || (inseam == "UNF" && inseamChooser.options[i].text == "unfinished")) {
                inseamChooser.options[i].selected = "selected";
                thisPHS.selectInseam();
            }
        }
    };
    this.pickCuffing = function(cuffing) {
        var cuffOpt;
        if (cuffingChooser == null) {
        	return;
        }
        if (cuffing === true || cuffing === "true") {
            cuffOpt = "cuffed";
        } else if (cuffing === false || cuffing === "false") {
            cuffOpt = "uncuffed";
        }
        var i;
        for (i = 0; i < cuffingChooser.options.length; i++) {
            if (cuffingChooser.options[i].text == cuffOpt) {
                cuffingChooser.options[i].selected = true;
                thisPHS.selectCuffing();
            }
        }      
    };
	this.buildUI();
	this.registerEvents();
};
