﻿var map = null;
var baseIcon1 = null;
var baseIconSrc = null;
var baseIconDst = null;
var srcMarkers = null;
var srcFullMarkers = null;
var dstMarkers = null;
var isDstActive = 1;

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 fclFullPath = "";
var HHGPath = "";
var pathModifier = "";
var aNCars = new Array(10);
var zipIni = 'Enter ZIP Code or City';
var alternateAnimElIdColor = ''; 
var altCount = 0;
var presel_c = 0;
var PathHHG = "";
var PathCARS = "";
var PathFCL = "";
var PathRORO = "";
var PathLCL = "";
var refUrl = "";
var presel_ref = 0;

function fillCarsInfo(showAlert) {
    var selCars = document.getElementById("selectCont");
    var ncars = aNCars[selCars.selectedIndex];
    if (ncars == 0 || ncars > 5) {
        if (showAlert) {
            alert('Invalid count of cars');
        }
        return 0;
    }
    var names = new Array(7);

    names[0] = "Zip";
    names[1] = "Make";
    names[2] = "Model";
    names[3] = "Year";
    names[4] = "Price";
    names[5] = "Insurance";
    var zipall = document.getElementById("zipall");
    zipall.value = "";

    var ar = document.getElementsByTagName("input");
    for (var i = 0; i < ar.length; i++) {
        if (ar[i].type == 'text') {
            ar[i].onfocus = function() {
                if (this.id != null) {
                    if (this.id.substr(0, 3) == "Zip") {
                        if (this.value == zipIni) {
                            this.value = '';
                        }
                    }
                }
                setbkcl(this, "White");
            };
        }
    }
    var isEmpty = false;
    
    var d = new Date();
    var curr_year = d.getFullYear();

    for (var g = 1; g <= ncars; g++) {
        for (var i = 0; i < 6; i++) {
            if (i < 5) {
                var elTxtDst = document.getElementById("h" + names[i] + g);
                var elTxtSrc = document.getElementById(names[i] + g);
                if (elTxtSrc.id.substr(0, 3) == "Zip") {
                    var trZip = document.getElementById('trZip' + g);
                    if (trZip.style.display != 'none') {
                        if (elTxtSrc.value == '' || !elTxtSrc.value || elTxtSrc.value == zipIni) {
                            setbkcl(elTxtSrc, errClr);
                            isEmpty = true;
                        }
                        else {
                            zipall.value = "1";
                        }
                    }
                }
                else {
                    if (elTxtSrc.value == '' || !elTxtSrc.value) {
                        setbkcl(elTxtSrc, errClr);
                        isEmpty = true;
                    }
                }
                elTxtDst.value = elTxtSrc.value;
            }
            else {
                document.getElementById("h" + names[i] + g).value = GetSelectedId(names[i] + g);
            }
        }
        if (GetSelectedId("cbPickup" + g) == "0") {
            document.getElementById("hZip" + g).value = "";
        }

        var el_car = document.getElementById("Year" + g);
        if (el_car.value != '' && el_car.value && el_car.value.toLowerCase()!="n/a") {
            var el_value = parseInt(el_car.value, 10);
            if (!isNaN(el_value) && isInteger(el_car.value)) 
            {
                if (el_value < 1900 || el_value > curr_year) {
                    el_car.value = "N/A";
                }
            }
            else {
                el_car.value = "N/A";
            }
        }
        var el_car = document.getElementById("Price" + g);
        if (el_car.value != '' && el_car.value && el_car.value.toLowerCase() != "n/a") {
            var el_value = parseInt(el_car.value, 10);
            if (!isNaN(el_value) && isInteger(el_car.value)) 
            {
                if (el_value < 0) {
                    setbkcl(el_car, errClr);
                    if (showAlert) {
                        alert('Invalid Price');
                    }
                    return 0;
                }
            }
            else {
                setbkcl(el_car, errClr);
                if (showAlert) {
                    alert('Invalid Price');
                }
                return 0;
            }
        }

    }
    var displ = zipall.value == "1" ? "" : "none";

    for (var g = 1; g <= 3; g++) {
        document.getElementById("optb_" + g).style.display = displ;
    }

    if (isEmpty) {
        if (showAlert) {
            alert('All fields are mandatory. Please fill out all missed fields');
        }
        return 0;
    }

    return 1;
}

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 = "";
                }
            }
            
        }
        document.getElementById("cont_info").style.display = "";
        if (showToStep == 3 || showToStep == 5) {
            clearBkColorId("selectCont");            
            gotobot();
        }
        if (showToStep == 4) {
            document.getElementById("step4").style.display = "";
        }

        if (showToStep == 3 && document.getElementById("selectCont").selectedIndex > 0) {
            CheckSteps(4);
            document.getElementById("hncars").value = aNCars[document.getElementById("selectCont").selectedIndex];
            document.getElementById('point2').scrollIntoView(true);
        }
        else {
            document.getElementById("hncars").value = "0";
        }
        if (showToStep == 4 && document.getElementById("cmtype").selectedIndex > 0) {
            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";
                }
            }
        }
        document.getElementById("step4").style.display = "none";
        document.getElementById("cont_info").style.display = "none";
        document.getElementById("step41").style.visibility = "hidden";
        document.getElementById("hintimg0").style.visibility = "hidden";
        document.getElementById("hintimg1").style.visibility = "hidden";
        
    }
}
function CheckShowPrice() {
    var el = document.getElementById("step6");
    if (!CalcPrice(0)) {
        if (el) {
            el.style.display = "none";
        }
    }
    else {
        if (el) {
            el.style.display = "";
        }
    }
}
function CalcPrice(showAlert) {
    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 container');
        }
        return 0;
    }
    if (!fillCarsInfo(showAlert))
        return 0;
   
    GetDist();
    document.getElementById("step6").style.display = "";
    //alert('222');
    document.getElementById('point1').scrollIntoView(true);
    return 1;
}
function SlidePrice() {
    var el = document.getElementById("bda1");
    var elPriceDet = document.getElementById("priceDet");
    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").slideToggle("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();
    SetBkColor("selCountry0", "White");
    SetBkColor("cmtype", "White");
    if (isDstActive) {
        ShowTowns(dstMarkers[value].id, 1);
    }
    else {
        ShowTowns(srcMarkers[value].id, 0);
    }
}


