// This global array holds all items that are currently in the database
// Each item is a database_record object
var database_records = new Array();

// This global variable tracks the total number of items ordered
var total_ordered = 0;

// This global variable holds the personal details about the user
var current_user;
var logged_in = 0;

// These global variables set the names of your pages
//var store_page = "products.html";
//var order_empty_page =  "empty_shopping_cart.html";
//var shopping_cart_page = "shopping_cart.html";

// These global variables set the tax rate and shipping charge per item
//var tax_rate = 0.05;
//var shipping_charge = 5;

// function database_record(description, number, price, quantity)
//
//     id             A numeric id for the item (unique).
//     description    A description of the item.
//     number         The model number (unique).
//     price          The price of the item.
//     quantity       The quantity of the item that the user has ordered.
//
// This function creates a new database_record object.
function database_record(link, description, number, order_info, price, quantity) {    
    this.link = link;
    this.description = description;
    this.number = number;
    this.order_info = order_info;
    this.price = price;
    this.quantity = quantity;
}

// function make_database()
//
// This function initializes the database_records array
function make_database() {

    // Create the default records
    var record_index = 0;
    database_records[record_index] = 
        new database_record("webrelay/",
                            "WebRelay~(5VDC, Opto-Input Range 3-12VDC)", 
                            "X-WR-1R12-1I5-5",
                            "WebRelay (X-WR-1R12-1I5-5)", 
                            108.95, 
                            0);
    record_index++;
    database_records[record_index] = 
        new database_record("webrelay/",
                            "WebRelay Industrial~(9-28VDC, Opto-Input Range 3-12VDC)", 
                            "X-WR-1R12-1I5-I",
                            "WebRelay, Industrial (X-WR-1R12-1I5-I)", 
                            128.95, 
                            0);
    record_index++;
    database_records[record_index] = 
        new database_record("webrelay/",
                            "WebRelay Industrial HI~(9-28VDC, Opto-Input Range 11-28VDC)", 
                            "X-WR-1R12-1I24-I",
                            "WebRelay, Industrial w/11-28V Inputs (X-WR-1R12-1I24-I)", 
                            128.95, 
                            0);
    record_index++;
    database_records[record_index] = 
        new database_record("webrelay/",
                            "WebRelay POE~(Power Over Ethernet, Opto-Input ~Range 3-12VDC)", 
                            "X-WR-1R12-1I5-E",
                            "WebRelay, Power Over Ethernet (X-WR-1R12-1I5-E)", 
                            133.95, 
                            0);
    record_index++;
    database_records[record_index] = 
        new database_record("webrelay-quad/",
                            "WebRelay-Quad (5VDC)", 
                            "X-WR-4R1-5",
                            "WebRelay-Quad (X-WR-4R1-5)", 
                            113.95, 
                            0);
    record_index++;
    database_records[record_index] = 
        new database_record("webrelay-quad/",
                            "WebRelay-Quad Industrial (9-28VDC)", 
                            "X-WR-4R1-I",
                            "WebRelay-Quad, Industrial (X-WR-4R1-I)", 
                            133.95,  
                            0);
    record_index++;
    database_records[record_index] = 
        new database_record("webrelay-quad/",
                            "WebRelay-Quad POE (Power Over Ethernet)", 
                            "X-WR-4R1-E",
                            "WebRelay-Quad, Power Over Ethernet (X-WR-4R1-E)", 
                            138.95,  
                            0);
    
    record_index++;
    database_records[record_index] = 
        new database_record("webrelay-dual/",
                            "WebRelay-Dual with Real-Time Clock (5VDC)", 
                            "X-WR-2R1-2I5-5",
                            "WebRelay-Dual with Real-Time Clock (X-WR-2R1-2I5-5)", 
                            133.95, 
                            0);
    
    record_index++;
    database_records[record_index] = 
        new database_record("webrelay-dual/",
                            "WebRelay-Dual Industrial with Real-Time Clock~(9-28VDC)", 
                            "X-WR-2R1-2I5-I",
                            "WebRelay-Dual with Real-Time Clock, Industrial (X-WR-2R1-2I5-I)", 
                            153.95, 
                            0);
    
    record_index++;
    database_records[record_index] = 
        new database_record("webrelay-dual/",
                            "WebRelay-Dual Industrial HI with Real-Time Clock~(9-28VDC, 11-28V Inputs)", 
                            "X-WR-2R1-2I24-I",
                            "WebRelay-Dual with Real-Time Clock, Industrial w/11-28V Inputs (X-WR-2R1-2I24-I)", 
                            153.95, 
                            0);
    
    record_index++;
    database_records[record_index] = 
        new database_record("webrelay-dual/",
                            "WebRelay-Dual POE with Real-Time Clock POE~(Power Over Ethernet)", 
                            "X-WR-2R1-2I5-E",
                            "WebRelay-Dual with Real-Time Clock, Power Over Ethernet (X-WR-2R1-2I5-E)", 
                            158.95, 
                            0);
                            
    record_index++;
    database_records[record_index] = 
        new database_record("five-input/",
                            "Five-Input Module (5VDC)", 
                            "X-DAQ-5I5-5",
                            "Five-Input Module (X-DAQ-5I5-5)", 
                            115.95, 
                            0);
    
    record_index++;
    database_records[record_index] = 
        new database_record("five-input/",
                            "Five-Input Module Industrial (9-28VDC)", 
                            "X-DAQ-5I5-I",
                            "Five-Input Module, Industrial (X-DAQ-5I5-I)", 
                            135.95, 
                            0);
    
    record_index++;
    database_records[record_index] = 
        new database_record("five-input/",
                            "Five-Input Module Industrial w/11-28V Inputs~(9-28VDC)", 
                            "X-DAQ-5I24-I",
                            "Five-Input Module (Industrial w/11-28VDC Inputs) (X-DAQ-5I24-I)", 
                            135.95, 
                            0);
    
    record_index++;
    database_records[record_index] = 
        new database_record("five-input/",
                            "Five-Input Module POE (Power Over Ethernet)", 
                            "X-DAQ-5I5-E",
                            "Five-Input Module, Power Over Ethernet (X-DAQ-5I5-E)", 
                            140.95, 
                            0);  
                            
    record_index++;
    database_records[record_index] = 
        new database_record("temperature/",
                            "Temperature Module (5VDC)", 
                            "X-DAQ-2R1-4T-5",
                            "DAQ-Series Temperature Module (X-DAQ-2R1-4T-5)", 
                            133.95, 
                            0);                                                     
    
    record_index++;
    database_records[record_index] = 
        new database_record("temperature/",
                            "Temperature Module Industrial (9-28VDC)", 
                            "X-DAQ-2R1-4T-I",
                            "DAQ-Series Temperature Module, Industrial (X-DAQ-2R1-4T-I)", 
                            153.95, 
                            0);  
                            
    record_index++;
    database_records[record_index] = 
        new database_record("temperature/",
                            "Temperature Module POE (Power Over Ethernet)", 
                            "X-DAQ-2R1-4T-E",
                            "DAQ-Series Temperature Module, Power Over Ethernet (X-DAQ-2R1-4T-E)", 
                            158.95, 
                            0);    
                            
    record_index++;
    database_records[record_index] = 
        new database_record("analog/",
                            "Analog Module (6VDC)", 
                            "X-DAQ-8A5-6",
                            "DAQ-Series 8-Channel Analog to Digital Converter (X-DAQ-8A5-6)", 
                            249.95, 
                            0);
                            
    record_index++;
    database_records[record_index] = 
        new database_record("analog/",
                            "Analog Module Industrial (9-28VDC)", 
                            "X-DAQ-8A5-I",
                            "DAQ-Series 8-Channel Analog to Digital Converter, Industrial (X-DAQ-8A5-I)", 
                            269.95, 
                            0);  
                            
    record_index++;
    database_records[record_index] = 
        new database_record("analog/",
                            "Analog Module POE (Power Over Ethernet)", 
                            "X-DAQ-8A5-E",
                            "DAQ-Series 8-Channel Analog to Digital Converter, Power Over Ethernet (X-DAQ-8A5-E)", 
                            289.95, 
                            0); 
                            
    record_index++;
    database_records[record_index] = 
        new database_record("webswitch/",
                            "WebSwitch (USA Outlets)", 
                            "XRDI-WS1",
                            "WebSwitch (XRDI-WS1)", 
                            195.95, 
                            0);                              
                            
                                                        
    record_index++;
    database_records[record_index] = 
        new database_record("products.html#accessories",
                            "Power Supply, Regulated, 5VDC, 2Amp (100-240VAC Input)", 
                            "PS5VW2-B",
                            "Power Supply, Regulated, 5VDC, 2Amp (PS5VW2-B)", 
                            19.95, 
                            0); 
   
    record_index++;
    database_records[record_index] = 
        new database_record("products.html#accessories",
                            "Single Port POE Power Injector", 
                            "PSA16U-480",
                            "Power Over Ethernet Power Injector (Single Port)", 
                            39.95, 
                            0);                              
                            
    record_index++;
    database_records[record_index] = 
        new database_record("products.html#accessories",
                            "Ethernet Cable, Cat-5E, 5ft, Blue", 
                            "UTP5-5-BLUE",
                            "Ethernet Cable (5 foot)", 
                            3.95, 
                            0); 
                            
    record_index++;
    database_records[record_index] = 
        new database_record("products.html#accessories",
                            "Temperature Sensor, Digital Temperature Sensor", 
                            "X-DTS-U",
                            "Temperature Sensor (X-DTS-U)", 
                            9.99, 
                            0);  
    record_index++;
    database_records[record_index] = 
        new database_record("webrelay-10/",
                            "WebRelay-10 Industrial (10-36VDC)", 
                            "X-WR-10R12-I",
                            "WebRelay-10, Industrial (X-WR-10R12-I)", 
                            459.95,  
                            0);  
                            
    record_index++;
    database_records[record_index] = 
        new database_record("x300/",
                            "X-300 Advanced Temperature Module (9-28VDC)", 
                            "X-300-I",
                            "X-300 Series Advanced Temperature Module, Industrial (X-300-I)", 
                            229.95, 
                            0);  
                            
    record_index++;
    database_records[record_index] = 
        new database_record("x300/",
                            "X-300 Advanced Temperature Module (9-28VDC) with Power Supply", 
                            "X-300-I+PS12-A",
                            "X-300 Series Advanced Temperature Module, Industrial, with Power Supply (X-300-I+PS12-A)", 
                            239.95, 
                            0);   
                            
    record_index++;
    database_records[record_index] = 
        new database_record("products.html#accessories",
                            "Power Supply, Regulated, 12VDC, 1.5Amp (100-240VAC Input)", 
                            "PS12VW1.5-B",
                            "Power Supply, Regulated, 12VDC, 1.5Amp (PS12VW1.5-B)", 
                            19.95, 
                            0); 
    
    record_index++;
    database_records[record_index] = 
        new database_record("x301/",
                            "Series III WebRelay-Dual, 9-28VDC Power Supply Input, 3-11V Opto-Inputs", 
                            "X-301-I",
                            "Series III WebRelay-Dual, 9-28VDC Power Supply Input, 3-11V Opto-Inputs (X-301-I)", 
                            199.95, 
                            0); 
                            
    record_index++;
    database_records[record_index] = 
        new database_record("x301/",
                            "Series III WebRelay-Dual, 9-28VDC Power Supply Input, 11-28V Opto-Inputs", 
                            "X-301-24I",
                            "Series III WebRelay-Dual, 9-28VDC Power Supply Input, 11-28V Opto-Inputs (X-301-24I)", 
                            199.95,
                            0);
    record_index++;
    database_records[record_index] =
        new database_record("products.html#accessories",
                            "EU Connector Prongs",
                            "PSB-EU",
                            "Power Supply Prongs, Europe (PSB-EU)",
                            1.00,
                            0);
    record_index++;
    database_records[record_index] =
        new database_record("products.html#accessories",
                            "UK Connector Prongs",
                            "PSB-UK",
                            "Power Supply Prongs, United Kingdom (PSB-UK)",
                            1.00,
                            0);
    record_index++;
    database_records[record_index] =
        new database_record("products.html#accessories",
                            "SAA Connector Prongs",
                            "PSB-SAA",
                            "Power Supply Prongs, Australia (PSB-SAA)",
                            1.00,
                            0);   

    
    // Load the orders from the cookie
    load_orders();    
}

