﻿/*
* Swisschecklist Javascript
* Copyright (c) 2010 2sic internet solutions gmbh
* Author: Pascal Schweizer
* 
* Description: 
* Requires jQuery 1.3.2
*
*/
// Initilize the SwissChecklist Object
// which holds the ChecklistController, Extensions Methods etc..
var Namespaces = new Array(
"SwissChecklist",
"SwissChecklist.UI",
"SwissChecklist.ChecklistController",
"SwissChecklist.ChecklistController.Checklist",
"SwissChecklist.ChecklistController.NewChecklist",
"SwissChecklist.ChecklistController.Checklist.ViewModes",
"SwissChecklist.ChecklistController.Steps",
"SwissChecklist.ChecklistController.Publish",
"SwissChecklist.ChecklistController.Delete",
"SwissChecklist.Extensions",
"SwissChecklist.Extensions.Attachments",
"SwissChecklist.Extensions.Forms",
"SwissChecklist.Ajax"
);


function RegisterNamespace(ns) {
    var nsParts = ns.split(".");
    var root = window;

    for (var i = 0; i < nsParts.length; i++) {
        if (typeof root[nsParts[i]] == "undefined")
            root[nsParts[i]] = new Object();

        root = root[nsParts[i]];
    }
}

// Create Namespaces
for (var i = 0; i < Namespaces.length; i++) {
    RegisterNamespace(Namespaces[i]);
}


// Global const.
SwissChecklist.Ajax.SaveChecklistUrl = "/SwissChecklist/SwissChecklistServices.asmx/SaveChecklist";


// Use this method to get a localized text by it's key
function GetLocalizedText(key, args) {
    if (args == undefined)
        args = new Array();
    if (typeof(SwissChecklist.LocalizedTexts[key]) == "undefined")
        return FormatString("MISSING TEXT: {0}", key);
    return FormatString(SwissChecklist.LocalizedTexts[key], args);
}
function FormatString(str, args) {
    if (typeof args == "object") {
        for (i = 0; i < args.length; i++) {
            str = str.replace('{' + i + '}', args[i]);
        }
    }
    else
        str = str.replace('{' + 0 + '}', args[0]);
    //Fix new-line contrants
    return str.replace("\\n", "\n");
}

function EscapeString(String) {
    var RegExpr = new RegExp("'", "g");
    String = String.replace(RegExpr, "&apos;");

    var RegExpr = new RegExp("\"", "g");
    String = String.replace(RegExpr, "&quote;");

    return String;
}

function UnEscapeString(String) {
    var RegExpr = new RegExp("&apos;", "g");
    String = String.replace(RegExpr, "'");

    var RegExpr = new RegExp("&quote;", "g");
    String = String.replace(RegExpr, "\"");

    return String;
}

var delay = (function() {
    var timer = 0;
    return function(callback, ms) {
        clearTimeout(timer);
        timer = setTimeout(callback, ms);
    };
})();

function newGuid() {
    var g = "";
    for (var i = 0; i < 32; i++)
        g += Math.floor(Math.random() * 0xF).toString(0xF) + (i == 7 || i == 11 || i == 15 || i == 19 ? "-" : "")
    return g;
}

function DeleteArrayElement(a, e) {
    //Delete an array element by its name
    for (var i = 0; i < a.length; i++) {
        if (a[i] == "'" + e + "'" || a[i] == e)
            a.splice(i, 1);
    }
    return a;
}

//Extend String with startswith Function
String.prototype.startsWith = function(str) { return (this.match("^" + str) == str) }

String.prototype.endsWith = function(str) { return (this.match(str + "$") == str) }
//Contains method for arrays
Array.prototype.contains = function(element) { for (var i = 0; i < this.length; i++) { if (this[i] == element) { return true; } } return false; }

// Extend jQuery to select extension tags (e.g: <span scl:extension="Forms")
$.extend($.expr[":"], {
    extension: function(a) {
        return $(a).attr("scl:extension") != "";
    }
});


(function($) {
    $.fn.disable = function() {
        ///	<summary>
        ///		Disables the set of input elements
        ///	</summary>
        ///	<returns type="jQuery" />

        return this.each(function() {
            this.disabled = 'true';
        });
    }

    $.fn.enable = function() {
        ///	<summary>
        ///		Disables the set of input elements
        ///	</summary>
        ///	<returns type="jQuery" />

        return this.each(function() {
            this.disabled = '';
        });
    }
})(jQuery);



// Ajax function
SwissChecklist.Ajax.AjaxRequest = function(WebserviceUrl, Data, Success, Error) {
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        url: WebserviceUrl,
        data: Data,
        success: function(msg) { Success(msg) },
        error: function(xhr, status, error) { Error(xhr, status, error) }
    });
}