﻿document.write('<scr' + 'ipt type="text/javascript" src="/Include/JS/prototype-1.6.0.3.js"></scr' + 'ipt>');

/*
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 that 
matches the ID.
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;
}


/*
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
*/
function PositionBelow(attachToID, elementID, offsetX, offsetY) {
	var attachTo = GetElementByIdOrReference(attachToID);
	var elementObj = GetElementByIdOrReference(elementID);
	
	//Use the Prototype library to position the attachTo element.
	elementObj.clonePosition(attachTo, { 
			setWidth:false, setHeight:false, 
			offsetTop: attachTo.getHeight() + 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
*/
function PositionInside(attachToID, elementID, offsetX, offsetY) {
	var attachTo = GetElementByIdOrReference(attachToID);
	var elementObj = GetElementByIdOrReference(elementID);
	
	//Use the Prototype library to position the attachTo element.
	elementObj.clonePosition(attachTo, { 
			setWidth:false, setHeight:false, 
			offsetTop: offsetY,
			offsetLeft: offsetX
		}
	);
	
	/*//Get the initial left and top positions for the attachTo element, relative to it's parent.
	var topValue = attachTo.offsetTop;
	var leftValue = attachTo.offsetLeft;
	
	//Calculate the absolute position of the attachTo element relative to the client area.
	while((attachTo = attachTo.offsetParent) != null) {
		topValue += attachTo.offsetTop;
		leftValue += attachTo.offsetLeft;
	}
	
	//Position the element.
	element.style.position = 'absolute';
	element.style.left = leftValue + offsetX + 'px';
	element.style.top = topValue + offsetY + 'px';*/
}


/*
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
*/
function PositionCenter(elementID, offsetX, offsetY) {
	var element = GetElementByIdOrReference(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();
}


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>"		
	;
}



/*
Inserts an element immediately after another element.
*/
function InsertAfter(attachToID, elementID) {
	var attachTo = GetElementByIdOrReference(attachToID);
	var element = GetElementByIdOrReference(elementID);
	
	var parentElement = attachTo.parentNode;
	var nextSibling = GetNextSibling(attachTo);
	
	if (nextSibling) {
		parentElement.insertBefore(element, nextSibling);
	}
	else {
		parentElement.appendChild(element);
	}
}

function GetNextSibling(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;
}


/*
UTILITY FUNCTIONS
*/

/*
Gets and returns an element by its ID.
*/
function Get(elementById) {
	return $(elementById);
}

//If idOrElement is an ID of an element, get a the element and return it.  If it's 
//already an element, return that.
function GetElementByIdOrReference(idOrElement) {
	/*if (typeof(idOrElement) == "string") {
		return $(idOrElement);
	}
	
	return Element.extend(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
    
    //Return the value, minus any trailing "#".
	return urlVarValue.replace(/#.*$/,'');
}// end getURLs 


//Leave days blank to write a session cookie.
function SetCookie(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=/";
}

function GetCookie(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;
}

function DeleteCookie(name) {
	SetCookie(name, "", -1);
}
