﻿var map = null;
var baseIcon1 = null;
var baseIconSrc = null;
var baseIconDst = null;
var baseIconCFS = null;
var srcMarkers = null;
var dstMarkers = null;
var isDstActive = 1;
var srcFullMarkers = null;

var townMarkers = null;

var selectedMarkers = new Array(2);

var selectedCount = 0;

var worldLat = 48.69096039092549;
var worldLng = -32.34375;
var worldZoom = 2;

var USALat = 37.8575;
var USALng = -100.546875;
var USAZoom = 4;
var townsBufSrc = null;
var townsBufDst = null;
var GoogleMap = 0;
var listMenu;
var arrow;
var first_changeCommodity = 1;
var color_blink = "#9dcb8f";
var timeColor;
var cntColor = 0;
var elIdColor;
var timerRunning = false;
var errClr = "#EA8664";
var firstChContType = true;
var home1flg = 0;
var carsFlg = 0;
var pathModifier = "";

var PathHHG = "";
var PathCARS = "";
var PathFCL = "";
var PathRORO = "";
var PathLCL = "";

var alternateAnimElIdColor = '';
var altCount = 0;
var presel_c = 0;
var presel_ref = 0;
var detHeader;
var detVal;
var nSurchEveryCont;
var refUrl = "";

var DisbursementMin = 0;
var DisbursementCoef = 0;
var qqLh = 0;
var needScrol = true;
var needAnim = true;

function setDispMainPriceInfo(val) {
    document.getElementById("cont_info").style.display = val;
    document.getElementById("cbmCalc").style.display = val;
    document.getElementById("step5").style.display = val;
 }

 function show_help(ur) {
    var _ur = ur.indexOf('.')>0 ? ur : "CalcHelp.aspx#" + ur;    
    help_win = window.open(_ur, "subWind", "");
    help_win.focus();
 }


function CheckSteps(showToStep1) {
    var showToStep = showToStep1;
    if (home1flg && showToStep == 3) {
        showToStep = 4;
    }
    if (selectedMarkers[0] != null && selectedMarkers[1] != null) {
        var i;
        for (i = 3; i <= showToStep; i++) {
            var el = document.getElementById("step" + i);
            if (el) {
                if (i == 3 || i == 4) {
                    el.style.visibility = "visible";
                }
                else {
                    el.style.display = "";
                }
            }

        }
        if (document.getElementById("selectCont").selectedIndex < 1) {
            document.getElementById("step6").style.display = "none";
        }

        setDispMainPriceInfo("");

        if (showToStep == 3 || showToStep == 5) {
            if (needScrol) {
                document.getElementById('imgcalc').scrollIntoView(true);
            }
            needScrol = false;
        }
        if (showToStep == 3 && document.getElementById("selectCont").selectedIndex > 0) {
            CheckSteps(4);
        }
        if (showToStep == 4 && GetSelectedId("cmtype") != "start") {
            CheckSteps(5);
            document.getElementById("step41").style.visibility = "visible";
        }
        document.getElementById("hintimg0").style.visibility = "";
        document.getElementById("hintimg1").style.visibility = "";
    }
    else {
        for (var i = 3; i <= 6; i++) {
            var el = document.getElementById("step" + i);
            if (el) {
                if (i == 3 || i == 4) {
                    el.style.visibility = "hidden";
                }
                else {
                    el.style.display = "none";
                }
            }
        }
        setDispMainPriceInfo("none");
        document.getElementById("shp_img").style.display = "none";
        document.getElementById("step41").style.visibility = "hidden";
        document.getElementById("hintimg0").style.visibility = "hidden";
        document.getElementById("hintimg1").style.visibility = "hidden";
    }
}
function CheckShowPrice(noGo) {
    var el = document.getElementById("step6");
    if (!CalcPrice(0, noGo)) {
        if (el) {
            el.style.display = "none";
        }
    }
    else {
        if (el) {
            el.style.display = "";
        }
    }
}
function CalcPrice(showAlert, noGo, callOk) {
    if (selectedMarkers[0] == null || selectedMarkers[1] == null) {
        if (showAlert)
            alert('Please select Destination and Origin cities');
        return 0;
    }
    var chk = GetSelectedId("selectCont");
    if (chk == "0") {
        if (showAlert) {
            alert('Please select Type of goods');
        }
        return 0;
    }
    chk = GetSelectedId("cmtype");
    if (chk == "start") {
        if (showAlert) {
            alert('Please select Type of commodity');
        }
        return 0;
    }
    chk = document.getElementById("numCont").value;
    if (!chk || chk == "") {
        if (showAlert) {
            alert('Please input number of countainers');
        }
        return 0;
    }
    if (callOk == null) {
        clearBkColorId("numCont");
        document.getElementById("step6").style.display = "none";
        return;
    }
    if (!volumeCalc(showAlert)) {
        return 0;
    }
    var wht = document.getElementById("allWPI").innerHTML;
    if (wht == 0) {
        if (showAlert) {
            alert('Please check weight');
        }
        return 0;
    }

    
    if (checkAllRowsOnErrorsAdditional()) {
       alert('Error: see input data on error.');
       return 0;
   }  // need check
   
    
        
    clearBkColorId("numCont");
    document.getElementById("step6").style.display = "";
    GetDist();
    if (showAlert) {
        var el1 = document.getElementById('puzip');
        if (el1) {
            if (el1.value == null || el1.value == '') {
                showAnimColor('puzip');
            }
        }
    }
    if (noGo != 1) {
        gotobot1();
    }

    return 1;
}
function SlidePrice(optB) {
    var add_str = "";
    if (optB)
        add_str = optB;
    var el = document.getElementById("bda1" + add_str);
    var elPriceDet = document.getElementById("priceDet" + add_str);
    if (el && elPriceDet) {
        if (elPriceDet.style.display != "none") {
            el.innerHTML = "(break down)";
        }
        else {
            el.innerHTML = "hide table<img src='images/reset1.gif' style='vertical-align:middle' />";

        }
    }
    $("#priceDet" + add_str).slideToggle("slow");
}
function SlidePrice1() {
    var el = document.getElementById("bda1");
    var elPriceDet = document.getElementById("priceDet");
    if (el && elPriceDet) {
        if (elPriceDet.style.display != "none") {
            el.innerHTML = "(break down)";
            elPriceDet.style.display = "none";
        }
        else {
            el.innerHTML = "hide table";
            elPriceDet.style.display = "";
        }
    }
}
function ProgrLoad(val) {
    var el = document.getElementById("tdload");
    var el1 = document.getElementById("tdload1");
    var h = getElementHeight("tdload1");
    if (h == 0)
        h = 72;
    el.style.height = h + "px";
    if (val) {
        el.style.display = "";
        el1.style.display = "none";
    }
    else {
        el.style.display = "none";
        $('#tdload1').fadeIn("slow");
    }
}
function markerTownClick(value) {
    if (selectedCount < 2) {
        if (selectedMarkers[0] == null) {
            selectedCount = 0;
        }
        else {
            selectedCount = 1;
        }
        SelectTown(townMarkers[value].id, selectedCount);
        SetCountrySelectMode(false);
        selectedCount++;
    }
    if (selectedCount > 1) {
        ShowMarkers(false, srcMarkers);
        ShowMarkers(false, dstMarkers);
    }

}
function markerCountryClick(value) {
    Clear2Anim();
    if (isDstActive) {
        ShowTowns(dstMarkers[value].id, 1);
    }
    else {
        ShowTowns(srcMarkers[value].id, 0);
    }
}


