// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function updateProductTotal(product_id) {
    price = getProductPrice(product_id)
    quantity = getProductQuantity(product_id)
    
    totalObj = document.getElementById('total_' + product_id)
    totalObj.innerHTML = price * quantity
}

function updateSubTotal(section_name) {
    subtotalObj = document.getElementById('subtotal_' + section_name)
    subtotal_value = 0
    elements = getElementsByClassName(document, 'span', section_name)
    for(var i=0; i<elements.length; i++){
        oElement = elements[i];      
        price = parseFloat(oElement.innerHTML)
        if (IsNumeric(price)) {
            subtotal_value += price
        }
    }    
    subtotalObj.value = subtotal_value
}

function updateDetailsPageTotal() {
    totalObj = document.getElementById('details_grandtotal')
    total = getTotalSoFar();
    elements = getElementsByClassName(document, 'span', 'details_totals')
    for(var i=0; i<elements.length; i++){
        oElement = elements[i];
        price = parseFloat(oElement.innerHTML)
        total += price
    }    
    totalObj.innerHTML = total
}

function updateAssemblyTotal(assembly_product_id, price) {
    assembly_total_obj = document.getElementById('total_' + assembly_product_id)
    if(parseInt(assembly_total_obj.innerHTML) == parseInt(price)) {
        assembly_total_obj.innerHTML = 0
    } else {
        assembly_total_obj.innerHTML = price
    }
}

function updateDeliveryTotal() {
    // get current value of dropdown, eg. 80
    dropdown_select = document.getElementById('delivery_select')
    selected_price = dropdown_select.value
    
    // set the delivery_total field to the value of the dropdown
    delivery_total =  document.getElementById('delivery_total')
    delivery_total.innerHTML = selected_price
}

function getTotalSoFar() {
    total_so_far = document.getElementById('total_so_far')
    return parseFloat(total_so_far.innerHTML)
}

function getProductPrice(product_id) {
    return document.getElementById('price_' + product_id).innerHTML
}
    
function getProductQuantity(product_id) {
    return document.getElementById('products[' + product_id + ']').value
}

function getElementsByClassName(oElm, strTagName, strClassName){
    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];      
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }   
    }
    return (arrReturnElements)
}    

function IsNumeric(sText) {
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

   for (i = 0; i < sText.length && IsNumber == true; i++) { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) {
            IsNumber = false;
        }
    }
    
    return IsNumber;   
}
