﻿String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, "");
}

function getWindowWidth() {
    return window.innerWidth || document.body.clientWidth;
}

function getWindowHeight() {
    return window.innerHeight || document.body.clientHeight;
}

/* WATERMARK */
function watermark_focus(textbox, text, css) {
    if (textbox && textbox.value == text) {
        if (css.trim().length > 0 && textbox.className.indexOf(css) > -1) {
            var r = new RegExp('\\b' + css + '\\b');
            textbox.className = textbox.className.replace(r, '');
        }

        textbox.value = '';
    }
}
function watermark_blur(textbox, text, css) {
    if (textbox && (textbox.value.trim() == text || textbox.value.trim() == 0)) {
        if (css.trim().length > 0 && textbox.className.indexOf(css) == -1)
            textbox.className += ' ' + css;

        textbox.value = text;
    }
}

/* CONTENT COMMAND BUTTON */
var currentContentObjectId = 0;
var currentListId = null;

function contentCommand_OpenDialog(id) {

    var d = document.getElementById(id);
    var b = document.getElementById(id + '_modal-background');
    var t = document.getElementById(id + '_titlebar');


    if (d && b) {
        d.style.display = '';
        d.style.marginLeft = '0px';
        b.style.display = '';
        d.style.visibility = '';
        b.style.visibility = '';
        d.style.left = getWindowWidth() / 2 - 330 + "px";
        d.style.top = 200 + "px";
    }

    Drag.init(t, d);

    contentCommand_Loading(id);
    contentCommand_Clear(id);
}
function contentCommand_Clear(id) {
    var d = document.getElementById(id + '_contents');

    if (d)
        contentCommand_DoRecursive(d, null, 'clear');
}
function contentCommand_Load(dialogId, contentId) {
    var d = document.getElementById(dialogId);

    if (d)
        Bazment.Web.Services.ContentService.GetContentObject(contentId, contentCommand_LoadCallbackSucceeded, contentCommand_LoadCallbackFailed, dialogId);
}
function contentCommand_LoadCallbackSucceeded(result, userContext) {
    var d = document.getElementById(userContext + '_contents');

    if (d)
        contentCommand_DoRecursive(d, result, 'load');

    contentCommand_Finished(userContext);
}
function contentCommand_DoRecursive(d, result, arg) {
    if (!d)
        return;


    for (var i = 0; i < d.childNodes.length; i++) {

        if ((d.childNodes[i].nodeName.toLowerCase() == 'input' || d.childNodes[i].nodeName.toLowerCase() == 'select') && d.childNodes[i].type.toLowerCase() != 'button' && d.childNodes[i].type.toLowerCase() != 'submit') {

            var dataField = d.childNodes[i].getAttribute('DataField');

            if (arg == 'load') {


                if (!dataField)
                    continue;

                if (d.childNodes[i].type.toLowerCase() == 'checkbox') {
                    if (eval('result.' + dataField)) {
                        d.childNodes[i].checked = eval('result.' + dataField);
                        document.getElementById(d.childNodes[i].id.substring(0, d.childNodes[i].id.indexOf('_chkBox')) + '_chkValue').value = eval('result.' + dataField);
                    }
                    else if (result.CustomData[dataField]) {
                        if (result.CustomData[dataField].toLowerCase() == 'false') {
                            d.childNodes[i].checked = false;
                            document.getElementById(d.childNodes[i].id.substring(0, d.childNodes[i].id.indexOf('_chkBox')) + '_chkValue').value = 'false';
                        }
                        else {
                            d.childNodes[i].checked = true;
                            document.getElementById(d.childNodes[i].id.substring(0, d.childNodes[i].id.indexOf('_chkBox')) + '_chkValue').value = 'true';
                        }
                    }
                }
                else if (d.childNodes[i].nodeName.toLowerCase() == 'select') {
                    if (result.CustomData[dataField]) {
                        var arr = result.CustomData[dataField].split(',');
                        for (j = 0; j < d.childNodes[i].options.length; j++) {
                            for (k = 0; k < arr.length; k++) {
                                if (d.childNodes[i].options[j].value == arr[k]) {
                                    d.childNodes[i].options[j].selected = true;
                                }
                            }
                        }
                    }
                }
                else {
                    var tV = '';

                    if (eval('result.' + dataField))
                        tV = eval('result.' + dataField);
                    else if (result.CustomData[dataField])
                        tV = result.CustomData[dataField];

                    try {
                        if (!isNaN(Date.parse(tV))) {
                            var tDate = new Date(Date.parse(tV));
                            tV = tDate.getFullYear() + '-' + (tDate.getMonth() + 1) + '-' + tDate.getDate();
                        }
                    }
                    catch (ex) {
                    }

                    if (dataField && dataField.toLowerCase() == "sortorder" && tV && tV.length == 0)
                        tV = '0';

                    d.childNodes[i].value = tV;
                }
            }
            else if (arg == 'clear') {
                if (d.childNodes[i].type.toLowerCase() == 'checkbox')
                    d.childNodes[i].checked = false;
                else if (d.childNodes[i].nodeName.toLowerCase() == 'select') {
                    if (d.childNodes[i].getAttribute('multiple') == "multiple")
                        d.childNodes[i].selectedIndex = -1;
                    else
                        d.childNodes[i].selectedIndex = 0;
                }
                else {
                    if (dataField && dataField.toLowerCase().indexOf('date') > -1)
                        d.childNodes[i].value = new Date().getFullYear() + '-' + (new Date().getMonth() + 1) + '-' + new Date().getDate();
                    else if (dataField && dataField.toLowerCase() == 'sortorder')
                        d.childNodes[i].value = '0';
                    else
                        d.childNodes[i].value = '';
                }
            }
        }
        else {
            try {
                var e = tinyMCE.get(d.childNodes[i].id);

                if (e) {
                    if (arg == 'load') {
                        var dataField = d.childNodes[i].parentNode.getAttribute('DataField');

                        if (!dataField)
                            continue;

                        if (eval('result.' + dataField))
                            e.execCommand('mceSetContent', false, eval('result.' + dataField))
                        else if (result.CustomData[dataField])
                            e.execCommand('mceSetContent', false, result.CustomData[dataField])
                    }
                    else if (arg == 'clear') {
                        e.execCommand('mceSetContent', false, '');
                    }
                }
                else
                    contentCommand_DoRecursive(d.childNodes[i], result, arg);
            }
            catch (ex) {
                contentCommand_DoRecursive(d.childNodes[i], result, arg);
            }
        }
    }
}
function contentCommand_LoadCallbackFailed(error, userContext) {
    alert(error.get_message());
    contentCommand_Finished(userContext);
}
function contentCommand_Loading(id) {
    var e = document.getElementById(id + '_modal-loader');

    if (e)
        e.style.display = 'block';
}

