/************************************************************************************
*		Project: All web development projects
*		Date: 02/13/2006
*		Author: Chad Wilson (© Robot Printing)
*		Description: 
*			- global.js: Commonly used JavaScript functions that can be used across all projects
*							Code should be very modular and support major browsers when possible
*							Code should not be copied out of this page unless you are exporting to 
*							a new JavaScript file to be imported.
*
*		Document History:
*		- 03/13/2007 - Chad Wilson		- querystring function created
*		- 09/27/2006 - Chad Wilson		- toggleSelectBoxVisibility function created
*										- iecompattest function created
*										- getDocumentHeight function created
*		- 09/21/2006 - Chad Wilson		- setupButtonClasses function created
*		- 09/08/2006 - Chad Wilson		- waitPopup_Custom function updated
*		- 09/06/2006 - Chad Wilson		- waitPopup_Custom function updated
*		- 08/29/2006 - Chad Wilson		- pauseComp function created
*										- waitPopup_Custom function updated
*		- 08/28/2006 - Chad Wilson		- waitPopup function created
*										- waitPopup_Custom function created
*		- 08/15/2006 - Chad Wilson		- createXMLHttpRequest function created
*		- 06/28/2006 - Chad Wilson		- getBrowserShort function created
*		- 05/02/2006 - Chad Wilson		- Asc function moved to string.js
*										- Chr function moved to string.js
*										- convertString function moved to string.js
*										- inStr function moved to string.js
*										- isAlphanumeric function moved to string.js
*										- isChar function moved to string.js
*										- isEmail function moved to string.js
*										- isEmpty function moved to string.js
*										- isNumeric function moved to string.js
*										- isWhitespace function moved to string.js
*										- Join function moved to string.js
*										- Left function moved to string.js
*										- Len function moved to string.js
*										- LTrim function moved to string.js
*										- mask function moved to string.js
*										- Mid function moved to string.js
*										- printString function moved to string.js
*										- properCase function moved to string.js
*										- reverseString function moved to string.js
*										- replace function moved to string.js
*										- Right function moved to string.js
*										- RTrim function moved to string.js
*										- Split function moved to string.js
*										- Trim function moved to string.js
*										- allowAlpha function moved to string.js
*										- allowDate function moved to string.js
*										- allowEmail function moved to string.js
*										- allowInteger function moved to string.js
*										- allowNumber function moved to string.js
*										- allowPhoneNumber function moved to string.js
*										- allowURL function moved to string.js
*										- allowZipCode function moved to string.js
*										- comboAddItem function moved to string.js
*										- comboDelItem function moved to string.js
*										- forceEntry function moved to string.js
*										- switchCheckBoxes function moved to string.js
*										- Abs function moved to math.js
*										- formatCurrency function moved to math.js
*										- formatNumber function moved to math.js
*										- formatPercent function moved to math.js
*										- Sgn function moved to math.js
*										- formatDateTime function moved to calendar.js
*										- getDayOfWeek function moved to calendar.js
*										- getDaysInMonth function moved to calendar.js
*										- isDate function moved to calendar.js
*										- isLeapYear function moved to calendar.js
*		- 04/28/2006 - Chad Wilson		- displayPopup function updated
*		- 04/27/2006 - Chad Wilson		- changeDisplayByName function updated
*										- changeCSS function created
*										- displayPopup function created
*		- 04/26/2006 - Chad Wilson		- changeDisplay function created
*										- changeVisibility function created
*										- flipSrc function created
*		- 04/24/2006 - Chad Wilson		- getBrowser function created
*										- getDaysInMonth function created
*										- Abs function created
*										- Len function created
*										- Chr function created
*										- Asc function created
*										- LTrim function created
*										- RTrim function created
*										- Trim function created
*										- Mid function created
*										- InStr function created
*										- Sgn function created
*										- LBound function created
*										- UBound function created
*										- Join function created
*										- printString function created
*										- Split function created
*										- isChar function created
*										- isNumeric function created
*										- isAlphanumeric function created
*										- reverseString function created
*										- convertString function created
*										- properCase function created
*										- comboAddItem function created
*										- comboDelItem function created
*										- formatNumber function created
*										- formatCurrency function created
*										- formatPercent function created
*										- formatDateTime function created
*										- isLeapYear function created
*										- mask function created
*										- isEmail function created
*										- switchCheckBoxes function created
*										- isDate function created
*										- getDayOfWeek function created
*		- 04/11/2006 - Steev Stapleton	- replace function created
*										- Left function created
*										- Right function created
*		- 03/22/2006 - Chad Wilson		- forceEntry function created
*										- isEmpty function created
*										- isWhitespace function created
*		- 03/03/2006 - Chad Wilson		- allowInteger function created
*		- 02/22/2006 - Chad Wilson		- Parameters added to documentation
*										- clearClipboard function created
*		- 02/20/2006 - Chad Wilson		- allowEmail function updated
*										- allowDate function updated
*										- allowURL function updated
*										- allowZipCode function updated
*										- createArray function created
*										- functions are alphabetized
*		- 02/13/2006 - Chad Wilson		- Page created
*										- Testing required
************************************************************************************/ 



