// 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 toggleDim(off, on) {

    var OffA = off.split("&&&");
    var OnA = on.split("&&&");

    for (loopC = 0; loopC < OffA.length; loopC++) {

        var x = document.getElementById(OffA[loopC]);
        x.style.display = 'none';

    }

    for (loopC = 0; loopC < OnA.length; loopC++) {

        var y = document.getElementById(OnA[loopC]);
        y.style.display = 'block';

    }

}

function changeDimensions(type, size_id) {

    var dimImg = document.createElement("div");

    var eIMG = document.createElement("img");

    var srcString = "/images/size_img/" + type + "/old/" + size_id + ".jpg";

    eIMG.setAttribute("src", srcString);
    dimImg.appendChild(eIMG);

    so_clearInnerHTML(document.getElementById("switchingDim"));
    document.getElementById("switchingDim").appendChild(dimImg);
}


function toggleCheck(me, whichCheckbox)
 {
    var checked = me.checked;
    if (checked) {
        var ck = document.getElementById(whichCheckbox);
        if (ck) ck.checked = false;
    } else {
        var ck = document.getElementById(whichCheckbox);
        if (ck) ck.checked = true;
    }

}

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 setPrice(size_id, frame_accessory, action, frame_accessory_price)
 {

    bed_quantity = document.getElementById("bed_quantity").value;

    if (action == 'price') {

        if (frame_accessory == true) {
            price = bed_quantity * (parseFloat(switchPrice[size_id]) + parseFloat(frame_accessory_price));
        } else {
            price = bed_quantity * (parseFloat(switchPrice[size_id]));
        }

        so_clearInnerHTML(document.getElementById("switchingPrice"));

        var eDIV = document.createElement("div");
        eDIV.appendChild(document.createTextNode(CurrencyFormatted(price)));
        document.getElementById("switchingPrice").appendChild(eDIV);
        /*  
  so_clearInnerHTML(document.getElementById("switchingSize"));

  var eDIV2 = document.createElement("div");         
  eDIV2.appendChild(document.createTextNode(switchContent[size_id]));
  document.getElementById("switchingSize").appendChild(eDIV2);
*/
    }

    if (action == 'frame_accessory') {

        if (frame_accessory == true) {
            price = (parseFloat(price) + (parseFloat(frame_accessory_price) * bed_quantity));
        } else {
            price = (parseFloat(price) - (parseFloat(frame_accessory_price) * bed_quantity));
        }

        so_clearInnerHTML(document.getElementById("switchingPrice"));

        var eDIV3 = document.createElement("div");
        eDIV3.appendChild(document.createTextNode(CurrencyFormatted(price)));
        document.getElementById("switchingPrice").appendChild(eDIV3);

    }

    changeDimensions(jsBedTypeId, document.build_bed1_form.size_id.value);

    return 0;
}

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;
    var add_aluminum_legs = document.getElementById("add_aluminum_legs").checked;
    var pad_id = document.getElementById("pad_selection").value;
    
    if(pad_id == '70a'){
    
      pad_id = 70;
      var slipColor = "a";
    
    }else if(pad_id == '70b'){

      pad_id = 70;
      var slipColor = "b";
    
    }
    
    document.getElementById("cart_quantity2").value = bed_quantity;
    var pad_quantity = document.getElementById("cart_quantity2").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 switchColorNames) {
	            
	            if (!in_array(whichColor, materialsAvoid)) {
	
	                var elOptNew = document.createElement('option');
	                elOptNew.text = switchColorNames[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
	    
        // UPDATE ACCESSORY POP UP PRICING

        if(popup == true){

            switch (size_id) {
    
            case "19":
                var pad_size_id = 58;
                break;
            case "12":
                var pad_size_id = 58;
                break;
            case "13":
                var pad_size_id = 59;
                break;
            case "14":
                var pad_size_id = 60;
                break;
            case "15":
                var pad_size_id = 61;
                break;
            case "16":
                var pad_size_id = 62;
                break;
            case "17":
                var pad_size_id = 63;
                break;
    
            }
  
            
            if (typeof(switchPadSizePrices[12]) != 'undefined'){
            
            
              var eDIV = document.createElement("div");
              var padPrice12 = parseFloat(switchPadSizePrices[12][pad_size_id]) + parseFloat(switchPadPrice[12][0]);
              eDIV.appendChild(document.createTextNode("$" + CurrencyFormatted(padPrice12)));
              so_clearInnerHTML(document.getElementById("pad_price_12"));
              document.getElementById("pad_price_12").appendChild(eDIV);  
            
            }

            if (typeof(switchPadSizePrices[2]) != 'undefined'){
            
            
              var eDIV = document.createElement("div");
              var padPrice2 = parseFloat(switchPadSizePrices[2][pad_size_id]) + parseFloat(switchPadPrice[2][0]);
              eDIV.appendChild(document.createTextNode("$" + CurrencyFormatted(padPrice2)));
              so_clearInnerHTML(document.getElementById("pad_price_2"));
              document.getElementById("pad_price_2").appendChild(eDIV);
      
            }
  
            if (typeof(switchPadSizePrices[70]) != 'undefined'){

            
              var eDIV = document.createElement("div");
              var padPrice70a = parseFloat(switchPadSizePrices[70][pad_size_id]) + parseFloat(switchPadPrice[70][0]);
              eDIV.appendChild(document.createTextNode("$" + CurrencyFormatted(padPrice70a)));
              so_clearInnerHTML(document.getElementById("pad_price_70a"));
              document.getElementById("pad_price_70a").appendChild(eDIV);
      
              var eDIV = document.createElement("div");
              var padPrice70b = parseFloat(switchPadSizePrices[70][pad_size_id]) + parseFloat(switchPadPrice[70][0]);
              eDIV.appendChild(document.createTextNode("$" + CurrencyFormatted(padPrice70b)));
              so_clearInnerHTML(document.getElementById("pad_price_70b"));
              document.getElementById("pad_price_70b").appendChild(eDIV);
              
              var eDIV = document.createElement("div");
              var padPrice70c = parseFloat(switchPadSizePrices[70][pad_size_id]) + parseFloat(switchPadPrice[70][0]);
              eDIV.appendChild(document.createTextNode("$" + CurrencyFormatted(padPrice70c)));
              so_clearInnerHTML(document.getElementById("pad_price_70c"));
              document.getElementById("pad_price_70c").appendChild(eDIV);              
              
            }
          
          }

    }

    size_id = document.getElementById("size_select").value;
    
    if(noColorChoice == '1'){

		color_fabric_id = 0;    
    
	}else{
		
    	color_fabric_id = document.getElementById("color_fabric_select").value;
    	document.getElementById("color_swatch").style.backgroundImage = "url('/images/material_img/" + switchColorSrc[color_fabric_id] + "')";
		
	}

    if (add_aluminum_legs == true) {

        add_aluminum_legs = 8;

        if (size_id == '16' || size_id == '17') {

            document.getElementById("products3_size_id").value = 57;

        } else {

            document.getElementById("products3_size_id").value = 56;

        }

    } else {

        add_aluminum_legs = 0;

    }

    //alert("Size: "+size_id+" Color: "+color_fabric_id+" Quan: "+bed_quantity+" Legs: "+add_aluminum_legs);
    var pad_price = 0;

    if (pad_id == 'false') {

        pad_price = 0;
        document.getElementById("pad_color").style.display = "none";

    } else {

        switch (size_id) {

        case "19":
            var pad_size_id = 58;
            break;
        case "12":
            var pad_size_id = 58;
            break;
        case "13":
            var pad_size_id = 59;
            break;
        case "14":
            var pad_size_id = 60;
            break;
        case "15":
            var pad_size_id = 61;
            break;
        case "16":
            var pad_size_id = 62;
            break;
        case "17":
            var pad_size_id = 63;
            break;

        }

        document.getElementById("pad_size_").value = pad_size_id;

        if (pad_id == 70) {

            document.getElementById("pad_color").style.display = "block";
            if(slipColor == "a"){
            
              //document.getElementById("whichSlipColor").value = 80;
              var pad_color_id = 80;
                        
            }else{

              //document.getElementById("whichSlipColor").value = 81;            
              var pad_color_id = 81;
                          
            }
             

            var pad_color_total = parseFloat(switchPadColorPrices[pad_id][pad_color_id]);

        } else {

            document.getElementById("pad_color").style.display = "none";
            var pad_color_total = 0;

        }

        for (i = 0; i < parseInt(switchPadLevels[pad_id].length); i++) {

            if (i == 0) {

                if (parseInt(bed_quantity) <= parseInt(switchPadLevels[pad_id][i])) {

                    pad_price = (parseFloat(switchPadPrice[pad_id][i]) + (parseFloat(switchPadSizePrices[pad_id][pad_size_id])) + (parseFloat(pad_color_total)));

                    break;

                }

            } else if (i == parseInt(switchPadLevels[pad_id][i].length)) {

                if (parseInt(bed_quantity) <= parseInt(switchPadLevels[pad_id][i])) {

                    pad_price = (parseFloat(switchPadPrice[pad_id][i]) + (parseFloat(switchPadSizePrices[pad_id][pad_size_id])) + (parseFloat(pad_color_total)));

                    break;

                }

            } else {

                if (parseInt(switchPadLevels[pad_id][i - 1]) < parseInt(bed_quantity) && parseInt(bed_quantity) <= parseInt(switchPadLevels[pad_id][i])) {

                    pad_price = (parseFloat(switchPadPrice[pad_id][i]) + (parseFloat(switchPadSizePrices[pad_id][pad_size_id])) + (parseFloat(pad_color_total)));

                    break;

                }


            }


        }




    }

    for (i = 0; i < parseInt(switchLevels.length); i++) {
	
        if (i == 0) {

            if (parseInt(bed_quantity) <= parseInt(switchLevels[i])) {

                price = parseInt(bed_quantity) * ((parseFloat(switchPrice[size_id][i])) + (parseFloat(switchColorPrice[color_fabric_id])) + (parseFloat(add_aluminum_legs)) + parseFloat(pad_price));

                break;

            }

        } else if (i == parseInt(switchLevels.length)) {

            if (parseInt(bed_quantity) <= parseInt(switchLevels[i])) {

                price = parseInt(bed_quantity) * ((parseFloat(switchPrice[size_id][i])) + (parseFloat(switchColorPrice[color_fabric_id])) + (parseFloat(add_aluminum_legs)) + parseFloat(pad_price));

                break;

            }

        } else {

            if (parseInt(switchLevels[i - 1]) < parseInt(bed_quantity) && parseInt(bed_quantity) <= parseInt(switchLevels[i])) {

                price = parseInt(bed_quantity) * ((parseFloat(switchPrice[size_id][i])) + (parseFloat(switchColorPrice[color_fabric_id])) + (parseFloat(add_aluminum_legs)) + parseFloat(pad_price));

                break;

            }


        }


    }



    so_clearInnerHTML(document.getElementById("switchingPrice"));

    var eDIV = document.createElement("div");
    eDIV.appendChild(document.createTextNode("$" + CurrencyFormatted(price)));
    document.getElementById("switchingPrice").appendChild(eDIV);
    /*  
  so_clearInnerHTML(document.getElementById("switchingSize"));
       
  var eDIV2 = document.createElement("div");         
  eDIV2.appendChild(document.createTextNode(switchContent[size_id]));
  document.getElementById("switchingSize").appendChild(eDIV2);
*/


    return 0;
}

// Used on pads page only
function setPadPrice(garbage) {

    var bed_quantity = document.getElementById("cart_quantity").value;
    if (isNaN(bed_quantity)) {

        //document.getElementById("cart_quantity").focus();
        alert("Please enter a number for the quantity.");


    } else {


        var size_id = document.getElementById("size_id").value;

        //var bed_quantity = document.getElementById("cart_quantity").value;

        //alert("Size: "+size_id+" Color: "+color_fabric_id+" Quan: "+bed_quantity+" Legs: "+add_aluminum_legs);
        var pad_price = 0;

        if (pad_id == 70) {

            //document.getElementById("whichSlipColor").style.display = "block";
            //var color_fabric_id = document.getElementById("whichSlipColor").value;
            var pad_color_id = document.getElementById("whichSlipColor").value;
            var pad_color_total = parseFloat(switchPadColorPrices[pad_id][pad_color_id]);

        } else {

            //document.getElementById("pad_color").style.display = "none";
            var pad_color_total = 0;

        }

        for (i = 0; i < parseInt(switchPadLevels[pad_id].length); i++) {

            if (i == 0) {

                if (parseInt(bed_quantity) <= parseInt(switchPadLevels[pad_id][i])) {

                    pad_price = bed_quantity * (parseFloat(switchPadPrice[pad_id][i]) + (parseFloat(switchPadSizePrices[pad_id][size_id])) + (parseFloat(pad_color_total)));

                    break;

                }

            } else if (i == parseInt(switchPadLevels[pad_id][i].length)) {

                if (parseInt(bed_quantity) <= parseInt(switchPadLevels[pad_id][i])) {

                    pad_price = bed_quantity * (parseFloat(switchPadPrice[pad_id][i]) + (parseFloat(switchPadSizePrices[pad_id][size_id])) + (parseFloat(pad_color_total)));

                    break;

                }

            } else {

                if (parseInt(switchPadLevels[pad_id][i - 1]) < parseInt(bed_quantity) && parseInt(bed_quantity) <= parseInt(switchPadLevels[pad_id][i])) {

                    pad_price = bed_quantity * (parseFloat(switchPadPrice[pad_id][i]) + (parseFloat(switchPadSizePrices[pad_id][size_id])) + (parseFloat(pad_color_total)));

                    break;

                }


            }


        }




        so_clearInnerHTML(document.getElementById("switchingPrice"));

        var eDIV = document.createElement("div");
        eDIV.appendChild(document.createTextNode("$" + CurrencyFormatted(pad_price)));
        document.getElementById("switchingPrice").appendChild(eDIV);
        /*  
    so_clearInnerHTML(document.getElementById("switchingSize"));
         
    var eDIV2 = document.createElement("div");         
    eDIV2.appendChild(document.createTextNode(switchContent[size_id]));
    document.getElementById("switchingSize").appendChild(eDIV2);
  */




    }

    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;
    }

}


