﻿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_ShowQuickHelp(event, name)
{
	quickHelp.startCallback(event, "name=" + name.toString(), null, atl_OnQuickHelpError);
}

function atl_HideQuickHelp()
{
	quickHelp.hide();
}

function atl_OnQuickHelpError(result)
{
	alert(result.message);
}

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);
}

//Pagecast
var atl_pagecast_data = "";
var atl_pagecast_file = "";
var atl_scroll_int;
var atl_scroll_time, atl_scroll_st, atl_scroll_dist, atl_scroll_dur, atl_scroll_int2;

function atlPagecastStart(firstAnchorId, arrow, nodeValue, shprtype) {
    atl_pagecast_file = shprtype;
    atlBubble(firstAnchorId, arrow, nodeValue)
}

function atlScrollToAnchor(anchorId) {
    var anchors, i, ele;
    if (!document.getElementById)
        return;
    
    anchors = document.getElementsByTagName("a");
    for (i = 0; i < anchors.length; i++) {
        if (anchors[i].name == anchorId) {
            ele = anchors[i];
            i = anchors.length;
        }
    }
    
    if (window.scrollY) { atl_scroll_st = window.scrollY; }
    else
        if (document.documentElement.scrollTop) { atl_scroll_st = document.documentElement.scrollTop; }
    else { atl_scroll_st = document.body.scrollTop; }
    atl_scroll_dist = ele.offsetTop - atl_scroll_st - 100;
    atl_scroll_dur = 500; atl_scroll_time = 0; atl_scroll_int2 = 10;
    clearInterval(atl_scroll_int); atl_scroll_int = setInterval(atlScrollPage, atl_scroll_int2);
}

function atlBubble(anchorId, arrow, nodeValue) {
    var anchor = document.getElementById(anchorId);

    if (anchor != null) {
        atlScrollToAnchor(anchorId);

        var spleft = sptop = spwidth = spheight = 0;
        if (anchor.offsetParent) {
            spleft = anchor.offsetLeft;
            sptop = anchor.offsetTop;
            spwidth = anchor.offsetWidth;
            spheight = anchor.offsetHeight;

            while (anchor = anchor.offsetParent) {
                spleft += anchor.offsetLeft;
                sptop += anchor.offsetTop;
            }
        }

        var height, width, left, top;
        var bubble = document.createElement('div');
        bubble.style.position = "absolute";
        var version = navigator.userAgent;
        var findIE6 = version.indexOf('MSIE 6');

        if (arrow == "LfTpStart") {
            bubble.style.left = spleft - 5 + spwidth + "px";
            bubble.style.top = sptop - 0 + "px";
            if (findIE6 != -1) {
                bubble.className = "sm_top_left_start_ie6";
            }
            else {
                bubble.className = "sm_top_left_start";
            }
        }
        else if (arrow == "LfTp") {
            bubble.style.left = spleft - 5 + spwidth + "px";
            bubble.style.top = sptop - 0 + "px";
            if (findIE6 != -1) {
                bubble.className = "sm_top_left_ie6"
            }
            else {
                bubble.className = "sm_top_left"
            }
        }
        else if (arrow == "RtTp") {
            bubble.style.left = spleft - 320 + "px";
            bubble.style.top = sptop + "px";
            if (findIE6 != -1) {
                bubble.className = "sm_top_right_ie6"
            }
            else {
                bubble.className = "sm_top_right"
            }
        }
        else if (arrow == "LfBt") {
            bubble.style.left = spleft + spwidth + "px";
            bubble.style.top = sptop - 125 + spheight + "px";
            if (findIE6 != -1) {
                bubble.className = "sm_bottom_left_ie6"
            }
            else {
                bubble.className = "sm_bottom_left"
            }
        }
        else if (arrow == "RtBt") {
            bubble.style.left = spleft - 320 + "px";
            bubble.style.top = sptop - 105 + spheight + "px";
            if (findIE6 != -1) {
                bubble.className = "sm_bottom_right_ie6"
            }
            else {
                bubble.className = "sm_bottom_right"
            }
        }
        else {
            something = document.getElementById('bubble');
            if (something) {
                something.parentNode.removeChild(something);
            }
            return;
        }

        bubble.setAttribute('id', 'bubble');
        var bubble_close = document.createElement('a');
        bubble_close.setAttribute('id', 'bubble_close');
        bubble_close.setAttribute('name', 'bubble');
        bubble_close.setAttribute('href', 'javascript: atlBubbleClose();');
        bubble_close.innerHTML = " Close ";

        var bubble_content = document.createElement('p');
        bubble_content.setAttribute('id', 'bubble_content');
        var bubble_nav = document.createElement('a');
        bubble_nav.setAttribute('id', 'bubble_nav');
        var bubble_inner = document.createElement('div');
        bubble_inner.setAttribute('id', 'bubble_inner');

        var present = document.getElementById('bubble');
        if (present) {
            present.parentNode.removeChild(present);
        }

        bubble_inner.appendChild(bubble_close);
        bubble_inner.appendChild(bubble_content);
        bubble_inner.appendChild(bubble_nav);
        bubble.appendChild(bubble_inner)
        document.body.appendChild(bubble);
        node2read = nodeValue;
        atlBubbleData();
    }
}

function atlBubbleClose() {
    var present = document.getElementById('bubble');
    if (present) {
        present.parentNode.removeChild(present);
    }
}

function atlBubbleData() {
    if (atl_pagecast_data.readyState == 4) {
        atlPagecastStateChanged();
    }
    else {
        atl_pagecast_data = atlGetXmlHttpObject();
        if (atl_pagecast_data == null) {
            return;
        }
        atl_pagecast_data.onreadystatechange = atlPagecastStateChanged;
        atl_pagecast_data.open("GET", atl_pagecast_file, true);
        atl_pagecast_data.send(null);
    }
}

function atlPagecastStateChanged() {
    if (atl_pagecast_data.readyState == 4) {
        var xmlDoc = atl_pagecast_data.responseXML.documentElement;
        document.getElementById("bubble_content").innerHTML = xmlDoc.getElementsByTagName("content")[node2read].childNodes[0].nodeValue;
        var vars = xmlDoc.getElementsByTagName("nextlink")[node2read].childNodes[0].nodeValue;
        var link = "javascript: atlBubble(" + vars + ")";
        var bubble_nav = document.getElementById("bubble_nav")
        bubble_nav.setAttribute("href", link);
        bubble_nav.innerHTML = "Next";
        if (xmlDoc.getElementsByTagName("close")[node2read].childNodes[0].nodeValue == "true") {
            bubble_nav.innerHTML = "Done";
        }
        if (xmlDoc.getElementsByTagName("start")[node2read].childNodes[0].nodeValue == "true") {
            bubble_nav.innerHTML = "Start Tour";
        }
    }
}

function atlScrollPage() {
    atl_scroll_time += atl_scroll_int2;
    if (atl_scroll_time < atl_scroll_dur) { window.scrollTo(0, atlEaseInOut(atl_scroll_time, atl_scroll_st, atl_scroll_dist, atl_scroll_dur)); }
    else { window.scrollTo(0, atl_scroll_st + atl_scroll_dist); clearInterval(atl_scroll_int); }
}

function atlEaseInOut(t, b, c, d) { return c / 2 * (1 - Math.cos(Math.PI * t / d)) + b; }

function atlGetXmlHttpObject() {
    var xmlHttp = null;
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    }
    catch (e) {
        // Internet Explorer
        try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); }
        catch (e) { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); }
    }
    return xmlHttp;
}

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;
}
