﻿function printing_product(id, div_string, id_attr, rows) {
    this.id = id;
    this.div_string = div_string;
    this.id_attr = id_attr;
    this.rows = rows;
}

var products = { bookmarks: new printing_product(1, "Instant Price Quote", "bookmarks", new Array(1,2,3)),
                 business_cards: new printing_product(2, "Instant Price Quote", "business_cards", new Array(1, 2, 3)),
                 catalogs_booklets: new printing_product(3, "Instant Price Quote", "catalogs_booklets", new Array(1, 6, 7, 8, 3)),
                 color_copies: new printing_product(4, "Instant Price Quote", "color_copies", new Array(1, 2, 3)),
                 dvd_case_covers: new printing_product(5, "Instant Price Quote", "dvd_case_covers", new Array(1, 2, 3)),
                 car_door_magnets: new printing_product(8, "Instant Price Quote", "car_door_magnets", new Array(1, 3)),                 
                 flyers: new printing_product(6, "Instant Price Quote", "flyers", new Array(1, 2, 11, 12, 3)),
                 hang_tags: new printing_product(7, "Instant Price Quote", "hang_tags", new Array(1, 2, 3)),
                 magnets: new printing_product(27, "Instant Price Quote", "magnets", new Array(1, 11, 2, 3)),
                 notepads: new printing_product(9, "Instant Price Quote", "notepads", new Array(1, 13, 11, 3)),
                 posters: new printing_product(10, "Instant Price Quote", "posters", new Array(1, 2, 11, 3)),
                 rack_cards: new printing_product(11, "Instant Price Quote", "rack_cards", new Array(1, 2, 3)),
                 sales_data_sheets: new printing_product(12, "Instant Price Quote", "sales_data_sheets", new Array(1, 2, 3)),
                 vinyl_banners: new printing_product(13, "VInstant Price Quote", "vinyl_banners", new Array(1, 2, 3)),
                 brochures: new printing_product(14, "Instant Price Quote", "brochures", new Array(1, 4, 2, 3)),
                 calendars: new printing_product(15, "Instant Price Quote", "calendars", new Array(1, 5, 3)),
                 cd_dvd_sleeves: new printing_product(16, "Instant Price Quote", "cd_dvd_sleeves", new Array(9, 3)),
                 door_hangers: new printing_product(17, "Instant Price Quote", "door_hangers", new Array(1, 2, 3)),
                 envelopes: new printing_product(18, "Instant Price Quote", "envelopes", new Array(1, 2, 10, 3)),
                 greeting_cards: new printing_product(19, "Instant Price Quote", "greeting_cards", new Array(1, 2, 3)),
                 letterheads: new printing_product(20, "Instant Price Quote", "letterheads", new Array(11, 2, 3)),
                 newsletters: new printing_product(21, "Instant Price Quote", "newsletters", new Array(6, 2, 11, 4, 3)),
                 postcards: new printing_product(22, "Instant Price Quote", "postcards", new Array(1, 2, 11, 3)),
                 presentation_folders: new printing_product(23, "Instant Price Quote", "presentation_folders", new Array(1, 2, 3)),
                 rolodex_cards: new printing_product(24, "Instant Price Quote", "rolodex_cards", new Array(1, 2, 11, 3)),
                 stickers: new printing_product(25, "Instant Price Quote", "stickers", new Array(1, 2, 3)),
                 vinyl_bumper_stickers: new printing_product(26, "Instant Price Quote", "vinyl_bumper_stickers", new Array(1, 2, 14, 11, 3))};


function printing_products_row(id, row_text, select_id) {
    this.id = id;
    this.row_text = row_text;
    this.select_id = select_id;
}

var product_rows = { 1: new printing_products_row(1, "Description:", "select_description"),
                     2: new printing_products_row(2, "Color:", "select_colors"),
                     3: new printing_products_row(3, "Quantity:", "select_quantity"),
                     4: new printing_products_row(4, "Fold:", "select_fold"),
                     5: new printing_products_row(5, "Hole:", "select_hole"),
                     6: new printing_products_row(6, "Pages:", "select_pages"),
                     7: new printing_products_row(7, "Cover Paper:", "select_cover_paper"),
                     8: new printing_products_row(8, "Inside Color:", "select_inside_color"),
                     9: new printing_products_row(9, "Style:", "select_style"),
                   	 10: new printing_products_row(10, "Window:", "select_window"),
                   	 11: new printing_products_row(11, "Paper:", "select_paper"),
                   	 12: new printing_products_row(12, "Coating:", "select_coating"),
					 13: new printing_products_row(13, "Sheets per pad:", "select_sheets"),
					 14: new printing_products_row(14, "Full Bleed:", "select_full_bleed")}