function aaa() {
    ShowTowns();
}

function ChangeDestCountry() {
    var countryId = GetSelectedId("selCountry0");
    RemoveSelectedMarker(0);
    RemoveSelectedMarker(1);
    selectedCount = 0;
    Clear2Anim();
    if (countryId > 0) {
        ShowTowns(countryId, 1);
        var i = 1;
        document.getElementById("selLabel" + i).style.visibility = "hidden";
        document.getElementById("selimage" + i).style.visibility = "hidden";
        document.getElementById("selTown" + i).style.visibility = "hidden";
        document.getElementById("selCountry" + i).style.visibility = "hidden";
        document.getElementById("step10").style.display = "none";
        document.getElementById("step11").style.display = "none";
    }
    else {
        ClearMarkers();
        RemoveSelectedMarker(0);
        RemoveSelectedMarker(1);
        document.getElementById('selTown0').selectedIndex = 0;
        SetCountrySelectMode(true);
        setDispMainPriceInfo("none");
    }
    CheckShowPrice();
}
function ChangeSrcCountry() {
    var countryId = GetSelectedId("selCountry1");
    RemoveSelectedMarker(1);
    selectedCount = 1;
    if (countryId > 0) {
        ShowTowns(countryId, 0);
    }
    else {
        ClearMarkers();
        RemoveSelectedMarker(1);
        document.getElementById('selTown1').selectedIndex = 0;
        ShowTowns(0, 0);
        //        SetCountrySelectMode(false);
    }
    CheckShowPrice();
}
function ChangePickup() {
    var pickId = GetSelectedId("selectPickup");
    var disp = 'none';
    if (pickId != "0") {
        disp = '';
    }
    for (var i = 1; i <= 3; i++) {
        document.getElementById('troptc' + i).style.display = disp;
    }
    showNoTiWa();
    CheckShowPrice(1);
}
function showNoTiWa() {
    var el = ge_id('wthtn');
    if (ge_id('troptc3').style.display == 'none' &&
    ge_id('troptb3').style.display == 'none') {
        el.style.display = 'none';
    }
    else {
        el.style.display = '';
    }
    
}
function CheckLineHaul() {
    var selOrigTown = GetSelectedText("selTown1");
    var disp = selOrigTown.indexOf('*') > -1 ? '' : 'none';
    for (var i = 1; i <= 3; i++) {
        document.getElementById('troptb' + i).style.display = disp;
    }
    showNoTiWa();
    document.getElementById("dopt_c2").style.display = disp;
    if (disp == '') {
        document.getElementById("dopt_c1").style.display = 'none';
    }
    else {
        document.getElementById("dopt_c1").style.display = '';
    }

}
function stA() {
    clearBkColorId("selectCont");
    Clear2Anim();
}
function ChangeContainer(noGo) {
    var countType = GetSelectedId("selectCont");
    var disp = 'none';
    if (noGo == 1) {
        stA();
    }
    if (countType != "0") {
        CheckSteps(4);
        var commType = GetSelectedId("cmtype");
        if (commType == "start") {
            // showAnimColor("cmtype");
        }
    }
    var chk = document.getElementById("numCont").value;
    CheckShowPrice(noGo);
    if (!chk || chk == "" || firstChContType) {
        // showAnimColor("numCont");
    }
    if (firstChContType) {
        document.getElementById("step6").style.display = "none";
    }
    first_changeCommodity = 0;
    firstChContType = false;
    chVal();
   
}
function chVal() {
    var el = document.getElementById("txtcargovalue");
    var _isCommoText = GetSelectedText("selectCont").toUpperCase(); // == "comm";
    var _isCommo = false;
    if (_isCommoText.indexOf('COMM') > -1)
        _isCommo = true;

    var est_val = parseInt(el.value, 10);
    //alert(est_val + ',' + estValue + ',' + estValue + ',' + isInteger(estValue));
    if (!_isCommo && (isNaN(est_val) || !isInteger(el.value) || est_val < 2500)) {
        document.getElementById("trIsSed1").style.display = "none";
        document.getElementById("trIsSed").style.display = "none";
    }
    else {
        document.getElementById("trIsSed1").style.display = "";
        document.getElementById("trIsSed").style.display = "";
    }
}
function ChangeCommodity() {
    var countType = GetSelectedId("cmtype");
    if (countType == "LCL") {
        document.location = "http://www.oceanfreightusa.com";
        return;
    }
    if (countType != "start" && countType.substr(0, 3) == "Com") {
        CheckSteps(4);
    }
    if (countType != "start" && countType.substr(0, 3) != "Com") {
        document.location = "ShippingCarsHouseholdGoodsUsaBySea.aspx";
        return;
    }
    var contType = GetSelectedId("selectCont");
    if (contType == "0") {
        showAnimColor("selectCont");
    }

    CheckShowPrice();
    if (!home1flg) {
        first_changeCommodity = 0;
    }
}
function ChangeCommodity1() {
    var countType = GetSelectedId("cmtype");
    if (countType == "LCL") {
        showAnimColor("selectCont");
        CheckShowPrice();
        return;
    }
    if (countType == "FCL") {
        document.location = PathFCL;
        return;
    }
    if (countType == "HHG") {
        document.location = PathHHG;
        return;
    }
    if (countType == "CARS") {
        var addLink = "";
        if (refUrl.length > 0) {
            addLink = "carshome.aspx?" + refUrl;
        }
    
        document.location = PathCARS + addLink;
        return;
    }
    if (countType == "RORO") {
        document.location = PathRORO;
        return;
    }

}

function ChangeDestTown() {
    selectedCount = 0;
    var objSel = document.getElementById("selTown0");
    SelectTown(objSel.options[objSel.selectedIndex].value, 0);
    SetCountrySelectMode(false);
    if (selectedCount < 2) {
        selectedCount++;
    }
    CheckShowPrice();
}
function ChangeSrcTown() {
    selectedCount = 1;
    var objSel = document.getElementById("selTown1");
    SelectTown(objSel.options[objSel.selectedIndex].value, 1);
    SetCountrySelectMode(false);
    if (selectedCount < 2) {
        selectedCount++;
    }
    CheckShowPrice();
}

function DestTownsSelect() {
    selectedCount = 0;
    var objSel = document.getElementById("selCountry0");
    SetTownSelectMode(selectedCount == 0, objSel.options[objSel.selectedIndex].value);
}

function fitMap(map, markers, zoomChange) {
    if (map == null)
        return;

    var bounds = new GLatLngBounds();
    for (var i = 0; i < markers.length; i++) {
        bounds.extend(markers[i].marker.getLatLng());
    }
    if (markers.length > 1) {
        var zoom = map.getBoundsZoomLevel(bounds);
        if (markers.length == 2 && zoom > 1) {
            zoom--;
        }
        else {
            if (zoomChange < 0) {
                zoom += zoomChange;
            }
        }
        map.setZoom(zoom);
    }
    else {
        map.setZoom(10);
    }
    map.setCenter(bounds.getCenter());
}