/*******************
* GLOBAL CONSTANTS *
*******************/ 

// whitespace characters
var whitespace = " \t\n\r";
// current page link in the browsers location bar
var thispage = location.href;
var xmlHttp;
var ie4up = (document.all) ? 1 : 0;
var ns6up = (document.getElementById&&!document.all) ? 1 : 0;



/***********************************************************************************
* Function Name:	changeCSS()
* Creation Date:	04/27/2006
* Author:			Chad Wilson (see reference)
* Description:		Dynamically reloads the CSS file for a page without reloading
*					the entire page
*
* Parameters:		css_file - new CSS file to change to
*
* History:			04/27/2006 - Chad Wilson - Function created
*
* Reference:		http://www.phpied.com/files/jinc/test7.html (Stoyan)
***********************************************************************************/

function changeCSS(css_file) {
	var html_doc = document.getElementsByTagName('head').item(0);
	var css = document.createElement('link');
	css.setAttribute('rel', 'stylesheet');
	css.setAttribute('type', 'text/css');
	css.setAttribute('href', css_file);
	html_doc.appendChild(css);
}



/***********************************************************************************
* Function Name:	changeDisplay()
* Creation Date:	04/26/2006
* Author:			Chad Wilson
* Description:		Changes the display status of an object
*
* Parameters:		obj - Object to show/hide 
*
* History:			04/26/2006 - Chad Wilson - Function created
***********************************************************************************/

function changeDisplay(obj) {
	if(obj) {
		if(obj.style.display == 'none')
			obj.style.display = '';
		else
			obj.style.display = 'none';
	}
}



/***********************************************************************************
* Function Name:	changeDisplayByName()
* Creation Date:	04/26/2006
* Author:			Chad Wilson
* Description:		Flips the display status of all elements with the name you supply
*
* Parameters:		name - name of element(s) to change display
*
* History:			
*					04/27/2006 - Chad Wilson - Function works
*					04/26/2006 - Chad Wilson - Function created
***********************************************************************************/

function changeDisplayByName(name) {
	collAll = document.getElementsByName(name);
	for ( i=1 ; i<=UBound(collAll) ; i++ ) {
		with (document.getElementsByName(name).item(i)) {
			if (style.display == 'none') {
				style.display = '';
			}
			else {
				style.display = 'none';
			}
		}
	}
}



/***********************************************************************************
* Function Name:	changeVisibility()
* Creation Date:	04/26/2006
* Author:			Chad Wilson
* Description:		Changes the visibility status of an object
*
* Parameters:		obj - Object to show/hide 
*
* History:			04/26/2006 - Chad Wilson - Function created
***********************************************************************************/

function changeVisibility(obj) {
	if (obj) {
		if (obj.style.visibility == 'hidden')
			obj.style.visibility = 'visible';
		else {
			obj.style.visibility = 'hidden';
		}
	}
}