var product_visible = null;
var prices = null;
var table_product = document.createElement("table");
table_product.id = "tbl_product";;

$(document).ready(function() {
    $("#print_products > a").bind("click", printing_product_click);
});


/** TODO: Unbind handler for previously clicked product **/
function printing_product_click(e){
    //$("#print_ajax").html("<p><span style=\"background-color:green\">Business Cards clicked</span></p>");
    var id_attr = e.target.id
    if (product_visible == null || product_visible.id != products[id_attr].id) {
        product_visible = products[e.target.id];
        $("#pricediv").html(products[id_attr].div_string);

        /** delete old rows **/
        while (table_product.rows.length > 0) {
            table_product.deleteRow(0);
        }

        /** Add Rows **/
        var i;
        for (i = 0; i < products[id_attr].rows.length; i++) {
            create_product_row(table_product.insertRow(-1), products[id_attr].rows[i], products[id_attr].id, (i == 0? false : true));
        }
        
        /** Add Price row **/
        var tbl_row = table_product.insertRow(-1);
        var tbl_cell = tbl_row.insertCell(-1);
        tbl_cell.innerHTML = "Price:";
        tbl_cell.id = "price_lbl";
        tbl_cell = tbl_row.insertCell(-1);
        tbl_cell.innerHTML = "";
        tbl_cell.id = "price";
        
        /** add table to document if it hasn't been added **/
        if(!(document.getElementById("tbl_product"))) {
            document.getElementById("print_ajax").appendChild(table_product);
        }

        var j = 0;
        for (i = 0; i < products[id_attr].rows.length; i++) {
            j = products[id_attr].rows[i]
            if (j != 3) {
                $("#" + product_rows[j].select_id).bind("change", select_change);
            }
            else {
                $("#" + product_rows[3].select_id).bind("change", function() {
                    $("#price").html("$" + prices[document.getElementById("select_quantity").value]);
                    $("#price_lbl").html("<form name=\"paynowform\" action=\"https://www.paypal.com/cgi-bin/webscr\" method=\"post\">"
                                         + "<input type=\"image\" name=\"submit\" border=\"0\" src=\"https://static.e-junkie.com/sslpic/22080.08ca69f538617e696a31dc3b3d66fe19.jpg\" alt=\"PayPal - The safer, easier way to pay online\" id=\"paypal_paynow_bttn\" onclick=\"paynow_click(this)\">"
                                         + "<input type=\"hidden\" name=\"cmd\" value=\"_xclick\">"
                                         + " <input type=\"hidden\" name=\"business\" value=\"payments@cogzdesigns.com\">"
                                         + " <input type=\"hidden\" name=\"currency_code\" value=\"USD\">"
                                         + " <input type=\"hidden\" name=\"return\" value=\"http://www.cogzdesigns.com/payment_success.html\">"
                                         + " <input type=\"hidden\" name=\"cancel_return\" value=\"http://www.cogzdesigns.com/cancel_order.html\">"
                                         + " <input type=\"hidden\" name=\"lc\" value=\"US\">"
                                         + " <input type=\"hidden\" name=\"item_name\" value=\"\">"
                                         + " <input type=\"hidden\" name=\"amount\" value=\"\">"
                                         + " <input type=\"hidden\" name=\"quantity\" value=\"\">"
                                         + "</form>");
                });
            }
        }
    }
    /**
    else {
        $("#print_ajax").html("<p>Already showing Business Cards</p>");
    }
    **/
}