function ShowWorld(isDst) {
//    if (map == null)
    //        return;
    var tmppresel = presel_c;
    if (dstMarkers == null) {
        dstMarkers = 1;
        ProgrLoad(true);
        GDownloadUrl("coords.aspx?world=1&src=" + selectedCount, function(data, responseCode) {
            // To ensure against HTTP errors that result in null or bad data,
            // always check status code is equal to 200 before processing the data
            if (responseCode == 200) {
                var xml = GXml.parse(data);
                var markers = xml.documentElement.getElementsByTagName("country");
                //ClearMarkers();

                //mymarkers = new Array(markers.length);
                //mymarkersId = new Array(markers.length);
                var iSrc = 0;
                var iDst = 0;
                for (var i = 0; i < markers.length; i++) {
                    if (markers[i].getAttribute("issrc") == "1") {
                        iSrc++;
                    }
                    if (markers[i].getAttribute("isdst") == "1") {
                        iDst++;
                    }
                }
                srcMarkers = new Array(iSrc);
                dstMarkers = new Array(iDst);
                srcFullMarkers = new Array(iSrc);


                var selDst = document.getElementById("selCountry0");
                selDst.options.length = 0;
                var selSrc = document.getElementById("selCountry1");
                selSrc.options.length = 0;
                selDst.options[0] = new Option("Destination (Ship to)", 0);
                selSrc.options[0] = new Option("Filter by State", 0);

                iSrc = 0;
                iDst = 0;

                for (var i = 0; i < markers.length; i++) {
                    var name = markers[i].getAttribute("name");
                    var id = markers[i].getAttribute("id");
                    var point = new GLatLng(
                                parseFloat(markers[i].getAttribute("lat")),
                                parseFloat(markers[i].getAttribute("lng"))
                             );


                    if (markers[i].getAttribute("issrc") == "1") {
                        srcMarkers[iSrc] = new Object();
                        srcMarkers[iSrc].marker = createMarker(point, iSrc, 1, name, 1, 0);
                        srcMarkers[iSrc].id = id;
                        selSrc.options[iSrc + 1] = new Option(name, id);
                        if (map != null) {
                            map.addOverlay(srcMarkers[iSrc].marker);
                        }

                        srcFullMarkers[iSrc] = new Object();
                        srcFullMarkers[iSrc].marker = createMarker(point, iSrc, 1, name, 1, 0);
                        srcFullMarkers[iSrc].id = id;
                        srcFullMarkers[iSrc].name = name;

                        iSrc++;
                    }
                    if (markers[i].getAttribute("isdst") == "1") {
                        dstMarkers[iDst] = new Object();
                        dstMarkers[iDst].marker = createMarker(point, iDst, 1, name, 1, 0);
                        dstMarkers[iDst].marker.hide();
                        dstMarkers[iDst].id = id;
                        selDst.options[iDst + 1] = new Option(name, id);
                        if (map != null) {
                            map.addOverlay(dstMarkers[iDst].marker);
                        }
                        iDst++;
                    }


                }
                if (map != null) {
                    map.setCenter(new GLatLng(worldLat, worldLng), worldZoom);
                }

                if (srcMarkers) {
                    ShowMarkers(isDstActive != 1, srcMarkers);
                    ShowMarkers(isDstActive == 1, dstMarkers);
                }
            } else if (responseCode == -1) {
                alert("Data request timed out. Please try later.");
            } else {
                alert("Request resulted in error. Check XML file is retrievable.");
            }
            ProgrLoad(false);
            if (presel_c > 0) {
                SelectCountry(presel_c);
                presel_c = 0;
            }
        });
    }

    if (tmppresel == 0) {
        if (srcMarkers) {
            ShowMarkers(isDstActive != 1, srcMarkers);
            ShowMarkers(isDstActive == 1, dstMarkers);
        }
        else {
            if (map != null) {
                map.setCenter(new GLatLng(worldLat, worldLng), worldZoom);
            }
        }
    }


}
function ShowMarkers(value, arr) {
    if (arr == null) {
    return;
    }
    for (var g = 0; g < arr.length; g++) {
        if (value) {
            arr[g].marker.show();
        }
        else {
            arr[g].marker.hide();
        }
    }
    if (value && map != null) {
        fitMap(map, arr, 0);
    }
}

function ClearMarkers() {
    if (townMarkers) {
        for (var i = 0; i < townMarkers.length; i++) {
            if (townMarkers[i] && map != null) {
                map.removeOverlay(townMarkers[i].marker);
            }
        }
        townMarkers = null;
    }
}
function HideMarkers() {
    if (townMarkers) {
        for (var i = 0; i < townMarkers.length; i++) {
            if (townMarkers[i]) {
                townMarkers[i].marker.hide();
            }
        }
    }
}
function SelectById(elementId, id) {
    var el = document.getElementById(elementId);
    if (el) {
        for (var i = 0; i < el.options.length; i++) {
            if (el.options[i].value == id) {
                el.options[i].selected = true;
            }
            else {
                el.options[i].selected = false;
            }
        }
    }
}
function SelectTown(townId, selectedIdx) {
//    if (map == null)
//        return;
    Clear2Anim();
    var value = -1;
    document.getElementById("shp_img").style.display = "none";
    var _srcEl = ge_id("priceFinalSrc1");
    if (_srcEl)
        _srcEl.innerHTML = GetSelectedText("selTown1");
    

    var p;
    var hint;
    if (selectedIdx == 0) {
        RemoveSelectedMarker(1);
    }
    if (townMarkers == null) {
    }
    else {
        for (var i = 0; i < townMarkers.length; i++) {
            if (!townMarkers[i])
                continue;
            if (townMarkers[i].id == townId) {
                value = i;
                break;
            }
        }
        if (value > -1) {
            p = townMarkers[value].marker.getPoint();
            hint = townMarkers[value].marker.getTitle();
        }
    }
    if (value < 0) {
        var townbuf;
        if (selectedIdx == 0) {
            townbuf = townsBufDst;
        }
        else {
            townbuf = townsBufSrc;
        }
        if (townbuf) {
            for (var i = 0; i < townbuf.length; i++) {
                if (townbuf[i]) {
                    if (townbuf[i].id == townId) {
                        p = new GLatLng(townbuf[i].lat, townbuf[i].lng);

                        hint = townbuf[i].name;

                        RemoveSelectedMarker(selectedIdx);

                        selectedMarkers[selectedIdx] = new Object();
                        selectedMarkers[selectedIdx].marker = createMarker(p, selectedIdx, 0, hint, 2, selectedIdx);
                        selectedMarkers[selectedIdx].id = townId;
                        if (map != null) {
                            map.addOverlay(selectedMarkers[selectedIdx].marker);
                        }

                        break;
                    }
                }
            }
        }
        if (selectedIdx == 0) {
            CheckSteps(4);
        }

        return;
    }



    RemoveSelectedMarker(selectedIdx);

    selectedMarkers[selectedIdx] = new Object();
    selectedMarkers[selectedIdx].marker = createMarker(p, selectedIdx, 0, hint, 2, selectedIdx);
    selectedMarkers[selectedIdx].id = townId;
    if (map != null) {
        map.addOverlay(selectedMarkers[selectedIdx].marker);
    }

    SelectById("selTown" + selectedCount, townId);
    if (_srcEl)
        _srcEl.innerHTML = GetSelectedText("selTown1");

    if (selectedIdx == 1) {
        clearBkColorId("selTown1");
        clearBkColorId("selTown0");
        if (selectedMarkers[0] != null && selectedMarkers[1] != null) {
            GetContainers();
        }

        var selOrigTown = GetSelectedText("selTown1");
        ShowAdditionalColumns(selOrigTown.indexOf('*') > -1);
        CheckLineHaul();

    }



    if (selectedIdx == 0 && selectedMarkers[1] == null) {
        clearBkColorId("selTown0");
        showAnimColor("selTown1");
    }

    if (carsFlg) {
    }
    else {
        if (home1flg) {
            if (selectedMarkers[0] != null && selectedMarkers[1] != null) {
                var commType = GetSelectedId("selectCont");
                clearBkColorId("selTown0");
                clearBkColorId("selTown1");
                if (commType == "0") {
                    showAnimColor("selectCont");
                }
            }
        }
        else {
            if (selectedMarkers[0] != null && selectedMarkers[1] != null) {
                var commType = GetSelectedId("cmtype");
                clearBkColorId("selTown0");
                clearBkColorId("selTown1");
                if (commType == "start") {
                    showAnimColor("cmtype");
                }
            }
        }
    }

    if (selectedIdx == 0) {
        CheckSteps(4);
    }



}
function SelectCountry(countryId) {
    SetTownSelectMode(selectedCount == 0, countryId);
}
function RemoveSelectedMarker(idx) {
    if (selectedMarkers[idx] != null) {
        if (map != null) {
            map.removeOverlay(selectedMarkers[idx].marker);
        }
        selectedMarkers[idx] = null;
    }
}