/***********************************************************************************
* Function Name:	clearClipboard()
* Creation Date:	02/22/2006
* Author:			Chad Wilson (see reference below)
* Description:		Clears user clipboard to prevent pasting into object
*
* Parameters:		showAlert = bool value that determines whether user is shown an alert message
*
* History:			02/22/2006 - Chad Wilson - Function created
*
* Implementation:	<input type="text" onpaste="clearClipboard(true)">
***********************************************************************************/

function clearClipboard(showAlert) {
	if (showAlert) {
		alert('Cannot paste into this object');
	}
	window.clipboardData.setData('Text','');
}



/***********************************************************************************
* Function Name:	createArray()
* Creation Date:	02/20/2006
* Author:			Chad Wilson (see reference below)
* Description:		Creates an array of size n with values initializes to 0
*					Older browsers that do not support direct initializing support this method
*
* Parameters:		n = size of array to be created
*
* History:			02/20/2006 - Chad Wilson - Function created
*
* Implementation:	myArray = createArray(10);
*
* Reference:		http://www.4guysfromrolla.com/ ?
***********************************************************************************/

//initialize array with n entries
function createArray(n) {
	//code taken from JavaScript Bible 4th edition CD-ROM version by Danny Goodman, 2001. Hungry Minds, Inc.
	this.length = n;
	for (var i=1; i<=n; i++) {	
		this[i] = 0;
	}
	return this;
}



/***********************************************************************************
* Function Name:	createXMLHttpRequest()
* Creation Date:	08/15/2006
* Author:			Chad Wilson (see reference below)
* Description:		Sets up xmlHttp as XmlHttpRequest object with browser check
*
* History:			08/15/2006 - Chad Wilson - Function created
*
* Reference:		From Apress's Foundations of Ajax book (http://www.apress.com)
***********************************************************************************/

function createXMLHttpRequest() {
    if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } 
    else if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
    }
}



/***********************************************************************************
* Function Name:	displayPopup()
* Creation Date:	04/27/2006
* Author:			Chad Wilson (see reference below)
* Description:		Creates a semi-transparent popup 'block' with text and an image
*
* Parameters:		text - text to display
*					color1 - gradient color 1 (hex value including #)
*					color2 - gradient color 2 (hex value including #)
*					img - image to use
*					action - "show" or "hide"
*
* History:		
*					05/01/2006 - Chad Wilson - Function still not working properly
*					04/28/2006 - Chad Wilson - Function still not working properly
*					04/27/2006 - Chad Wilson - Function created
*
* Implementation:	
*
* Reference:		Idea from: http://www.2enetworx.com/dev/projects/tableeditor.asp
*					Some code from: http://www.javascriptkit.com/javatutors/dom2.shtml
***********************************************************************************/

function displayPopup(text, color1, color2, img, action) {
	if (!action) {
		action = "show"
	}
	
	if (action=="show") {
		if (document.getElementById('popupSpan')) {
			// <span> has already been created - just show it
			document.getElementById('popupSpan').style.display = '';
			window.status = text;
		}
		else {
			window.status = text;
			//<span id="DisplayMessageBox" style="display:none; position:absolute; padding: 18 20 20 20; filter: progid:DXImageTransform.Microsoft.Shadow(direction=135,color=#B0B0B0,strength=5) progid:DXImageTransform.Microsoft.Alpha( style=0,opacity=75) progid:DXImageTransform.Microsoft.Gradient(gradientType=1,startColorStr=#A5CBF7,endColorStr=#08246B); font: bold 14pt/1.3 verdana; color: #FFFFFF; height: 60px" ><img src="images/loading.gif" align="absmiddle"> Please wait While loading data...</span>
			var span = document.createElement('img'); //this works with img tag but not span?
			span.setAttribute('id', 'popupSpan');
			span.setAttribute('name', 'popupSpan');
			span.setAttribute('src', img)
			span.setAttribute('style', 'display:inline; position:absolute; top:240px; left:2px; text-align:center; padding: 18 20 20 20; filter: progid:DXImageTransform.Microsoft.Shadow(direction=135,color=#B0B0B0,strength=5) progid:DXImageTransform.Microsoft.Alpha( style=0,opacity=75) progid:DXImageTransform.Microsoft.Gradient(gradientType=1,startColorStr=#A5CBF7,endColorStr=#08246B); font: bold 14pt/1.3 verdana; color: #FFFFFF; height: 60px');
			document.body.appendChild(span);
		}		
	}
	else {
		// hide popup window
		if (document.getElementById('popupSpan')) {
			document.getElementById('popupSpan').style.display = 'none';
			window.status = "";
		}
	}
}



