﻿
function DoGadget(strDivId, dtlGadgetData, templateID) {

    $(document).ready(function() {

        if (dtlGadgetData != null) {

            switch (templateID) {
                case '650x400bestbuytable':
                    var strTable = Build300x650BestBuyTable(dtlGadgetData);
                    $('#' + strDivId).html(strTable);
                    break;
                case '300x400bestbuytable':
                default:

                    var strTable = Build300x400BestBuyTable(dtlGadgetData);
                    $('#' + strDivId).html(strTable);
                    break;
            }

        }
    });
}

function Build300x400BestBuyTable(dtlGadgetData) {

    var strTable = "<table cellspacing=\"0\" cellpadding=\"0\">";


    if (typeof dtlGadgetData.WebServices.Results.BestBuys.Product == 'object') {

        strTable += "<thead><tr>";
        strTable += "<th>Supplier</th>";
        strTable += "<th>Speed</th>";
        strTable += "<th>Cost</th>";
        strTable += "<th>&nbsp;</th>";
        strTable += "</tr></thead><tbody>";

        $.each(dtlGadgetData.WebServices.Results.BestBuys.Product, function(i, item) {

            strTable += "<tr>";
            strTable += "<td><form target=\"_blank\" action=\"" + item.ProductRefURL.cdatasection + "\" method=\"post\"><input type=\"image\" src=\"" + item.ProviderLogo.cdatasection + "\" /></form></td>";
            strTable += "<td>" + item.Speed + "</td>";
            strTable += "<td>" + item.MonthlyCharge + "</td>";
            strTable += "<td>";
            strTable += "<form target=\"_blank\" action=\"" + item.ProductRefURL.cdatasection + "\" method=\"post\"><input type=\"submit\" value=\"\" class=\"goButton\" /></form>";
            strTable += "</td>";
            strTable += "</tr>";
        });

        strTable += "</tbody>";
    }
    strTable += "</table>";

    return strTable;
}

function Build300x650BestBuyTable(dtlGadgetData) {

    var strTable = "<table>";


    if (typeof dtlGadgetData.WebServices.Results.BestBuys.Product == 'object') {

        strTable += "<thead><tr>";
        strTable += "<th>Supplier</th>";
        strTable += "<th>Package</th>";
        strTable += "<th>Speed</th>";
        strTable += "<th>1st year cost</th>";
        strTable += "<th>Monthly cost</th>";
        strTable += "<th>Download limit</th>";
        strTable += "<th>&nbsp;</th>";
        strTable += "</tr></thead><tbody>";

        $.each(dtlGadgetData.WebServices.Results.BestBuys.Product, function(i, item) {

            strTable += "<tr>";
            strTable += "<td><form target=\"_blank\" action=\"" + item.ProductRefURL.cdatasection + "\" method=\"post\"><input type=\"image\" src=\"" + item.ProviderLogo.cdatasection + "\" /></form></td>";
            strTable += "<td>" + item.ProductName.cdatasection + "</td>";
            strTable += "<td>" + item.Speed + "</td>";
            strTable += "<td>" + item.FirstYearCost + "</td>";
            strTable += "<td>" + item.MonthlyCharge + "</td>";
            strTable += "<td>" + item.DownloadLimit + "</td>";
            strTable += "<td>";
            strTable += "<form target=\"_blank\" action=\"" + item.ProductRefURL.cdatasection + "\" method=\"post\"><input type=\"submit\" value=\"\" class=\"goButton\" /></form>";
            strTable += "</td>";
            strTable += "</tr>";
        });

        strTable += "</tbody>";
    }
    strTable += "</table>";

    return strTable;
}

$(document).ready(function() {

    $("#dtl-bestbuy-searchform").submit(function(event) {

        var isValidPostcode = dtlValidPostcode($("#dtl-postcodebox").val())

        if (!isValidPostcode) {
           
            alert("Please enter a valid UK post code");
            return false;
        }

    });

    $("#dtl-postcodebox").click(function(event) {
        $("#dtl-postcodebox").val("");
    });
});

function dtlValidPostcode(pcode) {

    /*
    There are 6 possible valid UK postcodes: where L = letter and N = Number
    LN NLL LLN NLL LNN NLL LNL NLL LLNN NLL LLNL NLL
    */

    var pcType1 = "^[a-zA-Z]{1}\\d{1}\\s?\\d{1}[a-zA-Z]{2}$";
    var pcType2 = "^[a-zA-Z]{2}\\d{1}\\s?\\d{1}[a-zA-Z]{2}$";
    var pcType3 = "^[a-zA-Z]{1}\\d{2}\\s?\\d{1}[a-zA-Z]{2}$";
    var pcType4 = "^[a-zA-Z]{2}\\d{2}\\s?\\d{1}[a-zA-Z]{2}$";
    var pcType5 = "^[a-zA-Z]{2}\\d{1}[a-zA-Z]{1}\\s?\\d{1}[a-zA-Z]{2}$";
    var pcType6 = "^[a-zA-Z]{1}\\d{1}[a-zA-Z]{1}\\s?\\d{1}[a-zA-Z]{2}$";
    if (pcode == null || pcode == "") { ; return false; }
    if (pcode.length < 6 || pcode.length > 8) { ; return false; }
    if (pcode.match(pcType1)
    || pcode.match(pcType2)
    || pcode.match(pcType3)
    || pcode.match(pcType4)
    || pcode.match(pcType5)
    || pcode.match(pcType6)) {
        return true;
    }
    else { return false; }
}