function SetCountrySelectMode(isDst) {
    HideMarkers();
    var idx = 0;
    if (isDst) {
        isDstActive = 1;
        idx = 0;
        clearBkColorId("selTown1");
    }
    else {
        isDstActive = 0;
        idx = 1;
        clearBkColorId("selTown0");
    }
    var i;
    for (i = idx + 1; i <= 1; i++) {
        document.getElementById("selLabel" + i).style.visibility = "hidden";
        document.getElementById("selTown" + i).style.visibility = "hidden";
        document.getElementById("selCountry" + i).style.visibility = "hidden";
        document.getElementById("selimage" + i).style.visibility = "hidden";
        document.getElementById("step" + i + "0").style.display = "none";
        document.getElementById("step" + i + "1").style.display = "none";
    }
    document.getElementById("selLabel" + idx).style.visibility = "";
    document.getElementById("selCountry" + idx).style.visibility = "";
    document.getElementById("step" + idx + "0").style.display = "";

    if (isDst) {
        document.getElementById("selTown" + idx).style.visibility = "hidden";
        document.getElementById("selimage" + idx).style.visibility = "hidden";
        document.getElementById("step" + idx + "1").style.display = "none";
    }

    if (isDst) {
        ShowWorld(isDst);
    }
    else {
        if (selectedMarkers[1] == null) {
            ShowTowns(0, 0);
            document.getElementById("selTown1").style.visibility = "";
            document.getElementById("step11").style.display = "";
        }
        else {
            ShowWorld(isDst);
        }
    }
    if (selectedMarkers[0] != null && selectedMarkers[1] != null) {
        ShowMarkers(false, srcMarkers);
        ShowMarkers(false, dstMarkers);
        //                GetDist();
        CheckSteps(3);
        if (map != null) {
            fitMap(map, selectedMarkers, 0);
        }
    }
    if (!isDst && selectedMarkers[1] == null) {
        document.getElementById("selCountry1").selectedIndex = 0;
    }
}
function addRow(c1, c2, c3, c4, style_add, idlab) {
    var addlab30 = '';
    var addlab31 = '';
    var addlab40 = '';
    var addlab41 = '';
    if (idlab) {
        addlab30 = "<label id='" + idlab + "1'>";
        addlab31 = "</label>";
        addlab40 = "<label id='" + idlab + "2'>";
        addlab41 = "</label>";
    }
    return '<tr ' + style_add + '><td align="left">' + c1 + '</td><td align="center">' + c2 + '</td><td align="center">' + addlab30 + c3 + addlab31 + '</td><td align="right">' + addlab40 + c4 + addlab41 + '</td></tr>'
}
function GoToBook() {
    var id1 = selectedMarkers[0].id
    var id2 = selectedMarkers[1].id;
    var contType = GetSelectedId("selectCont");
    //    var cmType = GetSelectedId("cmtype");
    var num = document.getElementById("numCont").value;
    //    window.location = "book.aspx?id1=" + id1 + "&id2=" + id2 + "&ct=" + contType + "&n=" + num; // "&cmt=" + cmType

    document.getElementById("hid1").value = id1;
    document.getElementById("hid2").value = id2;
    document.getElementById("hcontType").value = contType;
    document.getElementById("hnum").value = num;

    document.forms[0].action = "book.aspx"
    document.forms[0].submit();
}
function fixZip(srcName, srcZip) {
    var _srcName = srcName;
    if (srcZip.length > 2) {
        var _srch = srcZip.substr(0, 2);
        _srcName = _srcName.replace(_srch + '*', srcZip);
        if (_srcName != srcName)
            return _srcName;
        _srcName = _srcName.replace(", " +_srch, ", " + srcZip);
        if (_srcName != srcName)
            return _srcName;
        _srcName = _srcName.replace("," + _srch, ", " + srcZip);
    }
    return _srcName;

}
function wpi_1(num) {
   var d = document;   
    var ws = 'wI' + num;
    var w = d.getElementById(ws).value;

    var c2 = 1;
    if (d.getElementById('wType').value == '0')
        c2 = 1;
    else
        c2 = 2.20462262;

    var As = 'coutItem' + num;
    var A = d.getElementById(As).value;
    if (w * c2 >= 100) {
        return 1;
    }
    return 0;
}

