// File Name:     SkytekCommonFunctions.js
// Creation Date: 04.01.2005
// Author:        Adrian POPESCU

// Global Variables

function printWindow() {
bV = parseInt(navigator.appVersion);
if (bV >= 4) window.print();
}

function MM_callJS(jsStr) { //v2.0
  return eval(jsStr)
}

function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

function setFocus(obj_name)
{
    if (document.forms.length > 0)
    {
        var field = document.forms[0];
        for (i = 0; i < field.length; i++)
        {
            if (field.elements[i].name == obj_name)
            {
                document.forms[0].elements[i].focus();
                break;
            }
        }
    }
}

function placeFocus()
{
    if (document.forms.length > 0)
    {
        var field = document.forms[0];
        for (i = 0; i < field.length; i++)
        {
            if ((field.elements[i].type == "text") || (field.elements[i].type == "textarea") || (field.elements[i].type.toString().charAt(0) == "s"))
            {
                document.forms[0].elements[i].focus();
                break;
            }
        }
    }
}

// Start Math functions
function round_decimals(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return pad_with_zeros(result3, decimals)
}

function pad_with_zeros(rounded_value, decimal_places) {

    // Convert the number to a string
    var value_string = rounded_value.toString()
    
    // Locate the decimal point
    var decimal_location = value_string.indexOf(".")

    // Is there a decimal point?
    if (decimal_location == -1) {
        
        // If no, then all decimal places will be padded with 0s
        decimal_part_length = 0
        
        // If decimal_places is greater than zero, tack on a decimal point
        value_string += decimal_places > 0 ? "." : ""
    }
    else {

        // If yes, then only the extra decimal places will be padded with 0s
        decimal_part_length = value_string.length - decimal_location - 1
    }
    
    // Calculate the number of decimal places that need to be padded with 0s
    var pad_total = decimal_places - decimal_part_length
    
    if (pad_total > 0) {
        
        // Pad the string with 0s
        for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0"
        }
    return value_string
}
// *** End *** Math Functions

// *** Start *** Section of Fill Shipping and Billing info functions

function SameAsShipping(frm)
{
        // Autocomplete the Billing addres information if checkbox "Same as Sipping" is checked.
        if(document.forms[0].check_same.checked)
        {
            if(document.forms[0].addr_1.value != "")
            {
                  document.forms[0].addr_5.value = document.forms[0].addr_1.value;
            }
            if(document.forms[0].addr_2.value != "")
            {
                  document.forms[0].addr_6.value = document.forms[0].addr_2.value;
            }
            if(document.forms[0].addr_3.value != "")
            {
                  document.forms[0].addr_7.value = document.forms[0].addr_3.value;
            }
        }

        // Autocomplete the shipping labels with the Shipping address provided by custom.
        // The Shipping information from Shipping Labels is displayed in a new window,
        // It wohn't be autocompleted. To transmit the information the cookies are used,
        // to store it and then reloaded in the new page.

/*        if(document.forms[0].addr_1.value != "")
        {
              document.forms[0].addr_54.value = document.forms[0].addr_1.value;
              document.forms[0].addr_57.value = document.forms[0].addr_1.value;
        }
        if(document.forms[0].addr_2.value != "")
        {
              document.forms[0].addr_55.value = document.forms[0].addr_2.value;
              document.forms[0].addr_58.value = document.forms[0].addr_2.value;
        }
        if(document.forms[0].addr_3.value != "")
        {
              document.forms[0].addr_56.value = document.forms[0].addr_3.value;
              document.forms[0].addr_56.value = document.forms[0].addr_3.value;
        }
*/
}
// *** End *** Section of Fill Shipping and Billing info functions

