﻿/// <reference path="~/Content/js/lib/jquery/jquery.min.js" />

$(document).ready(function() {
    $("input:text").hint();
});

// Gets an object by its name; ideal for getting swf objects for
// external interface calls.
function getMyApp(appName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[appName];
    } else {
        return document[appName];
    }
}

// Extract an ID from the end of a string of the form <anything>_<id>.
// Good for cases where you're storing the object's server-side ID and
// need to extract it in Javascript.
function extractId(str)
{
     return str.substring(str.lastIndexOf("_") + 1);
}

// Generates a random string based on the current time.
function rnd() 
{
    return String((new Date()).getTime()).replace(/\D/gi, '');
}

// Adds an error message to the given container, with the given text.
function addErrorMessage(str, container) {
    var $obj = $("<div class=\"ui-widget error_dialog\">" +
        "<div class=\"ui-state-error ui-corner-all\" style=\"padding: 0pt 0.7em;\">" +
            "<p>" +
            "<span class=\"ui-icon ui-icon-alert\" style=\"float: left; margin-right: 0.3em;\"></span>" +
            "<span id=\"error_dialog_message\">" + str + "</span>" +
            "</p>" +
        "</div>" +
      "</div>").appendTo(container);

    setTimeout(function() {
        $obj.fadeOut('normal', function() { $(this).remove() });
    }, 5000);
}

// Mimics the String.Format() function from C#.
function StringFormat(text) {
    //check if there are two arguments in the arguments list
    if (arguments.length <= 1) {
        //if there are not 2 or more arguments there’s nothing to replace
        //just return the original text
        return text;
    }
    //decrement to move to the second argument in the array
    var tokenCount = arguments.length - 2;
    for (var token = 0; token <= tokenCount; token++) {
        //iterate through the tokens and replace their placeholders from the original text in order
        text = text.replace(new RegExp("\\{" + token + "\\}", "gi"),
                                            arguments[token + 1]);
    }
    return text;
}

// Remove all leading and trailing instances of charToTrim from str, returning
// the trimmed string.
function trim(str, charToTrim) {

    var startCut = 0;
    var endCut = 0;

    if (str == "")
        return str;

    var charIndex = 0;
    while ((charIndex < str.length) && (str[charIndex] == charToTrim)) {
        startCut += 1;
        charIndex += 1;
    }
    
    charIndex = str.length - 1;
    while ((charIndex > startCut) && (str[charIndex] == charToTrim)) {
        endCut += 1;
        charIndex -= 1;
    }

    return str.substring(startCut, str.length - endCut);
}

// Creates a path from an abitrary number of string elements. In effect, this just
// puts forward slashes in between adjacent elements, and makes sure that
// if any of the elements already have slashes leading or trailing, they
// are not included twice.
function formatPath() {

    var pathStr = "";
    var lastPart = "";
    var trimmed;

    for (var i = 0; i < arguments.length; i++) {
        trimmed = trim(arguments[i], '/');
        if (lastPart != "")
            pathStr += '/';
        pathStr += trimmed;
        lastPart = trimmed;
    }

    return pathStr;
    
}