function clickSwatch(whichColor,inactiveString){
  
  inactiveArray = inactiveString.split(",");
  
  for(var whichSwatch in inactiveArray){

    document.getElementById("swatch_"+inactiveArray[whichSwatch]).className = "inactiveSwatch";    
  
  }
  
  document.getElementById("swatch_"+whichColor).className = "activeSwatch";
  
  document.getElementById("color_fabric_select").value = whichColor;

  
}

function toggle(whichId){
  
  if(document.build_2.doMonogram.checked == true){
  
    document.getElementById(whichId).style.display = "block";
  
  }else{

    document.getElementById(whichId).style.display = "none";  
  
  }
  

}

function setMonogram(whichDiv){
  
  if(whichDiv == 'times'){

    document.getElementById("monogram_times").style.backgroundPosition = "0px 34px";
    document.getElementById("monogram_block").style.backgroundPosition = "57px 0px";
    document.getElementById("monogram_script").style.backgroundPosition = "114px 0px";    

    document.getElementById("embroidery_style").value = 73;
  
  }else if(whichDiv == 'script'){

    document.getElementById("monogram_times").style.backgroundPosition = "0px 0px";
    document.getElementById("monogram_block").style.backgroundPosition = "57px 0px";
    document.getElementById("monogram_script").style.backgroundPosition = "114px 34px";  
    
    document.getElementById("embroidery_style").value = 71;  
  
  }else if(whichDiv == 'block'){

    document.getElementById("monogram_times").style.backgroundPosition = "0px 0px";
    document.getElementById("monogram_block").style.backgroundPosition = "57px 34px";
    document.getElementById("monogram_script").style.backgroundPosition = "114px 0px";    

    document.getElementById("embroidery_style").value = 72;
  
  }  


}

