﻿//query string get
function getQuerystring(key, default_) {
    if (default_ == null) default_ = null;
    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
    var qs = regex.exec(window.location.href);
    if (qs == null)
        return default_;
    else
        return qs[1];
}

var selectInsRegionId = getQuerystring("insRegionid");
var selectInsCategoryId = getQuerystring("insCategoryId");
var selectInsLoanId = getQuerystring("insLoanid");
var selectCapRegionId = getQuerystring("capRegionid");
var selectCapCategoryId = getQuerystring("capCategoryId");
var selectCapLoanId = getQuerystring("capLoanid");

function redirectInsurance() {
    var url = "/Insurance-Needs-Profile?";
    url += "regionID=" + document.getElementById("insRegionID").value;
    url += "&categoryID=" + document.getElementById("insCategoryID").value;
    url += "&loanID=" + document.getElementById("insLoanID").value;
    document.forms[0].action = url;
}

function validateFormInsurance() {
    var y = document.getElementById("insRegionID").value;
    var z = document.getElementById("insLoanID").value;

    if (y == null || y == "0" || y == "") {
        alert("Please select a location.");
        return false;
    }

    if (z == null || z == "0" || z == "") {
        alert("Please select an insurance type.");
        return false;
    }

    if (document.getElementById("insLoanID").value != "term") {
        return true;
    } else {
        window.location = "https://www.termfinder.com/Term-Life-Insurance-Quote-TF.aspx?source=964-ibanknew";
    }
}

function redirectCap() {
    var url = "/Capital-Needs-Profile?loanApplicantAmount=" + document.getElementById("capLoanApplicantAmount").value;
    url += "&regionID=" + document.getElementById("capRegionID").value;
    url += "&loanID=" + document.getElementById("capLoanID").value;
    document.forms[0].action = url;
}

function validateFormCap() {
    var w = document.getElementById("capLoanApplicantAmount").value;
    var x = document.getElementById("capRegionID").value;
    var z = document.getElementById("capLoanID").value;

    if (w == "Capital Amount" || w.length == 0 || Number(w) == NaN) {
        alert("Please enter a number for the amount requested, without commas (,), dollar signs ($), or decimal points (.).\n");
        return false;
    }

    if (x == null || x == "" || x == "0") {
        alert("Please select a location.");
        return false;
    }

    if (z == null || z == "0" || z == "") {
        alert("Please select a capital type.");
        return false;
    }

    return true;
}

$(function () {
    //populates the states dropdown list box
    $.getJSON('/CMSCustomHandlers/GetStates.ashx', function (states) {
        $.each(states, function () {
            $("#insRegionID").append($("<option></option>").attr("value", this['RegionId']).html(this['State']));
            $("#capRegionID").append($("<option></option>").attr("value", this['RegionId']).html(this['State']));
        });
        $("#insRegionID").val(selectInsRegionId);
        $("#capRegionID").val(selectCapRegionId);
    });

    //populates the insurance categories dropdown list box
    $.getJSON('/CMSCustomHandlers/GetLoanCategories.ashx?searchfrom=insurance', function (categories) {
        $.each(categories, function () {
            $("#insCategoryID").append($("<option></option>").attr("value", this['LoanCategoryId']).html(this['LocanCategoryDescription']));
        });
        if (selectInsCategoryId) {
            $("#insCategoryID").val(selectCategoryId);
            $("#insCategoryID").change();
        }
    });

    //populates the insurance types dropdown list box according to the insurance category
    $("#insCategoryID").change(function () {
        $("#insLoanID").html("");
        $("#insLoanID").append($("<option></option>").attr("value", "0").html("Select Type").attr("selected", "selected"));
        selectCategoryId = $("#insCategoryID option:selected").val();
        $.getJSON('/CMSCustomHandlers/GetLoanTypes.ashx?categoryID=' + selectCategoryId, function (loanTypes) {
            $.each(loanTypes, function () {
                $("#insLoanID").append($("<option></option>").attr("value", this['LoanTypeId']).html(this['LoanTypeName']));
            });
            $("#insLoanID").val(selectInsLoanId);

        });
    });


    //populates the capital categories dropdown list box
    $.getJSON('/CMSCustomHandlers/GetLoanCategories.ashx?searchfrom=capital', function (categories) {
        $.each(categories, function () {
            $("#capCategoryID").append($("<option></option>").attr("value", this['LoanCategoryId']).html(this['LocanCategoryDescription']));
        });
        if (selectCapCategoryId) {
            $("#capCategoryID").val(selectCategoryId);
            $("#capCategoryID").change();
        }
    });

    //populates the insurance types dropdown list box according to the insurance category
    $("#capCategoryID").change(function () {
        $("#capLoanID").html("");
        $("#capLoanID").append($("<option></option>").attr("value", "0").html("Select Type").attr("selected", "selected"));
        selectCategoryId = $("#capCategoryID option:selected").val();
        $.getJSON('/CMSCustomHandlers/GetLoanTypes.ashx?categoryID=' + selectCategoryId, function (loanTypes) {
            $.each(loanTypes, function () {
                $("#capLoanID").append($("<option></option>").attr("value", this['LoanTypeId']).html(this['LoanTypeName']));
            });
            $("#capLoanID").val(selectCapLoanId);

        });
    });
});
