﻿var xmlHttp;

String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

function makePOSTRequest(url, params, callback) {
    xmlHttp = GetXmlHttpObject();

    xmlHttp.onreadystatechange = callback;
    xmlHttp.open('POST', url, true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
    xmlHttp.send(params)
}

function GetXmlHttpObject() {
    var xmlHttp = null;
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    } catch (e) {
        // Internet Explorer
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}

function refresh() {
    location.reload(true);
}

function gotoDirections(fromLocation, toLocation) {
    if (!fromLocation) {
        if (navigator.geolocation) {
            //alert("using navigator");
            navigator.geolocation.getCurrentPosition(getGeoCallback_gotoDirectionsWithTo(toLocation), geoError);
        } else if (google.loader.ClientLocation) {
            //alert("using cl");
            var cl = google.loader.ClientLocation;
            
            //format the position to be the same as navigator.getCurrentPosition callback expects
            // cl also has address.city, address.region, and address.country
            var geoObject = new Object();
            geoObject.coords = new Object();
            geoObject.coords.latitude = cl.latitude;
            geoObject.coords.longitude = cl.longitude;
            getGeoCallback_gotoDirectionsWithTo(toLocation)(geoObject);
        } else {
            //couldn't get a location. try and see if user has set a location; if not, we'll just use lincoln and let them change it at google maps
            gotoDirections("Lincoln, IL", toLocation);
        }
    } else {
        //alert("actualy send to gmaps with from:'" + fromLocation + "' to:'" + toLocation + "'");
        //use &mrad= in url for additional destinations
        window.location = "http://www.google.com/maps?saddr=" + fromLocation + "&daddr=" + toLocation;
    }
}

function getGeoCallback_gotoDirectionsWithTo(toLocation) {
    return function geoCallback_gotoDirections(position) {
        //alert("geoCallback_gotoDirection with location '" + toLocation + "'");
        gotoDirections(position.coords.latitude + ", " + position.coords.longitude, toLocation);
    }
}

function geoError(error) {
    alert("geoLocation error: " + error.message);
}





function getRequestParam(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return results[1];
}

//store current search info in the navtree element
var navTree = new Object();
navTree.tags = getRequestParam("tags");
navTree.cat = getRequestParam("cat");
navTree.city = getRequestParam("city");
navTree.page = getRequestParam("page");
navTree.edit = getRequestParam("edit");
navTree.entID = getRequestParam("id");



function setTags(toTags) {
    navTree.entID = "";
    navTree.tags = toTags;
    gotoListing();
}

function addTag(theTag) {
    //first make sure the tag isn't already in the list
    var split = navTree.tags.split(",");

    var exists = false;
    for (i = 0; i < split.length - 1; i++) {
        if (split[i].trim == theTag) {
            exists = true;
        }
    }

    if (!exists) {
        navTree.tags += "," + theTag;
        gotoListing();
    }
}

function removeTag(theTag) {
    var split = navTree.tags.split(",");

    var newTagString = "";
    for (i = 0; i <= split.length - 1; i++) {
        //alert("trim='" + split[i].trim() + "'\ntheTag='" + theTag + "'");
        if (split[i].trim() != theTag) {
            newTagString += split[i] + ",";
        }
    }

    //remove last comma if it's there
    if (newTagString.length > 0) {
        newTagString = newTagString.substr(0, newTagString.length - 1);
    }
    navTree.tags = newTagString;

    gotoListing();
}

function setCity(toCity) {
    navTree.entID = "";
    navTree.city = toCity;
    gotoListing();
}

function setCategory(toCat) {
    navTree.entID = "";
    navTree.cat = toCat;
    gotoListing();
    //window.location="blah"
}

function setPage(toPage) {
    navTree.page = toPage;
    gotoListing();
}

function enterEditMode(id) {
    navTree.entID = id;
    navTree.edit = "true";
    gotoListing();
}

function exitEditMode(id) {
    navTree.edit = "";
    gotoListing();
}

function gotoListing() {
    var strURL = "/list.aspx?"
    if (navTree.entID!="") {
        strURL += "id=" + navTree.entID;
    } else {
        strURL += "cat=" + navTree.cat + "&tags=" + navTree.tags + "&city=" + navTree.city + "&p=" + navTree.page;
    }
    if (navTree.edit == "true") strURL += "&edit=true";

    window.location = strURL;
}

function deleteEntity(id) {
    if(confirm("Are you sure you want to delete this entity?")) {
        makePOSTRequest("/sub/deleteentity_submit.aspx", "id=" + id, POSTReturn_Refresh);
    }
}


function POSTReturn_Refresh() {
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
        //alert(xmlHttp.responseText);
        refresh();
    }
}