function contentCommand_Finished(id) {
    var e = document.getElementById(id + '_modal-loader');

    if (e)
        e.style.display = 'none';
}
function contentCommand_CloseDialog(id) {
    var d = document.getElementById(id);
    var b = document.getElementById(id + '_modal-background');

    if (d && b) {
        /*d.style.display = 'none';
        d.style.visibility = 'hidden';
        */
        b.style.display = 'none';
        b.style.visibility = 'hidden';
        d.style.marginLeft = '-5000px';
    }
}
function contentCommand_SaveButton(id, validationGroup) {
    if (validationGroup && validationGroup.length > 0) {
        if (!Page_ClientValidate(validationGroup)) {
            //var dlgError = document.getElementById(id + '_error');

            for (var i in Page_Validators) {
                if (Page_Validators[i].display && Page_Validators[i].errormessage && Page_Validators[i].display == 'None' && Page_Validators[i].errormessage.length > 0) {
                    alert(Page_Validators[i].errormessage);
                }
            }

            return false;
        }
    }

    __doPostBack(id, 'save,' + currentListId + ',' + currentContentObjectId);
}
function contentCommand_CloseButton(id) {
    __doPostBack(id, 'close,' + currentListId + ',' + currentContentObjectId);
}
function contentCommand_Add(listId, dialogId) {
    currentContentObjectId = 0;
    currentListId = listId;

    contentCommand_OpenDialog(dialogId);
    contentCommand_Finished(dialogId);

    return false;
}
function contentCommand_Edit(listId, dialogId, arg) {
    currentListId = listId;
    currentContentObjectId = arg;

    contentCommand_OpenDialog(dialogId);
    contentCommand_Load(dialogId, arg);

    return false;
}
function contentCommand_AddEdit(listId, dialogId, arg) {
    currentListId = listId;
    currentContentObjectId = arg;

    contentCommand_OpenDialog(dialogId);

    if (arg)
        contentCommand_Load(dialogId, arg);
    else
        contentCommand_Add(listId, dialogId);

    return false;
}
function contentCommand_Remove(dialogId, arg) {
    return true;
}
function contentCommand_ChangeState(dialogId, arg) {
    return true;
}