// function load_orders()
//
// This function reads the order quantities from the order
// cookie and loads them into the database_records array.
function load_orders() {

    // Get the orders cookie
    var orders_cookie = get_cookie("orders");

    // If the cookie doesn't exist, return
    if (!orders_cookie) {
        return;
    }

    // Split all the orders into an array
    var user_orders = orders_cookie.split("+");

    //alert("user_orders.length = " + user_orders.length);

    // Run through the orders
    for (var counter = 0; counter < user_orders.length; counter++) {
    
        // Store the quantity
        var order_quantity = user_orders[counter];
        
        // Update the database
        database_records[counter].quantity = order_quantity;

        // Update the number of items that are in the shopping cart
        if (order_quantity > 0) {
            total_ordered++;
        }
    }
}

// function save_orders()
//
// This function saves the orders in the database_records 
// array to a cookie named "orders".
function save_orders() {

    // This variable holds the string that's saved to the cookie
    var order_string = "";
    
    // Store the total number of records in the database
    var total_records = database_records.length;
    
    // Run through the database array
    for (var counter = 0; counter < total_records; counter++) {

        // Add the quantity to the cookie string
        order_string += database_records[counter].quantity;

        // If this isn't the last item, add the order delimiter (+)
        if (counter < total_records - 1) {
            order_string += "+";
        }
    }
    
    // Save the orders to a per-session cookie
    set_cookie("orders", order_string);
}


