
// A function for determining how far horizontally the browser is scrolled
function scrollX() 
{
    var de = document.documentElement; //  Internet Explorer 6 in Strict Mode
    return self.pageXOffset || ( de && de.scrollLeft ) || document.body.scrollLeft;
}
    
// A function for determining how far vertically the browser is scrolled
function scrollY() 
{    
    var de = document.documentElement; //  Internet Explorer 6 in Strict Mode
    return self.pageYOffset || ( de && de.scrollTop ) || document.body.scrollTop;     
}

// Find the height of the viewport
function windowHeight() 
{
    var de = document.documentElement;
    return self.innerHeight || ( de && de.clientHeight ) || document.body.clientHeight;
}

// Find the width of the viewport
function windowWidth() 
{
    var de = document.documentElement;
    return self.innerWidth || ( de && de.clientWidth ) || document.body.clientWidth;  
}

// Get the actual height (using the computed CSS) of an element
function getHeight( elem ) {
    // Gets the computed CSS value and parses out a usable number
    var height = getStyle( elem, "height" );
    
    if (height == "auto")
    {
        height = elem.clientHeight;
    }
    return parseInt( height );
}

// Get the actual width (using the computed CSS) of an element
function getWidth( elem ) {
    // Gets the computed CSS value and parses out a usable number
    var width = getStyle( elem, "width" );
    
    if (width == "auto")
    {
        width = elem.clientWidth;
    }
    
    return parseInt( width );
}

// A function for setting the horizontal position of an element
function setX(elem, pos) 
{
    // Set the left CSS property, using pixel units
    elem.style.left = pos + "px";
}

// A function for setting the vertical position of an element
function setY(elem, pos) 
{
    // Set the left CSS property, using pixel units
    elem.style.top = pos + "px";
}

// Get a style property (name) of a specific element (elem)
function getStyle( elem, name ) 
{
    // If the property exists in style[], then its been set recently (and is current)
    if (elem.style[name])
        return elem.style[name];

    // Otherwise, try to use IE's method
    else if (elem.currentStyle)
        return elem.currentStyle[name];

    // Or the W3Cs method, if it exists
    else if (document.defaultView && document.defaultView.getComputedStyle) {
        // It uses the traditional Ôtext-alignÕ style of rule writing, instead of textAlign
        name = name.replace(/([A-Z])/g,"-$1");
        name = name.toLowerCase();

        // Get the style object and get the value of the property (if it exists)
        var s = document.defaultView.getComputedStyle(elem,"");
        return s && s.getPropertyValue(name);

    // Otherwise, we're using some other browser
    } else
        return null;
}

// Set an opacity level for an element
// (where level is a number 0-100)
function setOpacity(elem, level ) 
{
    // If filters exist, then this is IE, so set the Alpha filter
    if (elem.filters )
    {   
  /*
    // commented this out because IE is not showing transparency
    // animated fade in and fade out is lost when this is removed
        var overlay = document.getElementById('overlay');
        overlay.filters.alpha.opacity = level;
    }
    else
    {
*/
        // Otherwise use the W3C opacity property - this probably doesn't work in IE 6
        elem.style.opacity = level / 100;
    }
}


function hide( elem ) 
{
    elem.className = "hide";
}

function show( elem ) 
{
    //elem.style.display = "block";
    elem.className = "show";
}

// Returns the height of the web page
// (could change if new content is added to the page)
function pageHeight() 
{
    return document.body.scrollHeight;
}

// Returns the width of the web page
function pageWidth() 
{
    return document.body.scrollWidth;
}
function fadeIn( elem, to, speed ) 
{
    setOpacity( elem, 0 ); // Starting opacity
    show( elem );

    // 20 frame animation  takes place over one second
    for ( var i = 0; i <= 100; i += 5 ) 
    {
        (function()
        { 
        		var opacity = i;
        		
            // Set the timeout to occur at the specified time in the future
            setTimeout(function()
            {
                // Set the new opacity of the element
                setOpacity( elem, ( opacity / 100 ) * to );

            }, ( i + 1 ) * speed );
        })();
    }
}

function fadeOut( elem, to, speed ) 
{
    // skip the fancy stuff
    hide( elem );
    return;
    
    // Start the opacity at 1
    setOpacity( elem, 1 );

    // 20 frame animation that takes place over one second
    for ( var i = 0; i < 100; i += 5 ) 
    {
        // A closure to make sure that we have the right 
        (function(){
        		var opacity = i;
        		
            // Set the timeout to occur at the specified time in the future
            setTimeout(function(){

                // Set the new opacity of the element
                setOpacity( elem, 100 - opacity );
                
                if ( opacity == 95 )
                    hide( elem );

            }, ( i + 1 ) * speed );
        })();
    }
}

function id( name ) {
    return document.getElementById( name );
}

function tag( name, root ) {
    return ( root || document ).getElementsByTagName( name );
}