function create_product_row(tbl_row, product_row_id, product_id, select_disabled) {
    var row_id = product_rows[product_row_id].id;
    var row_text = product_rows[product_row_id].row_text;
    var select_id = product_rows[product_row_id].select_id;    

    var tbl_cell = null;
    var sel = null;
    var opt = null;

    tbl_cell = tbl_row.insertCell(-1);
    tbl_cell.innerHTML = row_text;

    sel = document.createElement("select");
    sel.id = select_id;

    opt = document.createElement("option");
    opt.selected = true;
    opt.innerHTML = "";
    sel.appendChild(opt);
    //sel.add(opt, null);   IE doesn't understand this

    if (select_disabled) {
        sel.disabled = select_disabled;
    }
    else {
        jQuery.post("purchase.php",
                    { req_type: product_id, var0: row_id},
                    function(data) {
                        var options = data.split(/##/);

                        //data string ends with '##' so the last element of options is an empty string
                        for (var i = 0; i < options.length - 1; i++) {
                            if(options[i].length > 0){
                                opt = document.createElement("option");
                                opt.innerHTML = options[i];
                                sel.appendChild(opt);
                            }
                        }
                    },
                    "text");
    }
    tbl_cell = tbl_row.insertCell(-1);
    tbl_cell.appendChild(sel);
}


function select_change() {
    var k;
    var post_data = { req_type: product_visible.id };
    for (k = 1; k < product_visible.rows.length; k++) {
        post_data["var" + k] = product_rows[product_visible.rows[k - 1]].id;
        post_data["var" + k + "val"] = document.getElementById(product_rows[product_visible.rows[k - 1]].select_id).value
        post_data["vars"] = k;

        if (this.id == product_rows[product_visible.rows[k - 1]].select_id) {
            break;
        }
    }
    

    var next_sel = document.getElementById(product_rows[product_visible.rows[k]].select_id)
    if (next_sel.disabled) {
        next_sel.disabled = false;
    }
    else {
        while (next_sel.options.length > 1) {
            next_sel.remove(1);
        }
        next_sel.selectedIndex = 0;

        //reset rows below changed row if any exist
        var l;
        var after_next_sel;
        for (l = k + 1; l < product_visible.rows.length; l++) {
            after_next_sel = document.getElementById(product_rows[product_visible.rows[l]].select_id)
            if (after_next_sel.disabled) {
                break;
            }
            else {
                while (after_next_sel.options.length > 1) {
                    after_next_sel.remove(1);
                }
                after_next_sel.selectedIndex = 0;
            }
            
            //$("#price").html("");
        }

        $("#price").html("");
        $("#price_lbl").html("Price:");
    }

    post_data["var0"] = product_rows[product_visible.rows[k]].id;
    var ajax_settings = { type: 'POST',
                          url: "purchase.php",
                          data: post_data,
                          dataType: "text"};

    if(product_rows[product_visible.rows[k]].id != 3)
    {
        ajax_settings.success = function(data) {
            var options = data.split(/##/);
            var opt;

            //data string ends with '##' so the last element of options is an empty string
            for (var i = 0; i < options.length - 1; i++) {
                opt = document.createElement("option");
                opt.innerHTML = options[i];                
                next_sel.appendChild(opt);
            }
         };
    }
    else {
        ajax_settings.success = function(data) {
            var options = data.split(/##/);
            var opt;
            var q_and_p;

            prices = {};
            //data string ends with '##' so the last element of options is an empty string
            for (var i = 0; i < options.length - 1; i++) {
                q_and_p = options[i].split(/~~/);
                prices[q_and_p[0]] = q_and_p[1];

                opt = document.createElement("option");
                opt.innerHTML = q_and_p[0];
                next_sel.appendChild(opt);
            }
         }
    }
    
    jQuery.ajax(ajax_settings);
}


function paynow_click(e) {
    document.paynowform.item_name.value = document.getElementById(product_visible.id_attr).innerHTML + ' - ';
    var i, j;
    for (i = 0; i < products[product_visible.id_attr].rows.length; i++) {
        j = products[product_visible.id_attr].rows[i];

        //if (j != 3) {
            if (i == 0) {
                document.paynowform.item_name.value += document.getElementById(product_rows[j].select_id).value;
            }
            else {
                document.paynowform.item_name.value += ", " + document.getElementById(product_rows[j].select_id).value;
            }
        /*}
        else {  //quantity
            //document.paynowform.quantity.value = document.getElementById(product_rows[j].select_id).value;
            
        }*/
    }

    document.paynowform.quantity.value = 1;

    document.paynowform.amount.value = document.getElementById("price").innerHTML
    //alert(document.paynowform.item_name.value + ", " + document.paynowform.amount.value + ", " + document.getElementById("price").innerHTML);
    document.paynowform.submit();
}

