/* wood variables */  


// populate all drop down lists  
function populateSelects()  
{  
   populateSelect(arrWood, 'wood_select');  
     
   populateSelect(arrWoodFinish, 'woodfinish_select');  
     
   populateSelect(arrHardwareFinish, 'hardwarefinish_select');  
     
   populateSelect(arrTopFixture, 'topfixtures_select');  
        
   populateSelect(arrBottomFixture, 'bottomfixtures_select');  
     
   populateSelect(arrCurve, 'curve_select');  
     
   populateSelect(arrBrackets, 'bracket_select');  
     
   document.getElementById('track_finish').innerHTML = getOptionSel(arrHardwareFinish, 'hardwarefinish_select');  

   document.getElementById('rods_finish').innerHTML = getOptionSel(arrHardwareFinish, 'hardwarefinish_select');  
     
   document.getElementById('track_amount').innerHTML =  (parseFloat(getOptionMisc("Track"))).toFixed(2);  
     
   document.getElementById('rods_amount').innerHTML =  (parseFloat(getOptionMisc("Rods"))).toFixed(2);     
}  

// populate a drop down list from the array of options  
function populateSelect(arrOptions, selectID)  
{  
   // get the select list  
   var oSelect = document.getElementById(selectID);  

   for(var i=0; i<arrOptions.length; i++)  
   {  
      // create a new select list item  
      var oOption = document.createElement('option');  
      oOption.text = arrOptions[i][0];  
      oOption.value = arrOptions[i][0];  
        
      // add the option to the select list  
      oSelect.options.add(oOption);  
   }  
}  

// get the price for an option based on the array and the select drop down list item  
function getOptionPrice(arrOptions, selectID)  
{  
   var oSelect = document.getElementById(selectID);  
   var selOption = oSelect.options[oSelect.selectedIndex].value;  
     
   for(var i=0; i<arrOptions.length; i++)  
   {  
      if(arrOptions[i][0] == selOption)  
      {  
         return arrOptions[i][1];  
      }  
   }  
}  

// get a misc item price  
function getOptionMisc(selOption)  
{  
   for(var i=0; i<arrMisc.length; i++)  
   {  
      if(arrMisc[i][0] == selOption)  
      {  
         return arrMisc[i][1];  
      }  
   }  
}  


// get the drop down list item selected  
function getOptionSel(arrOptions, selectID)  
{  
   var oSelect = document.getElementById(selectID);  
   var selOption = oSelect.options[oSelect.selectedIndex].value;  
     
   for(var i=0; i<arrOptions.length; i++)  
   {  
      if(arrOptions[i][0] == selOption)  
      {  
         return arrOptions[i][0];  
      }  
   }  
}  