// This function creates a new user_data object
function user_data(email, password,
                    firstName_s,lastName_s,title_s,company_s,address1_s,address2_s,city_s,state_s,postalCode_s,country_s,phone_s,fax_s,email_s,
                    name_b,company_b,address1_b,address2_b,city_b,state_b,postalCode_b,country_b,phone_b,
                    hear_about_us, email_list) {
    // account info
    this.email = email;
    this.password = password;
    
    // shipping info
    this.firstName_s = firstName_s;
    this.lastName_s = lastName_s;
    this.title_s = title_s;
    this.company_s = company_s;
    this.address1_s = address1_s;
    this.address2_s = address2_s;
    this.city_s = city_s;
    this.state_s = state_s;
    this.postalCode_s = postalCode_s;
    this.country_s = country_s;
    this.phone_s = phone_s;
    this.fax_s = fax_s;
    this.email_s = email_s;
    
    // billing info
    this.name_b = name_b;
    this.company_b = company_b;
    this.address1_b = address1_b;
    this.address2_b = address2_b;
    this.city_b = city_b;
    this.state_b = state_b;
    this.postalCode_b = postalCode_b;
    this.country_b = country_b;
    this.phone_b = phone_b;
    
    // other info
    this.hear_about_us = hear_about_us;
    this.email_list = email_list;
    
}

