//	GLOBAL VARIABLES
var popWin;
var popWinType;

var ie;
ie = (document.all && !window.opera) ? true : false;

function ieVersion()
{
    var version = 999;
    if (navigator.appVersion.indexOf("MSIE") != -1)
    {
        version = parseFloat(navigator.appVersion.split("MSIE")[1]);
    }
    return version;
}

//Create a printer friendly pop up window
function printerFriend(hrefTarget) {
    var name, width, height;
    name = "printerVersion";
    width = "703";
    height = "560";
    popWindow(hrefTarget, name, width, height);
}
//Create a new browser window
function newWindow(hrefTarget) {
    window.open(hrefTarget)
}

/* Opens a window and positions it on the center of screen */
function popWindow(pageURL, title, width, height) {
    var left = (screen.width / 2) - (width / 2);
    var top = (screen.height / 2) - (height / 2);
    var targetWin = window.open(pageURL, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no, width=' + width + ', height=' + height + ', top=' + top + ', left=' + left);
}

function PopupClose() {
    window.close();
}

function Trim(str) {
    while (str.charAt(0) == " ") str = str.substring(1);
    while (str.charAt(str.length - 1) == " ") str = str.substring(0, str.length - 1);
    return str;
}

function IsTypedCharNumeric() {
    var ValidChars = "0123456789., -";
    var IsNumber = true;
    var charTyped = window.event.keyCode;
    var IsValidChar = (ValidChars.indexOf(String.fromCharCode(charTyped)) >= 0);

    return (IsValidChar || charTyped == 8 || charTyped == 13 || charTyped == 9)    
}


function LoadMainContent(url, height, pagetitle) {
    var spanTitle = document.getElementById('spanPageSubTitle');
    if (spanTitle != null)
        spanTitle.innerHTML = pagetitle;
    var newLocation = window.location.protocol + '//' + window.location.host +
                        "/uStore/CustomMain.aspx?url=" + url
                        + ((height != null) ? "&height=" + height : "")
                        + ((pagetitle != null) ? "&pagetitle=" + pagetitle : "");
    window.location.href = newLocation;
}

//Dynamic DUKES
function UpdateDialValue(val, txtDialValueClientId) {
    document.getElementById(txtDialValueClientId).value = val;
}

//Encode text to HTML
function escapeHTML(str) {
    var div = document.createElement('div');
    var text = document.createTextNode(str);
    div.appendChild(text);
    return div.innerHTML;
}

/* Closes a rad window */
function CloseWindow() {
    if (GetRadWindow() == null) {
        window.close();
    } else {
        GetRadWindow().Close();
    }

    return false;
}

/* Get the Rad window object */
function GetRadWindow() {
    if (window.radWindow == null && window.frameElement == null)
        return null;

    var oWindow = null;
    if (window.radWindow) oWindow = window.radWindow;
    else
        if (window.frameElement.radWindow)
        oWindow = window.frameElement.radWindow;

    return oWindow;
}

function RedirectPopupToParent() {
    if (GetRadWindow() != null)
        GetRadWindow().BrowserWindow.location.href = window.location.href;
}

/* HTML controls visibility */

function ToggleControlDisplay(controlId) {
    var control = document.getElementById(controlId);
    if (control.style.display != undefined) { //display is supported
        if (control.style.display != 'none') //hide
            control.style.display = 'none';
        else //show
            control.style.display = 'block';
    } else { //display is not supported
        if (control.style.visibility != 'visible') //show
            control.style.visibility = 'visible';
        else //hide
            control.style.visibility = 'collapse';
    }
}

function SetControlDisplay(controlId, visible) {
    var control = document.getElementById(controlId);
    if (control.style.display != undefined) { //display is supported
        if (visible) //show
            control.style.display = 'block';
        else //hide
            control.style.display = 'none';
    } else { //display is not supported
        if (visible) //show
            control.style.visibility = 'visible';
        else //hide
            control.style.visibility = 'collapse';
    }
}

function GetElementPosition(el) {
    var parent = null;
    var pos = { x: 0, y: 0 };
    var box;
    if (el.getBoundingClientRect) {
        // IE   
        box = el.getBoundingClientRect();
        var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
        var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft;

        pos.x = box.left + scrollLeft - 2;
        pos.y = box.top + scrollTop - 2;

        return pos;
    }
    else if (document.getBoxObjectFor) {
        // gecko   
        box = document.getBoxObjectFor(el);
        pos.x = box.x - 2;
        pos.y = box.y - 2;
    }
    else {
        // safari/opera   
        pos.x = el.offsetLeft;
        pos.y = el.offsetTop;
        parent = el.offsetParent;
        if (parent != el) {
            while (parent) {
                pos.x += parent.offsetLeft;
                pos.y += parent.offsetTop;
                parent = parent.offsetParent;
            }
        }
    }

    if (window.opera) {
        parent = el.offsetParent;
        alert("opera");
        while (parent && parent.tagName != 'BODY' && parent.tagName != 'HTML') {
            pos.x -= parent.scrollLeft;
            pos.y -= parent.scrollTop;
            parent = parent.offsetParent;
        }
    }
    else {
        alert("else");
        parent = el.parentNode;
        while (parent && parent.tagName != 'BODY' && parent.tagName != 'HTML') {
            pos.x -= parent.scrollLeft;
            pos.y -= parent.scrollTop;

            parent = parent.parentNode;
        }
    }
    return pos;
}

function addElementClickFunction(id) {
    var b = document.getElementById(id);
    if (b && typeof (b.click) == 'undefined') b.click = function() {
        var result = true; if (b.onclick) result = b.onclick();
        if (typeof (result) == 'undefined' || result) { eval(b.getAttribute('href')); }
    }
}

function ellipseText(spanID, width) {
	var span = document.getElementById(spanID);
	if (span.offsetWidth > width) {
		var i = 1;
		var text = htmlDecode(span.innerHTML);
		span.innerHTML = '';
		while (span.offsetWidth < (width) && i < text.length) {
			span.innerHTML = htmlEncode(text.substr(0, i)) + '...';
      		i++;
		}
	}
}

function htmlEncode(value) {
	return $('<div/>').text(value).html();
}

function htmlDecode(value) {
	return $('<div/>').html(value).text();
} 