function calcAmount()  
{  
   // sanitize fields  
   makeInt(document.getElementById('measurement_piece'));  
   makeInt(document.getElementById('measurement_ft'));     
   makeInt(document.getElementById('measurement_in'));     
     
   makeInt(document.getElementById('wood_piece'));        
   makeInt(document.getElementById('woodfinish_piece'));        
   makeInt(document.getElementById('hardwarefinish_piece'));        
   makeInt(document.getElementById('topfixtures_piece'));     
   makeInt(document.getElementById('bottomfixtures_piece'));     
   makeInt(document.getElementById('curve_piece'));     
     
   // measurements  
   var measurementPerItem = 0;  

   $ladderbase = getOptionMisc("Ladderbase");  
   
   if(parseFloat(document.getElementById('measurement_ft').value) > 14)
   {
   	document.getElementById('measurement_ft').value = 14;
   	alert("The ladder height cannot exceed 14ft");
   }
   
   if((parseFloat(document.getElementById('measurement_ft').value) == 14) && (parseFloat(document.getElementById('measurement_in').value) > 0))
   {
   	document.getElementById('measurement_ft').value = 14;
   	alert("The ladder height cannot exceed 14ft");
   }   

   if(parseFloat(document.getElementById('measurement_ft').value) < 9)  
   {  
      measurementPerItem = parseFloat($ladderbase);  
   }  
   else  
   {  
      measurementPerItem = (parseFloat(document.getElementById('measurement_ft').value)-8)*50 + parseFloat($ladderbase);  
   }  
     
     
   document.getElementById('measurement_amount').innerHTML = (measurementPerItem).toFixed(2);  
   
   measurementTotal = (parseFloat(document.getElementById('measurement_piece').value) * measurementPerItem);
   
   document.getElementById('measurement_total').innerHTML = measurementTotal.toFixed(2);     
//   document.getElementById('crating_total').innerHTML = Math.ceil(parseFloat(document.getElementById('measurement_total').innerHTML) * 20) / 100;  
     
     
   cratingTotal = (Math.ceil(parseFloat(document.getElementById('measurement_total').innerHTML) * 20) / 100)
   document.getElementById('crating_total').innerHTML = cratingTotal.toFixed(2);  

     
   // wood  
   woodPrice = getOptionPrice(arrWood, 'wood_select');          
   document.getElementById('wood_amount').innerHTML = (parseFloat(woodPrice)).toFixed(2);    
   woodTotal = (parseFloat(document.getElementById('wood_piece').value) * parseFloat(woodPrice));   
   document.getElementById('wood_total').innerHTML = woodTotal.toFixed(2);     
     
   // wood finish  
   $woodfinishPrice = getOptionPrice(arrWoodFinish, 'woodfinish_select');  
     
   if($woodfinishPrice > 0)
   {
	  $woodfinishPrice = (Math.ceil(parseFloat(document.getElementById('measurement_total').innerHTML) * 20) / 100).toFixed(2);  
   }
   
   document.getElementById('woodfinish_amount').innerHTML = (parseFloat($woodfinishPrice)).toFixed(2);  
   woodFinishTotal = (parseFloat(document.getElementById('woodfinish_piece').value) * parseFloat($woodfinishPrice));
   document.getElementById('woodfinish_total').innerHTML = woodFinishTotal.toFixed(2);     
     
   // hardware finish  
   hardwarefinishPrice = getOptionPrice(arrHardwareFinish, 'hardwarefinish_select');  
     
   document.getElementById('hardwarefinish_amount').innerHTML = (parseFloat(hardwarefinishPrice)).toFixed(2);  
   hardwareFinishTotal = (parseFloat(document.getElementById('hardwarefinish_piece').value) * parseFloat(hardwarefinishPrice));
   document.getElementById('hardwarefinish_total').innerHTML = hardwareFinishTotal.toFixed(2);     

   // top fixture  
   topfixturesPrice = getOptionPrice(arrTopFixture, 'topfixtures_select');  
     
   document.getElementById('topfixtures_amount').innerHTML = (parseFloat(topfixturesPrice)).toFixed(2);  
   topfixturesTotal = (parseFloat(document.getElementById('topfixtures_piece').value) * parseFloat(topfixturesPrice));
   document.getElementById('topfixtures_total').innerHTML = topfixturesTotal.toFixed(2);     

   // bottom fixture  
   bottomfixturesPrice = getOptionPrice(arrBottomFixture, 'bottomfixtures_select');       
   document.getElementById('bottomfixtures_amount').innerHTML = (parseFloat(bottomfixturesPrice)).toFixed(2);  
   bottomfixturesTotal = (parseFloat(document.getElementById('bottomfixtures_piece').value) * parseFloat(bottomfixturesPrice));
   document.getElementById('bottomfixtures_total').innerHTML = bottomfixturesTotal.toFixed(2);     
     
   // curve  
   curvePrice = getOptionPrice(arrCurve, 'curve_select');  
     
   document.getElementById('curve_amount').innerHTML = (parseFloat(curvePrice)).toFixed(2);  
   curveTotal = (parseFloat(document.getElementById('curve_piece').value) * parseFloat(curvePrice));
   document.getElementById('curve_total').innerHTML = curveTotal.toFixed(2);     
        
   // brackets  
   // $bracketPrice = getOptionPrice(arrBrackets, 'bracket_select');  
     
   document.getElementById('bracket_amount').innerHTML = (0).toFixed(2);//parseFloat($bracketPrice);  
   bracketTotal = 0;
   document.getElementById('bracket_total').innerHTML = (0).toFixed(2);//parseFloat(document.getElementById('bracket_piece').value) * parseFloat($bracketPrice);     
     
   // tracks     
   document.getElementById('track_finish').innerHTML = getOptionSel(arrHardwareFinish, 'hardwarefinish_select');  
   trackTotal = (parseFloat(document.getElementById('track_piece').value) * document.getElementById('track_amount').innerHTML);
   document.getElementById('track_total').innerHTML = trackTotal.toFixed(2);     
     
   // rods  
   document.getElementById('rods_finish').innerHTML = getOptionSel(arrHardwareFinish, 'hardwarefinish_select');  
   rodsTotal = (parseFloat(document.getElementById('rods_piece').value) * document.getElementById('rods_amount').innerHTML);
   document.getElementById('rods_total').innerHTML = rodsTotal.toFixed(2);     

   // sub-total   
   document.getElementById('sub_total').innerHTML =   
      (measurementTotal +  
      cratingTotal +  
      woodTotal +        
      woodFinishTotal +              
      hardwareFinishTotal +                    
      topfixturesTotal +                          
      bottomfixturesTotal +                                
      curveTotal +                                      
      bracketTotal +                                            
      trackTotal +                                                  
      rodsTotal).toFixed(2);                                                        
     
   // total  
   document.getElementById('total_cost').innerHTML = document.getElementById('sub_total').innerHTML;  
}  

// ajax code  
// declare a global  XMLHTTP Request object  

var XmlHttpObj;  