function byClass(name,type) {
    var r = [];
    // Locate the class name (allows for multiple class names)
    var re = new RegExp("(^|\\s)" + name + "(\\s|$)");

    // Limit search by type, or look through all elements
    var e = document.getElementsByTagName(type || "*");
    for ( var j = 0; j < e.length; j++ )
        // If the element has the class, add it for return
        if ( re.test(e[j].className) ) r.push( e[j] );

    // Return the list of matched elements
    return r;
}

function next( elem ) {
    do {
        elem = elem.nextSibling;
    } while ( elem && elem.nodeType != 1 );
    return elem;
}

function prev( elem ) {
    do {
        elem = elem.previousSibling;
    } while ( elem && elem.nodeType != 1 );
    return elem;
}

function findPos(obj)
{
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        do {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        }
        while (obj = obj.offsetParent);
    }
    return [curleft, curtop];
}

function showBadgePopup( obj, badgeId, badgeDate, lpId )
{
    showBadgePopupInternal( obj, document.getElementById('badgeOverlay'), 210, 209, badgeId, badgeDate, lpId);
}

function showBadgePopupR( obj, badgeId, badgeDate, lpId )
{
    showBadgePopupInternal( obj, document.getElementById('badgeOverlayR'), 45, 209, badgeId, badgeDate, lpId);
}

function showBadgePopupInternal( obj, badgeDiv, offsetX, offsetY, badgeId, badgeDate, lpId ) {
    hideBadgePopup();
    var parentPos = findPos(obj);
    var mouseoverArea = document.getElementById('mouseoverArea');
    var areaWidth = getWidth(obj);
    var areaHeight = getHeight(obj);
    
    mouseoverArea.style['left'] = (parentPos[0])+'px'; // +'px' is for firefox compatibility
    mouseoverArea.style['top'] = (parentPos[1])+'px';
    mouseoverArea.style['width'] = areaWidth+'px';
    mouseoverArea.style['height'] = areaHeight+'px';
    mouseoverArea.removeAttribute("className");
    mouseoverArea.removeAttribute("class");
    badgeDiv.style['left'] = (parentPos[0]-offsetX+(areaWidth/2))+'px';
    badgeDiv.style['top'] = (parentPos[1]-offsetY+(areaHeight/2))+'px';

    if (obj.badgeHtml == undefined) {

        var req;

        var url = "/sitepages/ExpertZone/services/BadgePopup.ashx?badgeId=" + badgeId + "&badgeDate=" + badgeDate + "&learningplandetailId=" + lpId;

        if (typeof XMLHttpRequest != "undefined") {
            req = new XMLHttpRequest();
        }
        else if (window.ActiveXObject) {
            req = new ActiveXObject("Microsoft.XMLHTTP");
        }
        req.open("GET", url, true);
        req.onreadystatechange =
        function() {
            if (req.readyState == 4) {
                if (req.status == 200) {
                    var html = req.responseText;
                    badgeDiv.innerHTML = html;
                    badgeDiv.removeAttribute("className");
                    badgeDiv.removeAttribute("class");
                    obj.badgeHtml = html;
                }
            }
        };
        req.send(null);
    }
    else {
        badgeDiv.innerHTML = obj.badgeHtml;
        badgeDiv.removeAttribute("className");
        badgeDiv.removeAttribute("class");
    }

    badgeDiv.onmouseover = function() {
        hideBadgePopup();
    }
    badgeDiv.onmouseout = function() {
        hideBadgePopup();
    }
}

function hideBadgePopup() {
    var mouseoverArea = document.getElementById('mouseoverArea');
    mouseoverArea.className = 'hide';
    var badgeDiv = document.getElementById('badgeOverlay');
    badgeDiv.className = 'hide';
    var badgeDivR = document.getElementById('badgeOverlayR');
    badgeDivR.className = 'hide';
    
}

var orderDetails = {
    trigger: false,
    overlayDiv: false,
    show: function(obj) {
        if (obj.initialized) {
            this.trigger = obj;
            this.placeDetails(obj.details);
        }
        else {
            var dims = findPos(obj);
            obj.curTop = dims[1];
            this.overlayDiv = document.getElementById("orderDetailsOverlay");
            this.trigger = obj;
            this.getDetails(obj.href);
            obj.initialized = true;
        }
    },
    getDetails: function(url) {
        var parentThis = this;
        var req;
        if (typeof XMLHttpRequest != "undefined") {
            req = new XMLHttpRequest();
        } else if (window.ActiveXObject) {
            req = new ActiveXObject("Microsoft.XMLHTTP");
        }
        req.open("GET", url, true);
        req.onreadystatechange =
                    function() {
                        if (req.readyState == 4) {
                            if (req.status == 200) {
                                parentThis.placeDetails(req.responseText);
                                parentThis.trigger.details = req.responseText;
                            }
                        }
                    };
        req.send(null);
    },
    placeDetails: function(details) {
        this.overlayDiv.innerHTML = details;
        this.overlayDiv.removeAttribute("class");
        this.overlayDiv.removeAttribute("className");
        this.overlayDiv.style.top = this.trigger.curTop + "px";
    },
    hide: function() {
        var overlay = document.getElementById('orderDetailsOverlay');
        if (overlay != null) {
           overlay.className = 'hide';
        }
    }
}

