﻿/*
TOOLS - Javascript tools library

Parameters:
	TOOLS_PROTOTYPE_VERSION - Set this to the version of Prototype to be loaded before the script loads.
							  If not given a default version if used (see below).
*/

//Create our namespaces
if(!SPS) var SPS={};
if(!Tools) var Tools={};

Tools = {
	prototypeDefault: '1.6.1',		//Version of prototype loaded by default.
	prototypeVersion: '',			//The currently loaded prototype version.
	prototypeLoaded: false,			//True if prototype has been loaded.
	
	LoadScript: function(scriptPath) {
		try{
			//For Safari 2, which fails with the DOM method.
			document.write('<scr'+'ipt type="text/javascript" src="' + scriptPath + '"></scr'+'ipt>');
		} 
		catch(err) {
			// for xhtml+xml served content, fall back to DOM methods
			var script = document.createElement('script');
			script.type = 'text/javascript';
			script.src = scriptPath;
			document.getElementsByTagName('head')[0].appendChild(script);
		}
    },
	
	LoadPrototype: function() {
		if (!this.prototypeLoaded) {
			//If the prototype version is being overriden before this script is loaded...
			if (typeof(TOOLS_PROTOTYPE_VERSION) != 'undefined')
				this.prototypeVersion = TOOLS_PROTOTYPE_VERSION;
			else
				this.prototypeVersion = this.prototypeDefault;
			
			this.LoadScript('/Include/JS/prototype-' + this.prototypeVersion + '.js');
			
			this.prototypeLoaded = true;
		}
	},
	
	//Adds an event listener to an element.
	//	element - reference to the element
	//	eventName - the name of the event, without the "on" part (load, click, etc)
	//	handler - reference to a function that will handle the event
	AddListener: function(element, eventName, handler) {
		if (element.addEventListener)
			element.addEventListener(eventName, handler, false);
		else
			element.attachEvent("on" + eventName, handler);
		
		return element;
	},
	
	//
	//POSITION CLASS
	//
	Position: {
		/*
		Positions an element below another element, with X and Y offsets.
		Parameters:
			elementID  - the element or the element's ID
			attachToID - the element to attach to, or it's ID
			offsetX    - an amount in pixels to offset the left position
			offsetY    - an amount in pixels to offset the top position
		*/
		Below: function(attachToID, elementID, offsetX, offsetY) {
			var attachTo = $(attachToID);
			var elementObj = $(elementID);
			var attachHeight = attachTo.getHeight();
			
			//Use the Prototype library to position the attachTo element.
			elementObj.clonePosition(attachTo, { 
					setWidth:false, setHeight:false, 
					offsetTop: attachHeight + offsetY,
					offsetLeft: offsetX
				}
			);
		},

		/*
		Positions an element inside another element, with X and Y offsets.
		Parameters:
			elementID  - the element or the element's ID
			attachToID - the element to attach to, or it's ID
			offsetX    - an amount in pixels to offset the left position
			offsetY    - an amount in pixels to offset the top position
		*/
		Inside: function(attachToID, elementID, offsetX, offsetY) {
			var attachTo = $(attachToID);
			var elementObj = $(elementID);
			
			//Use the Prototype library to position the attachTo element.
			elementObj.clonePosition(attachTo, { 
					setWidth:false, setHeight:false, 
					offsetTop: offsetY,
					offsetLeft: offsetX
				}
			);
		},


		/*
		Positions an element in the absolute center of the window, with X and Y offsets.
		Parameters:
			elementID  - the element or the element's ID
			offsetX    - an amount in pixels to offset the left position
			offsetY    - an amount in pixels to offset the top position
		*/
		Center: function(elementID, offsetX, offsetY) {
			var element = $(elementID);
			
			//Maintain the popup in the middle of the screen.
			var height = parseInt(element.style.height);
			var width = parseInt(element.style.width);
			
			//Use non-standards compliant properties when doctype is set to html4.
			var windowHeight = (window.innerHeight ? window.innerHeight : document.body.offsetHeight);
			var windowWidth = Math.max(document.documentElement.clientWidth, document.body.offsetWidth);
			var scrollHeight = Math.max(document.body.scrollTop, document.documentElement.scrollTop);
			
			element.style.top = Math.round((windowHeight / 2) - (height / 2) + scrollHeight) + "px";
			element.style.left = Math.round((windowWidth / 2) - (width / 2)) + "px";
			
			//TestSizes();
		},
		
		
		/*
		Inserts an element immediately after another element.
		*/
		After: function(attachToID, elementID) {
			var attachTo = $(attachToID);
			var element = $(elementID);
			
			var parentElement = attachTo.parentNode;
			var nextSibling = this.GetNextSibling(attachTo);
			
			if (nextSibling) {
				parentElement.insertBefore(element, nextSibling);
			}
			else {
				parentElement.appendChild(element);
			}
		},
		
		//Get the element immediately after the given one.
		GetNextSibling: function(element) {
			if (element.nextSibling) {
				var sib = element.nextSibling;
				while (sib.nodeType != 1) {
					if (sib.nextSibling) {
						sib = sib.nextSibling;
					} 
					else {
						return false;
					}
				} 
				
				return sib;
			} 
			
			return false;
		}
	},
	
	
	Form: {
		AddOption: function(selectObj, text, value) {
			if (!selectObj)
				return;
			
			var option = document.createElement('option');
			option.text = text;
			option.value = value;
			
			try {
				selectObj.add(option, null); // standards compliant; doesn't work in IE
			}
			catch(ex) {
				selectObj.add(option); // IE only
			}
			
			//return option;
		}
	},
	
		
	//
	//COOKIE CLASS
	//
	Cookie: {
		//Leave days blank to write a session cookie.
		Set: function(name, value, days) {
			var expires = "";
			var date = null;
			
			if (days != null) {
				//Set cookie with expiration date.
				date = new Date();
				date.setDate(date.getDate() + days);
				expires = "; expires=" + date.toGMTString();
			}
			
			document.cookie = name + "=" + value + expires + "; path=/";
		},
		
		Get: function(name) {
			var search = name + "=";
			var value = null;
			
			if (document.cookie.length > 0) {
				offset = document.cookie.indexOf(search)
				//if cookie exists
				if (offset != -1) {
					offset += search.length
					//set index of beginning of value
					end = document.cookie.indexOf(";", offset);
					
					//set index of end of cookie value
					if (end == -1) 
						end = document.cookie.length;
						
					value = unescape(document.cookie.substring(offset, end));
				}
			}
			
			return value;
		},
		
		Delete: function(name) {
			this.Set(name, "", -1);
		}
	}
}



