﻿//
//
//

function focusOn(element){
    if(document.getElementById(element))
    {
        setTimeout("document.getElementById('" + element + "').focus();",100);
    }  
}

function submitenter(myfield,e){
    var keycode;
    
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    else return true;

    if (keycode == 13)
       {
           myfield.form.submit();
           return false;
       }
    else
       return true;
}

function debug(msg) {

    if ((_console == null) || (_console.closed)) {
        _console = window.open("","console","width=800,height=600,resizable");
        _console.document.open("text/plain");
    }
    
    _console.focus();
    _console.document.writeln(msg);
}

//
// form validation
//

function isNotEmpty(txt)
{
    if(txt == null || txt.length == 0) {
        return false;
    } else {
        return true;
    }
}

function isNumber(str)
{
    var re = /^[-]?\d*\.?\d*$/;
    str = str.toString();
    if (!str.match(re)) {return false;}
    return true;
}

//
// map functions
//

function minimizer()
{
    if ($("#divMinimize").html() == "Minimize")
    {$("#divMinimize").html("Maximize");}
    else
    {$("#divMinimize").html("Minimize");}
}

function loadPublicMap(_lat,_lng,strXML){
    if (GBrowserIsCompatible()) {
	
        map = new GMap2(document.getElementById("divMap"));
        map.setCenter(new GLatLng(_lat,_lng), 9);
        map.addControl(new GLargeMapControl());
        map.enableScrollWheelZoom();
		
		var markers = GXml.parse(strXML).getElementsByTagName("marker");
		
		for (var i = 0, il = markers.length; i < il; i++) {
		
            var Icon = new GIcon();
            Icon.image = markers[i].getAttribute("png_path");
            Icon.iconSize = new GSize(23,36);
            Icon.iconAnchor = new GPoint(11,36);
            Icon.mozPrintImage = markers[i].getAttribute("gif_path");
            
			var point = new GLatLng(markers[i].getAttribute("latitude"),markers[i].getAttribute("longitude"));
			var marker = new GMarker(point,Icon);
			
			map.addOverlay(marker);
		
		}
		
    }
}

function loadMap(lat,lng,zoom) {
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("divMap"));
        geocoder = new GClientGeocoder();
        map.setCenter(new GLatLng(39.810556,-98.556111), 4);
        map.addControl(new GLargeMapControl());
        map.enableScrollWheelZoom();
    }
}

function toggleCustomMarker(name,showIt) {

    var index;

    for (var i = 0, il = gmarkers.length; i < il; i++) {
        
        if ((gmarkers[i].myCategory == "custom") && (gmarkers[i].safeName == name)) {
            index = i;
            gmarkers[i].isChecked = showIt;
            break;
        }
    
    }
    
    if (showIt) {
        gmarkers[index].show();
        gmarkers[index].openInfoWindowHtml(gmarkers[index].myHTML,{maxWidth:250});
    } else {
        gmarkers[index].hide();
        gmarkers[index].closeInfoWindow();
        map.returnToSavedPosition();
    }

}

function toggleMarker(ID,showIt,category) {

    var index;

    for (var i = 0, il = gmarkers.length; i < il; i++) {
        
        if ((gmarkers[i].myCategory == category) && (gmarkers[i].myID == ID)) {
            index = i;
            break;
        }
    
    }
         
    switch (category) {
    
        case "hotel":
            
            if (showIt) {
                gmarkers[index].show();
                gmarkers[index].openInfoWindowHtml(gmarkers[index].myHTML,{maxWidth:250});
                totalCheckedHotels++;
            } else {
                gmarkers[index].hide();
                gmarkers[index].closeInfoWindow();
                totalCheckedHotels--;
                map.returnToSavedPosition();
            }
                              
            var eleChk = document.getElementById('chkAllHotels');
            
            if (totalCheckedHotels == totalHotels) {
                if (!eleChk.checked) {
                    eleChk.checked = true;
                }
                
            } else if (eleChk.checked) {
                eleChk.checked = false;
            }
            
            break;
            
        case "attraction":
        
            if (showIt) {
                gmarkers[index].show();
                gmarkers[index].openInfoWindowHtml(gmarkers[index].myHTML,{maxWidth:250});
                totalCheckedAttractions++;
            } else {
                gmarkers[index].hide();
                gmarkers[index].closeInfoWindow();
                totalCheckedAttractions--;
                map.returnToSavedPosition();
            }

            var eleChk = document.getElementById('chkAllAttractions');

            if (totalCheckedAttractions == totalAttractions) {
                
                if (!eleChk.checked) {
                    eleChk.checked = true;
                }
                
            } else if (eleChk.checked) {
                eleChk.checked = false;
            }
            
            break;
    }   
}

