﻿var IsIEv6orBefore = false;

if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ // capture x.x portion of user agent string
	//Rounds up to nearest integer and tests
	if( Number(RegExp.$1) < 7 ) {IsIEv6orBefore = true;}
}
String.prototype.trim = function () {
    return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}
function isShimAlreadyPresent(element){
    //Quick check:
    if( element.previousSibling != null )
        return (element.previousSibling.nodeName == 'IFRAME');

    //More detailed check:
    if( element.parentElement != null )
    {
        var found = false;
        var parent = element.parentElement;
        for(var i=0; i < parent.childNodes.length; i++){
            if( parent.childNodes[i].nodeName == 'IFRAME' ) found = true;
        }
        return found;
    }
}

function placeShimBehindElem(element){
	if( IsIEv6orBefore && !isShimAlreadyPresent(element) ){
	    
	    //Now add the shim (to cover the select elements)
		var html = '<iframe id="shimElem" src="about:blank" scrolling="no" frameborder="0" tabindex="-1" style="z-index:-1;position:absolute;display:block;filter: progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0);"></iframe>';

		var shim = document.createElement(html);

		element.parentElement.style['position'] = "relative";
		
		var lrBdrWidth = parseInt(getStyle(element, 'border-left-width')) + parseInt(getStyle(element, 'border-right-width'));
		var tbBdrWidth = parseInt(getStyle(element, 'border-top-width')) + parseInt(getStyle(element, 'border-bottom-width'));
		
		if( isNaN(lrBdrWidth) ) lrBdrWidth = 0;
		if( isNaN(tbBdrWidth) ) tbBdrWidth = 0;

		//Set the size/pos to be the same as the UL
		shim.style['top'] = element.offsetTop + "px";
		shim.style['left'] = element.offsetLeft + "px";
		shim.style['height'] = (element.offsetHeight + tbBdrWidth) + "px";
		shim.style['width'] = (element.offsetWidth + lrBdrWidth) + "px";

		if( element !== null && element.parentElement !== null){
			element.parentElement.insertBefore(shim, element);}
	}
}
function removeShimFromBehindElement(element){
	if( IsIEv6orBefore ){
		element.parentElement.style['position'] = "";
		//Remove the shim element
		var shim = document.getElementById('shimElem');
		if(shim !== null){
			try {element.parentElement.removeChild(shim);}
			catch(e) {shim.parentElement.removeChild(shim);}
		}
	}
}
function setMouseHoverClass(listOfElements, hoverClassName){
	for (var i=0; i<listOfElements.length; i++) {
		listOfElements[i].onmouseover=function() {
			this.className+=" " + hoverClassName;
		};
		listOfElements[i].onmouseout=function() {
			this.className = this.className.replace(new RegExp(hoverClassName+"\\b"), "");
			this.className = this.className.replace(/\s*/g, "");
		};
	}
}
function checkMouseLeave2(e,el)
{
	var rel = (window.event) ? e.toElement : e.relatedTarget;
	return !(containsDOM(el,rel));
}
// returns true if containee is inside container, false otherwise
function containsDOM (container, containee) {
	do {
		if ((isParent = container == containee))
			break;
		try {containee = containee.parentNode;}
		catch (e) {containee = null} 			
	}
	while (containee != null);
	return isParent;
}
// returns true if mouse moved from outside of element onto element, false otherwise
function checkMouseEnter (evt, element) {
	if (element.contains && evt.fromElement) {
		return !element.contains(evt.fromElement);
	}else if (evt.relatedTarget) {
		return !containsDOM(element, evt.relatedTarget);
	}
}
// returns true if mouse moved from insde of element to outside of element, false otherwise
function checkMouseLeave (evt, element) {
	if (element.contains && evt.toElement) {
		return !element.contains(evt.toElement);
	}
	else if (evt.relatedTarget) {
		return !containsDOM(element, evt.relatedTarget);
	}
}
//Browser safe next sibling
function bsNextSibling( node ){
	var nS = node.nextSibling;
	while (nS.nodeType != 1) {
		nS = nS.nextSibling;
	}
	return nS;
}
function removeCssClass(node, cssClass){
	if( node != null ){
		node.className = node.className.replace(new RegExp(cssClass + "\\b"), "");
		node.className = node.className.trim();
		if(node.className === "") {node.className = null;}
	}
}
function addCssClass(node, cssClass){
	if( node != null ){
		//Only add if the tag is not present in the CssClass name
		if (node.className.indexOf(cssClass) == -1 ){
			if( node.className.length > 0 ){
				node.className += " " + cssClass;
			}else{
				node.className = cssClass;
			}
		}
	}
}
//Returns the computed style of an emelemnt rather than just the inline style (which is the case if the style property of the object is queried)
function getStyle(oElm, strCssRule){
	var strValue = "";
	//All browsers (except IE)
	if(document.defaultView && document.defaultView.getComputedStyle){
		strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
	}
	else if(oElm.currentStyle){ //IE
		strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
			return p1.toUpperCase();
		});
		strValue = oElm.currentStyle[strCssRule];
	}
	return strValue;
}
//Gets all the 'tag' elements under the specified 'node' by css class name 'searchClass'
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node === null ){
		node = document;
	}
	if ( tag === null ){
		tag = '*';
	}
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}
//Pass a url search string key name, and receive the value of that parameter from the document location
//Same as function below, but is quicker as it doesnt have to parse the document.location url
function getSearchStringValue(key){
	if( document.location.search != ''){
		var kvp = document.location.search.substr(1).split('&');
		var i=kvp.length; 
		var x; 
		while(i--){
			x = kvp[i].split('=');
			if (x[0]==key){
				return unescape(x[1]);
			}
		}
    }
    return '';
}
//Pass a url search string key name, and receive the value of that parameter from the url taht you supply
function getUrlSearchStringValue(url, key){
    key = escape(key);
    var Url = parseURL(url);
    if( Url.search != ''){
		var kvp = Url.search.substr(1).split('&');
		var i=kvp.length; 
		var x; 
		while(i--){
			x = kvp[i].split('=');
			if (x[0]==key){
				return unescape(x[1]);
			}
		}
    }
    return '';
}
//Inserts a key/value pair into the url search string
//If the key/value pair is already in the string it is updated with the new value
function insertUrlSearchTerm(url, key, value)
{
    key = escape(key); value = escape(value);
    var Url = parseURL(url);
    var kvp = Url.search.substr(1).split('&');
    var i=kvp.length; 
    var x; 
    while(i--){
        x = kvp[i].split('=');
        if (x[0] == key){
            x[1] = value;
            kvp[i] = x.join('=');
            break;
        }
    }
    if(i<0) {//add a new one
		if( kvp[kvp.length-1]==='' ){
			kvp[kvp.length-1] = [key,value].join('=');
		}else{
			kvp[kvp.length] = [key,value].join('=');
		}
    }
    return Url.protocol + '//' + Url.host + Url.pathname + '?' + kvp.join('&');
}
//Removes a key/value pair fomr a url if it is there
function removeUrlSearchTerm(url, key){
    key = escape(key);
    var Url = parseURL(url);
    var kvp = Url.search.substr(1).split('&');
    var i=kvp.length; var x; 
    while(i--){
        x = kvp[i].split('=');
        if (x[0]==key) {
			//Remove the offending key/value
			kvp.splice(i,1);
            break;
        }
    }
	if( kvp.length == 0 ){
		return Url.protocol + '//' + Url.host + Url.pathname; 
	}else{
		if( kvp[kvp.length-1] === '' ){
			return Url.protocol + '//' + Url.host + Url.pathname; 
		}else{
			return Url.protocol + '//' + Url.host + Url.pathname + '?' + kvp.join('&'); 
		}
	}
}
//parse a URL to form an object of properties
//Similar to the document.location property of the page
function parseURL(url){ 
    //save the unmodified url to href property 
    //so that the object we get back contains 
    //all the same properties as the built-in location object 
    var loc = { 'href' : url }; 
 
    //split the URL by single-slashes to get the component parts 
    var parts = url.replace('//', '/').split('/'); 
 
    //store the protocol and host 
    loc.protocol = parts[0]; 
    loc.host = parts[1]; 
 
    //extract any port number from the host 
    //from which we derive the port and hostname 
    parts[1] = parts[1].split(':'); 
    loc.hostname = parts[1][0]; 
    loc.port = parts[1].length > 1 ? parts[1][1] : ''; 
 
    //splice and join the remainder to get the pathname 
    parts.splice(0, 2); 
    loc.pathname = '/' + parts.join('/'); 
 
    //extract any hash and remove from the pathname 
    loc.pathname = loc.pathname.split('#'); 
    loc.hash = loc.pathname.length > 1 ? '#' + loc.pathname[1] : ''; 
    loc.pathname = loc.pathname[0]; 
 
    //extract any search query and remove from the pathname 
    loc.pathname = loc.pathname.split('?'); 
    loc.search = loc.pathname.length > 1 ? '?' + loc.pathname[1] : ''; 
    loc.pathname = loc.pathname[0]; 
 
    //return the final object 
    return loc; 
}
// This is the callback function that
// processes the Web Service return value.
function FailCallback(error){
	alert(error.get_message());
}
//Stops events bubbling up the DOM
function stopPropagation(e)
{
	e = e||event;/* get IE event ( not passed ) */
	e.stopPropagation ? e.stopPropagation() : e.cancelBubble = true;
}
