﻿

/* These funtions were taken out of AppExt.js in the beta and put in this shared file
 * has tooltip related functions
 */

function enableTooltips(id) {
    var links, i, h;
    if (!document.getElementById || !document.getElementsByTagName) return;
    //AddCss();
    h = document.createElement("span");
    h.id = "btc";
    h.setAttribute("id", "btc");
    h.style.position = "absolute";
    document.getElementsByTagName("body")[0].appendChild(h);
    if (id == null) links = document.getElementsByTagName("a");
    else links = document.getElementById(id).getElementsByTagName("a");
    for (i = 0; i < links.length; i++) {
        //        Prepare(links[i]);
    }
}

function Prepare(el) {
    var tooltip, t, b, s, l;
    t = el.getAttribute("title");
    if (t == null || t.length == 0) t = "Coming Soon!";
    el.removeAttribute("title");
    tooltip = CreateEl("span", "tooltip");
    s = CreateEl("span", "top");
    s.appendChild(document.createTextNode(t));
    tooltip.appendChild(s);
    b = CreateEl("b", "bottom");
    l = 'Recently Earned Points:';
    //if(l.length>28) l=l.substr(0,25)+"...";
    b.appendChild(document.createTextNode(l));
    tooltip.appendChild(b);
    
    setOpacity(tooltip);
    el.tooltip = tooltip;
    el.onmouseover = showTooltip;
    el.onmouseout = hideTooltip;
    el.onmousemove = Locate;
}

function showTooltip(e) {
    document.getElementById("btc").appendChild(this.tooltip);
    Locate(e);
}

function hideTooltip(e) {
    var d = document.getElementById("btc");
    if (d.childNodes.length > 0) d.removeChild(d.firstChild);
}

function setOpacity(el) {
    el.style.filter = "alpha(opacity:95)";
    el.style.KHTMLOpacity = "0.95";
    el.style.MozOpacity = "0.95";
    el.style.opacity = "0.95";
}

function CreateEl(t, c) {
    var x = document.createElement(t);
    x.className = c;
    x.style.display = "block";
    return (x);
}

//function AddCss() {
//    var l = CreateEl("link");
//    l.setAttribute("type", "text/css");
//    l.setAttribute("rel", "stylesheet");
//    l.setAttribute("href", "bt.css");
//    l.setAttribute("media", "screen");
//    document.getElementsByTagName("head")[0].appendChild(l);
//}

function Locate(e) {
    var posx = 0, posy = 0;
    if (e == null) e = window.event;
    if (e.pageX || e.pageY) {
        posx = e.pageX; posy = e.pageY;
    }
    else if (e.clientX || e.clientY) {
        if (document.documentElement.scrollTop) {
            posx = e.clientX + document.documentElement.scrollLeft;
            posy = e.clientY + document.documentElement.scrollTop;
        }
        else {
            posx = e.clientX + document.body.scrollLeft;
            posy = e.clientY + document.body.scrollTop;
        }
    }
    //document.getElementById("btc").style.width = 400 + "px";
    document.getElementById("btc").style.top = (posy + 10) + "px";
    document.getElementById("btc").style.left = (posx - 150) + "px";
}

/***************************************************************************/


function showHelp(id, title) {
    var url;


    var window = new Ext.Window({
        title: title + ' Help',
        width: 600,
        height: 500,
        minWidth: 600,
        minHeight: 500,
        layout: 'fit',
        plain: true,
        bodyStyle: 'padding:5px;',
        buttonAlign: 'center',
        autoScroll: true,
        items: [{
            bodyStyle: 'padding-left:5px; overflow:scroll;',
            autoLoad: {
                url: '/Settings/GetExternalSite?id=' + id
            }
}],

            buttons: [{
                text: 'Do not show again',
                handler: function() {
                    window.hide();
                    //Set to Viewed.
                    Ext.Ajax.request({
                        method: 'POST',
                        headers: { 'Return-Form': 'text/json' },
                        url: '/Settings/SetSetting',
                        params: { Key: 'Help' + id, Value: 'viewed' }
                    });
                } 
            },
            {
                text: 'Close',
                handler: function() {
                    window.hide();

                }
}]
            });

            window.show(Ext.get(title + id));
        }

        function getID(id) {
            var viewed;
            Ext.Ajax.request({
                async: false,
                method: 'POST',
                headers: { 'Return-Form': 'text/json' },
                url: '/Settings/GetSetting',
                params: { Key: 'Help' + id, DefaultValue: 'none' },
                callback: function(options, success, response) {
                    if (success) {
                        var jsonData = Ext.util.JSON.decode(response.responseText);
                        viewed = jsonData.ValueX;
                    }
                }
            });
            return viewed == 'viewed';
        }


function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest(); //Not IE
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP"); //IE
	} else {
		//Display your error message here. 
		//and inform the user they might want to upgrade
		//their browser.
		alert("Your browser doesn't support the XmlHttpRequest object.  Better upgrade to Firefox.");
	}
}

var receiveReq = getXmlHttpRequestObject();

function handleWeightChange() {
	//Check to see if the XmlHttpRequests state is finished.
    if (receiveReq.readyState == 4) {
        if (receiveReq.responseText.length < 5) {
            //Set the contents of our span element to the result of the asyncronous call.
            document.getElementById('UserWeightField').innerHTML = receiveReq.responseText;
        }
	}
}

//Initiate the asyncronous request.
function changeUserWeight() {
    var reply = prompt("What is your new weight?", "")
    //if (typeof reply == "number")
    if (reply!='')
    {
	    //If our XmlHttpRequest object is not in the middle of a request, start the new asyncronous call.
	    if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
		    //Setup the connection as a GET call to SayHello.html.
		    //True explicity sets the request to asyncronous (default).
		    receiveReq.open("POST", '/Goals/ChangeWeightJSON?value=' + reply, true);
		    //Set the function that will be called when the XmlHttpRequest objects state changes.
		    receiveReq.onreadystatechange = handleWeightChange; 
		    //Make the actual request.
		    receiveReq.send(null);
	    }			
	}
}