function aaa() {
    ShowTowns();
}

function ChangeDestCountry() {
    SetBkColor("selCountry0", "White");
    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);
    }
    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 ChangeContainer() {
    var countType = GetSelectedId("selectCont");
    if (countType != "start") {
        CheckSteps(4);
        var commType = GetSelectedId("cmtype");
        if (commType == "start") {
            showAnimColor("cmtype");
        }
    }
    var chk = document.getElementById("numCont").value;
    CheckShowPrice();
    if (!chk || chk == "" || firstChContType)
    {
        showAnimColor("numCont");
    }
    if (firstChContType) {
        document.getElementById("step6").style.display = "none";
    }
    first_changeCommodity = 0;
    firstChContType = false;
}
function ChangeCommodity() {
    var countType = GetSelectedId("cmtype");
    if (countType == "CARS") {
        showAnimColor("selectCont");
        CheckShowPrice();
        return;
    }
    if (countType == "HHG") {
        document.location = PathHHG;
        return;
    }
    if (countType == "LCL") {
        var addLink = "";
        if (refUrl.length > 0) {
            addLink = "home.aspx?" + refUrl;
        }
        document.location = PathLCL + addLink;
        return;
    }
    if (countType == "FCL") {
        document.location = PathFCL;
        return;
    }
    if (countType == "RORO") {
        document.location = PathRORO;
        return;
    }       
}
function ChangeCommodity1() {
    var countType = GetSelectedId("cmtype");
/*    if (countType != "start" && countType.substr(0, 3) == "Com") {
        CheckSteps(4);
    }*/
    if (countType != "start" && countType.substr(0, 5) != "Motor") {
        if (countType.substr(0, 3) == "Com") {
            document.location = fclFullPath;
            return;
        }
        document.location = HHGPath;
        return;
    }
    var contType = GetSelectedId("selectCont");
    if (contType == "start") {
        showAnimColor("selectCont");
    }

    CheckShowPrice();
}

function ChangeDestTown() {
    SetBkColor("selCountry0", "White");
    selectedCount = 0;
    var objSel = document.getElementById("selTown0");
    SelectTown(objSel.options[objSel.selectedIndex].value, 0);
    SetCountrySelectMode(false);
    if (selectedCount < 2) {
        selectedCount++;
    }
    CheckShowPrice();
}
function ChangeSrcTown() {
    SetBkColor("selCountry0", "White");
    SetBkColor("selCountry1", "White");
    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) {
    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--;
       }
       map.setZoom(zoom);
   }
   else {
     map.setZoom(10);
   }
   map.setCenter(bounds.getCenter());
}

