// JavaScript doesn't have actual constants or finals, but these things// in all caps are constants and should never be changed in the code.// The width here matches what's specified in the #front in the CSSvar VISIBLE_AREA_WIDTH  = 320;var RATINGS_KEY_HEIGHT = 0;var MAX_URL_CHARS_PER_WIDTH = Math.floor(VISIBLE_AREA_WIDTH / 6);var TIMEOUT_MILLIS = 300;//var BASE_URL = "http://www.restaurantratingz.com/";var BASE_URL = "../../";var WIDGET_STATE_LIST_JSP  = "local/widgetStateList.jsp";var WIDGET_CITY_LIST_JSP   = "local/widgetCityList.jsp";var WIDGET_REST_LIST_JSP   = "local/widgetRestList.jsp";var WIDGET_RATING_INFO_JSP = "local/widgetRatingInfo.jsp";var SHOW_RATINGS_JSP = "ShowRatings.jsp";var ADD_RATING_JSP   = "AddRating.jsp";var RATINGS_CAT_LABELS = new Array("Food", "Ambience", "Service");var GREEN_BKG_COLOR  = "#A8D578";var ORANGE_BKG_COLOR = "#F9D285";var currentState;var currentCity;var req;function loaded() {	if (navigator.userAgent.indexOf("iPhone") > 0) {		BASE_URL = "../../";	} else {		BASE_URL = "http://www.restaurantratingz.com/";	}    document.getElementById("innerTable").setAttribute("bgcolor", GREEN_BKG_COLOR);    getStateList();}function getStateList() {    req = new XMLHttpRequest();    //req.setRequestHeader("Cache-Control", "no-cache");    req.onreadystatechange = buildStateSelect;    req.open("GET", BASE_URL + WIDGET_STATE_LIST_JSP, true);    req.send(null);}function buildStateSelect() {    //console.log("buildStateSelect called, req.readyState=" + req.readyState);    if (req.readyState == 4) {        //console.log("buildStateSelect, status=" + req.status);        buildSelectMenu("states", "Select your state", "state");    }}function checkStatesList(event) {    /*    var selectObj = event.target;    if (selectObj && selectObj.options.length == 1) {        getStateList();    }    */}function getCityList(event) {    var selectObj = event.target;    if (selectObj.selectedIndex == 0) {        return;    }    var selectedOption = selectObj.options[selectObj.selectedIndex];    currentState = selectedOption.innerHTML;    hideElement("divCities");    hideElement("divRestaurants");    clearRestaurantInfo();    showWait();    req = new XMLHttpRequest();    req.open("GET", BASE_URL + WIDGET_CITY_LIST_JSP + "?tcvid=" + selectedOption.value + "&noredirect=1", false);    req.setRequestHeader("Cache-Control", "no-cache");    req.onreadystatechange = buildCitySelect;    // The extra complexity here of using setTimeout rather than just    // calling req.send directly, is needed to insert a slight delay    // so that the call to showWait() above has time to actually update    // the screen.    setTimeout('sendCityListGet()', TIMEOUT_MILLIS);}function sendCityListGet() {    req.send(null);}function buildCitySelect() {    if (req.readyState != 4) {        return;    }    try {        buildSelectMenu("cities", "Select a city", "city");        showElement("divCities");    } catch (err) {    }    hideWait();}function getRestaurantList(event) {    var selectObj = event.target;    if (selectObj.selectedIndex == 0) {        return;    }    var selectedOption = selectObj.options[selectObj.selectedIndex];    currentCity = selectedOption.innerHTML;    hideElement("divRestaurants");    clearRestaurantInfo();    showWait();    req = new XMLHttpRequest();    req.open("GET", BASE_URL + WIDGET_REST_LIST_JSP + "?tcvid=" + selectedOption.value + "&numrows=1000", false);    req.setRequestHeader("Cache-Control", "no-cache");    req.onreadystatechange = buildRestaurantSelect;    setTimeout('sendRestaurantListGet()', TIMEOUT_MILLIS);}function sendRestaurantListGet() {    req.send(null);}function buildRestaurantSelect() {    if (req.readyState != 4) {        return;    }    try {        buildSelectMenu("restaurants", "Select a restaurant", "restaurant");        showElement("divRestaurants");    } catch (err) {    }    hideWait();}function showElement(elName) {    document.getElementById(elName).style.display = "block";}function hideElement(elName) {    document.getElementById(elName).style.display = "none";}function showWait() {    document.getElementById("pleaseWait").style.display = "block";}function hideWait() {    document.getElementById("pleaseWait").style.display = "none";}function clearRestaurantInfo() {    hideElement("ratingInfo");    var infoTable = document.getElementById("infoTable");    clearTable(infoTable);    var commentsTable = document.getElementById("commentsTable");    clearTable(commentsTable);}function clearSelectList(select) {    while (select.length > 0) {        select.remove(0);    }}// Create and append a new <option> tag to the specified <select>// The content parameter is expected to be a string.function appendToSelect(select, value, content) {    var opt = document.createElement("option");    opt.value = value;    opt.innerHTML = content;    select.appendChild(opt);}function buildSelectMenu(selectName, firstLine, xmlElementName) {    var select = document.getElementById(selectName);    clearSelectList(select);    appendToSelect(select, 0, firstLine);    var elementList = req.responseXML.getElementsByTagName(xmlElementName);    for (var idx = 0; idx < elementList.length; idx++) {        var element = elementList.item(idx);        var idAttr = element.getAttribute("id");        var nameNode = element.getElementsByTagName("name").item(0);        var nameText = nameNode.firstChild.nodeValue;        appendToSelect(select, idAttr, nameText);        //for debugging: appendToSelect(select, idAttr, nameText + " " + idAttr);    }}function clearTable(tableNode) {    var oneRow = tableNode.getElementsByTagName("TR").item(0);    while (oneRow != null) {        tableNode.removeChild(oneRow);        oneRow = tableNode.getElementsByTagName("TR").item(0);    }}function getRestaurantInfo(event) {    var selectObj = event.target;    if (selectObj.selectedIndex == 0) {        return;    }    var selectedOption = selectObj.options[selectObj.selectedIndex];    showWait();    req = new XMLHttpRequest();    req.open("GET", BASE_URL + WIDGET_RATING_INFO_JSP + "?tcvid=" + selectedOption.value + "&noredirect=1", false);    req.setRequestHeader("Cache-Control", "no-cache");    req.onreadystatechange = showRestaurantInfo;    setTimeout('sendRestaurantInfoGet()', TIMEOUT_MILLIS);}function sendRestaurantInfoGet() {    req.send(null);}function showRestaurantInfo() {    if (req.readyState != 4) {        return;    }    try {        buildRatingTable();        buildInfoTable();        buildCommentsTable();    } catch (err) {    }    hideWait();}function buildRatingTable() {    var restaurantId = req.responseXML.getElementsByTagName("restaurant").item(0).getAttribute("id");    document.getElementById("numRatingsValue").innerText =        req.responseXML.getElementsByTagName("numRatings").item(0).firstChild.nodeValue;    document.getElementById("avgRatingValue").innerText =        req.responseXML.getElementsByTagName("avgRating").item(0).firstChild.nodeValue;    var smileyUrlText = req.responseXML.getElementsByTagName("smileyUrl").item(0).firstChild.nodeValue;    document.getElementById("ratingSmileyImage").setAttribute("src", smileyUrlText);    document.getElementById("rateThisRestLink").            setAttribute("onClick", "window.open('" + BASE_URL + ADD_RATING_JSP + "?tcvid=" + restaurantId + "');return false;");    showElement("ratingInfo");}function buildInfoTable() {    var row;    var cell;    var textNode;    var aTag;    var elementList;    var element;    var idx;    var nameText;    var valueNode;    var valueText;    var websiteUrl;    var websiteText;    var infoTable = document.getElementById("infoTable");    clearTable(infoTable);    var cuisineText = getNodeText("cuisine");    if (cuisineText != null) {        row = document.createElement("TR");        cell = document.createElement("TD");        cell.setAttribute("width", "40%");        cell.setAttribute("align", "right");        cell.setAttribute("class", "rating");        textNode = document.createTextNode("Cuisine:");        cell.appendChild(textNode);        row.appendChild(cell);        cell = document.createElement("TD");        cell.innerHTML = "&nbsp;" + cuisineText;        row.appendChild(cell);        infoTable.appendChild(row);    }    elementList = req.responseXML.getElementsByTagName("info");    // walk the list, outputing address, zip, etc.    for (idx = 0; idx < elementList.length; idx++) {        element = elementList.item(idx);        nameText  = element.getElementsByTagName("name").item(0).firstChild.nodeValue;        valueNode = element.getElementsByTagName("value").item(0).firstChild;        if (valueNode != null) {            valueText = valueNode.nodeValue;            row = document.createElement("TR");            cell = document.createElement("TD");            cell.setAttribute("width", "40%");            cell.setAttribute("align", "right");            cell.setAttribute("class", "rating");            textNode = document.createTextNode(nameText + ":");            cell.appendChild(textNode);            row.appendChild(cell);            cell = document.createElement("TD");            if (nameText.indexOf("Address") > -1 && valueText.length > 0) {                var mapLink = valueText.link("http://maps.google.com/maps?q=" +                                           encodeURI(valueText + ", " + currentCity + ", " + currentState));                cell.innerHTML = "&nbsp;" + mapLink;            } else {                cell.innerHTML = "&nbsp;" + valueText;            }            row.appendChild(cell);            infoTable.appendChild(row);        }    }    websiteUrl = getNodeText("website");    if (websiteUrl != null) {        websiteText = new String(websiteUrl);        if (websiteText.length > MAX_URL_CHARS_PER_WIDTH) {            websiteText = helpWrapUrlString(websiteText);        }        row = document.createElement("TR");        cell = document.createElement("TD");        cell.setAttribute("colspan", "2");        cell.setAttribute("style", "text-align:center;");        aTag = document.createElement("A");        aTag.setAttribute("onClick", "window.open('" + websiteUrl + "');return false;");        aTag.setAttribute("href", "");        textNode = document.createTextNode(websiteText);        aTag.appendChild(textNode);        cell.appendChild(aTag);        row.appendChild(cell);        infoTable.appendChild(row);    }}function buildCommentsTable() {    var row;    var cell;    var textNode;    var elementList;    var element;    var idx;    var jdx;    var ratingNode;    var ratingText;    var ratingCatTable;    var rcRow;    var ratingCatVals;    var ratingVal;    var dateNode;    var rcDate;    var commentsTable = document.getElementById("commentsTable");    clearTable(commentsTable);    elementList = req.responseXML.getElementsByTagName("rating");    for (idx = 0; idx < elementList.length; idx++) {        element = elementList.item(idx);        ratingNode = element.getElementsByTagName("ratingText").item(0).firstChild;        if (ratingNode != null) {            ratingText = ratingNode.nodeValue;            row = document.createElement("TR");            if ((idx % 2) == 1) {                row.setAttribute("bgcolor", GREEN_BKG_COLOR);            } else {                row.setAttribute("bgcolor", ORANGE_BKG_COLOR);            }            ratingCatTable = document.createElement("TABLE");            ratingCatTable.setAttribute("width", "100%");            ratingCatTable.setAttribute("border", "0");            ratingCatTable.setAttribute("cellspacing", "0");            ratingCatTable.setAttribute("cellpadding", "1");            rcRow = document.createElement("TR");            ratingCatVals = element.getElementsByTagName("ratingCatValue");            for (jdx = 0; jdx < ratingCatVals.length; jdx++) {                ratingVal = ratingCatVals.item(jdx).firstChild.nodeValue;                cell = document.createElement("TD");                cell.setAttribute("class", "rating");                textNode = document.createTextNode(RATINGS_CAT_LABELS[jdx] + ":" + ratingVal);                cell.appendChild(textNode);                rcRow.appendChild(cell);            }            dateNode = element.getElementsByTagName("date");            if (dateNode != null) {                rcDate = dateNode.item(0).firstChild;                if (rcDate != null) {                    cell = document.createElement("TD");                    textNode = document.createTextNode(rcDate.nodeValue);                    cell.appendChild(textNode);                    rcRow.appendChild(cell);                }            }            ratingCatTable.appendChild(rcRow);            cell = document.createElement("TD");            cell.appendChild(ratingCatTable);            row.appendChild(cell);            commentsTable.appendChild(row);            row = document.createElement("TR");            if ((idx % 2) == 1) {                row.setAttribute("bgcolor", GREEN_BKG_COLOR);            } else {            	row.setAttribute("bgcolor", ORANGE_BKG_COLOR);            }            cell = document.createElement("TD");            cell.setAttribute("class", "comment");            cell.innerHTML = ratingText;            row.appendChild(cell);            commentsTable.appendChild(row);        }    }}function helpWrapUrlString(str) {    var start = Math.floor(str.length / 2);    for (var idx = start; idx < str.length; idx++) {        if (str.charAt(idx) == "/") {            return str.substring(0, idx+1) + " " + str.substr(idx+1);        }    }    return str;}// Get the text for the node of the specified name. This function handles// all the intermediate checking for null things and returns a null if any// of them are null.function getNodeText(nodeName) {    var theTag = req.responseXML.getElementsByTagName(nodeName);    if (theTag == null) {        return null;    } else {        var theNode = theTag.item(0);        if (theNode == null) {            return null;        } else {            return theNode.firstChild.nodeValue;        }    }}function findPosY(obj) {    var curtop = 0;    if (obj.offsetParent) {        while (obj.offsetParent) {            curtop += obj.offsetTop;            obj = obj.offsetParent;        }    } else if (obj.y) {        curtop += obj.y;    }    return curtop;}function showBack() {    var front = document.getElementById("front");    var back  = document.getElementById("back");    front.style.display = "none";    back.style.display  = "block";}function hideBack() {    var front = document.getElementById("front");    var back  = document.getElementById("back");    back.style.display  = "none";    front.style.display = "block";}