// function save_personal_details(current_form)
//
//
// This function saves the data in current_form 
// to a cookie named "personal_details".
// The cookie is a collection of pseudo-cookies:
//     - Each pseudo-cookie is separated with a colon (:)
//     - Each pseudo-cookie name and value is separated by a plus sign (+)
function save_personal_details(pages_back) {

    // This variable holds the string that's saved to the cookie
    var cookie_string = ""
    
    // Run through the form
    for (var counter = 0; counter < document.user_information.length; counter++) {

        // Is the field a Text object?
        if (document.user_information[counter].type == "text") {
        
            // If so, add the field name and value to the pseudo-cookie
            cookie_string += document.user_information[counter].name + ":" +
                             escape(document.user_information[counter].value) + "&"
        
    }
        // Is the field a Select (single) object?
        else if (document.user_information[counter].type == "select-one") {
        
            // If so, add the field name and the text 
            // of the selected option to the pseudo-cookie
            var selected_option = document.user_information[counter].options[document.user_information[counter].selectedIndex].text
            cookie_string += document.user_information[counter].name + ":" +
                             escape(selected_option) + "&"
        }
    }
    
    // Remove the last delimiter
    cookie_string = cookie_string.substring(0, cookie_string.length - 1)

    // Save the personal_details to a temporary cookie
    set_cookie("personal_details", cookie_string);
    
    history.go(pages_back);
}

// function load_personal_details()
//
// This function reads the personal_details cookie and 
// loads the data into the global current_user array. 
function load_personal_details() {
    
    // Create a new, empty, user_data object
    current_user = new user_data("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "")

    // Get the personal_details cookie
    var user_cookie = get_cookie("personal_details")

    // If it didn't exist, return
    if (!user_cookie) {
        return -1;
    }

    // Split the cookie into an array of pseudo-cookies
    var pseudo_cookies = user_cookie.split("&")
    
    // Run through the pseudo-cookies
    for (var counter = 0; counter < pseudo_cookies.length; counter++) {

        // Split the pseudo-cookie into a name:value pair
        var pseudo_cookie_pair = pseudo_cookies[counter].split(":")
        
        // Store the array items
        var property_name = pseudo_cookie_pair[0]
        var property_value = pseudo_cookie_pair[1]
        
        // Update the corresponding user_data object property
        current_user[property_name] = unescape(property_value)
    }
    
    return 1;
}

function fill_in_user_info() {

    // This variable holds the string that's saved to the cookie
    var cookie_string = ""
    
    // Run through the form
    for (var counter = 0; counter < document.user_information.length; counter++) {

        // Is the field a Text object?
        if (document.user_information[counter].type == "text") {
        
            // If so find the users info that matches the field name and stuff it in the text field
            document.user_information[counter].value = current_user[document.user_information[counter].name];
            
        }
        
    }
      
}

// this function deletes the cookie with the user' information and returns
// to the main page
function logout(new_location){
    delete_cookie("personal_details");
    location = new_location;
}

function display_database() {
    
    var alert_string = "";
    for (var counter = 0; counter < database_records.length; counter++) {
        alert_string += database_records[counter].description + "\n";
        alert_string += database_records[counter].number + "\n";
        alert_string += database_records[counter].quantity + "\n";
        alert_string += database_records[counter].price + "\n\n";
    }
    alert(alert_string);
}

function get_login_status() {
    
    // look for login cookie
    var login_cookie = get_cookie("dbakc");

    // If the cookie doesn't exist, return
    if (!login_cookie) {
        return 0;
    }
    else {
        return 1;
    }
}