function ShowWorld(isDst) {
//    if (map == null)
//        return;
    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;

                        srcFullMarkers[iSrc] = new Object();
                        srcFullMarkers[iSrc].marker = createMarker(point, iSrc, 1, name, 1, 0);
                        srcFullMarkers[iSrc].id = id;
                        srcFullMarkers[iSrc].name = name;

                        selSrc.options[iSrc + 1] = new Option(name, id);
                        if (map != null) {
                            map.addOverlay(srcMarkers[iSrc].marker);
                        }
                        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) {
                var tc = presel_c;
                presel_c = 0;

                SelectCountry(tc);
            }

        });
    }

    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) {
        fitMap(map, arr);
    }
}

function ClearMarkers() {
    if (townMarkers) {
        for (var i = 0; i < townMarkers.length; i++) {
            if (townMarkers[i]) {
                if (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) {
    Clear2Anim();
    var value = -1;
    document.getElementById("shp_img").style.display = "none";
    SetBkColor("selCountry0", "White");

    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 (selectedIdx == 1) {
        clearBkColorId("selTown1");
        clearBkColorId("selTown0");
        if (selectedMarkers[0] != null && selectedMarkers[1] != null) {
            GetContainers();
        }
        
    }

    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");
                }
            }
        }
    }
    var commType1 = GetSelectedId("selectCont");
    if (selectedMarkers[0] != null && selectedMarkers[1] != null && commType1 == "0") {
        showAnimColor("selectCont");
    }
   
    if (selectedIdx == 0) {
        CheckSteps(4);
    }



}
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");

            objSel.options[0] = new Option("Select type of load", "0");
            aNCars[0] = 0;
            for (var i = 0; i < markers.length; i++) {
                var name = markers[i].getAttribute("name");
                var id = markers[i].getAttribute("id");
                objSel.options[i + 1] = new Option(name, id);
                aNCars[i + 1] = markers[i].getAttribute("ncars");
            }
        } else if (responseCode == -1) {
            alert("Data request timed out. Please try later.");
        } else {
            alert("Request resulted in error. Check XML file is retrievable.");
        }
    });    
    
}
function ShowCarsinfo()
{
    var objSel = document.getElementById("selectCont");
    if (objSel) {
        for (var i=2; i<=5; i++)
        {
            var el = document.getElementById("carsinfo" + i);
            el.style.display = i<=aNCars[objSel.selectedIndex] ? "" : "none";
            
        }
    }
}

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);
        fitMap(map, selectedMarkers);
    }
    if (!isDst && selectedMarkers[1] == null) {
        selCountry1.selectedIndex = 0;
    }
}
function addRow(c1, c2, c3, c4, style_add) {
    return '<tr ' + style_add + '><td align="left">' + c1 + '</td><td align="center">' + c2 + '</td><td align="center">' + c3 + '</td><td align="right">' + c4 + '</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 GetDist() {
    var id1 = selectedMarkers[0].id
    var id2 = selectedMarkers[1].id;
    var contType = GetSelectedId("selectCont");
//    document.getElementById('testLabel').innerHTML = id1 + ' ' + id2;
    document.getElementById("priceFinal").innerHTML = "";
    document.getElementById("l_src").innerHTML = "";
    document.getElementById("l_dst").innerHTML = "";
    var insAll = '';
    var priceAll = '';
    var sedAll = '';
    
    var selCars = document.getElementById("selectCont");
    var ncars = aNCars[selCars.selectedIndex];
    
    if (ncars == 0)
        ncars = 1;
    sedAll = sedAll + GetSelectedId("g_sed") + ";";
    for (var iv = 1; iv <= ncars; iv++) {
        var el = document.getElementById("Insurance" + iv);
        if (el) {
            var ins = GetSelectedId("Insurance" + iv);
            insAll = insAll + ins + ";";
            
            priceAll = priceAll + document.getElementById("Price" + iv).value + ";";
        }
    }

    GDownloadUrl("calc.aspx?id1=" + id1 + "&id2=" + id2 + "&ct=" + contType + "&ins=" + insAll + "&prc=" + priceAll + "&sed=" + sedAll, 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 contTypeText = GetSelectedText("selectCont");
        if (responseCode == 200) {
            var xml = GXml.parse(data);
            var num = 1;
            var markers = xml.documentElement.getElementsByTagName("town");

            var srcName = '';
            var dstName = '';
            for (var i = 0; i < townsBufSrc.length; i++) {
                if (townsBufSrc[i]) {
                    if (townsBufSrc[i].id == id2) {
                        srcName = townsBufSrc[i].name;
                        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(4);
            detHeader[0] = 'Ocean rate';
            detHeader[1] = 'Service fee';
            detHeader[2] = 'Insurance';
            detHeader[3] = 'Fee for filling SED';

            var detVal = new Array(8);
            detVal[0] = 'bsd';
            detVal[1] = 'serfee';
            detVal[2] = 'ins';
            detVal[3] = 'sed';


            for (var i = 0; i < markers.length; i++) {
                var prc = markers[i].getAttribute("price");
                if (prc != 0) {
                    document.getElementById("tablePrice").style.display = "";
                    document.getElementById("messagePrice").style.display = "none";
                    document.getElementById("priceFinal").innerHTML = "US$ " + markers[i].getAttribute("price");

                    var linkRefer = '';
                    var prevLinkReferText = '';
                    /*                    var idx = cmType.indexOf('(', 0);
                    if (idx > -1) {
                    var len = cmType.length - idx;
                    linkRefer = cmType.substr(idx, len);
                    prevLinkReferText = cmType.substring(0, idx - 1);
                    }*/
                    document.getElementById("priceFinal1").innerHTML = contTypeText + ' ' + prevLinkReferText + ' <a href="ReferencesShippingSeaFreightUsa.aspx" target="_blank">' + linkRefer + '</a';
                    document.getElementById("priceFinalSrc").innerHTML = srcName + ', USA';
                    document.getElementById("l_src").innerHTML = srcName + ', USA';
                    document.getElementById("l_dst").innerHTML = dstName;
                    var detTableHtml = addRow('Charge Name', 'Num', 'Rate/Min', 'US$', "style='background:#DDDDDD'");

                    for (var g = 0; g < detHeader.length; g++) {
                        var _detVal = markers[i].getAttribute(detVal[g]);
                        if (_detVal == "0")
                            continue;
                        var _val = parseFloat(_detVal);
                        if (g < 6) {
                            detTableHtml += addRow(detHeader[g], num, '$' + _val, '$' + num * _val, '');
                        }
                        else { // serfee and SED
                            detTableHtml += addRow(detHeader[g], '&nbsp;', '$' + _val, '$' + _val, '');
                        }
                    }
                    detTableHtml += addRow('Total', '&nbsp;', '&nbsp;', '$' + prc, "style='background:#DDDDDD'");

                    document.getElementById("priceDet").innerHTML = "<table class='calclabel' id='tabPriceDet'>" + detTableHtml + "</table>";
                }
                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);
    return objSel.options[objSel.selectedIndex].text;
}

function Clear2Anim() {
    SetBkColor("cmtype", "White");
    alternateAnimElIdColor = '';
}
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;
        }
    }


    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;
                //alert('get src ' + req + setDestId);
            }

            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")));
                    var zoom = parseFloat(markers[i].getAttribute("zoom"));
                    // map.setCenter(point, zoom);
                    continue;
                }
                var name = markers[i].getAttribute("name");
                var id = markers[i].getAttribute("id");
                objSel.options[i] = new Option(name, id);


                townsbuf[i - 1] = new Object();
                townsbuf[i - 1].name = name;
                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);
                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";
            }
            fitMap(map, townMarkers);
        } else if (responseCode == -1) {
            alert("Data request timed out. Please try later.");
        } else {
            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) {
    // 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);

    }

    //            baseIcon.infoWindowAnchor = new GPoint(9, 2);
    //            baseIcon.infoShadowAnchor = new GPoint(18, 25);



    var marker = new GMarker(point);
    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();
        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.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() {
    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) {
    var el1 = "#" + elId;
    $(el1).animate({
    backgroundColor: newcolor
    }, interval);
}

function altPause()
{
    SetBkColor(elIdColor, "White");
    SetBkColor(alternateAnimElIdColor, "White");
    timeColor = setTimeout("timedCount()", 3400);        
}

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;
    
    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;
    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 ProgrLoad(val)
{
    var el = document.getElementById("tdload");
    var el1 = document.getElementById("tdload1");
    if (val) {
        el.style.display = "";
        el1.style.display = "none";
    }
    else 
    {
        el.style.display = "none";
        el1.style.opacity = 0;
        el1.style.display = "";
        $('#tdload1').animate({
			opacity: 1
		}, 600 );
    }
}