Tools.LoadPrototype();


/*
Finds and returns a .Net control by ID and tagName.  We don't know the exact ID because
.Net renames the ID's, so this searches for it.  Returns the first control found with an
ID that contains the one we are searching for.
Parameters:
	findID - the control's ID
	tagName- the type of tag to look for, like "div", "table", etc.
*/
function FindControl(findID, tagName) {
	var elementList = document.getElementsByTagName(tagName);
	
	for (var i=0; i < elementList.length; i++) {
		var element = elementList[i];
		
		if (element.id.indexOf(findID) != -1)
			return element;
	}
	
	return null;
}

var maxDepth = 10;
function FindChildControl(findID, parentElement) { 
	if (maxDepth == 0) {
		return null;
	}
	
	var elementList = parentElement.childNodes;
	
	for (var i=0; i < elementList.length; i++) {
		var element = elementList[i];
		
		//If this is an element type and the id matches...
		if (element.nodeType == 1 && element.id.indexOf(findID) != -1) {
			return element;
		}
		else {
			//If there are children, recurse down the DOM tree to search them.
			if (element.childNodes.length > 0) {
				maxDepth--;
				
				FindChildControl(findID, element);
			}
		}
	}
	
	return null;
}

/*
Finds and returns one or more .Net controls by ID and tagName.  We don't know the exact ID because
.Net renames the ID's, so this searches for it.  Returns an array.
Parameters:
	findID - the control ID
	tagName- the type of tag to look for, like "div", "table", etc.
*/
function FindControls(findID, tagName) {
	var elementList = document.getElementsByTagName(tagName);
	var elements = new Array();
	
	for (var i=0; i < elementList.length; i++) {
		var element = elementList[i];
		
		if (element.id.indexOf(findID) != -1)
			elements.push(element);
	}
	
	return elements;
}