/*
    Company Name Auto complete function -- BEGIN
*/

var Autocomplete = {
	initialized: false,
	textbox: false,
	dropdown: false,
	timer: null,
	nextField: "unset",
	countryId: null,
	register: function(obj, event, countryId) {
		if (!this.initialized) {
			this.textbox = obj;
			this.countryId = countryId;
			this.makeDropdown();
			this.initialized = true;
			this.getSuggestions();
		}
		else if (event.keyCode == 40) { //Down Arrow key code
			this.dropdown.focus();
			this.dropdown[0].selected = true;
			this.updateTextbox();
		}
		else {
			this.getSuggestions();
		}
	},
	updateTextbox: function() {
		this.textbox.value = this.dropdown.value;
	},
	hideDropdown: function() {
		this.dropdown.style.display = "none";
		this.dropdown.innerHTML = "";
	},
	makeDropdown: function() {
		if (document.getElementById("autocompleteSelect") == null) {
			var select = document.createElement("select");
			select.id = "autocompleteSelect";
			select.className = "selectCompanyName";
			select.size = 5;
			this.textbox.parentNode.insertBefore(select, this.textbox.nextSibling);
			this.dropdown = select;

			var parentThis = this;
			this.textbox.onblur = function() {
				parentThis.timer = setTimeout(function() {
					parentThis.hideDropdown();
				}, 200);
			}
			this.dropdown.onkeyup = function(event) {
				var evt = event || window.event;
				if (evt.keyCode == 13) { //Enter key code
					parentThis.hideDropdown();
					parentThis.moveToNextField();
				}
			}
			this.dropdown.onfocus = function() {
				clearTimeout(parentThis.timer);
			}
			this.dropdown.onblur = function() {
				parentThis.hideDropdown();
			}
			this.dropdown.onchange = function() {
				parentThis.updateTextbox();
			}
			this.dropdown.onclick = function() {
				parentThis.hideDropdown();
				parentThis.moveToNextField();
			}
		}
	},
	getSuggestions: function() {
		var textValue = this.textbox.value;
		if (textValue.length > 0) {
			var req;
			var url = "/sitepages/services/CompanyListing.ashx?companySearchString=" + textValue;

			if (this.countryId) {
				url += '&countryId=' + this.countryId;
			}

			if (window.XMLHttpRequest) {
				req = new XMLHttpRequest();
			}
			else if (window.ActiveXObject) {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			}
			//req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			req.open("GET", url, true);
			var parentThis = this;
			req.onreadystatechange =
            function() {
            	if (req.readyState == 4) {
            		if (req.status == 200) {
            			var companiesString = req.responseText;
            			parentThis.popDropdown(companiesString);
            		}
            	}
            };
			req.send(null);
		}
		else {
			this.hideDropdown();
		}
	},
	popDropdown: function(companiesString) {
		var companiesList = companiesString.split(";");
		if (companiesList.length > 1) {
			this.dropdown.innerHTML = "";
			var parentThis = this;
			for (var i = 0; i < companiesList.length; i++) {
				var company = companiesList[i];
				if (company != "") {
					var opt = document.createElement("option");
					opt.value = company;
					opt.innerHTML = company;
					parentThis.dropdown.appendChild(opt);
				}
			}
			this.dropdown.removeAttribute("style");
		}
		else {
			this.hideDropdown();
		}
	},
	moveToNextField: function() {
		if (this.nextField == "unset") {
			var form = this.textbox.form;
			var formFields = form.elements;
			var parentThis = this;
			for (var i = 0; i < formFields.length; i++) {
				if (formFields[i] == parentThis.textbox) {
					parentThis.nextField = formFields[i + 2]; //+1 is the hidden select
					break;
				}
			}
		}
		if (this.nextField) {
			this.nextField.focus();
		}
	}
}
/*
Company Name Auto complete function -- END
*/


function acceptOnlyNumbers(evt, textbox) {
    var evt = (evt) ? evt : ((event) ? event : null);
    if (textbox == null) return;

    if ((evt.keyCode == 48) || // 0
                (evt.keyCode == 49) || // 1
                (evt.keyCode == 50) || // 2
                (evt.keyCode == 51) || // 3
                (evt.keyCode == 52) || // 4
                (evt.keyCode == 53) || // 5
                (evt.keyCode == 54) || // 6
                (evt.keyCode == 55) || // 7
                (evt.keyCode == 56) || // 8
                (evt.keyCode == 57) || // 9
                (evt.keyCode == 96) || // numpad 0
                (evt.keyCode == 97) || // numpad 1
                (evt.keyCode == 98) || // numpad 2
                (evt.keyCode == 99) || // numpad 3
                (evt.keyCode == 100) || // numpad 4
                (evt.keyCode == 101) || // numpad 5
                (evt.keyCode == 102) || // numpad 6
                (evt.keyCode == 103) || // numpad 7
                (evt.keyCode == 104) || // numpad 8
                (evt.keyCode == 105) ||  // numpad 9
                (evt.keyCode == 8) ||  // DELETE
                (evt.keyCode == 9))  // TAB
    { return true; }

    // bad char found
    return false;

}