// create an instance of XMLHTTPRequest Object, varies with browser type, try for IE first then Mozilla  
function CreateXmlHttpObj()  
{  
   XmlHttpObj = null;  
     
   try  
   {  
      XmlHttpObj = new XMLHttpRequest();   
   }  
   catch(e)  
   {  
      try  
      {  
         XmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");  
      }  
      catch(e)  
      {  
         try  
         {  
            XmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");  
        
         }  
         catch(e)  
         {  
         }  
      }  
   }  

   if(XmlHttpObj == null)  
      alert("Your browser does not support ajax! Please download the latest version of firefox at http://getfirefox.com .");  
}  

   
function loadCSVData(url)  
{  
   CreateXmlHttpObj();  
     
   var d = new Date();                           
     
   // verify XmlHttpObj variable was successfully initialized     
   if(XmlHttpObj)  
   {  
      XmlHttpObj.onreadystatechange = CSVLoaded;  
     
      // define the iteraction with the server -- true for as asynchronous.     
      if(url.indexOf("?") == -1)  
         url += "?a=1";  
        
      url += "&nocache=" + d.getTime();  
        
      XmlHttpObj.open("GET", url,  true);  
     
      // send request to server, null arg  when using "GET"  
      XmlHttpObj.setRequestHeader("X-Requested-With", "XMLHttpRequest");                          
      XmlHttpObj.send(null);  
   }    
}  


function CSVLoaded()  
{  
   // state == 4 indicates receiving response data from server is completed     
   if(XmlHttpObj.readyState == 4)  
   {  
      // To make sure valid response is received from the server, 200 means response received is OK     
      if(XmlHttpObj.status == 200)  
      {  
         parseCSV(XmlHttpObj.responseText);  
      }  
      else  
      {  
         alert("problem retrieving data from the server, status code: "  + XmlHttpObj.status);  
      }  
   }  
}  

   
function parseCSV(sCSV)  
{  
   // reset arrays  

   arrWood = new Array();                  
   arrWoodFinish = new Array();                      
   arrHardwareFinish = new Array();  
   arrTopFixture = new Array();  
   arrBottomFixture = new Array();  
   arrCurve = new Array();  
   arrBrackets = new Array();  
   arrMisc = new Array();  

   // get each line in CSV by splitting on new line      

   var sLines = sCSV.split("\n");  
     
   for(var i=0; i<sLines.length; i++)  
   {  
      // get each column in csv by splitting on ,  
      var sColumns = sLines[i].split(",");  

      // make sure we have 3 columns  
      if(sColumns.length == 3)  
      {  
         // 0 - type  
         // 1 - option  
         // 2 - price  

         // remove spaces  
         sColumns[0] = sColumns[0].replace(' ','');  
         sColumns[1] = sColumns[1].replace(' ','');  
         sColumns[2] = sColumns[2].replace(' ','');                               
           
         switch(sColumns[0].toLowerCase())  
         {  
            case 'wood':                             
               arrWood.push(new Array(sColumns[1], sColumns[2]));  
            break;  
            case 'woodfinish':  
               arrWoodFinish.push(new Array(sColumns[1], sColumns[2]));  
            break;        
            case 'hardwarefinish':  
               arrHardwareFinish.push(new Array(sColumns[1], sColumns[2]));  
            break;   
            case 'topfixture':  
               arrTopFixture.push(new Array(sColumns[1], sColumns[2]));  
            break;                                                                                                                                                                                                        
            case 'bottomfixture':  
               arrBottomFixture.push(new Array(sColumns[1], sColumns[2]));  
            break;  
            case 'hardwarefinish':  
               arrHardwareFinish.push(new Array(sColumns[1], sColumns[2]));  
            break;                                               
            case 'curve':  
               arrCurve.push(new Array(sColumns[1], sColumns[2]));  
            break;                                               
            case 'brackets':  
               arrBrackets.push(new Array(sColumns[1], sColumns[2]));  
            break;                        
            case 'misc':  
               arrMisc.push(new Array(sColumns[1], sColumns[2]));  
            break;                       
         }                                                                
      }  
   }  

   populateSelects();  
   
   document.getElementById('measurement_piece').value = '1';
   document.getElementById('wood_piece').value = '1';
   document.getElementById('woodfinish_piece').value = '1';
   document.getElementById('hardwarefinish_piece').value = '1';
	document.getElementById('topfixtures_piece').value = '1';
   
   document.getElementById('bottomfixtures_piece').value = '1';
   document.getElementById('rods_piece').value = '1';
   //document.getElementById('track_piece').value = '1';
   document.getElementById('curve_piece').value = '1';
   
	calcAmount();     
}  

function makeInt(oEl)  
{  
   oEl.value = oEl.value.replace(/[^0-9]/g, '');  
     
   if(oEl.value == '')  
   {  
      oEl.value = "0";  
   }  
}
