﻿//needs to show Edit Node popup box
function showEditNodeDialog(pageId, pageTitle, width, height) {
    pageTitle = "[" + String(pageTitle ? pageTitle : pageId) + "]";
    width = width && Number(width) > 0 ? width : 980;
    height = height && Number(hei) > 0 ? height : 720;
    UmbClientMgr.openModalWindow('/umbraco/editContent.aspx?id=' + pageId, pageTitle + ' Page Editing', true, width, height);
};


function showSearchResult(url, keyFldId) {
    var v = URLEncode(jQuery("#" + keyFldId).val());
    window.location = url + "?search=" + v;
}



function submitByEnter(event, resUrl, controls, methodName) {
    if (event.keyCode == 13) {
        GenerateSubmitForm(resUrl, controls, methodName);
        event.preventDefault();
    }
}


function GenerateSubmitForm(resUrl, controls, methodName) {
    methodName = methodName ? methodName : "post";
    var frm = jQuery("<div style='display:none; border:1px solid transparent;'><form action=\"" + resUrl + "\" method=\"" + methodName + "\"><input type=\"submit\" value=\"submit\" class=\"submitybttn\"/></form></div>");
    frm.appendTo(jQuery(document.body));
    frm = jQuery(frm.find("form")[0]);
    var btn = frm.find(".submitybttn")[0];
    ///generate controls
    var i = 0;
    while (i <= controls.length - 1) {
        var cname = controls[i];
        var cselector;
        if (typeof (cname) == 'object') {
            cname = controls[i].name;
            cselector = controls[i].selector;
        }
        AddInputElements(frm, cname, methodName);
        i++;
    }
    RemoveTagsFromAllChilds(frm);
    frm[0].submit();
}


function AddInputElements(frm, elname, methodName) {
    var sourceEl = $("#" + elname);
    if (methodName == "post" || (methodName == "get" && sourceEl.val() != '')) {  // post all elements, get only not empty
        var el = jQuery("<input type=\"text\" name=\"" + elname + "\" id=\"" + elname + "\"/>");
        el.appendTo(frm);
        el.val(sourceEl.val());
    }
}


function URLEncode(c) {
    var o = ''; var x = 0; c = c.toString(); var r = /(^[a-zA-Z0-9_.]*)/;
    while (x < c.length) {
        var m = r.exec(c.substr(x));
        if (m != null && m.length > 1 && m[1] != '') {
            o += m[1]; x += m[1].length;
        } else {
            if (c[x] == ' ') o += '+'; else {
                var d = c.charCodeAt(x); var h = d.toString(16);
                o += '%' + (h.length < 2 ? '0' : '') + h.toUpperCase();
            } x++;
        }
    } return o;
}

function URLDecode(s) {
    var o = s; var binVal, t; var r = /(%[^%]{2})/;
    while ((m = r.exec(o)) != null && m.length > 1 && m[1] != '') {
        b = parseInt(m[1].substr(1), 16);
        t = String.fromCharCode(b); o = o.replace(m[1], t);
    } return o;
}


function RemoveTagsOnSubmit(frmel) {

    var frms = frmel && String(frmel).length > 0 ? frmel : $(document.body).find("form");
    frms.each(function () {
        var $frm = $(this);
        $frm.submit(function () {
            RemoveTagsFromAllChilds($frm);
        });
    });
}

function RemoveTagsFromAllChilds(mainEl) {
    mainEl.find("input:not(.dontdeletetags)").each(function () {
        if ($(this).attr("name").indexOf("_") != 0) {
            var elval = String($(this).val());
            var resval = elval ? elval.replace(new RegExp("<", "g"), "&lt;").replace(new RegExp(">", "g"), "&gt;") : "";
            $(this).val(resval);
        }
    });
    mainEl.find("textarea:not(.dontdeletetags)").each(function () {
        if ($(this).attr("name").indexOf("_") != 0) {
            var elval = String($(this).val());
            var resval = elval ? elval.replace(new RegExp("<", "g"), "&lt;").replace(new RegExp(">", "g"), "&gt;") : "";
            $(this).val(resval);
        }
    });
}


(function ($) {
    $.fn.watermark = function (css, text) {
        return this.each(function () {
            var i = $(this), w;
            i.focus(function () {
                w && !(w = 0) && i.removeClass(css).data('w', 0).val('');
            })
			.blur(function () {
			    !i.val() && (w = 1) && i.addClass(css).data('w', 1).val(text);
			})
            //			.closest('form').submit(function () {
            //			    w && i.val('');
            //			});
            i.blur();
        });
    };
    $.fn.removeWatermark = function () {
        return this.each(function () {
            $(this).data('w') && $(this).val('');
        });
    };
})(jQuery);



$(document).ready(function () {
    RemoveTagsOnSubmit();
});

function initComments(formselector, commentsselector, iscanvas) {
    var delegate = function () {
        $.CommentsFormManager(formselector, iscanvas);
        $.CommentsManager(commentsselector, iscanvas);
    };
    ExecuteDelegate(function () { return typeof ($.CommentsFormManager) == 'function'; }, delegate);
};

function ExecuteDelegate(checkrule, delegate) {
    if (checkrule.call()) delegate.call();
    else {
        setTimeout(function () { ExecuteDelegate(checkrule, delegate); }, 100);
    }
}


function RequestTypeRequired(sender, args) {
    if ($(".requesttype input:checked").length > 0)
        args.IsValid = true;
    else
        args.IsValid = false;
}

jQuery.cookie = function (name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) { value = ''; options.expires = -1; }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') { date = new Date(); date.setTime(date.getTime() + (options.expires * 60 * 60 * 1000)); }
            else { date = options.expires; }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else {
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