/***********************************************************************************
* Function Name:	flipSrc()
* Creation Date:	04/26/2006
* Author:			Chad Wilson
* Description:		Flips an element's src attribute between 2 values
*					Designed for images but should work with anything
*
* Parameters:		obj 	- object
*					src1 	- src 1
*					src2 	- src 2
*
* Notes:			Must pass ABSOLUTE PATH of image
*
* History:			04/26/2006 - Chad Wilson - Function created
***********************************************************************************/

function flipSrc(obj,src1,src2) {
	if (obj.src == src2) {
		obj.src = src1;
	}
	else {
		obj.src = src2;
	}
}



/***********************************************************************************
* Function Name:	getBrowser()
* Creation Date:	04/24/2006
* Author:			Chad Wilson (see reference below)
* Description:		Returns browser name and version number (only IE and NS)
*
* History:			04/24/2006 - Chad Wilson - Function created
*
* Reference:		Public domain - from JavaScript Library  v1.0.03
*
***********************************************************************************/

function getBrowser() {
	var temp = navigator.appName;
	temp = temp.toLowerCase();
	if (temp == 'microsoft internet explorer') {
		return 'IE' + navigator.appVersion;
	}
	else {
		return 'NS' + navigator.appVersion;
	}
}



/***********************************************************************************
* Function Name:	getBrowserShort()
* Creation Date:	06/28/2006
* Author:			Chad Wilson (see reference below)
* Description:		Returns IE or NS only
*
* History:			04/24/2006 - Chad Wilson - Function created
*
* Reference:		Public domain - from JavaScript Library  v1.0.03
*
***********************************************************************************/

function getBrowserShort() {
	var temp = navigator.appName;
	temp = temp.toLowerCase();
	if (temp == 'microsoft internet explorer') {
		return 'IE';
	}
	else {
		return 'NS';
	}
}



/***********************************************************************************
* Function Name:	getDocumentHeight()
* Creation Date:	09/27/2006
* Author:			Chad Wilson (see reference below)
* Description:		Gets height of visible document (only what they can see without scrolling)
*					Is useful when trying to fit to browser window
*
* History:			09/27/2006 - Chad Wilson - Function created
*
* Reference:		http://www.dynamicdrive.com/dynamicindex3/snow.htm
***********************************************************************************/

function getDocumentHeight() {
	var doc_height;
	if (window.innerHeight) {
		doc_height = window.innerHeight;
	}
	else if (ie4up) {
		doc_height = iecompattest().clientHeight;
	}
	else if	(ie4up && !window.opera) {
		doc_height = iecompattest().scrollHeight;
	}
	else {
		doc_height = iecompattest().offsetHeight;
	}
	return doc_height;
}



/***********************************************************************************
* Function Name:	iecompattest()
* Creation Date:	09/27/2006
* Author:			Chad Wilson (see reference below)
* Description:		Returns true if IE
*
* History:			09/27/2006 - Chad Wilson - Function created
*
* Reference:		Public domain - Used in many DynamicDrive scripts
*
***********************************************************************************/

function iecompattest() {
	return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}


/***********************************************************************************
* Function Name:	LBound()
* Creation Date:	04/24/2006
* Author:			Chad Wilson - see reference below
* Description:		Returns a Long containing the smallest available 
*       			subscript for the indicated dimension of an array
*
* Parameters:		array = Array to verify
*
* Returns: 			Integer  (-1 if Array does not contain any subscript)
*
* History:			04/24/2006 - Chad Wilson - Function created
*
* Reference:		Public domain - from JavaScript Library  v1.0.03
***********************************************************************************/

