﻿document.onmousedown = function() { if (event.button == 2) { alert('(C) 2009 Message Planet, Inc'); return false; } }
document.onmouseover = function() { window.status = ''; return true; }
document.onmouseout = function() { window.status = ''; return true; }

function ParseData(Data, Name, Separator) {
    if (typeof Separator != "undefined") {
        var Value = "";
        var Start = Data.indexOf(Name + "=");
        if (Start >= 0) {
            Start = Start + Name.length + 1;
            var End = Data.indexOf(Separator, Start);
            if (End > 0) {
                Value = Data.substring(Start, End);
            }
            else {
                Value = Data.substring(Start);
            }
        }
        LowerCaseValue = Value.toLowerCase();
        if (LowerCaseValue == "true") {
            return true;
        }
        else if (LowerCaseValue == "false") {
            return false;
        }
    }
    else {
        return ParseData(Data, Name, ";&"); // Default separator = ';&'
    }
    return Value;
}

function SetMessage(Message, Period) {
    SetInnerText(document.getElementById("MessageLabel"), Message);
    if (Message != "") {
        if (typeof Period != "undefined") {
            if (Period != 0) {
                setTimeout("SetMessage('')", Period);
            }
        }
        else {
            setTimeout("SetMessage('')", 3000);
        }
    }
}

// Reporting functions

function IsDebug() {
    if (typeof Debugging != "undefined") {
        return true;
    }
    return false;
}

function ReportServiceError() {
    if (IsDebug()) {
        var Result = ReportServiceError.caller.arguments[0];
        Debug.writeln("JS SERVICE ERROR: " + location.pathname + "." + GetCallerName() + "(): " + Result.errorDetail.code + " - " + Result.errorDetail.string + "!");
    }
}

function ReportException(Exception) {
    if (IsDebug()) {
        Debug.writeln("JS EXCEPTION: " + location.pathname + "." + GetCallerName() + "(): Exception - " + Exception.message + "!");
    }
}

function ReportGenericError(Message) {
    if (IsDebug()) {
        Debug.writeln("JS ERROR: " + location.pathname + "." + GetCallerName() + "(): Error - " + Message);
    }
}

function DebugMessage(Message) {
    if (IsDebug()) {
        Debug.writeln("JS: " + location.pathname + "." + GetCallerName() + "(): " + Message);
    }
}

function GetCallerName(Level) {
    var CallerName = "";
    if (Level == null) {
        Level = 1;
    }
    try {
        if (Level == 1) {
            CallerName = GetCallerName.caller.caller.toString().substring(9);
        }
        else if (Level == 2) {
            CallerName = GetCallerName.caller.caller.caller.toString().substring(9);
        }
        else {
            return "Unknown";
        }
    } catch (E) {
        return "Unknown";
    }
    return CallerName.substring(0, CallerName.indexOf("("));
}

// Misc functions

function GetClientTime() {
    var Clock = new Date();
    return Clock.toTimeString();
}

function SetTime() {
    SetInnerText(document.getElementById("TimeString"), GetClientTime());
    setTimeout("SetTime()", 1000);
}

function SetDate() {
    SetInnerText(document.getElementById("DateString"), GetClientDate());
    setTimeout("SetDate()", 1000);
}

function GetParam(Param, Default) {
    var Return = Default;
    try {
        var URLQuery = location.href.split("?");
        var URLQueryPairs = URLQuery[1].split("&");
        var NameValuePair = URLQueryPairs[Param - 1].split("=");
        Return = NameValuePair[1];
    } catch (E) { }
    return unescape(Return);
}

function GetNamedParam(Param, Default) {
    var Return = Default;
    try {
        var URLQuery = location.href.split("?");
        var URLQueryPairs = URLQuery[1].split("&");
        for (var I = 0; I < URLQueryPairs.length; I++) {
            if(URLQueryPairs[I].indexOf(Param) == 0)
            {
                var NameValuePair = URLQueryPairs[I].split("=");
                Return = NameValuePair[1];
            }
        }
    } catch (E) { }
    return unescape(Return);
}

function ShowElement(ElementName, Flag) {
    try {
        if (Flag) {
            document.getElementById(ElementName).style.display = "block";
        } else {
            document.getElementById(ElementName).style.display = "none";
        }
    } catch (E) { }
}

function SetVisible(ElementName, Flag) {
    try {
        if (Flag) {
            document.getElementById(ElementName).style.visibility = "visible";
        } else {
            document.getElementById(ElementName).style.visibility = "hidden";
        }
    } catch (E) { }
}

function SetFocus(FocusElement) {
    try {
        document.getElementById(FocusElement).focus();
    } catch (E) { }
}

function CreateId() {
    var Chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    var StringId = '';
    while (StringId.length < 5) {
        var N = Math.floor(Math.random() * Chars.length);
        StringId += Chars.substring(N, N + 1);
    }
    return StringId;
}

function CheckUserId() {
    var UserId = Prefs.getString("UserId");
    if (UserId == 0) {
        UserId = CreateId();
        Prefs.set("UserId", UserId);
    }
}

function SetCookie(Name, Value) {
    document.cookie = Name + "=" + escape(Value);
    // Expires the cookie in one month
    var date = new Date();
    date.setMonth(date.getMonth() + 1);
    document.cookie += ("; expires=" + date.toUTCString());
}

function GetCookie(Name) {
    // Cookies separated by semicolons
    var Cookie = document.cookie.split("; ");
    for (var I = 0; I < Cookie.length; I++) {
        // Name / value pair is separated by an equal sign
        var Crumb = Cookie[I].split("=");
        if (Name == Crumb[0])
            return unescape(Crumb[1]);
    }
    // Cookie with the requested name does not exist
    return null;
}

function SetInnerText(Element, Text) {
    if (document.all) { // IE
        Element.innerText = Text;
    } else {
        Element.textContent = Text;
    }
}

String.prototype.startsWith = function(str) {
    return (this.match("^" + str) == str)
}

function Failure(Error) {
    var StackTrace = Error.get_stackTrace();
    var Message = Error.get_message();
    var StatusCode = Error.get_statusCode();
    var ExceptionType = Error.get_exceptionType();
    var Timedout = Error.get_timedOut();
    SetMessage("Stack Trace: " + StackTrace + "Service Error: " + Message + "Status Code: " + StatusCode + "Exception Type: " + ExceptionType + "Timedout: " + Timedout);
}