/*
For checkboxes that have an image next to them instead of text, this hooks 
up the <label> type functionality for Internet Explorer, which doesn't work 
properly with images.
*/
function CheckboxImageLabel(checkboxID) {
	var checkbox = FindControl(checkboxID, 'input');
	checkbox.checked = !checkbox.checked;
}


/*
Gets and returns an element, extending it with Prototype functionality.
*/
function Get(idOrElement) {
	return $(idOrElement);
}


/*
Parses the querystring for a particular parameter and returns its value.
*/
function GetUrlParam( paramName )
{
	//divide the URL in half at the '?'
	var urlHalves = String( document.location ).split( '?' );
	var urlVarValue = '';
    
	if( urlHalves[ 1 ] )
	{
		//load all the name/value pairs into an array
		var urlVars = urlHalves[ 1 ].split( '&' );
        
		//loop over the list, and find the specified url variable
		for( i = 0; i <= ( urlVars.length ); i++ )
		{
			if( urlVars[ i ] )
			{
				//load the name/value pair into an array
				var urlVarPair = urlVars[ i ].split( '=' );
                
				if( urlVarPair[ 0 ] && urlVarPair[ 0 ] == paramName )
				{
					//I found a variable that matches, load it's value into the return variable
					urlVarValue = urlVarPair[ 1 ];
				}// end if
			}// end if
		}// end for
	}// end if
    
    //Remove any trailing "#" and un-escape if necessary.
    urlVarValue = urlVarValue.replace(/#.*$/,'')
    urlVarValue = unescape(urlVarValue);
    
	return urlVarValue;
}// end getURLs 



/*
var testSizesDiv = null;
function TestSizes() {
	//				Firefox/Safari/Chrome					Internet Explorer		
	//Scrolling		document.documentElement.scrollTop		document.body.scrollTop
	//Width			document.documentElement.clientWidth	document.body.offsetWidth
	//Height		window.innerHeight						document.body.offsetHeight
	if (testSizesDiv == null) {
		testSizesDiv = document.createElement("div");
		testSizesDiv.style.position = "absolute";
		testSizesDiv.style.top = "300px";
		testSizesDiv.style.left = "10px";
		testSizesDiv.style.backgroundColor = "black";
		testSizesDiv.style.color = "white";
		document.body.appendChild(testSizesDiv);
	}
	
	testSizesDiv.innerHTML = 
		"window.innerHeight: " + window.innerHeight + "<br>" +
		"window.innerWidth: " + window.innerWidth + "<br>" +
		"<br>" +
		"document.documentElement.clientHeight: " + document.documentElement.clientHeight + "<br>" +
		"document.documentElement.clientWidth: " + document.documentElement.clientWidth + "<br>" +
		"document.documentElement.scrollTop: " + document.documentElement.scrollTop + "<br>" +
		"<br>" +
		"document.body.offsetWidth: " + document.body.offsetWidth + "<br>" + 
		"document.body.offsetHeight: " + document.body.offsetHeight + "<br>" + 
		"document.body.offsetTop: " + document.body.offsetTop + "<br>" + 
		"document.body.scrollHeight: " + document.body.scrollHeight + "<br>" +
		"document.body.scrollTop: " + document.body.scrollTop + "<br>"		
	;
}
*/