function LBound(array) {
	var i = 0;
	var temp = '';

	if (array.length == 0)
		return (-1);

	for (i = 0; i < array.length; i++) 	{
		temp = array[i];
		if (temp != null) {
			var temp = i;
			return temp;
		}
	}
	return (-1);
}



/***********************************************************************************
* Function Name:	pauseComp()
* Creation Date:	08/29/2006
* Author:			Chad Wilson - see reference below
* Description:		Pauses comp for specified time in milliseconds
*					Generally bad practice but there are exceptions
*
* Parameters:		millis = Time to pause in milliseconds
*
* History:			08/29/2006 - Chad Wilson - Function created
*
* Reference:		Sean McManus - http://www.sean.co.uk/a/webdesign/javascriptdelay.shtm
***********************************************************************************/

function pauseComp(millis) {
	date = new Date();
	var curDate = null;
	do { 
		var curDate = new Date(); 
	} 
	while(curDate-date < millis);
}



/***********************************************************************************
* 	Function Name:	querystring()
* 	Creation Date:	03/13/2007
* 	Author:			Chad Wilson (see reference)
* 	Description:	Parses the querystring and returns the value matching specified name
*
* 	Parameters:		name - querystring 'name' to return value for
*
* 	History:		03/13/2007 - Chad Wilson - Function created
*
*	Reference:		http://www.activsoftware.com/code_samples/code.cfm/CodeID/59/
*						JavaScript/Get_Query_String_variables_in_JavaScript
***********************************************************************************/

function querystring(name) {
	var query = unescape(window.location.search.substring(1));
	var vars = query.split("&");
	for (var i=0; i<vars.length; i++) {
		var pair = vars[i].split("=");
		if (pair[0] == name) {
			return pair[1];
		}
	}
	return '';
}



/***********************************************************************************
* Function Name:	setupButtonClasses()
* Creation Date:	09/21/2006
* Author:			Chad Wilson
* Description:		Updates the classes for all buttons on page
*
* Parameters:		normalClass = class of button
*					hoverClass = class of button onmouseover
*
* History:			09/21/2006 - Chad Wilson - Function created
*
***********************************************************************************/

function setupButtonClasses(normalClass,hoverClass) {
	//make an array of all input elements
	var aryInputs = document.getElementsByTagName('input')
	//loop through array
	for (i=0;i<aryInputs.length;i++) {
		// check if element is a button (button,submit,reset)
		if (aryInputs[i].getAttribute('type') == 'button' || aryInputs[i].getAttribute('type') == 'submit' || aryInputs[i].getAttribute('type') == 'reset') {
			//set classes for events
			aryInputs[i].className = normalClass;
			// can't use setAttribute - can't use for 'events' in IE
			aryInputs[i].onmouseover = function() { this.className = normalClass + " " + hoverClass; };
			aryInputs[i].onmouseout = function() { this.className = normalClass; };
		}
	}
}



/***********************************************************************************
* Function Name:	toggleSelectBoxVisibility()
* Creation Date:	09/27/2006
* Author:			Chad Wilson
* Description:		Toggles the visibility of all select boxes on a page
*					Select boxes will appear "above" all other elements, even absolute
*					positioned elements, so if something is meant to appear "above" a select,
*					box, it must be hidden. 
*
* Parameters:		blnShow (boolean)
*						true: show all select boxes
*						false: hide all select boxes
*
* History:			09/27/2006 - Chad Wilson - Function created
***********************************************************************************/

function toggleSelectBoxVisibility(blnShow) {
	//make an array of all <SELECT> elements
	var arySelects = document.getElementsByTagName('select')
	//loop through array
	for (i=0;i<arySelects.length;i++) {
		if (blnShow) {
			//show select box
			arySelects[i].style.visibility = 'visible';
		}
		else {
			//hide select box
			arySelects[i].style.visibility = 'hidden';
		}
	}
}