var Drag = {

    obj: null,

    init: function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper) {
        o.onmousedown = Drag.start;

        o.hmode = bSwapHorzRef ? false : true;
        o.vmode = bSwapVertRef ? false : true;

        o.root = oRoot && oRoot != null ? oRoot : o;

        if (o.hmode && isNaN(parseInt(o.root.style.left))) o.root.style.left = "0px";
        if (o.vmode && isNaN(parseInt(o.root.style.top))) o.root.style.top = "0px";
        if (!o.hmode && isNaN(parseInt(o.root.style.right))) o.root.style.right = "0px";
        if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";

        o.minX = typeof minX != 'undefined' ? minX : null;
        o.minY = typeof minY != 'undefined' ? minY : null;
        o.maxX = typeof maxX != 'undefined' ? maxX : null;
        o.maxY = typeof maxY != 'undefined' ? maxY : null;

        o.xMapper = fXMapper ? fXMapper : null;
        o.yMapper = fYMapper ? fYMapper : null;

        o.root.onDragStart = new Function();
        o.root.onDragEnd = new Function();
        o.root.onDrag = new Function();
    },

    start: function(e) {
        var o = Drag.obj = this;
        e = Drag.fixE(e);
        var y = parseInt(o.vmode ? o.root.style.top : o.root.style.bottom);
        var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right);
        o.root.onDragStart(x, y);

        o.lastMouseX = e.clientX;
        o.lastMouseY = e.clientY;

        if (o.hmode) {
            if (o.minX != null) o.minMouseX = e.clientX - x + o.minX;
            if (o.maxX != null) o.maxMouseX = o.minMouseX + o.maxX - o.minX;
        } else {
            if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
            if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
        }

        if (o.vmode) {
            if (o.minY != null) o.minMouseY = e.clientY - y + o.minY;
            if (o.maxY != null) o.maxMouseY = o.minMouseY + o.maxY - o.minY;
        } else {
            if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
            if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
        }

        document.onmousemove = Drag.drag;
        document.onmouseup = Drag.end;

        return false;
    },

    drag: function(e) {
        e = Drag.fixE(e);
        var o = Drag.obj;

        var ey = e.clientY;
        var ex = e.clientX;
        var y = parseInt(o.vmode ? o.root.style.top : o.root.style.bottom);
        var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right);
        var nx, ny;

        if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
        if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
        if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
        if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);

        nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
        ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));

        if (o.xMapper) nx = o.xMapper(y)
        else if (o.yMapper) ny = o.yMapper(x)

        Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
        Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
        Drag.obj.lastMouseX = ex;
        Drag.obj.lastMouseY = ey;

        Drag.obj.root.onDrag(nx, ny);
        return false;
    },

    end: function() {
        document.onmousemove = null;
        document.onmouseup = null;
        Drag.obj.root.onDragEnd(parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]),
									parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
        Drag.obj = null;
    },

    fixE: function(e) {
        if (typeof e == 'undefined') e = window.event;
        if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
        if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
        return e;
    }
};