function changeDim(){
	
	var sizeId = document.getElementById("size_select").value;
	var bedId = document.getElementById("bed-type").value;
	
	if(bedId == '10'){
		
		bedId = 18;
		
	}
	
	so_clearInnerHTML(document.getElementById("switching-image"));
	
	var eIMG = document.createElement("img");
	
	if(bedId == '18'){
		
		eIMG.setAttribute('src', '/images/size_img/18/'+sizeId+'.jpg');
		eIMG.setAttribute('alt', 'Dimension Photo');
		eIMG.setAttribute('height', '200px');
		eIMG.setAttribute('width', '280px');
		
	}else if(bedId == '17'){
		
		eIMG.setAttribute('src', '/images/size_img/17/'+sizeId+'.jpg');
		eIMG.setAttribute('alt', 'Dimension Photo');
		eIMG.setAttribute('height', '175px');
		eIMG.setAttribute('width', '290px');
		
	}else if(bedId == '6'){
		
		eIMG.setAttribute('src', '/images/size_img/6/'+sizeId+'.jpg');
		eIMG.setAttribute('alt', 'Dimension Photo');
		eIMG.setAttribute('height', '170px');
		eIMG.setAttribute('width', '277px');
		
	}
	
	document.getElementById("switching-image").appendChild(eIMG);	
		
}