function setPrice(whichAction){

    var price = 0;
    var size_id = document.getElementById("size_select").value;
    var material_id = document.getElementById("material_id").value;
    var color_fabric_id = document.getElementById("color_fabric_select").value;
    var bed_quantity = document.getElementById("cartquantity").value;
    var pad_id = document.getElementById("pad_selection").value;
    var add_aluminum_legs = false;

    
    if(material_id == 2){
    
      document.getElementById("doMonogram").checked = false;
      document.getElementById("monogram_wrapper").style.display = "none";
      document.getElementById("monogram_wrapper_line").style.display = "none";
      document.getElementById("monogram_details").style.display = "none";      
    
    }else{

      document.getElementById("monogram_wrapper").style.display = "block";    
      document.getElementById("monogram_wrapper_line").style.display = "block";
          
    }

    var monogramming = document.getElementById("doMonogram").checked;    

    if(monogramming == true){
            
            var goldEmb = 76;
            var silverEmb = 77;
            var blackEmb = 75;                        
            
            switch(color_fabric_id){
            
              // BURGUNDY
              case '30':
              var embColor = silverEmb;
              break;

              case '54':
              var embColor = silverEmb;
              break; 
              
              case '25':
              var embColor = silverEmb;
              break; 
              
              case '67':
              var embColor = silverEmb;
              break;                
              
              // FOREST GREEN
              case '31':
              var embColor = goldEmb;
              break;
              
              case '20':
              var embColor = goldEmb;
              break;      

              case '24':
              var embColor = goldEmb;
              break;   
              
              case '44':
              var embColor = goldEmb;
              break;                                                                                                                                                                  
              
              // ROYAL BLUE
              case '29':
              var embColor = silverEmb;
              break;                                            

              case '22':
              var embColor = silverEmb;
              break; 
              
              case '28':
              var embColor = silverEmb;
              break;                           
              
              case '53':
              var embColor = silverEmb;
              break;   
  
              //CHOCOLATE BROWN
              case '21':
              var embColor = silverEmb;
              break;              

              //GOLD
              case '42':
              var embColor = blackEmb;
              break; 

              //BIRCH
              case '27':
              var embColor = blackEmb;
              break; 
              
              //SLATE BLUE
              case '26':
              var embColor = blackEmb;
              break;  
              
              // SIERRA
              case '82':
              var embColor = goldEmb;
              break; 
              
              default:
              var embColor = silverEmb;
           
            }    

            document.getElementById("embroidery_color").value = embColor;

            switch(embColor){
            
              case goldEmb:
              var label = 'gold';
              break;

              case blackEmb:
              var label = 'black';
              break;            

              case silverEmb:
              var label = 'silver';
              break;            

            }
            var aDiv = document.createElement("div");
            var message1 = document.createTextNode("Enter text to be embroidered in "+label+" thread. ");
            var message2 = document.createTextNode("10 character limit");
            var font22 = document.createElement("font");
            font22.setAttribute('style', 'font-weight:normal;');
            font22.appendChild(message2);
            
            so_clearInnerHTML(document.getElementById("emb_color_text"));
            
            aDiv.appendChild(message1);
            aDiv.appendChild(font22);
                        
            //document.getElementById("emb_color_text").appendChild(message1);  
            document.getElementById("emb_color_text").appendChild(aDiv);

    }

    if(monogramming == true){
    
      monogramming = 8;
    
    }else{

      monogramming = 0;
    
    }
    

    
    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 = bed_quantity;  

    if (whichAction == 'material' || whichAction == 'load') {
        
        so_clearInnerHTML(document.getElementById("colorSwatches"));
        
        var y = document.getElementById("color_fabric_select");     
        var i;
        for (i = y.length - 1; i >= 0; i--) {

            y.remove(i);

        }
        
        var inactiveString = [];
        var objectArray = [];
        
        for( var color in colorOptions[material_id]){
      
            var skuTest = size_id+"-"+color;
            
            if (!in_array(color, materialsAvoid) && productSkus[skuTest] == 1 ) {        
      
              inactiveString.push(color);
              //var eIMG1 = document.createElement("img");
              //objectArray.push(eIMG1);
              
            }  
       
        }
        
        var counter = 0;
        for( var color in colorOptions[material_id]){
        
//          alert(color+" "+colorOptions[material_id][color]);
          
          var skuTest = size_id+"-"+color;
          
          if (!in_array(color, materialsAvoid) && productSkus[skuTest] == 1 ) {
              
              
              var eIMG1 = document.createElement("img");
              eIMG1.setAttribute("src","/images/material_img/"+swatchImgs[color]);          
              eIMG1.setAttribute("alt",colorOptions[material_id][color]+" sample");   
              eIMG1.setAttribute("width","22");              
              eIMG1.setAttribute("height","22");
              eIMG1.setAttribute("id","swatch_"+color);   

              eIMG1.onclick = new Function('clickSwatch(' + color + ',"' + inactiveString + '");setPrice("color");');
 
              //eIMG1.setAttribute("onclick","clickSwatch('" + color + "','" + inactiveString + "')");
              //eIMG1.addEventListener("click", clickSwatch, false);    
              /*
              eIMG1.onclick = function() { 
                
                //alert(color+" "+inactiveString);
                clickSwatch(color,inactiveString);
                return false;
                
              } 
              */  
                          

              if(counter == 0){
              
                eIMG1.className = "activeSwatch";
              
              }else{

                eIMG1.className = "inactiveSwatch";
              
              }    
              
              document.getElementById("colorSwatches").appendChild(eIMG1);

              var jqueryId = "#swatch_"+color;
                            
              $("#swatch_"+color).click(function () {
                //alert("hi"+color);
              });
              
              var elOptNew = document.createElement('option');
              elOptNew.text = colorOptions[material_id][color];
              elOptNew.value = color;

              try {
                  y.add(elOptNew, null);
                  // standards compliant; doesn't work in IE
              }
              catch(ex) {
                  y.add(elOptNew);
                  // IE only
              }
          
          }
          
          counter++;
          
        }

        // 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;
    color_fabric_id = document.getElementById("color_fabric_select").value;

    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;

    }

    var pad_price = 0;

    if (pad_id == 'false') {

        pad_price = 0;

    } 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) {

            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 {

            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) + parseFloat(monogramming));

                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) + parseFloat(pad_price) + parseFloat(monogramming));

                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) + parseFloat(pad_price) + parseFloat(monogramming));

                break;

            }


        }


    }


    so_clearInnerHTML(document.getElementById("all_purpose_price_switcher"));
    document.getElementById("all_purpose_price_switcher").appendChild(document.createTextNode("$" + CurrencyFormatted(price)));    


}



// 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 checkEmbroidery(){

  if(document.getElementById("doMonogram").checked == true && isEmpty(document.getElementById("embroidery_text").value)){
  
    alert("Please enter embroidery text or de-select \"Yes, I would like to monogram this bed\" before continuing.");
    return false;
    
  }else{
  
    return true;
  
  }
  

}

function isEmpty( inputStr ) { if ( null == inputStr || "" == inputStr ) { return true; } return false; }

function so_clearInnerHTML(obj) {
    while (obj.firstChild) obj.removeChild(obj.firstChild);
}