﻿// JScript File

function enableElement(elementId, enabled) {
    var element = document.getElementById(elementId);
    if (element !== null)
        element.disabled = !enabled;
}

function focusElement(elementId) {
    var element = document.getElementById(elementId);
    if (element !== null)
        element.focus();
}

function showElementBlock(element) {
    if (element === null)
        alert("showElementBlock() was passed a null element");
    element.style.display = "block";
}

function showElementInline(element) {
    if (element === null)
        alert("showElementInline() was passed a null element");
    element.style.display = "inline";
}

function hideElement(element) {
    if (element === null)
        alert("hideElement() was passed a null element");
    element.style.display = "none";
}

function xmlReadNullable(documentElement, elementName) {
    // Returns the value of elementName in the XML element documentElement
    // If the XML element is not found, it returns null.
    var element = documentElement.getElementsByTagName(elementName)[0].childNodes[0];
    if (element == null)
        return null;
    else
        return element.nodeValue;
}

function xmlReadString(documentElement, elementName) {
    // Returns the value of elementName in the XML element documentElement
    // If the XML element is not found, it returns empty string.
    var element = documentElement.getElementsByTagName(elementName)[0].childNodes[0];
    if (element == null)
        return "";
    else
        return element.nodeValue;    
}

function xmlReadNumber(documentElement, elementName) {
    // Returns the value of elementName in the XML element documentElement
    // If the XML element is not found, it returns 0.
    var element = documentElement.getElementsByTagName(elementName)[0].childNodes[0];
    if (element == null)
        return 0;
    else
        return element.nodeValue;    
}

function xmlReadBoolean(documentElement, elementName) {
    // Returns the value of elementName in the XML element documentElement
    // If the XML element is not found, it returns false.
    var element = documentElement.getElementsByTagName(elementName)[0].childNodes[0];
    if (element == null)
        return false;
    else
        return element.nodeValue;    
}

function mightBeLatLng(s) {
    var result = true;
    var lastCharWasDirectional = false;    
    var i;
    var c;
    
    for (i = 0; i < s.length; i++) {
        c = s.charAt(i);
        if (    ((c >= "0") && (c <= "9")) ||
                (c === "-") ||
                (c === ",") ||
                (c === "-") ||
                (c === "'") ||
                (c === "\"") ||
                (c === "°") ||
                (c === ".") ||
                (c === " ")) {
            // All of these characters are valid lat / lng characters, onto
            // the next character
            lastCharWasDirectional = false;
         }
         else if (  (c === "n") || (c === "N") ||
                    (c === "e") || (c === "E") ||
                    (c === "w") || (c === "W") ||
                    (c === "s") || (c === "S")) {
            // Directional charcters are valid lat /lng characters, but
            // we don't want to see more than one in a row, otherwise
            // it's probably not a lat / lng. For example, don't want
            // 'news' to match.
            if (lastCharWasDirectional) {
                result = false;
                break;
            }
            
            lastCharWasDirectional = true;
         }
         else {
            result = false;
            break;
         }
    }
    
    return result;
}

function disableEnterKey(e) {
    if(window.event) // IE
    {
        keynum = e.keyCode
    }
    else if(e.which) // Netscape/Firefox/Opera
    {
        keynum = e.which
    }
    //keychar = String.fromCharCode(keynum)

    //alert(keynum);
    if (keynum === 13)
        return false;
     else
        return true;
}

function ClearText(elementToClear, textToMatch)
{
    if (elementToClear!=null)
    {
        if (elementToClear.getAttribute("value") == textToMatch)
        {
            elementToClear.setAttribute("value","");
        }
    }
}

function ToggleOnElement(element)
{
    element.style.display="block";
}

function ToggleOffElement(element)
{
    element.style.display="none";
}

function FocusOnElement(element)
{
    element.focus();
}

function SelectInputText(elementID) {
    var txtInput = document.getElementById(elementID);
    if (txtInput != null)
        txtInput.select();
}

function kilometersPerHourToMilesPerHour(kilometersPerHour) {
    return kilometersPerHour * 0.621371192;
}

function kilometersPerHourToMinutesPerKilometer(kilometersPerHour) {
    return 60 / kilometersPerHour;
}

function milesPerHourToMinutesPerMile(milesPerHour) {
    return 60 / milesPerHour;
}

function metersToFeet(meters) {
    return meters * 3.2808399;
}

