﻿function atl_ToggleDisplay(divId) {
	var div = document.getElementById(divId);
	if (div)
		div.style.display = (div.style.display=='block' ? 'none' : 'block');
	return true;
}
function atl_SwapDisplay(div1Id, div2Id) {
	atl_ToggleDisplay(div1Id);
	atl_ToggleDisplay(div2Id);
	return true;
}
function atl_Go(url,t) {
	if ((t == null) || (t == '')) { t="_self"; }
	window.open(url,t);
}
function atl_PopHelp(url) {
	var win = window.open(url, 'spop', 'left=20,top=20,resizable=yes,scrollbars=yes,width=610,height=620');
}
function atl_PopUp(url, name, features) {
	var win = window.open(url, name, features);
}
function atl_GetQuickHelpContent(name, setQuickHelpDiv, onQuickHelpError, sourceObject) {
    if ((typeof (DynamicHtmlContent) != 'undefined') && (typeof (DynamicHtmlContent.GetQuickHelpContent) != 'undefined')) {
        DynamicHtmlContent.GetQuickHelpContent(name, setQuickHelpDiv, onQuickHelpError, sourceObject);
    }
}
var atl_HideInvoked = false;
function atl_ShowQuickHelp(event, name) {
    atl_HideInvoked = false;
    var popupDiv = document.getElementById('atl_quickhelp');
    if (popupDiv == null) {
        return;
    }
    var sourceObject;
    if (event.target != null) {
        sourceObject = event.target;
    }
    else {
        if (event.srcElement != null) {
            sourceObject = event.srcElement;
        }
        else {
            return;
        }
    }
    if (!$.jCache.hasItem(name)) {
        atl_GetQuickHelpContent(name, atl_SetQuickHelpDiv, atl_OnQuickHelpError, sourceObject);
    }
    else {
        popupDiv.innerHTML = $.jCache.getItem(name);
        atl_ShowHelpById(sourceObject, popupDiv);
        atl_ShowDivContent(popupDiv);
    }
}
function atl_HideQuickHelp() {
    atl_HideInvoked = true;
    var tempElem = document.getElementById('atl_quickhelp');
    if (tempElem != null) {
        tempElem.style.display = "none";
        tempElem.style.visibility = "hidden";
    }
}
function atl_OnQuickHelpError(result) {
    alert("Error: " + result.get_message());
}
function atl_SetQuickHelpDiv(result, sourceObject) {
    var popupDiv = document.getElementById('atl_quickhelp');
    if (popupDiv != null && result != null) {
        popupDiv.innerHTML = result.Html;
        atl_ShowHelpById(sourceObject, popupDiv)
        atl_ShowDivContent(popupDiv);
        if (!$.jCache.hasItem(result.Name)) {
            $.jCache.setItem(result.Name, result.Html);
        }
    }
}
function atl_ShowDivContent(div) {
    if (div != null && !atl_HideInvoked) {
        div.style.display = "block";
        div.style.visibility = "visible";
    }
}
function atl_ShowHelpById(obj, helpObj) {
    if (helpObj) {
        var divWidth = 340;
        var offsetLeft = atl_getOffsetLeft(obj);
        var screenWidth = (window.innerWidth) ? window.innerWidth - 25 : document.body.clientWidth;
        if ((offsetLeft + divWidth) > screenWidth) offsetLeft = screenWidth - divWidth;
        newX = offsetLeft;
        var divHeight = helpObj.offsetHeight;
        var offsetTop = atl_ShowHelp(obj) + obj.offsetHeight;
        var screenHeight = (window.innerHeight) ? window.innerHeight - 25 : document.body.clientHeight;
        if ((offsetTop + divHeight) > screenHeight + atl_getScrollY()) offsetTop = atl_ShowHelp(obj) - divHeight;
        newY = offsetTop;
        helpObj.style.top = newY + 'px';
        helpObj.style.left = newX + 'px';
        helpObj.left = newX + 'px';
        helpObj.left = newY + 'px';
    }
}
function atl_ShowHelp(elm) {
    var mOffsetTop = elm.offsetTop;
    var mOffsetParent = elm.offsetParent;
    while (mOffsetParent) {
        mOffsetTop += mOffsetParent.offsetTop;
        mOffsetParent = mOffsetParent.offsetParent;
    }
    return mOffsetTop;
}
function atl_getOffsetLeft(elm) {
    var mOffsetLeft = elm.offsetLeft;
    var mOffsetParent = elm.offsetParent;
    while (mOffsetParent) {
        mOffsetLeft += mOffsetParent.offsetLeft;
        mOffsetParent = mOffsetParent.offsetParent;
    }
    return mOffsetLeft;
}
function atl_getScrollY() {
    var scrOfY = 0;
    if (typeof (window.pageYOffset) == 'number') {
        scrOfY = window.pageYOffset;
    }
    else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
        scrOfY = document.body.scrollTop;
    }
    else if (document.documentElement &&
      (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
        scrOfY = document.documentElement.scrollTop;
    }
    return scrOfY;
}
function atlCookieDomain() {
    var hname = window.location.hostname;
    var tldstart = hname.lastIndexOf('.');
    if (tldstart < 0) {
        return '.' + hname;
    }
    else {
        var dname = '';
        if (hname.lastIndexOf('.', tldstart - 1) > -1) {
            dname = hname.substr(hname.lastIndexOf('.', tldstart - 1));
        }
        else {
            dname = '.' + hname;
        }
        return dname;
    }
}
function atlSetMemCookie(name, value, years) {
    var currentDT = new Date();
    var curCookie = name + "=" + value + "; path=/; domain=" + atlCookieDomain();
    document.cookie = curCookie;
}
function atlSetCookie(name, value, years) {
    var currentDT = new Date();
    var expires = new Date(Date.parse(currentDT.getDay() + "/" + currentDT.getMonth() + "/" + (currentDT.getFullYear() + years)));
    var curCookie = name + "=" + value +
      "; expires=" + expires.toGMTString() + "; path=/; domain=" + atlCookieDomain();
    document.cookie = curCookie;
}
function atlReadCookie(name) {
    var cookiecontent = new String();
    if (document.cookie.length > 0) {
        var cookiename = name + '=';
        var cookiebegin = document.cookie.indexOf(cookiename);
        var cookieend = 0;
        if (cookiebegin > -1) {
            cookiebegin += cookiename.length;
            cookieend = document.cookie.indexOf(";", cookiebegin);
            if (cookieend < cookiebegin) {
                cookieend = document.cookie.length;
            }
            cookiecontent = document.cookie.substring(cookiebegin, cookieend);
        }
    }
    return unescape(cookiecontent);
}