function addCustomAddress(){
    if (geocoder) {
    
        var booContinue = true;
        var ErrArray = new Array();
        
        if (!isNotEmpty($("#txtMarkerName").val()))
        {
            booContinue = false;
            ErrArray.push("Marker title is required.");
        }
        
        if (!isNotEmpty($("#txtMarkerAddress").val()))
        {
            booContinue = false;
            ErrArray.push("Address is required.");
        }
        
        if (!isNotEmpty($("#txtMarkerCity").val()))
        {
            booContinue = false;
            ErrArray.push("City is required.");
        }
        
        if ($(".selMarkerState option:selected").val() == 255)
        {
            booContinue = false;
            ErrArray.push("State is required.");
        }
        
        var zipCode = $("#txtMarkerZIP").val();
        
        if (!(isNumber(zipCode) && zipCode > 0 && zipCode.length == 5))
        {
            booContinue = false;
            ErrArray.push("ZIP code is invalid.");
        }
        
        var strPhone = $("#txtMarkerPhone").val();
                
        if (isNotEmpty(strPhone))
        {
            strPhone = strPhone.replace(/[\(\)\.\-\ ]/g,"");
            
            if (!(isNumber(strPhone) && strPhone.length == 10))
            {
                booContinue = false;
                ErrArray.push("Phone number is invalid.");
            }
        }
        
        if (!booContinue){alert(ErrArray.join("\n"));}
        else
        {
            var geoAddress = $("#txtMarkerAddress").val() + ", " + $("#txtMarkerCity").val() + ", " + $(".selMarkerState option:selected").text() + " " + $("#txtMarkerZIP").val();

            geocoder.getLatLng(geoAddress,
                function(point) {
                    if (!point) {
                        alert("The specified address was not found.");
                    } else {

                        $('#divAddCustomMarker').slideUp('slow');
                        var marker = new GMarker(point,gicons[8]);
                        marker.myName = $("#txtMarkerName").val();
                        marker.safeName = strToJS(marker.myName);
                        marker.myID = 0;
                        marker.myCategory = "custom";
                        marker.myAddress = $("#txtMarkerAddress").val();
                        marker.myCity = $("#txtMarkerCity").val();
                        marker.myState = $(".selMarkerState option:selected").val();
                        marker.myZIP = $("#txtMarkerZIP").val();
                        marker.myDescription = $("#txtMarkerDescription").val();
                        marker.myPhone = strPhone;
                       
                        var strURL = $("#txtMarkerURL").val();
                        
                        if (isNotEmpty(strURL) && !(strURL.substring(0,7) == "http://")){strURL = "http://" + strURL;}
                        
                        marker.myURL = strURL;
                        marker.myLat = point.lat();
                        marker.myLng = point.lng();
                        marker.isChecked = true;
                        
                        var txtArray = new Array();
                        
                        txtArray.push('<div class="markerInfo"><h3>');
                        txtArray.push(marker.myName);
                        txtArray.push('</h3>');
                        txtArray.push(marker.myDescription);
                        txtArray.push('<br /><br />');
                        txtArray.push(marker.myAddress);
                        txtArray.push('<br />');
                        txtArray.push(marker.myCity);
                        txtArray.push(', ');
                        txtArray.push($(".selMarkerState option:selected").text());
                        txtArray.push(' ');
                        txtArray.push(marker.myZIP);
                        txtArray.push('<br /><br />');
                        
                        if (isNotEmpty(marker.myPhone))
                        {               
                            strPhone = marker.myPhone.substr(0,3) + "." + marker.myPhone.substr(3,3) + "." + marker.myPhone.substr(6);
                            txtArray.push(strPhone);          
                            txtArray.push('<br />');
                        }
                        
                        if (isNotEmpty(marker.myURL))
                        {
                            txtArray.push('<a href="');
                            txtArray.push(marker.myURL);
                            txtArray.push('" target="_blank">Visit Website</a>');
                        }
                        
                        txtArray.push('</div>');
                        
                        marker.myHTML = txtArray.join('');

                        marker.bindInfoWindowHtml(marker.myHTML,{maxWidth:250});
                        map.addOverlay(marker);
                        marker.openInfoWindowHtml(marker.myHTML,{maxWidth:250});
                        gmarkers.push(marker);
                                               
                        txtArray = new Array();
                        
                        txtArray.push('<tr onmouseover="this.bgColor=\'Gray\';" onmouseout="this.bgColor=\'#E6E6E6\';"><td class="tdCheckCustom"><input type="checkbox" class="chkCustom" value="0" checked="true" onclick="toggleCustomMarker(\'');
                        txtArray.push(marker.safeName);
                        txtArray.push('\',this.checked);" /></td><td class="tdCustomTitle">');
                        txtArray.push(marker.myName);
                        txtArray.push('</td><td><input type="image" onclick="$(this).parent().parent().slideUp(\'slow\');deleteCustomAddress(\'');
                        txtArray.push(marker.safeName);
                        txtArray.push('\');" style="padding-right:6px;" src="images/icon_delete.png" /></td></tr>');

                        $("#tblCustom").append(txtArray.join(''));
                        txtArray = null;
                    
                    }
                }
            );
        }
        ErrArray = null;
    }
}

function deleteCustomAddress(name){

    for (var i = 0, il = gmarkers.length; i < il; i++) {
        
        if ((gmarkers[i].myCategory == "custom") && (gmarkers[i].myID == 0) && (gmarkers[i].safeName == name)) {
            gmarkers[i].hide();
            gmarkers[i].closeInfoWindow();
            gmarkers.splice(i,1);
            break;
        }
    
    }
    
}

function saveMap(){
    if (isNotEmpty($("#txtMapName").val()))
    {
        if(editingExistingMap == true){ajaxUpdateMap();}
        else{ajaxInsertMap();}
    }
    else{alert("Map name is required.");}

}