function ShowPrReady(val, visible, manual) {
    var displ = visible ? "" : "none";
    var displN = visible ? "none" : "";
    var _spnMan = "none";
    if (manual == 1) {
        displ = 'none';
        displN = 'none';
        _spnMan = "";
        document.getElementById("h" + val + "Res").value = "0";
        document.getElementById("h" + val + "Orig").value = "0";
    }
    for (var i=0; i<=2; i++) {
        document.getElementById(val + "Spn2" + i).style.display = _spnMan;
    }
    document.getElementById("div" + val + "Res").style.display = displ;
    document.getElementById("lab" + val + "Res1").style.display = displ;
    document.getElementById(val + "Spn1").style.display = displN;
    document.getElementById(val + "Btn").style.display = displN;
    document.getElementById(val + "Btn1").style.display = displN;
    if (visible) {
    }
}
var lstclc = '';
function GetDist() {
    var id1 = selectedMarkers[0].id
    var id2 = selectedMarkers[1].id;
    var contType = GetSelectedId("selectCont");
    var cmType = GetSelectedId("cmtype");
    var pckp = GetSelectedId("selectPickup");
    var num = document.getElementById("numCont").value;
    var wht = document.getElementById("allWPI").innerHTML;
    num = num.replace(",", ".");
    //    document.getElementById('testLabel').innerHTML = id1 + ' ' + id2;

    var estValue = document.getElementById("hcargovalue").value;
    var estValue1 = estValue;
    var isInsur = GetSelectedId("selectInsur");
    if (isInsur == "0") {
        estValue = 0;
    }
    var isSed = GetSelectedId("selectSed");

    var lfreq = 0;
    var vrr = new Array();
    vrr = document.getElementsByTagName("tr");
    var coutRow = 0;
    for (i = 0; i < vrr.length; i++) {
        if (vrr[i].id.indexOf("lrow") > -1)
            coutRow++;
    }
    for (ss = 1; ss <= coutRow; ss++) {
        if (wpi_1(ss) == 1) {
            lfreq = 1;
            break;
        }
        //tvupdate(i);
    }
    var _lstclc = "calc.aspx?id1=" + id1 + "&id2=" + id2 + "&ct=" + contType + "&cmt=" + cmType + "&n=" + num + "&wht=" + wht + "&est=" + estValue + "&est1=" + estValue1 + "&sed=" + isSed + "&pckp=" + pckp + "&lf=" + lfreq;
    if (lstclc == _lstclc)
        return;
    lstclc = _lstclc;
    document.getElementById("priceFinal").innerHTML = "";
    document.getElementById("l_src").innerHTML = "";
    document.getElementById("l_dst").innerHTML = "";


    ge_id("hLhOrig").value = "0";
    ge_id("hLhRes").value = "0";
    ge_id("hPickupOrig").value = "0";
    ge_id("hPickupRes").value = "0";
    ge_id("puzip").disabled = false;
    
    
    document.getElementById("haprc").value = "0";
    ge_id("labLhResSt").innerHTML = "Please wait..";
    ShowPrReady("Lh", false);
    ShowPrReady("Pickup", false);
    GDownloadUrl(lstclc, function(data, responseCode) {
        // To ensure against HTTP errors that result in null or bad data,
        // always check status code is equal to 200 before processing the data
        needAnim = false;
        if (responseCode == 200) {
            var xml = GXml.parse(data);
            var markers = xml.documentElement.getElementsByTagName("town");
            var qqs = xml.documentElement.getElementsByTagName("qq");
            document.getElementById("qqId").innerHTML = qqs[0].getAttribute("id");

            var _wht = document.getElementById("allWPI").innerHTML;

            var srcName = '';
            var dstName = '';
            var srcZip = '';
            for (var i = 0; i < townsBufSrc.length; i++) {
                if (townsBufSrc[i]) {
                    if (townsBufSrc[i].id == id2) {
                        srcName = townsBufSrc[i].name;
                        srcZip = townsBufSrc[i].zip;
                        break;
                    }
                }
            }
            for (var i = 0; i < townsBufDst.length; i++) {
                if (townsBufDst[i]) {
                    if (townsBufDst[i].id == id1) {
                        dstName = townsBufDst[i].name;
                        break;
                    }
                }
            }
            dstName += ", " + GetSelectedText("selCountry0");

            /*var detHeader = new Array(9);
            detHeader[0] = 'Ocean rate';
            detHeader[1] = 'BAF';
            detHeader[2] = 'DOC fee';
            detHeader[3] = 'SEO fee';
            detHeader[4] = 'MAR fee';
            detHeader[5] = 'Origin Inland Add-on';
            detHeader[6] = 'Service fee';
            detHeader[7] = 'SED';
            detHeader[8] = 'Destination Add-on';

            var detVal = new Array(9);
            detVal[0] = 'bsd';
            detVal[1] = 'baf';
            detVal[2] = 'doc';
            detVal[3] = 'seo';
            detVal[4] = 'mar';
            detVal[5] = 'originadd';
            detVal[6] = 'serfee';
            detVal[7] = 'sed';
            detVal[8] = 'destadd';*/


            for (var i = 0; i < markers.length; i++) {
                var prc = markers[i].getAttribute("price");
                var prcB = markers[i].getAttribute("price_optb");
                if (prc != 0) {
                    document.getElementById("tablePrice").style.display = "";
                    document.getElementById("messagePrice").style.display = "none";
                    document.getElementById("priceFinal").innerHTML = "US$ " + markers[i].getAttribute("price");
                    document.getElementById("haprc").value = markers[i].getAttribute("price");
                    //document.getElementById("priceFinal_B").innerHTML = "US$ " + markers[i].getAttribute("price_optb");
                    var line_haul_price = markers[i].getAttribute("line_haul");
                    var optb_price = markers[i].getAttribute("price_optb");
                    var line_haul_dist = markers[i].getAttribute("lineh_dist");

                    var linkRefer = cmType;
                    var prevLinkReferText = '';
                    var cfsAddr = '';
                    var lhAddr = '';

                    if (i == 0) {
                        var _cfs = markers[i].getAttribute("cfs");
                        var _lh = markers[i].getAttribute("lh");
                        var _cfstown = markers[i].getAttribute("cfstown");
                        var _zip = markers[i].getAttribute("zip"); // cfs zip

                        if (_cfs != '') {
                            cfsAddr = _cfs;
                        }
                        else {
                            cfsAddr = srcName + ", USA ";
                        }

                        if (_lh != '') {
                            lhAddr = _lh;
                        }
                        if ((srcZip == "" || srcZip == null) && _cfstown != null) {
                            var _cfsU = _cfstown.toUpperCase();
                            if (_cfsU.indexOf(srcName) > -1) {
                                srcZip = _zip;
                            }
                        }

                        var _contType = GetSelectedText("selectCont");
                        document.getElementById("priceFinal1").innerHTML = _contType + ': ' + num + ' Cubic Meters, ' + _wht + ' LBS';

                        document.getElementById("priceFinalSrc").innerHTML = _cfstown;
                        document.getElementById("priceFinalSrc1").innerHTML = _cfstown;
                        document.getElementById("priceFinalSrcAddr").innerHTML = cfsAddr;

                        document.getElementById("priceFinalSrc_B").innerHTML = lhAddr.replace('*', '');

                        if (srcZip != null && srcZip != "") {
                            var _srcName = fixZip(srcName, srcZip);
                            document.getElementById("l_src").innerHTML = _srcName + ", USA";
                        }
                        else {
                            document.getElementById("l_src").innerHTML = srcName + ', USA ';
                        }
                        document.getElementById("l_dst").innerHTML = dstName;
                    }

                    var idx = cmType.indexOf('(', 0);
                    if (idx > -1) {
                        var len = cmType.length - idx;
                        linkRefer = cmType.substr(idx, len);
                        prevLinkReferText = cmType.substring(0, idx - 1);
                    }
                    var detTableHtml = addRow('Charge Name', 'Vol', 'Rate/Min', 'US$', "style='background:#DDDDDD'");

                    for (var g = 0; g < detHeader.length; g++) {
                        var _detVal = markers[i].getAttribute(detVal[g]);
                        if (_detVal == "0" || !_detVal)
                            continue;
                        var _isMin = markers[i].getAttribute(detVal[g] + 'ismin');
                        _detVal = _detVal.replace(",", ".");
                        var _val = parseFloat(_detVal);
                        if (g < nSurchEveryCont && !_isMin) {
                            detTableHtml += addRow(detHeader[g], num, '$' + _val, '$' + Math.round(num * _val), '', "det_main" + detVal[g]);
                        }
                        else { // serfee and SED
                            detTableHtml += addRow(detHeader[g] + (_isMin ? ' (min.)' : ''), '&nbsp;', '$' + _val, '$' + _val, '', "det_main" + detVal[g]);
                        }
                        if (detVal[g] == "dsb") {
                            document.getElementById("adsb").value = _val;
                        }
                    }
                    var price_detAB = detTableHtml;


                    document.getElementById("priceDet").innerHTML = "<table class='calclabel' id='tabPriceDet'>" + price_detAB +
                    addRow('Total', '&nbsp;', '&nbsp;', '$' + prc, "style='background:#DDDDDD'");


                    document.getElementById("priceDetLh").innerHTML = "<table class='calclabel' id='tabPriceDetLh'>" + price_detAB.replace(/det_main/g, 'det_mainLh') +
                    addRow("Pickup from Linehaul to CFS", '', '$0', '$0', '', 'det_mainLhRes') +
                    addRow('Total', '&nbsp;', '&nbsp;', '$' + prc, "style='background:#DDDDDD'", 'totalLh') +
                    "</table>";

                    document.getElementById("priceDetPickup").innerHTML = "<table class='calclabel' id='tabPriceDetPickup'>" + price_detAB.replace(/det_main/g, 'det_mainPickup') +
                    addRow("Pickup from <label id='detzip1'></label> to CFS", '', '$0', '$0', '', 'det_mainPickupRes') +
                    addRow('Total', '&nbsp;', '&nbsp;', '$' + prc, "style='background:#DDDDDD'", 'totalPickup') +
                    "</table>";

                    if (line_haul_price != "0") {
                        detTableHtml += addRow("LineHaul inland", line_haul_dist + " mi", '$0.03*lbs', '$' + line_haul_price, '');
                        price_detAB = detTableHtml + addRow('Total', '&nbsp;', '&nbsp;', '$' + prcB, "style='background:#DDDDDD'");
                    }
                }
                else {
                    document.getElementById("tablePrice").style.display = "none";
                    document.getElementById("messagePrice").style.display = "";
                }
                break;
            }
        } else if (responseCode == -1) {
            alert("Data request timed out. Please try later.");
        } else {
            alert("Request resulted in error. Check XML file is retrievable.");
        }
    });
    return;
}
function SetTownSelectMode(isDst, countryId) {
    Clear2Anim();
    if (isDst) {
        selectedCount = 0;
    }
    else {
        selectedCount = 1;
    }

    var i;
    for (i = 0; i <= selectedCount; i++) {
        document.getElementById("selLabel" + i).style.visibility = "";
        document.getElementById("selTown" + i).style.visibility = "";
        document.getElementById("selCountry" + i).style.visibility = "";
        document.getElementById("selimage" + i).style.visibility = "";
        document.getElementById("step" + i + "0").style.display = "";
        document.getElementById("step" + i + "1").style.display = "";

    }
    ShowTowns(countryId, isDst);

}
function GetSelectedId(selId) {
    var objSel = document.getElementById(selId);
    return objSel.options[objSel.selectedIndex].value;
}
function GetSelectedText(selId) {
    var objSel = document.getElementById(selId);
    if (objSel == null)
        return '';
    if (objSel.options.length == 0) {
        return '';
    }
        
    return objSel.options[objSel.selectedIndex].text;
}