Type.registerNamespace("Atlantis");

Atlantis.DynamicForm = function(formId) {
    Atlantis.DynamicForm.initializeBase(this);
    this._formId = formId;
    this._form = $get(this._formId);
}
Atlantis.DynamicForm.prototype = {
    setFormValue: function(keyName, formValue) {
        var input = this._getFormInput(keyName);
        input.value = formValue;
    },

    _getFormInput: function(keyName) {
        var input = null;
        for (var i = 0; i < this._form.length; i++) {
            if (this._form.elements[i].id == keyName) {
                input = this._form.elements[i];
                break;
            }
        }
        if (input == null) {
            var newInput = document.createElement("input");
            newInput.setAttribute("id", keyName);
            newInput.setAttribute("type", "hidden");
            newInput.name = keyName;
            input = newInput;
            this._form.appendChild(input);
        }
        return input;
    },

    submit: function() {
        this._form.submit();
    },

    submitAction: function(action) {
        this._form.action = action;
        this._submit();
    }
}

Atlantis.DynamicForm.registerClass("Atlantis.DynamicForm");

function atl_isemailvalid(email) {
    var at = "@";
    var dot = ".";
    var lat = email.indexOf(at);
    var lstr = email.length;
    var ldot = email.indexOf(dot);
    if (email.indexOf(at)==-1){return false;}
    if (email.indexOf(at)==-1 || email.indexOf(at)==0 || email.indexOf(at)==lstr){return false;}
    if (email.indexOf(dot)==-1 || email.indexOf(dot)==0 || email.indexOf(dot)==lstr){return false;}
    if (email.indexOf(at,(lat+1))!=-1){return false;}
    if (email.substring(lat-1,lat)==dot || email.substring(lat+1,lat+2)==dot){return false;}
    if (email.indexOf(dot,(lat+2))==-1){return false;}
    if (email.indexOf(" ")!=-1){return false;}
    return true;
}
function atl_isnoscript(text) {
    var regExInvalidChars = /[<>]+/;
    if (regExInvalidChars.test(text)) {
        return false;
    }
    return true;
}
function atl_textarea_trim(field, maxlength) {
    if (field.value.length <= maxlength) { return; }
    field.value = field.value.substr(0, maxlength);
}
function atl_textarea_canaddchar(field, maxlength) {
    var keyCode = null;
    if (typeof (field.onkeypress.arguments[0]) != 'undefined') {
        keyCode = field.onkeypress.arguments[0].keyCode;
    }
    else {
        if (document.selection.createRange().text.length != 0) { return true; }
        var keyCode = event.keyCode;
    }
    var allowedKeys = new Array(8, 37, 38, 39, 40, 46);
    for (var x = 0; x < allowedKeys.length; x++) {
        if (allowedKeys[x] == keyCode) { return true; }
    }
    if (field.value.length < maxlength) { return true; }
    return false;
}
(function(jQuery) {
    this.version = '(beta)(0.0.1)';
    this.maxSize = 10;
    this.keys = new Array();
    this.cache_length = 0;
    this.items = new Array();
    this.setItem = function(pKey, pValue) {
        if (typeof (pValue) != 'undefined') {
            if (typeof (this.items[pKey]) == 'undefined') {
                this.cache_length++;
            }

            this.keys.push(pKey);
            this.items[pKey] = pValue;

            if (this.cache_length > this.maxSize) {
                this.removeOldestItem();
            }
        }
        return pValue;
    }
    this.removeItem = function(pKey) {
        var tmp;
        if (typeof (this.items[pKey]) != 'undefined') {
            this.cache_length--;
            var tmp = this.items[pKey];
            delete this.items[pKey];
        }

        return tmp;
    }
    this.getItem = function(pKey) {
        return this.items[pKey];
    }
    this.hasItem = function(pKey) {
        return typeof (this.items[pKey]) != 'undefined';
    }
    this.removeOldestItem = function() {
        this.removeItem(this.keys.shift());
    }
    this.clear = function() {
        var tmp = this.cache_length;
        this.keys = new Array();
        this.cache_length = 0;
        this.items = new Array();
        return tmp;
    }
    jQuery.jCache = this;
    return jQuery;
})(jQuery);
