/**
 * jquery_api_quote.js - JavaScript Functions.
 * @package jquery_api_quote.js
 * @copyright 2005 - 2008 OSD
 * Functions for quote module
*/

$(document).ready(function() { 
    var debug = false;
    var temp_data = "";
    var s_path = window.location.pathname;
    var s_path_lower = s_path.toLowerCase();
    var s_page_raw = s_path.substring((s_path.lastIndexOf('/') + 1), s_path.lastIndexOf('.')).toLowerCase();
    var s_page = s_page_raw.toLowerCase();
    //alert("s_path:"+s_path+"\ns_path_lower:"+s_path_lower+"\ns_page_raw:"+s_page_raw+"\nPage : '"+ s_page+"'");
    if (s_page == "motorassumptions") { 
    	$('#info_1_link').fancyZoom({width:400, height:200});
    }
    if (s_page == "motor1") { 
        $("#ddlCoverRequired").val("01");
    }
    if (s_page == "houseqq") { 
        $("#radHouseUnattended").val("1");
        $("tr#unattended").hide();
    }
    if (s_page == "quoteall") { 
        temp_data = $("#ResultScreenMotor1").html().replace("Next", "'<b>Next</b>'");
        $("#ResultScreenMotor1").html(temp_data);
        $(".GridItem a").text("[Policy Benefits]");
        $(".GridAltItem a").text("[Policy Benefits]");
        $(".GridItem:first").removeClass("GridItem").addClass("GridItemFirst");
        var call_me_url_array = $("#callme_row a").attr("href").split("?");
        var quote_ref_param = call_me_url_array[2].split("=");
        var quote_ref = quote_ref_param[1];
        //$("#test").text("<!-- " + quote_ref_param[1]+" -->");
        // AJAX POST call
        var data_helper = "/data_helper.php";
        $.ajax({ 
                type: "POST",
                url: data_helper,
                cache: false,
                data: { 
        			ref_code: quote_ref,
        			q_type: "mq",
        			q_stage: "Q"
                },
                success: function(return_data) { 
                	//$("#test").html("<!-- "+return_data+ " -->");
                	//alert("Return Data : "+return_data);
                }
        }); // ajax
    }
    if (s_page == "finalquoteall") { 
        temp_data = $("#ResultScreenMotor1").html().replace("Next", "'<b>Purchase</b>'");
        $("#ResultScreenMotor1").html(temp_data);
        $(".GridItem:first").removeClass("GridItem").addClass("GridItemFirst");
        var call_me_url_array = $("#callme_row a").attr("href").split("?");
        var quote_ref_param = call_me_url_array[2].split("=");
        var quote_ref = quote_ref_param[1];
        //alert(quote_ref);
        var data_helper = "/data_helper.php";
        $.ajax({ 
                type: "POST",
                url: data_helper,
                cache: false,
                data: { 
        			ref_code: quote_ref,
        			q_type: "mq",
        			q_stage: "F"
                },
                success: function(return_data) { 
                	//$("#test").html("<!-- "+return_data+ " -->");
                	//alert("Return Data : "+return_data);
                }
        }); // ajax
    }
    if (s_page == "shownotes") { 
        var stage = self.location.search.toLowerCase();
        stage = stage.indexOf('qq');
        // If Quick Quote
        if (stage > -1) { 
            $("#stage_qq").html("<b>Quote</b>").removeClass("stageoff").addClass("stageon");
            temp_data = $("#motorNotes").html().replace("Accept Quotation", "<b>Click to Proceed with Full Quote</b>");
            $("#motorNotes").html(temp_data);
            $("#btAccept").val("Click to Proceed with Full Quote");
            $("#Image1").css('cursor', 'pointer');
        }
        else { 
            $("#stage_fq").html("<b>Confirm</b>").removeClass("stageoff").addClass("stageon");
            temp_data = $("#motorNotes").html().replace("Accept Quotation", "<b>Accept Quotation</b>");
            $("#motorNotes").html(temp_data);
            //$("#back_link").attr("href", "/finalquoteall.aspx");
            $("#Image1").css('cursor', 'pointer');
        }
    }
    if (s_page == "houseqqall") { 
        $(".GridItem a").text("[Policy Benefits]");
        $(".GridAltItem a").text("[Policy Benefits]");
        $(".GridItem:first").removeClass("GridItem").addClass("GridItemFirst");
        temp_data = $("#ResultScreenHousetable").html().replace("Next", "'<b>Next</b>'");
        $("#ResultScreenHousetable").html(temp_data);
        var call_me_url_array = $("#callme_row a").attr("href").split("?");
        var quote_ref_param = call_me_url_array[2].split("=");
        var quote_ref = quote_ref_param[1];
        var params = "quote_ref: "+quote_ref;
        //$("#test").text("<!-- " + quote_ref_param[1]+" -->");
        // AJAX POST call
        var data_helper = "/data_helper.php";
        $.ajax({ 
                type: "POST",
                url: data_helper,
                cache: false,
                data: { 
        			ref_code: quote_ref,
        			q_type: "hq",
        			q_stage: "Q"
                },
                success: function(return_data) { 
                	//$("#test").html("<!-- quote_ref: "+quote_ref+", return_data:"+return_data+ " -->");
                	//alert("Return Data : "+return_data);
                }
        }); // ajax
    }
    if (s_page == "shownoteshouse") { 
        var stage = self.location.search.toLowerCase();
        stage = stage.indexOf('qq');
        // If Quick Quote
        if (stage > -1) { 
            $("#stage_qq").html("<b>Quote</b>").removeClass("stageoff").addClass("stageon");
            temp_data = $("#houseNotes").html().replace("Accept Quotation", "'<b>Click to Proceed with Full Quote</b>'");
            $("#houseNotes").html(temp_data);
            $("#Image1").css('cursor', 'pointer');
        }
        else { 
            $("#stage_fq").html("<b>Confirm</b>").removeClass("stageoff").addClass("stageon");
            temp_data = $("#houseNotes").html().replace("Accept Quotation", "'<b>Accept Quotation</b>'");
            $("#houseNotes").html(temp_data);
            //$("#back_link").attr("href", "/housefcresultsall.aspx");
            $("#Image1").css('cursor', 'pointer');
        }
    }
    if (s_page == "housefcresultsall") { 
        temp_data = $("#ResultScreenHouse").html().replace("Next", "'<b>Purchase</b>'");
        $("#ResultScreenHouse").html(temp_data);
        $(".GridItem:first").removeClass("GridItem").addClass("GridItemFirst");
        var call_me_url_array = $("#callme_row a").attr("href").split("?");
        var quote_ref_param = call_me_url_array[2].split("=");
        var quote_ref = quote_ref_param[1];
        //$("#test").text("<!-- " + quote_ref_param[1]+" -->");
        // AJAX POST call
        var data_helper = "/data_helper.php";
        $.ajax({ 
                type: "POST",
                url: data_helper,
                cache: false,
                data: { 
        			ref_code: quote_ref,
        			q_type: "hq",
        			q_stage: "F"
                },
                success: function(return_data) { 
                	$("#test").html("<!-- "+return_data+ " -->");
                	//alert("Return Data : "+return_data);
                }
        }); // ajax
    }
    if ((s_page == "acceptedquote") || (s_page == "acceptquotehouse")) { 
        // Simple AJAX load call to get Broker Name
        //var data_helper = "data_helper.php?phone=" + $("#lblTelephone").html();
        //$("#lblBroker").load(data_helper);
        // Simple AJAX POST call to get Broker Name
        /*$.post(data_helper,{
            phone: $("#lblTelephone").html()
            }, function(broker_name) {
                alert(broker_name);
        });*/
    	var qtype = ((s_page == "acceptedquote") ? "mq" : "hq");
        var query_string = self.location.search.toLowerCase();
        var ref_code = "";
        var ref_code_string = query_string.substring((query_string.lastIndexOf('?') + 1), query_string.length);
        if (ref_code_string != query_string) { 
            var temp_data = ref_code_string.split("=");
            if (temp_data[0] == "refcode") { 
                ref_code = temp_data[1].toUpperCase();
                $("#lblRefCodeLabel").html("Quote Reference");
                $("#lblRefCode").html(ref_code);
            }
        }
        var broker_email = $("#lblEmail").html();
        $("#lblEmail").html("<a href=\"mailto:"+broker_email+"?Subject=Bestquote%20Query%20Reference%20:%20"+ref_code+"\">"+broker_email+"</a>")
                      .attr("style", "font-weight: bold;");
        //$("#test").html("<!-- ref_code:"+ref_code+", q_type:" +qtype+", q_stage: , -->");
        // AJAX POST call to get Broker Name
        var data_helper = "/data_helper.php";
        $.ajax({ 
                type: "POST",
                url: data_helper,
                cache: false,
                data: { 
                    email: broker_email 
                    //phone: $("#lblTelephone").html()
                },
                success: function(broker_name) { 
                    $("#lblBrokerLabel").html("Bestquote Broker");
                    $("#lblBroker").html(broker_name);
                }
        }); // ajax
        //alert(ref_code+" " + qtype);
        $.ajax({ 
                type: "POST",
                url: data_helper,
                cache: false,
                data: { 
        			ref_code: ref_code,
        			q_type: qtype,
        			q_stage: "A"
                },
                success: function(return_data) { 
                	//$("#test").html("<!-- data_helper: "+data_helper+", ref_code:"+ref_code+", q_type:" +qtype+", q_stage: , "+ return_data+ " -->");
                	//alert("Return Data : "+return_data);
                }
        }); // ajax
    }
    if ((s_page == "client") || (s_path_lower = "/client/default.aspx")) { 
        $(".GridHeader td").addClass("tb12b");
        $(".Pager td").addClass("tb12b");
        $(".GridItem a").addClass("tb12b");
        $(".GridAltItem a").addClass("tb12b");
        //$("#dgTransactions .GridItem a").attr("href","").text("");
        //$("#dgTransactions .GridAltItem a").attr("href","").text("");
    }
    if (debug) { 
        debug_defaults(s_page);
    }
}
);

function get_user_id() { 
    var details = $("td .MenuLink").text();
    //var user_email = details.substring((details.lastIndexOf(':') + 1), details.length).replace(/^\s+|\s+$/g, '');
    var user_email = details.substring((details.lastIndexOf(':') + 1), details.lastIndexOf('Archived Quotes')).replace(/^\s+|\s+$/g, '');
    $("#user_email").val(user_email);
    // AJAX POST call to get Broker Name
    var data_helper = "/data_helper.php";
    $.ajax({ 
        type: "POST",
        url: data_helper,
        cache: false,
        data: { 
            user_email: user_email 
        },
        success: function(return_data) { 
            $("#user_id").val(return_data);
        }
    }); // ajax
    $("#account-manager").submit();
}