function ShowTowns(countryId, isDst) {
//    if (map == null)
//        return;
    if (isDst && (selectedMarkers[0] == null || selectedMarkers[1] == null)) {
        showAnimColor("selTown0");
    }

    ShowMarkers(false, srcMarkers);
    ShowMarkers(false, dstMarkers);

    var setDestId = "&dstid=0";
    if (selectedMarkers[0] != null) {
        setDestId = "&dstid=" + selectedMarkers[0].id;
    }

    var req;
    if (isDst) {
        req = "country=" + countryId;
        if (presel_c > 0) {
            req += "&cnts=1";
            if (presel_ref > 0) {
                req += "&refcntr=1";
            }
        }

    }
    else {  //src
        if (countryId == 0) {
            req = "country=0";
        }
        else {
            req = "townstate=" + countryId;
        }
    }
    ProgrLoad(true);
    GDownloadUrl("coords.aspx?" + req + setDestId, function(data, responseCode) {
        // To ensure against HTTP errors that result in null or bad data,
        // always check status code is equal to 200 before processing the data

        if (responseCode == 200) {
            var xml = GXml.parse(data);
            var markers = xml.documentElement.getElementsByTagName("town");


            ClearMarkers();
            townMarkers = new Array(markers.length - 1);
            var idx = 0;

            if (isDst) {
                idx = 0;
            }
            else {
                idx = 1;
            }

            SelectById("selCountry" + idx, countryId);
            var objSel = document.getElementById("selTown" + idx);
            objSel.style.visibility = "";
            objSel.options.length = 0;
            objSel.options[0] = new Option("Select Nearest City", 0);
            document.getElementById("selimage" + idx).style.visibility = "";
            document.getElementById("step" + idx + "1").style.display = "";

            var townsbuf;
            if (isDst) {
                townsBufDst = new Array(markers.length - 1);
                townsbuf = townsBufDst;
            }
            else {
                townsBufSrc = new Array(markers.length - 1);
                townsbuf = townsBufSrc;
            }
            var zoomChange = 0;
            for (var i = 0; i < markers.length; i++) {
                var point;

                if (i == 0) {
                    point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
                                parseFloat(markers[i].getAttribute("lng")));
                    zoomChange = parseFloat(markers[i].getAttribute("zoom"));
                    // map.setCenter(point, zoom);
                    continue;
                }
                var name = markers[i].getAttribute("name");
                if (idx == 1 && name.indexOf('*') < 0) {
                    name = name.toUpperCase();
                }
                var id = markers[i].getAttribute("id");
                var zip = markers[i].getAttribute("zip");
                objSel.options[i] = new Option(name, id);
                var _isCfs = null;
                if (idx == 1) {
                    if (name.indexOf('*') < 0) {
                        objSel.options[i].className = "cfsopt";
                        _isCfs = 1;
                    }
                }


                townsbuf[i - 1] = new Object();
                townsbuf[i - 1].name = name;
                townsbuf[i - 1].zip = zip;
                townsbuf[i - 1].id = id;
                townsbuf[i - 1].lat = parseFloat(markers[i].getAttribute("lat"));
                townsbuf[i - 1].lng = parseFloat(markers[i].getAttribute("lng"));
                if (!isDst && countryId == 0) {
                    townsbuf[i - 1].parentId = markers[i].getAttribute("parentid");
                }
                else {
                    townsbuf[i - 1].parentId = 0;
                }


                point = new GLatLng(townsbuf[i - 1].lat, townsbuf[i - 1].lng);
                townMarkers[i - 1] = new Object();
                townMarkers[i - 1].marker = createMarker(point, i - 1, 1, name, 0, 0, _isCfs);
                townMarkers[i - 1].id = id;
                if (map != null) {
                    map.addOverlay(townMarkers[i - 1].marker);
                }
            }
            if (!isDst && countryId == 0) {
                RebuildStateFilters();
            }
            if (objSel.options.length == 1) {
                objSel.options[0].text = "No Origin cities found";
                document.getElementById("step6").style.display = "none";
            }
            if (map != null) {
                fitMap(map, townMarkers, zoomChange);
            }
            ProgrLoad(false);
        } else if (responseCode == -1) {
            ProgrLoad(false);
            alert("Data request timed out. Please try later.");
        } else {
            ProgrLoad(false);
            alert("Request resulted in error. Check XML file is retrievable.");
        }
    });

}
function RebuildStateFilters() {
    var selSrc = document.getElementById("selCountry1");
    selSrc.options.length = 0;
    selSrc.options[0] = new Option("Filter by State", 0);
    var iSrc = 0;
    //    ClearMarkers();
    for (var i = 0; i < srcFullMarkers.length; i++) {
        for (var g = 0; g < townsBufSrc.length; g++) {
            if (townsBufSrc[g].parentId == srcFullMarkers[i].id) {
                selSrc.options[iSrc + 1] = new Option(srcFullMarkers[i].name, srcFullMarkers[i].id);
                //                map.addOverlay(srcFullMarkers[i].marker);
                iSrc++;
                break;
            }
        }
    }


}
function createMarker(point, number, isdefault, hint, isCountry, isSrc, isCFS) {
    // new GIcon("http://www.housingmaps.com/mapfiles/marker.png")
    //isCountry = 1  - c, 0 - t, 2 - none

    if (!baseIcon1) {
        baseIcon1 = new GIcon();
        baseIcon1.shadow = "msmarker.shadow.png";
        baseIcon1.image = "red-dot.png";
        baseIcon1.iconSize = new GSize(32, 32);
        baseIcon1.shadowSize = new GSize(59, 32);
        baseIcon1.iconAnchor = new GPoint(15, 32);

        baseIconSrc = new GIcon();
        baseIconSrc.shadow = "msmarker.shadow.png";
        baseIconSrc.image = "blue-dot.png";
        baseIconSrc.iconSize = new GSize(32, 32);
        baseIconSrc.shadowSize = new GSize(59, 32);
        baseIconSrc.iconAnchor = new GPoint(15, 32);

        baseIconDst = new GIcon();
        baseIconDst.shadow = "msmarker.shadow.png";
        baseIconDst.image = "green-dot.png";
        baseIconDst.iconSize = new GSize(32, 32);
        baseIconDst.shadowSize = new GSize(59, 32);
        baseIconDst.iconAnchor = new GPoint(15, 32);

        baseIconCFS = new GIcon();
        baseIconCFS.shadow = "msmarker.shadow.png";
        baseIconCFS.image = "CFS-dot.png";
        baseIconCFS.iconSize = new GSize(32, 32);
        baseIconCFS.shadowSize = new GSize(59, 32);
        baseIconCFS.iconAnchor = new GPoint(15, 32);

    }

    //            baseIcon.infoWindowAnchor = new GPoint(9, 2);
    //            baseIcon.infoShadowAnchor = new GPoint(18, 25);



    var marker = new GMarker(point);

    if (isCFS) {
        markerOptions = { icon: baseIconCFS, title: hint };
        marker = new GMarker(point, markerOptions);
    }
    else {

        if (isdefault) {  // sel town or cnt
            markerOptions = { icon: baseIcon1, title: hint };
            marker = new GMarker(point, markerOptions);
            //                mymarkers[number] = marker;
            //                mymarkersId[number] = markerId;

        }
        else {
            if (isSrc)
                markerOptions = { icon: baseIconSrc, title: hint };
            else
                markerOptions = { icon: baseIconDst, title: hint };
            marker = new GMarker(point, markerOptions);

            //                selectedMarkers[number] = marker;
            //                selectedMarkersId[number] = markerId;

        }
    }



    //            marker.value = number;
    if (isCountry == 1) {
        GEvent.addListener(marker, "click", function() {
            markerCountryClick(number);
        });
    }
    else {
        if (isCountry == 0) {
            GEvent.addListener(marker, "click", function() {
                markerTownClick(number);
            });
        }
    }
    return marker;
}