function ajaxUpdateMap(){

    var addAttractions = new Array();
    var addHotels = new Array();
    var delAttractions = new Array();
    var delHotels = new Array();
    var mapCustomAddresses = new Array();

    $('.chkAttractions').each(function(){ 
    
        if (this.checked){
        
            var booFound = false;
            
            for (var i = 0, il = chkdAttractions.length; i < il; i++) {
                             
                if (this.value == chkdAttractions[i]) {
                    booFound = true;
                    break;
                }

            }
            
            if(booFound == false){
                addAttractions.push(this.value);
            }
            
        }
        else{
           
            for (var i = 0, il = chkdAttractions.length; i < il; i++) {
                             
                if (this.value == chkdAttractions[i]) {
                    delAttractions.push(this.value);
                    break;
                }

            }
            
        }
    
    });
    
    $('.chkHotels').each(function(){ 
    
        if (this.checked){
        
            var booFound = false;
            
            for (var i = 0, il = chkdHotels.length; i < il; i++) {
                             
                if (this.value == chkdHotels[i]) {
                    booFound = true;
                    break;
                }

            }
            
            if(booFound == false){
                addHotels.push(this.value);
            }
            
        }
        else{
           
            for (var i = 0, il = chkdHotels.length; i < il; i++) {
                             
                if (this.value == chkdHotels[i]) {
                    delHotels.push(this.value);
                    break;
                }

            }
            
        }
    
    });
    
    mapCustomAddresses.push('<customAddresses>');
    
    for (var i = 0, il = gmarkers.length; i < il; i++) {

        if ((gmarkers[i].myCategory == "custom") && (gmarkers[i].myID == 0)) {
            mapCustomAddresses.push('<customAddress><name><![CDATA[');
            mapCustomAddresses.push(gmarkers[i].myName);
            mapCustomAddresses.push(']]></name><description><![CDATA[');
            mapCustomAddresses.push(gmarkers[i].myDescription);
            mapCustomAddresses.push(']]></description><address><![CDATA[');
            mapCustomAddresses.push(gmarkers[i].myAddress);
            mapCustomAddresses.push(']]></address><city><![CDATA[');
            mapCustomAddresses.push(gmarkers[i].myCity);
            mapCustomAddresses.push(']]></city><stateID>');
            mapCustomAddresses.push(gmarkers[i].myState);
            mapCustomAddresses.push('</stateID><zip>');
            mapCustomAddresses.push(gmarkers[i].myZIP);
            mapCustomAddresses.push('</zip><phone>');
            mapCustomAddresses.push(gmarkers[i].myPhone);
            mapCustomAddresses.push('</phone><url>');
            mapCustomAddresses.push(gmarkers[i].myURL);
            mapCustomAddresses.push('</url><latitude>');
            mapCustomAddresses.push(gmarkers[i].myLat);
            mapCustomAddresses.push('</latitude><longitude>');
            mapCustomAddresses.push(gmarkers[i].myLng);
            mapCustomAddresses.push('</longitude><isChecked>');
            mapCustomAddresses.push(gmarkers[i].isChecked);
            mapCustomAddresses.push('</isChecked></customAddress>');
        }

    }
    
    mapCustomAddresses.push('</customAddresses>');
    
    if(addAttractions == ''){addAttractions=0;}
    if(addHotels == ''){addHotels=0;}
    if(delAttractions == ''){delAttractions=0;}
    if(delHotels == ''){delHotels=0;}
    if(delCustoms == ''){delCustoms=0;}
    
    $.ajax({
        type: "POST",
        url: "ajaxServants/UpdateMap.aspx",
        data: {
            mapID: editingMapID 
            ,mapName: $("#txtMapName").val()
            ,mapDesc: $("#txtMapDesc").val()
            ,addAttractions: addAttractions
            ,addHotels: addHotels
            ,delAttractions: delAttractions
            ,delHotels: delHotels
            ,mapCustomAddresses: XMLtoString(mapCustomAddresses.join(''))
            ,delCustoms: delCustoms
        },
        success: function(returnData)
        {
            if (returnData == "SessionExpired") {window.location="Default.aspx";}
            else
            {
                if(returnData == "Fail"){alert("Error updating the map.");}
                else 
                {
                    ajaxGetMapArchive();
                    var intX = editingMapID;
                    resetMapCommons();
                    ajaxGetMapInfo(intX);
                    alert("Map saved.");
                }
            }

        },
        error: function (xhr, textStatus, errorThrown)
            {
                alert("Error updating the map.\n\nxhr response: " + xhr.responseText + "\n\nHTTP status code: " + xhr.status + "\n\nerrorThrown: " + errorThrown);
            }     
    });
    
    addAttractions = null;
    addHotels = null;
    delAttractions = null;
    delHotels = null;
    mapCustomAddresses = null;

}

function ajaxGetMapArchive(){

    $.ajax({
        type: "GET",
        url: "ajaxServants/GetMapArchive.aspx",
        success: function(returnData)
        {
            if (returnData == "SessionExpired") {window.location="Default.aspx";}
            else
            {
                $("#tblMapArchive").empty();
                loadMapArchive(returnData);
            }
        },
        error: function (xhr, textStatus, errorThrown)
            {
                alert("Error loading the map archive.\n\nxhr response: " + xhr.responseText + "\n\nHTTP status code: " + xhr.status + "\n\nerrorThrown: " + errorThrown);
            }     
    });
    
}

function createMap(){

    if (editingMap == true){
        if (confirm('Are you sure you want to create a new map?  Any unsaved changes will be lost!')){
            $('#divLinkPrint').slideUp('slow');
			$('#selRegion').slideDown('slow');
            resetMapCommons();
        }
    }
    else{
		$('#divLinkPrint').slideUp('slow');
        $('#selRegion').slideDown('slow');
        editingMap = true;
    }
    
}

