/* AmwayCommon.js */

function AttachToopTip(control, textHtml, showHand) {
    if (typeof control == 'undefined' || control == null || control == "") {
        return;
    }


    if (typeof control == "string") {
        var controlid = control;
        var control = document.getElementById(controlid);
        if (control.nodeName == 'OPTION' && document.all) {
            var title = textHtml.replace(/<br>/g, "\r\n");
            control.setAttribute("title", title);
            return;
        }

    }
    control.setAttribute("setpointer", showHand);
    control.onmouseover = function(e) {
        var showHandPointer = control.getAttribute("setpointer") == "true" || control.getAttribute("setpointer") == "True" ? true : false;
        if (showHandPointer)
            this.style.cursor = "pointer";
        else
            this.style.cursor = "default";

        var cursor = getPosition(e);
        var tempX = tempY = 0;
        //text cursor

        if (document.all) { // grab the x-y pos.s if browser is IE
            tempX = event.clientX + document.body.scrollLeft
            tempY = event.clientY + document.body.scrollTop
        } else {  // grab the x-y pos.s if browser is NS
            tempX = e.pageX
            tempY = e.pageY
        }

        tooltip_show(this, textHtml, cursor.x, cursor.y);
        //tooltip_show(this, textHtml, tempX, tempY);
    }
    control.onmouseout = tooltip_hide;
}

function tooltip_show(parent, textHtml, cx, cy) {

    var div = document.getElementById('tooltipdhtmlx');
    if (div == null || typeof div == 'undefined') {
        div = document.createElement('div');
        div.id = "tooltipdhtmlx";
        div.style.backgroundColor = 'lightyellow';
        div.style.color = 'black';
        div.style.paddingRight = "3px";
        div.style.paddingTop = "3px";
        div.style.paddingLeft = "3px";
        div.style.paddingBottom = "3px";
        div.style.borderLeft = "black 1px solid";
        div.style.borderRight = "black 1px solid";
        div.style.borderBottom = "black 1px solid";
        div.style.borderTop = "black 1px solid";
        div.style.position = 'absolute';
        document.body.appendChild(div);
    }
    div.style.zIndex = 9000;
    div.innerHTML = textHtml;
    x = cx + 5;
    y = cy + 15;
    div.style.top = y + 'px';
    div.style.left = x + 'px';
    div.style.display = 'block'
    window.setTimeout(tooltip_hide, 10000);
}
function tooltip_hide() {

    //this.style.cursor = "pointer";
    var div = document.getElementById('tooltipdhtmlx');
    if (div == null || typeof div == 'undefined')
        return;
    div.style.zIndex = 1;
    div.style.display = 'none';
}
//Gets the postion
function getPosition(e) {
    e = e || window.event;
    var cursor = { x: 0, y: 0 };
    if (e.pageX || e.pageY) {
        cursor.x = e.pageX;
        cursor.y = e.pageY;
    }
    else {
        var de = document.documentElement;
        var b = document.body;
        cursor.x = e.clientX +
            (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
        cursor.y = e.clientY +
            (de.scrollTop || b.scrollTop) - (de.clientTop || 0);
    }
    return cursor;
}

//To apply Css 
function ApplyCss(cssPath) {

    if (cssPath != null && cssPath != 'undefined') {
        cssPath = cssPath + '.css';
        var head = document.getElementsByTagName("head")[0];
        var cssNode = document.getElementById("themecss");
        if (cssNode == null || cssNode == 'undefined') {
            cssNode = document.createElement('link');
            cssNode.type = 'text/css';
            cssNode.rel = 'stylesheet';
            cssNode.href = cssPath;
            cssNode.media = 'screen';
            cssNode.id = 'themecss';
            head.appendChild(cssNode);
        }
        else {
            cssNode.href = cssPath;
        }
    }
}

// Added to support ajax analytics and common ajax error handling
function ajaxResponseBegin(divErrorId, response) {
    if (response.error != null) {
        showSpinnerError(divAjaxResponse, response.error.Message);
    }
}

function ajaxResponseEnd(response) {
    if (response.analytics != null && response.analytics.trim().length > 0) {
        // TODO try/catch here
        eval(response.analytics);
    }
}

function ajaxResponseValue(response) {
    if (response.value != null && response.value.trim().length > 0) {
        return response.value;
    }
    else return "";
}

// This method is used to modify links within a Telerik popup (iframe) so that they don't change the
// current frame's URL, but instead change the parent page's URL.  There is some trickery of moving
// the href value around due to the way IE will display the value on the page if you simply try
// moving the page through top.document.location.
function OnLoadModifyPopupLinks() {
	$("a[href^='/']").each(function() {
		$(this).click(function() {
			GetAmwayRootWindow().document.location = $(this).attr('navhref');
		});
		$(this).attr('navhref', $(this).attr('href'));
		$(this).attr('href', 'javascript:void(null);');
	});
}