function initialize(refNum) {
    if (refNum > 0) {
        ShowWorld(1);
        return;
    }

    GoogleMap = 1;
    var needShowCntr = presel_c;
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map_canvas"));
        map.setUIToDefault();
        /*
        GEvent.addListener(map, "tilesloaded", function() {
        jQuery('.gmnoprint').next('div').css('white-space', 'normal');

        });         
        */
        ShowWorld(1);
    }
    if (needShowCntr == 0) {
        SetCountrySelectMode(1);
    }
    else {
        SelectCountry(needShowCntr);
    }
    
}

function f1() {
    listMenu = new FSMenu('listMenu', true, 'display', 'block', 'none');

    listMenu.animations[listMenu.animations.length] = FSMenu.animFade;
    //    listMenu.animations[listMenu.animations.length] = FSMenu.animSwipeDown;
    listMenu.animations[listMenu.animations.length] = FSMenu.animClipDown;
    listMenu.animInSpeed = 0.2;
    listMenu.animOutSpeed = 0.2;

    arrow = null;
    if (document.createElement && document.documentElement) {
        arrow = document.createElement('span');
        arrow.appendChild(document.createTextNode('>'));
        arrow.className = 'subind';
    }
}
function mymenu(refNum) {
    if (refNum > 0)
        return;
    f1();
    listMenu.activateMenu("listMenuRoot", arrow);
}
function myinitialize(refNum) {
    
    initialize(refNum);
    mymenu(refNum);
}
function getSty(elN) {
    return document.getElementById(elN).style;
}
function show_el(o, ctrlName) {
    getSty(ctrlName).display = (o == 1) ? 'block' : 'none';
}
function nt_h(o, ctrlName) {
    getSty(ctrlName).display = (o.value == 1) ? 'block' : 'none';
}
function nt_h1(o, ctrlName) {
    getSty(ctrlName).display = (o.value == 1) ? '' : 'none';
}

function sel_hide01(o, ctrlName) {
    getSty(ctrlName + o.value).display = 'none';
    getSty(ctrlName + (o.value == 1 ? "0" : "1")).display = 'block';
}

function animBkColor(elId, newcolor, interval) {
    if (!needAnim) {
        return;
    }
    var el1 = "#" + elId;
    $(el1).animate({
        backgroundColor: newcolor
    }, interval);
}