function resetMapCommons(){
    map.clearOverlays();
    $('#divMapEdit').slideUp('slow');
    $("#tblHotels").empty();
    $("#tblAttractions").empty();
    $("#tblCustom").empty();
    $("form")[0].reset();
    gmarkers = [];
    totalHotels = 0;
    totalAttractions = 0;
    totalCheckedHotels = 0;
    totalCheckedAttractions = 0;
    regionID = 0;
    editingMapID = 0;
    editingExistingMap = false;
    chkdAttractions = new Array();
    chkdHotels = new Array();
    delCustoms = new Array();
}

function cancelMapEdit(){
    if (confirm('Are you sure you want to cancel?  Any unsaved changes will be lost!')){
        resetMapCommons();
        editingMap = false;
    }
}

function loadMapArchive(strXML) {

    var mapsX = GXml.parse(strXML).getElementsByTagName("map");

    for (var i = 0, il = mapsX.length; i < il; i++) {       
        var _mapID = mapsX[i].getAttribute("ID")
        var _mapName = mapsX[i].getAttribute("name")
        injectMapRow(_mapID,_mapName);
    }
}

function injectMapRow(_mapID,_mapName)
{

        //if (_mapName.length > 22){_mapName = _mapName.substring(0,22) + "...";}

        var txtArray = new Array();

        txtArray.push('<tr onmouseover="this.bgColor=\'Gray\';" onmouseout="this.bgColor=\'#BFBFBF\';"><td class="mapCol1"><a href="javascript:void(0);" onclick="loadMapInfo(');
        txtArray.push(_mapID);
        txtArray.push(');">');
        txtArray.push(_mapName);
        txtArray.push('</a></td><td><input type="image" src="images/icon_copyFile.png" title="Create Copy" onclick="ajaxCloneMap(');
        txtArray.push(_mapID);
        txtArray.push(',\'');
        txtArray.push(_mapName.replace(/\'/g,"\\\'").replace(/\"/g,"&quot;"));
        txtArray.push('\');" /></td><td><input type="image" title="Delete" onclick="ajaxDeleteMap(this,');
        txtArray.push(_mapID);
        txtArray.push(');" style="padding-right:3px;" src="images/icon_delete.png" /></td></tr>');
     
        $("#tblMapArchive").append(txtArray.join(''));
        txtArray = null;    
}

function ajaxCloneMap(_mapID,_mapName)
{
    $.ajax({
        type: "POST",
        url: "ajaxServants/CloneMap.aspx",
        data: { 
            mapID: _mapID
        },
        success: function(returnData){
        
            if (returnData == "SessionExpired") {window.location="Default.aspx";}
            else
            {
                if (_mapName.length > 45){_mapName = _mapName.substring(0,45);}
            
                var newMapID = returnData.split(",")[1];
                injectMapRow(newMapID,_mapName + ' Copy');
                loadMapInfo(newMapID);  
            }
                  
        },
        error: function (xhr, textStatus, errorThrown)
            {
                alert("Error cloning the map.\n\nxhr response: " + xhr.responseText + "\n\nHTTP status code: " + xhr.status + "\n\nerrorThrown: " + errorThrown);
            }     
    });
}

function ajaxDeleteMap(elm,_mapID)
{
    if (confirm('Are you sure you want to delete the map?'))
    {
        $.ajax({
            type: "POST",
            url: "ajaxServants/DeleteMap.aspx",
            data: { 
                mapID: _mapID
            },
            success: function(returnData)
            {
                if (returnData == "SessionExpired") {window.location="Default.aspx";}
                else
                {
                    $(elm).parent().parent().slideUp('slow');
                
                    if (editingMapID == _mapID){
                        resetMapCommons();
                        editingMapID = 0;
                        editingMap = false;
                    }
                }            
            },
        error: function (xhr, textStatus, errorThrown)
            {
                alert("Error deleting the map.\n\nxhr response: " + xhr.responseText + "\n\nHTTP status code: " + xhr.status + "\n\nerrorThrown: " + errorThrown);
            }     
        });
    }
}

function ajaxGetMapInfo(_mapID){

    $.ajax({
        type: "POST",
        url: "ajaxServants/GetMapInfo.aspx",
        data: { 
            mapID: _mapID
        },
        success: function(returnData){
            
            if (returnData == "SessionExpired") {window.location="Default.aspx";}
            else
            {
                var XMLDoc = GXml.parse(returnData);
                
                var mapInfo = XMLDoc.getElementsByTagName("map");

                regionID = mapInfo[0].getAttribute("regionID");
                ajaxGetRegionInfo(regionID);
                $('#divMapEdit').slideDown('slow');
                $("#txtMapName").val(mapInfo[0].getAttribute("name"));
                $("#txtMapDesc").val(mapInfo[0].getAttribute("description"));
                editingMapID = _mapID;
                mapGUID = mapInfo[0].getAttribute("publicGUID");
                
                totalCheckedHotels = 0;
                totalCheckedAttractions = 0;
                editingMap = true;
                editingExistingMap = true;
                
                var strX = mapInfo[0].getAttribute("hotels");    
                setTimeout("matchHotels('" + strX + "');",1500);
                
                strX = mapInfo[0].getAttribute("attractions");
                setTimeout("matchAttractions('" + strX + "');",1500);
                
                var customAddresses = XMLDoc.getElementsByTagName("customAddress");
                
                for (var i = 0, il = customAddresses.length; i < il; i++) {
                    
                    var marker = new GMarker(new GLatLng(customAddresses[i].getAttribute("latitude"),customAddresses[i].getAttribute("longitude")),gicons[8]);
                    marker.myName = customAddresses[i].getAttribute("name");
                    marker.safeName = strToJS(marker.myName);
                    marker.myID = customAddresses[i].getAttribute("ID");
                    marker.myCategory = "custom";
                    marker.myHTML = customAddresses[i].getAttribute("markerHTML");
                    
                    marker.bindInfoWindowHtml(marker.myHTML,{maxWidth:250});
                    map.addOverlay(marker);
                    gmarkers.push(marker);
                    
                    var txtArray = new Array();
                    
                    txtArray.push('<tr onmouseover="this.bgColor=\'Gray\';" onmouseout="this.bgColor=\'#E6E6E6\';"><td class="tdCheckCustom"><input type="checkbox" class="chkCustom" value="');
                    txtArray.push(marker.myID);
                    
                    if(customAddresses[i].getAttribute("isChecked") == "True")
                        {txtArray.push('" checked="checked');}
                    else{marker.hide();}
                    
                    txtArray.push('" onclick="toggleCustomMarker(\'');
                    txtArray.push(marker.safeName);
                    txtArray.push('\',this.checked);" /></td><td class="tdCustomTitle">');
                    txtArray.push(marker.myName);
                    txtArray.push('</td><td><input type="image" onclick="$(this).parent().parent().slideUp(\'slow\');delCustoms.push(\'');
                    txtArray.push(marker.myID);
                    txtArray.push('\');" style="padding-right:6px;" src="images/icon_delete.png" /></td></tr>');

                    $("#tblCustom").append(txtArray.join(''));
                    txtArray = null;
                
                }
                
                $('#divLinkPrint').slideDown('slow');
            
            }
            


        },
        error: function (xhr, textStatus, errorThrown)
            {
                alert("Error loading the map.\n\nxhr response: " + xhr.responseText + "\n\nHTTP status code: " + xhr.status + "\n\nerrorThrown: " + errorThrown);
            }     
    });
        
}

function loadMapInfo(_mapID)
{
    var booContinue = true;

    if (editingMap == true)
    {
        if (confirm('Are you sure you want to load the map?  Any unsaved changes to the current map will be lost!')){
            resetMapCommons();
            $('#selRegion').slideUp('slow');
        }
        else{booContinue = false;}
    }

    if (booContinue == true){ajaxGetMapInfo(_mapID);}
}

function matchAttractions(strX){

    strX = strX.split(",");
    strX.length--;

    for (var i = 0, il = strX.length; i < il; i++) {
               
        $('.chkAttractions').each(function(){ 
            if (this.value == strX[i]) {
                this.click();
            }   
        });
        
        chkdAttractions.push(strX[i]);

    }
    
    map.closeInfoWindow();

}

function matchHotels(strX){

    strX = strX.split(",");
    strX.length--;

    for (var i = 0, il = strX.length; i < il; i++) {
               
        $('.chkHotels').each(function(){ 
            if (this.value == strX[i]) {
                this.click();
            }   
        });
        
        chkdHotels.push(strX[i]);

    }
    
    map.closeInfoWindow();
                
}

function loadRegionSel(strXML) {

    var regionsX = GXml.parse(strXML).getElementsByTagName("region");
    var txtArray = new Array();

    for (var i = 0, il = regionsX.length; i < il; i++) {

        var region = {};
        region.ID = regionsX[i].getAttribute("ID");
        region.name = regionsX[i].getAttribute("name");
        region.latitude = regionsX[i].getAttribute("latitude");
        region.longitude = regionsX[i].getAttribute("longitude");
                  
        regions.push(region);
        
        txtArray.push('<option value="');
        txtArray.push(region.ID);
        txtArray.push('">');
        txtArray.push(region.name);
        txtArray.push('</option>');
        
    }
    
    $("#selRegion").html(txtArray.join(''));
    txtArray = null;

}

function ajaxGetRegionInfo(_regionID){
              
    $.ajax({
        type: "POST",
        url: "ajaxServants/GetRegionInfo.aspx",
        data: { 
            regionID: _regionID
        },
        success: function(returnData)
        {
            if (returnData == "SessionExpired") {window.location="Default.aspx";}
            else
            {
                for (var i = 0, il = regions.length; i < il; i++)
                {    
                    if ( regions[i].ID == _regionID ) {
                        map.setCenter(new GLatLng(regions[i].latitude,regions[i].longitude),9);
                        map.savePosition();
                        break;
                    }
                }
                
                loadMarkers(returnData);
                regionID = _regionID;
            }
        },
        error: function (xhr, textStatus, errorThrown)
            {
                alert("Error loading region information.\n\nxhr response: " + xhr.responseText + "\n\nHTTP status code: " + xhr.status + "\n\nerrorThrown: " + errorThrown);
            }     
    });
        
}

function loadIcons(){

    var Icon = new GIcon();
    
    Icon.image = "images/markers/CY.png";
    Icon.iconSize = new GSize(23,36);
    Icon.iconAnchor = new GPoint(11,36);
    Icon.infoWindowAnchor = new GPoint(11,0);
    Icon.transparent = "images/markers/CY_b.png";
    Icon.mozPrintImage = "images/markers/CY.gif";
    Icon.imageMap = [11,36,1,15,1,7,8,0,15,0,22,8,22,15];
    gicons.push(Icon);
    
    Icon = new GIcon();
    Icon.image = "images/markers/FFI.png";
    Icon.iconSize = new GSize(23,36);
    Icon.iconAnchor = new GPoint(11,36);
    Icon.infoWindowAnchor = new GPoint(11,0);
    Icon.transparent = "images/markers/FFI_b.png";
    Icon.mozPrintImage = "images/markers/FFI.gif";
    Icon.imageMap = [11,36,1,15,1,7,8,0,15,0,22,8,22,15];
    gicons.push(Icon);
    
    Icon = new GIcon();
    Icon.image = "images/markers/JW.png";
    Icon.iconSize = new GSize(23,36);
    Icon.iconAnchor = new GPoint(11,36);
    Icon.infoWindowAnchor = new GPoint(11,0);
    Icon.transparent = "images/markers/JW_b.png";
    Icon.mozPrintImage = "images/markers/JW.gif";
    Icon.imageMap = [11,36,1,15,1,7,8,0,15,0,22,8,22,15];
    gicons.push(Icon);
    
    Icon = new GIcon();
    Icon.image = "images/markers/MHR.png";
    Icon.iconSize = new GSize(23,37);
    Icon.iconAnchor = new GPoint(11,37);
    Icon.infoWindowAnchor = new GPoint(11,0);
    Icon.transparent = "images/markers/MHR_b.png";
    Icon.mozPrintImage = "images/markers/MHR.gif";
    Icon.imageMap = [11,37,1,15,1,7,8,0,15,0,22,8,22,15];
    gicons.push(Icon);
    
    Icon = new GIcon();
    Icon.image = "images/markers/RHR.png";
    Icon.iconSize = new GSize(23,37);
    Icon.iconAnchor = new GPoint(11,37);
    Icon.infoWindowAnchor = new GPoint(11,0);
    Icon.transparent = "images/markers/RHR_b.png";
    Icon.mozPrintImage = "images/markers/RHR.gif";
    Icon.imageMap = [11,37,1,15,1,7,8,0,15,0,22,8,22,15];
    gicons.push(Icon);
    
    Icon = new GIcon();
    Icon.image = "images/markers/RI.png";
    Icon.iconSize = new GSize(23,36);
    Icon.iconAnchor = new GPoint(11,36);
    Icon.infoWindowAnchor = new GPoint(11,0);
    Icon.transparent = "images/markers/RI_b.png";
    Icon.mozPrintImage = "images/markers/RI.gif";
    Icon.imageMap = [11,36,1,15,1,7,8,0,15,0,22,8,22,15];
    gicons.push(Icon);
    
    Icon = new GIcon();
    Icon.image = "images/markers/SHS.png";
    Icon.iconSize = new GSize(23,37);
    Icon.iconAnchor = new GPoint(11,37);
    Icon.infoWindowAnchor = new GPoint(11,0);
    Icon.transparent = "images/markers/SHS_b.png";
    Icon.mozPrintImage = "images/markers/SHS.gif";
    Icon.imageMap = [11,37,1,15,1,7,8,0,15,0,22,8,22,15];
    gicons.push(Icon);
    
    Icon = new GIcon();
    Icon.image = "images/markers/TPS.png";
    Icon.iconSize = new GSize(23,37);
    Icon.iconAnchor = new GPoint(11,37);
    Icon.infoWindowAnchor = new GPoint(11,0);
    Icon.transparent = "images/markers/TPS_b.png";
    Icon.mozPrintImage = "images/markers/TPS.gif";
    Icon.imageMap = [11,37,1,15,1,7,8,0,15,0,22,8,22,15];
    gicons.push(Icon);
      
    Icon = new GIcon();
    Icon.image = "images/markers/CUSTOM.png";
    Icon.iconSize = new GSize(23,36);
    Icon.iconAnchor = new GPoint(11,36);
    Icon.infoWindowAnchor = new GPoint(11,0);
    Icon.transparent = "images/markers/CUSTOM_b.png";
    Icon.mozPrintImage = "images/markers/CUSTOM.gif";
    Icon.imageMap = [11,36,1,15,1,7,8,0,15,0,22,8,22,15];
    gicons.push(Icon);

}

function loadMarkers(strXML){
 
    var markers = GXml.parse(strXML).getElementsByTagName("marker");

    for (var i = 0, il = markers.length; i < il; i++) {
    
        var Icon;
        
        switch (markers[i].getAttribute("brandID")) {
            case "1":
                Icon = gicons[0];
                break;             
            case "2":
                Icon = gicons[1];
                break;
            case "3":
                Icon = gicons[1];
                break;
			case "4":
                Icon = gicons[2];
			    break;
            case "5":
                Icon = gicons[3];
                break;
            case "6":
                Icon = gicons[4];
                break;
            case "7":
                Icon = gicons[5];
                break;
            case "8":
                Icon = gicons[6];
                break;
            case "9":
                Icon = gicons[7];
                break;
            default:
                Icon = gicons[8];
                break;
        }
           
        var point = new GLatLng(markers[i].getAttribute("latitude"),markers[i].getAttribute("longitude"));
        var marker = new GMarker(point,Icon);
        marker.myName = markers[i].getAttribute("name");
        marker.myID = markers[i].getAttribute("ID");
        marker.myCategory = markers[i].getAttribute("category");
        marker.myHTML = markers[i].getAttribute("markerHTML");
        marker.bindInfoWindowHtml(marker.myHTML,{maxWidth:250});      

        map.addOverlay(marker);
        marker.hide();
        gmarkers.push(marker);
                      
        switch (marker.myCategory) {
            case "hotel":
            
                var txtArray = new Array();
                
                txtArray.push('<tr><td class="tdCheck"><input type="checkbox" class="chkHotels" name="');
                txtArray.push(marker.myCategory);
                txtArray.push('" value="');
                txtArray.push(marker.myID);
                txtArray.push('" onclick="toggleMarker(this.value,this.checked,this.name);" /></td><td>');
                txtArray.push(marker.myName);
                txtArray.push('</td></tr>');
 
                $("#tblHotels").append(txtArray.join(''));
                txtArray = null;
                totalHotels++;
                break;
                
            case "attraction":
            
                var txtArray = new Array();
                
                txtArray.push('<tr><td class="tdCheck"><input type="checkbox" class="chkAttractions" name="');
                txtArray.push(marker.myCategory);
                txtArray.push('" value="');
                txtArray.push(marker.myID);
                txtArray.push('" onclick="toggleMarker(this.value,this.checked,this.name);" /></td><td>');
                txtArray.push(marker.myName);
                txtArray.push('</td></tr>');
                
                $("#tblAttractions").append(txtArray.join(''));
                txtArray = null;
                totalAttractions++;
                break;
        }
    }
    
}

function toggleMarkerGroup(className,showIt){
          
    if (showIt) {
       
        $(className).each(function(){  
            if (!this.checked) {
                this.click();
            }                   
        });
        
        map.closeInfoWindow();
        
    } else {
        $(className).each(function(){  
            if (this.checked) {
                this.click();
            }                   
        });
    }

}

function ajaxGeneratePDF(_mapURL)
{
    $.ajax({
        type: "POST",
        url: "ajaxServants/GeneratePDF.aspx",
        data: { 
            mapURL: _mapURL
            ,mapName: $("#spanMapName").text()
        },
        success: function(returnData){
            if (returnData == "SessionExpired") {window.location="Default.aspx";}
            else
            {
                if(returnData == "Fail"){alert("Error generating the PDF.");}
                else
                {
                    var strLocationary = location.href;
                    window.location=strLocationary.slice(0,strLocationary.lastIndexOf("/")) + "/PDF/" + returnData.split(",")[1];
                }
            }        
        },
        error: function (xhr, textStatus, errorThrown)
            {
                alert("Error generating the PDF.\n\nxhr response: " + xhr.responseText + "\n\nHTTP status code: " + xhr.status + "\n\nerrorThrown: " + errorThrown);
            }     
    });
}

function ajaxInsertMap(){
    
    var mapHotels = new Array();
    var mapAttractions = new Array();
    var mapCustomAddresses = new Array();
    
    $('.chkHotels').each(function(){  
        if(this.checked){
            mapHotels.push(this.value);
            chkdHotels.push(this.value);
        }
    });
    
    $('.chkAttractions').each(function(){  
        if(this.checked){
            mapAttractions.push(this.value);
            chkdAttractions.push(this.value);
        }
    });
    
    mapCustomAddresses.push('<customAddresses>');
    
    for (var i = 0, il = gmarkers.length; i < il; i++) {
        
        if (gmarkers[i].myCategory == "custom") {
            mapCustomAddresses.push('<customAddress><name><![CDATA[');
            mapCustomAddresses.push(gmarkers[i].myName);
            mapCustomAddresses.push(']]></name><description><![CDATA[');
            mapCustomAddresses.push(gmarkers[i].myDescription);
            mapCustomAddresses.push(']]></description><address><![CDATA[');
            mapCustomAddresses.push(gmarkers[i].myAddress);
            mapCustomAddresses.push(']]></address><city><![CDATA[');
            mapCustomAddresses.push(gmarkers[i].myCity);
            mapCustomAddresses.push(']]></city><stateID>');
            mapCustomAddresses.push(gmarkers[i].myState);
            mapCustomAddresses.push('</stateID><zip>');
            mapCustomAddresses.push(gmarkers[i].myZIP);
            mapCustomAddresses.push('</zip><phone>');
            mapCustomAddresses.push(gmarkers[i].myPhone);
            mapCustomAddresses.push('</phone><url>');
            mapCustomAddresses.push(gmarkers[i].myURL);
            mapCustomAddresses.push('</url><latitude>');
            mapCustomAddresses.push(gmarkers[i].myLat);
            mapCustomAddresses.push('</latitude><longitude>');
            mapCustomAddresses.push(gmarkers[i].myLng);
            mapCustomAddresses.push('</longitude><isChecked>');
            mapCustomAddresses.push(gmarkers[i].isChecked);
            mapCustomAddresses.push('</isChecked></customAddress>');
        }
    
    }
    
    mapCustomAddresses.push('</customAddresses>');
    
    if(mapHotels == ''){mapHotels=0;}
    if(mapAttractions == ''){mapAttractions=0;}
    
    var booContinue = true;
    
    $.ajax({
        type: "POST",
        url: "ajaxServants/InsertMap.aspx",
        data: { 
            mapName: $("#txtMapName").val()
            ,mapDesc: $("#txtMapDesc").val()
            ,mapRegionID: regionID
            ,mapHotels: mapHotels
            ,mapAttractions: mapAttractions
            ,mapCustomAddresses: XMLtoString(mapCustomAddresses.join(''))
        },
        success: function(returnData)
        {
            if (returnData == "SessionExpired") {window.location="Default.aspx";}
            else
            {
                if(returnData == "Fail")
                {
                    booContinue = false;
                    alert("Error saving the map.");
                }
                else
                {		
                    ajaxGetMapArchive();
                    resetMapCommons();
                    ajaxGetMapInfo(returnData);
                    alert("Map saved.");
                }
            }

        },
        error: function (xhr, textStatus, errorThrown)
            {
                booContinue = false;
                alert("Error saving the map.\n\nxhr response: " + xhr.responseText + "\n\nHTTP status code: " + xhr.status + "\n\nerrorThrown: " + errorThrown);
            }     
    });
    
    if(!booContinue)
    {
        chkdAttractions = new Array();
        chkdHotels = new Array();
    }
    
    mapHotels = null;
    mapAttractions = null;
    mapCustomAddresses = null;
        
}

function ajaxResetPassword(){

    $("#bttnForgot").slideUp("slow");
    $("#imgAjax2").slideDown("slow");

    $.ajax({
        type: "POST",
        url: "ajaxServants/ResetPassword.aspx",
        data: { 
            email1: $("#txtForgotEmail1").val()
            ,email2: $("#txtForgotEmail2").val()
        },
        success: function(returnData){
            
            var strX = returnData.split(",");
            
            switch (strX[0]) {
				case "Success":
                    $("#divForgotMessage").html("Your new password has been emailed to you.<br /><br />");
                    $("#divForgotInfo").slideUp("slow");
                    $("#divForgotForm").slideUp("slow");
					break;
				case "Fail":
                    $("#divForgotMessage").html(strX[1] + "<br /><br />");
                    $("#imgAjax2").slideUp("slow");
                    $("#bttnForgot").slideDown("slow");
					break;
				default:
                    $("#divForgotMessage").html("Error resetting your password.<br /><br />");
                    $("#imgAjax2").slideUp("slow");
                    $("#bttnForgot").slideDown("slow");
					break;
			}
            
            strX = null;
                      
        },
        error: function()
            {
                alert("Error resetting your password.\n\nxhr response: " + xhr.responseText + "\n\nHTTP status code: " + xhr.status + "\n\nerrorThrown: " + errorThrown);
                $("#imgAjax2").slideUp("slow");
                $("#bttnForgot").slideDown("slow");
            }
    });

}

function ajaxCreateAccount(){

    $("#bttnCreate").slideUp("slow");
    $("#imgAjax").slideDown("slow");

    $.ajax({
        type: "POST",
        url: "ajaxServants/CreateAccount.aspx",
        data: { 
            name: $("#txtName").val()
            ,email1: $("#txtEmail1").val()
            ,email2: $("#txtEmail2").val()
            ,username: $("#txtUsername").val()
            ,password1: $("#txtPass1").val()
            ,password2: $("#txtPass2").val()
            ,phone: $("#txtPhone").val()
            ,organization: $("#txtOrganization").val()
        },
        success: function(returnData)
        {                        
            var strX = returnData.split(",");
            
            switch (strX[0]) 
            {
			    case "Success":
                    if (strX[1] == "False"){$("#divCreateMessage").html("Your request has been submitted for review. You will receive an email to confirm your account eligibility. For additional information or to answer any questions that you have, please email contact@bookmarriott.com.<br /><br />");}
                    else{$("#divCreateMessage").html("Your new Map Tool account has been successfully created. You may now login above. For additional information or to answer any questions that you have, please email contact@bookmarriott.com.<br /><br />");}
                    $("#divCreateForm").slideUp("slow");
				    break;
			    case "Fail":
                    $("#divCreateMessage").html(strX[1] + "<br /><br />");
                    $("#imgAjax").slideUp("slow");
                    $("#bttnCreate").slideDown("slow");
				    break;
			    default:
                    $("#divCreateMessage").html("Error creating the account.<br /><br />");
                    $("#imgAjax").slideUp("slow");
                    $("#bttnCreate").slideDown("slow");
				    break;
		    }
            
            strX = null;
          
        },
        error: function()
            {
                alert("Error creating the account.\n\nxhr response: " + xhr.responseText + "\n\nHTTP status code: " + xhr.status + "\n\nerrorThrown: " + errorThrown);
                $("#imgAjax").slideUp("slow");
                $("#bttnCreate").slideDown("slow");
            }
    });

}

function XMLtoString(strX)
{
    var rx1 = /</g;
    var rx2 = />/g;
    
    return strX.replace(rx1,"&lt;").replace(rx2,"&gt;");
}

function strToHTML(strX)
{
    var rx1 = /&/g;
    var rx2 = /</g;
    var rx3 = />/g;
    
    return strX.replace(rx1,"&amp;").replace(rx2,"&lt;").replace(rx3,"&gt;");
}

function strToJS(strX)
{
    var rx1 = /'/g;
    var rx2 = /"/g;
    var rx3 = /\\/g;
    
    return strX.replace(rx1,"").replace(rx2,"").replace(rx3,"");
}

//function GlorifyHTMLString(strX)
//{
//    var rx1 = /</g;
//    var rx2 = />/g;
//    var rx3 = /"/g;
//    var rx4 = /'/g;
//    var rx5 = /&/g;
//    var rx6 = /\\/g;
//    var rx7 = /\//g;
//    
//    return strX.replace(rx1,"&lt;").replace(rx2,"&gt;").replace(rx3,"&quot;").replace(rx4,"&apos;").replace(rx5,"&amp;").replace(rx6,"&#92;").replace(rx7,"&#47;");
//}
