// JavaScript Document
function CurrencyFormatted(amount)
 {
    var i = parseFloat(amount);
    if (isNaN(i)) {
        i = 0.00;
    }
    var minus = '';
    if (i < 0) {
        minus = '-';
    }
    i = Math.abs(i);
    i = parseInt((i + .005) * 100);
    i = i / 100;
    s = new String(i);
    if (s.indexOf('.') < 0) {
        s += '.00';
    }
    if (s.indexOf('.') == (s.length - 2)) {
        s += '0';
    }
    s = minus + s;
    return s;
}

function in_array(needle, haystack, argStrict) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: vlado houba
    // *     example 1: in_array('van', ['Kevin', 'van', 'Zonneveld']);
    // *     returns 1: true
    // *     example 2: in_array('vlado', {0: 'Kevin', vlado: 'van', 1: 'Zonneveld'});
    // *     returns 2: false
    // *     example 3: in_array(1, ['1', '2', '3']);
    // *     returns 3: true
    // *     example 3: in_array(1, ['1', '2', '3'], false);
    // *     returns 3: true
    // *     example 4: in_array(1, ['1', '2', '3'], true);
    // *     returns 4: false
    var key = '',
    strict = !!argStrict;

    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {
                return true;
            }
        }
    } else {
        for (key in haystack) {
            if (haystack[key] == needle) {
                return true;
            }
        }
    }

    return false;
}


function so_clearInnerHTML(obj) {
    while (obj.firstChild) obj.removeChild(obj.firstChild);
}

function showAlert(whichDiv, tempText) {

    var errorMessage = "Sorry, " + tempText + " is not available in the selected size.";

    so_clearInnerHTML(document.getElementById(whichDiv));
    document.getElementById(whichDiv).appendChild(document.createTextNode(errorMessage));
    document.getElementById(whichDiv).style.display = "block";

}

function setMultiPrice(whichAction,isClearance,popup)
 {
    var price = 0;
    var size_id = document.getElementById("size_select").value;
	
    if(noColorChoice == '0'){

    	var color_fabric_id = document.getElementById("color_fabric_select").value;    	
    	
    }

    var bed_quantity = document.getElementById("cartquantity").value;
    
    document.getElementById("colorAlertBox").style.display = "none";

    if (whichAction == 'size' || whichAction == 'load') {

        var x = document.getElementById("size_select");
        var x_index = document.getElementById("size_select").selectedIndex;

	
		if(noColorChoice == '0'){
			
	        var y = document.getElementById("color_fabric_select");
	        var y_index = document.getElementById("color_fabric_select").selectedIndex;
	        
	        var i;
	        for (i = y.length - 1; i >= 0; i--) {
	
	            y.remove(i);
	
	        }
	        
	        var oldSelectedIndex = -1;
	        var loopCounter = 0;
	        for (var whichColor in switchPadColorNames[padJsId]) {
	            
	            if (!in_array(whichColor, materialsAvoid)) {
	
	                var elOptNew = document.createElement('option');
	                elOptNew.text = switchPadColorNames[padJsId][whichColor];
	                elOptNew.value = whichColor;
	
	                
	                
	                try {
	                    y.add(elOptNew, null);
	                    // standards compliant; doesn't work in IE
	                }
	                catch(ex) {
	                    y.add(elOptNew);
	                    // IE only
	                }
	
	
	                if (whichColor == color_fabric_id) {
	
	                    oldSelectedIndex = loopCounter;
	
	                }
	
	                loopCounter++;
	
	            }
	
	        }
	
	        if (oldSelectedIndex == -1) {
	
	            //alert(oldSelectedIndex);
	            }
	
	        y = document.getElementById("color_fabric_select");
	
	        var i;
	        for (i = y.length - 1; i >= 0; i--) {
	
	            var arrayIndex = size_id+"-"+y.options[i].value;
	
	            if (productSkus[arrayIndex] == isClearance || productSkus[arrayIndex] == 3) {
	
	                var tempValue = y.options[i].value;
	                var tempText = y.options[i].text;
	                y.remove(i);
	
	                if (color_fabric_id == tempValue && whichAction != 'load') {
	
	                    showAlert("colorAlertBox", tempText);
	
	                }
	
	            }
	
	        }
	        
	        if(y.length == 0){
	        	
				
	        	x.remove(x.options.selectedIndex);
	        	setMultiPrice(whichAction,isClearance);
	
	        	
	        }
	        
        } // end if noColorChoice == 0
	    
       
    }

    size_id = document.getElementById("size_select").value;
    
    if(noColorChoice == '1'){

		color_fabric_choice = 0;    
    
	}else{
		
    	color_fabric_id = document.getElementById("color_fabric_select").value;
		color_fabric_choice = switchPadColorPrices[padJsId][color_fabric_id];
		
	}

    var add_aluminum_legs = 0;

    var pad_price = 0;

    for (i = 0; i < parseInt(switchPadLevels[padJsId].length); i++) {
	
        if (i == 0) {

            if (parseInt(bed_quantity) <= parseInt(switchPadLevels[padJsId][i])) {

                price = parseInt(bed_quantity) * ((parseFloat(switchPadPrice[padJsId][i])) + (parseFloat(switchPadSizePrices[padJsId][size_id])) + (parseFloat(color_fabric_choice)));

                break;

            }

        } else if (i == parseInt(switchPadLevels[padJsId].length)) {

            if (parseInt(bed_quantity) <= parseInt(switchPadLevels[i])) {

                price = parseInt(bed_quantity) * ((parseFloat(switchPadPrice[padJsId][i])) + (parseFloat(switchPadSizePrices[padJsId][size_id])) + (parseFloat(color_fabric_choice)));

                break;

            }

        } else {

            if (parseInt(switchPadLevels[padJsId][i - 1]) < parseInt(bed_quantity) && parseInt(bed_quantity) <= parseInt(switchPadLevels[padJsId][i])) {

                price = parseInt(bed_quantity) * ((parseFloat(switchPadPrice[padJsId][i])) + (parseFloat(switchPadSizePrices[padJsId][size_id])) + (parseFloat(color_fabric_choice)));

                break;

            }


        }


    }

    so_clearInnerHTML(document.getElementById("switchingPrice"));

    var eDIV = document.createElement("div");
    eDIV.appendChild(document.createTextNode("$" + CurrencyFormatted(price)));
    document.getElementById("switchingPrice").appendChild(eDIV);

    return 0;
}


function changeDropDown(newIndex, whichSelect)
 {
    var x = document.getElementById(whichSelect);
    x.selectedIndex = newIndex;
}

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function isTextEmpty(quantity, message) {

    var i = 0;
    var failed = 0;
    for (i = 0; i < quantity; i++)
    {
        if (document.getElementById("hidden_input_id" + i).value != '0' && trim(document.getElementById("embroidery_text" + i).value) == '') {

            failed = 1;

        }
    }
    if (failed == 0) {
        return true;
    } else {
        alert(message);
        return false;
    }

}
