﻿/// <reference path="jquery-1.2.6.js" />

function Left(str, n) {
    if (n <= 0)
        return "";
    else if (n > String(str).length)
        return str;
    else
        return String(str).substring(0, n);
}
function Right(str, n) {
    if (n <= 0)
        return "";
    else if (n > String(str).length)
        return str;
    else {
        var iLen = String(str).length;
        return String(str).substring(iLen, iLen - n);
    }
}

function loadCountry(ctrlHidCountry, ctrlCountry) {
    if (document.getElementById(ctrlHidCountry).innerHTML != "") {
        var selectMenu = document.getElementById(ctrlCountry);

        var selectedValue = $(selectMenu).val();
        
        while (selectMenu.hasChildNodes()) {
            selectMenu.removeChild(selectMenu.firstChild);
        }

        var optvalues3 = document.createElement("option");
        optvalues3.value = "0";
        optvalues3.appendChild(document.createTextNode("Select a country"));
        selectMenu.appendChild(optvalues3);

        var optvalues1 = document.createElement("option");
        optvalues1.value = "0";
        optvalues1.appendChild(document.createTextNode("Any"));
        selectMenu.appendChild(optvalues1);

        var SplitCountryOpt = document.getElementById(ctrlHidCountry).innerHTML.split("#");
        var SplitCountry;
        for (var i = 0; i < SplitCountryOpt.length; i++) {
            SplitCountry = SplitCountryOpt[i].split("||");

            if (SplitCountryOpt[i].substring(0, 8) == "optgroup")
                var stroptgroup = document.createElement("optgroup");

            for (var j = 0; j < SplitCountry.length - 1; j++) {
                if ((SplitCountry[j] != "") && (SplitCountry[j] == "optgroup")) {
                    stroptgroup.label = SplitCountry[j + 1];
                } else if ((SplitCountry[j] != "") && (SplitCountry[j] != "optgroup")) {
                    var optvalues = document.createElement("option");
                    optvalues.value = SplitCountry[j + 1];

                    if (selectedValue == optvalues.value)
                        optvalues.setAttribute("selected", "selected");

                    optvalues.appendChild(document.createTextNode(SplitCountry[j]));
                    stroptgroup.appendChild(optvalues);
                }
            }
            if (SplitCountryOpt[i].substring(0, 8) == "optgroup")
                selectMenu.appendChild(stroptgroup);
        }

        if (selectedValue != "")
            $(selectMenu).trigger("change");
    }
}