function timedCount() {
    var elId = elIdColor;
    var el = document.getElementById(elId);
    if (alternateAnimElIdColor != '') {
        SetBkColor(elIdColor, "White");
        SetBkColor(alternateAnimElIdColor, "White");
        if (cntColor == 0) {
            SetBkColor(alternateAnimElIdColor, color_blink);
            cntColor = 1;
        }
        else {
            SetBkColor(elIdColor, color_blink);
            cntColor = 0;
        }
        timerRunning = true;
        altCount++;
        if (altCount == 2) {
            timeColor = setTimeout("altPause()", 1700);
            altCount = 0;
        }
        else {
            timeColor = setTimeout("timedCount()", 1700);
        }
        return;
    }
    if (el) {
        if (cntColor == 0) {
            el.style.backgroundColor = color_blink;
            cntColor = 1;
        }
        else {
            el.style.backgroundColor = "White";
            cntColor = 0;
        }
    }
    timerRunning = true;
    timeColor = setTimeout("timedCount()", 1700);
}

function showAnimColor(elId) {
    if (elIdColor != "" && elIdColor != null) {
        clearBkColorId(elIdColor);
    }
    cntColor = 0;
    timerRunning = false;
    if (!needAnim)
        return;

    elIdColor = elId;
    timerRunning = true;
    timeColor = setTimeout("timedCount()", 100);
    // clearBkColor(elId);
}
function pause(millis) {
    var date = new Date();
    var curDate = null;

    do {
        curDate = new Date();
    }
    while (curDate - date < millis)
}
function clearBkColorId(elId) {
    if (timerRunning && (elIdColor == elId || elIdColor == "")) {
        clearTimeout(timeColor);
        elIdColor = "";
    }
    timerRunning = false;
    var el = document.getElementById(elId);
    el.style.backgroundColor = "White";

}
function SetBkColor(elId, newcolor) {
    var el = document.getElementById(elId);
    if (el) {
        setbkcl(el, newcolor);
    }
}
function setbkcl(el, newcolor) {
    el.style.backgroundColor = newcolor;
}

function clearBkColor(el) {
    if (timerRunning && el.id == elIdColor) {
        clearTimeout(timeColor);
        elIdColor = "";
    }
    timerRunning = false;

    el.style.backgroundColor = "White";
}
function em_valid(m) {
    if (!m)
        return false;
    if (m == '')
        return false;
    if (m.indexOf(',', 0) > -1)
        return false;
    if (m.indexOf(' ', 0) > -1)
        return false;
    if (m.indexOf(';', 0) > -1)
        return false;
    var reg = /[\w-]+@([\w-]+\.)+[\w-]+/;
    if (!reg.test(m)) {
        return false;
    }
    return true;
}
function isBlank(val) {
    if (val == null) { return true; }
    for (var i = 0; i < val.length; i++) {
        if ((val.charAt(i) != ' ') && (val.charAt(i) != "\t") && (val.charAt(i) != "\n") && (val.charAt(i) != "\r")) { return false; }
    }
    return true;
}

function isInteger(val) {
    if (isBlank(val)) { return false; }
    for (var i = 0; i < val.length; i++) {
        if (!isDigit(val.charAt(i))) { return false; }
    }
    return true;
}
function isDigit(num) {
    if (num.length > 1) { return false; }
    var string = "1234567890";
    if (string.indexOf(num) != -1) { return true; }
    return false;
}
function elHasClass(elName, classVal) {
    var el = document.getElementById(elName);
    if (!el)
        return false;
    if (el.className == '' || !el.className) {
        return false;
    }
    var ar = el.className.split(" ");
    for (var i = 0; i < ar.length; i++) {
        if (ar[i] == classVal)
            return true;
    }
    return false;
}
function Clear2Anim() {
    SetBkColor("cmtype", "White");
    alternateAnimElIdColor = '';
}
function altPause() {
    SetBkColor(elIdColor, "White");
    SetBkColor(alternateAnimElIdColor, "White");
    timeColor = setTimeout("timedCount()", 3400);
}
function GetContainers() {
    var el = document.getElementById("selectCont");
    el.style.display = "none";
    var req = "container=1&id1=" + selectedMarkers[0].id + "&id2=" + selectedMarkers[1].id;

    GDownloadUrl("coords.aspx?" + req, function(data, responseCode) {
        // To ensure against HTTP errors that result in null or bad data,
        // always check status code is equal to 200 before processing the data
        var objSel = document.getElementById("selectCont");
        objSel.style.display = "";

        objSel.options.length = 0;
        if (responseCode == 200) {
            var xml = GXml.parse(data);
            var markers = xml.documentElement.getElementsByTagName("town");

            var i_beg = 1;
            if (markers.length < 1) {
                objSel.options[0] = new Option("Select type of goods", "0");
            }
            else {
                i_beg = 0;
            }
            for (var i = 0; i < markers.length; i++) {
                var name = markers[i].getAttribute("name");
                var id = markers[i].getAttribute("id");
                objSel.options[i + i_beg] = new Option(name, id);
            }
            document.getElementById("step6").style.display = "none";
        } else if (responseCode == -1) {
            alert("Data request timed out. Please try later.");
        } else {
            alert("Request resulted in error. Check XML file is retrievable.");
        }
    });

}
function getElementHeight(id) {
    var el = document.getElementById(id);
    if (el) {
        var h = 0;
        try {
            h = el.offsetHeight;
        }
        catch (err) {
            h = 0;
        }
        return h;
    }
    return 0;
}
function DsbFee(value) {
    var res = Math.round(value * DisbursementCoef + 0.49);
    if (res < DisbursementMin) {
        res = DisbursementMin;
    }
    return res;
}
function SecurDepos(value) {
    var res = Math.floor((value) * 0.75 + 0.5);   
    return res;
}
function findPosX(obj) {
    var curleft = 0;
    if (obj.offsetParent) {
        while (1) {
            curleft += obj.offsetLeft;
            if (!obj.offsetParent) {
                break;
            }
            obj = obj.offsetParent;
        }
    } else if (obj.x) {
        curleft += obj.x;
    }
    return curleft;
}
function ge_id(nam) {
    return document.getElementById(nam);
}
function hide_el(nam, hide) {
    var el = ge_id(nam);
    if (el) {
        el.style.display = hide == 1 ? '' : 'none';
    }
}
function findPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent) {
        while (1) {
            curtop += obj.offsetTop;
            if (!obj.offsetParent) {
                break;
            }
            obj = obj.offsetParent;
        }
    } else if (obj.y) {
        curtop += obj.y;
    }
    return curtop;
}

function yrc_o(val) {
    var ar = val.split(",");
    var o = new Object();
    if (ar.length == 2) {
        o.orig = parseInt(ar[0], 10);
        o.res = parseInt(ar[1], 10);
    }
    else {
        o.orig = 0;
        o.res = 0;
    }
    return o;
    
}
function winHeight() {
    var myWidth = 0, myHeight = 0;
    if (typeof (window.innerWidth) == 'number') {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }
    return myHeight;
}