/***********************************************************************************
* Function Name:	UBound()
* Creation Date:	04/24/2006
* Author:			Chad Wilson - see reference below
* Description:		Returns a Long containing the largest available 
*         			subscript for the indicated dimension of an array
*
* Parameters:		array = Array to verify
*
* Returns: 			Integer   (-1 if Array does not contain any subscript)
*
* History:			04/24/2006 - Chad Wilson - Function created
*
* Reference:		Public domain - from JavaScript Library  v1.0.03
***********************************************************************************/

function UBound(array) {
	return (array.length - 1);
}



/***********************************************************************************
* Function Name:	waitPopup()
* Creation Date:	08/28/2006
* Author:			Chad Wilson
* Description:		Displays Flash popup saying "please wait"
*					Can be used as a general default to waitPopup_Custom
*
* Parameters:		action - "on" to show, "off" to hide
*
* History:			08/28/2006 - Chad Wilson - Function created
*
***********************************************************************************/

function waitPopup(action) {
	waitPopup_Custom("http://www.robotprinting.com/share/flash/wait.swf",(34*2.5),500,action);
}



/***********************************************************************************
* Function Name:	waitPopup_Custom()
* Creation Date:	08/28/2006
* Author:			Chad Wilson
* Description:		Displays custom Flash popup 
*
*					Scrolling solution based on 
*						http://www.dynamicdrive.com/dynamicindex1/slideinlink.htm
*
* Parameters:		src - url of flash file
*					height - height of file
*					width - width of file
*					action - "on" to show, "off" to hide
*
* Implementation: 	Use this inside your handleStateChange() function
*					
*					if (xmlHttp.readyState == 1) {
*	    				waitPopup("on");
*	    			}
*	    			else if (xmlHttp.readyState == 4) {
*	    				waitPopup("off");
*	    				...
*	       			}
*
* Issues:			1) Firefox doesn't hide the popup
*					2) Could support horizontal scrolling - not a huge issue now
*
* History:			
*					09/08/2006 - Chad Wilson - Bugfix: Checks that popup still exists before centering
*					09/06/2006 - Chad Wilson - Supports scrolling
*					08/28/2006 - Chad Wilson - Function created
*
***********************************************************************************/

function waitPopup_Custom(src,height,width,action) {
	if (action == "on") {
		// show flash popup
		// add random number at end of file name so browser doesn't cache
		src = src + "?" + Math.random();
		// create div to hold flash file
		var div  = document.createElement('div');
		// center on screen
		var winl = (screen.width-width)/2;
		// for some reason object is always displaying a little bit too low so I bumped it up 200 px
		var wint = document.body.scrollTop + (screen.height/2) - 200;
		div.setAttribute("id", "divWaitingPopup");
		document.body.appendChild(div);
		var divContent = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://active.macromedia.com/flash4/cabs/swflash.cab#version=4,0,0,0" id="Movie1" width="' + width + '" height="' + height + '" >';
		divContent = divContent + '<param name="movie" value="' + src + '"><param name="quality" value="high"><param name="bgcolor" value="#FFFFFF">';
		divContent = divContent + '<embed name="Movie1" src="' + src + '" quality="high" bgcolor="#FFFFFF" width="' + width + '" height="' + height + '" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash">';
		divContent = divContent + '</embed></object><style>#divWaitingPopup { border: 1px solid #000000; top: ' + wint + 'px; left: ' + winl + 'px; position: absolute; }</style>';
		document.getElementById('divWaitingPopup').innerHTML = divContent;
		// recenter when page is scrolled
		window.onscroll = centerPopup;
	}
	else {
		// hide/destroy flash popup
		// for some reason firefox won't destroy popup
		div = document.getElementById('divWaitingPopup');
		document.body.removeChild(div);
		window.onscroll = "";
	}
}
// recenters when page is scrolled - keep this function with waitPopup_Custom()
function centerPopup() {
	if (document.getElementById('divWaitingPopup')) {
		with (document.getElementById('divWaitingPopup')) {
			style.top = eval(document.body.scrollTop + (screen.height / 2)) - 200;
		}
	}
}


