
/**********************************************/

/*
	Library: 		kstools.js
	Version: 		200704250804
	Author: 		Ken Scott
	Website: 		http://kstechnotes.blogspot.com
	Creation Date: 	Monday, March 19, 2007
	Description: 	An object that includes various child objects that I use in various development projects
	
*/

/**********************************************/

kstools = {

	ajax : {

		/*
			Object: 		ajax
			Author:		Ken Scott
			Creation Date: 	Tuesday, March 20, 2007
			Last Update: 	Tuesday, March 20, 2007
			Description:  	This object includes methods to retrieve data via an asynchronous HTTP request
		*/
	
		request : '',
		response : 'AJAX request failed.',

		handleResponse : function() {

			/*
				Method: 		handleResponse()
				Author: 		Ken Scott
				Creation Date: 	Monday, March 19, 2007
				Description: 	Retrieves the response from the XMLHTTPRequest
				Arguments: 		none
			*/
	
			//if (kstools.ajax.request.readyState == 4) { kstools.ajax.response = kstools.ajax.request.responseText; }
	
		},

		sendRequest : function(method, url) {

			/*
				Method: 		sendRequest()
				Arguments: 		method: 	either 'post' or 'get' 
							url: 		the URL to be requested from the server
				Author: 		Ken Scott
				Creation Date: 	Monday, March 19, 2007
				Description: 	establish an XMLHTTPRequest to the server for the given URL
			*/
	
			/*
			// Determine which browser XMLHTTPRequest type needs to be implemented
			if (navigator.appName == "Microsoft Internet Explorer") {
				kstools.ajax.request = new ActiveXObject("Microsoft.XMLHTTP");
			} else {
				kstools.ajax.request = new XMLHttpRequest();
			}
	
			// Create a date object for use in the cacheBuster
			var now = new Date();
	
			//Create a cacheBuster variable containing the current date and time in order to eliminate any caching issues by the browser
			var cacheBuster = '';
			cacheBuster += now.getYear();
			cacheBuster += now.getMonth();
			cacheBuster += now.getDate();
			cacheBuster += now.getHours();
			cacheBuster += now.getMinutes();
			cacheBuster += now.getSeconds();
			cacheBuster += now.getMilliseconds();

			// Create the XMLHTTPRequest
			kstools.ajax.request.open(method, url + '?cachebuster=' + cacheBuster);
			kstools.ajax.request.onreadystatechange = kstools.ajax.handleResponse;
			kstools.ajax.request.send(null);
			*/
		}

	},

/**********************************************/

	browser : {

		/*
			Object: 		window
			Author:		Ken Scott
			Creation Date: 	Tuesday, March 20, 2007
			Last Update: 	Tuesday, March 20, 2007
			Description:  	This object includes methods related to the browser window
		*/
	
		getInnerHeight : function() {
					
			/*
				Method: 		getHeight
				Arguments: 		none
				Author: 		Ken Scott
				Creation Date: 	Tuesday, March 20, 2007
				Last Update: 	Tuesday, March 20, 2007
				Description: 	Returns the inner visible height of the browser window
			*/
					
			var height = 0;
					
			if (parseInt(navigator.appVersion) > 3) {
				if (navigator.appName == "Netscape") {
					height = window.innerHeight;
				}
				if (navigator.appName.indexOf("Microsoft") != -1) {
					height = document.body.offsetHeight;
				}
			}
					
			return height;
		},

		getInnerWidth : function() {
						
			/*
				Method: 		getWidth
				Arguments: 		none
				Author: 		Ken Scott
				Creation Date: 	Tuesday, March 20, 2007
				Last Update: 	Tuesday, March 20, 2007
				Description: 	Returns the inner visible width of the browser window
			*/
					
			var width = 0;
					
			if (parseInt(navigator.appVersion) > 3) {
				if (navigator.appName == "Netscape") {
					width = window.innerWidth;
				}
				if (navigator.appName.indexOf("Microsoft") != -1) {
					width = document.body.offsetWidth;
				}
			}
						
			return width;
		}

	},

/**********************************************/

	cookie : {
			
		/*
			Object: 		cookie
			Author:		Ken Scott
			Creation Date: 	Tuesday, March 20, 2007
			Last Update: 	Tuesday, March 20, 2007
			Description:  	This object includes methods related to the cookie manipulation
		*/
	
		createCookie : function(name, value, days) {

			/*
				Method: 		createCookie
				Arguments: 		name: 	the name of the cookie
							value: 	the value stored in the cookie
							days: 		the number of days until the cookie expires
				Author: 		Ken Scott
				Creation Date: 	Tuesday, March 20, 2007
				Last Update: 	Tuesday, March 20, 2007
				Description: 	Creates a cookie with the given name, value and expiration
			*/
				
			if (days) {
				var date = new Date();
				date.setTime(date.getTime() + (days * 12 * 60 * 60 * 1000));
				var expires = '; expires=' + date.toGMTString();
			} else {
				var expires = '';
			}
	
			document.cookie = name + '=' + value + expires + '; path=/';

		},

		eraseCookie : function(name) {
	
			/*
				Method: 		eraseCookie
				Arguments: 		name: 	the name of the cookie to delete
				Author: 		Ken Scott
				Creation Date: 	Tuesday, March 20, 2007
				Last Update: 	Tuesday, March 20, 2007
				Description: 	Deletes the given cookie
			*/
					
			kstools.cookie.createCookie(name, '', -1);

		},
			
		readCookie : function(name) {
	
			/*
				Method: 		readCookie
				Arguments: 		name: 	the name of the cookie to read
				Author: 		Ken Scott
				Creation Date: 	Tuesday, March 20, 2007
				Last Update: 	Tuesday, March 20, 2007
				Description: 	Returns the value stored in the given cookie
			*/
				
			var nameEQ = name + "=";
			var ca = document.cookie.split(';');
	
			for (var i = 0; i < ca.length; i++) {
				var c = ca[i];
				while (c.charAt(0) == ' ') { c = c.substring(1, c.length); }
				if (c.indexOf(nameEQ) == 0) { return c.substring(nameEQ.length, c.length); }
			}
	
			return '';

		}

	},

/**********************************************/

	document: {
	
		/*
			Object: 		document
			Author:		Ken Scott
			Creation Date: 	Wednesday, March 21, 2007
			Last Update: 	Wednesday, March 21, 2007
			Description:  	This object includes methods to manipulate the currently loaded document
		*/
		
		brightenScreen : function() {
			
			/*
				Method: 		brightenScreen
				Arguments: 		none
				Author: 		Ken Scott
				Creation Date: 	Thursday, March 22, 2007
				Last Update: 	Thursday, March 22, 2007
				Description: 	Returns the browser viewing area to full brightness
			*/
					
			try { 
				document.getElementById('dimLayer').style.visibility = 'hidden';
				document.getElementById('dimBacker').style.visibility = 'hidden';
				var bodyRef = document.getElementsByTagName("body").item(0);
				bodyRef.removeChild(document.getElementById('dimLayer'));
				bodyRef.removeChild(document.getElementById('dimBacker'));
			}
			catch(err) {
				//Do nothing
			}
				
		},
	
		createHiddenIFrame : function(iframeID, url) {

			/*
				Method: 		createHiddenIFrame
				Arguments: 		iframeID: 	the id  of the newly created iframe
							url:		the url to load into the iframe
				Author: 		Ken Scott
				Creation Date: 	Tuesday, March 20, 2007
				Last Update: 	Tuesday, March 20, 2007
				Description: 	creates a hidden iframe in the current document
			*/
					
			var hiddenIFrame = document.createElement('iframe');
			hiddenIFrame.style.display = 'none';
			hiddenIFrame.src = 'about:blank';
			hiddenIFrame.id = iframeID;
			if (url != '') { hiddenIFrame.src = url; }
			
			var bodyRef = document.getElementsByTagName("body").item(0);
			bodyRef.appendChild(hiddenIFrame);
			
		},
		
		dimScreen : function(zindex, opacity, color) {

			/*
				Method: 		dimScreen
				Arguments: 		zindex: 	the z-index of the dim layer
							opacity:	the opacity level of the dim layer
							color: 	the color of the dim layer
				Author: 		Ken Scott
				Creation Date: 	Thursday, March 22, 2007
				Last Update: 	Thursday, March 22, 2007
				Description: 	Dims the browser viewing area
			*/
					
			try { document.getElementById('dimLayer').style.visibility = 'visible'; }
			catch(err) {
				
				if (navigator.appName == 'Microsoft Internet Explorer') {
					var height = kstools.browser.getInnerHeight() - 4; 
					var width = kstools.browser.getInnerWidth() - 20; 
				} else {
					var height = kstools.browser.getInnerHeight(); 
					var width = kstools.browser.getInnerWidth(); 
				}
				
				var dimBacker = document.createElement('iframe');
				dimBacker.style.filter = 'alpha(opacity=0)';
				dimBacker.style.opacity = 0;
				dimBacker.style.MozOpacity = 0;
				dimBacker.style.zIndex = zindex - 1; 
				dimBacker.style.height = height;
				dimBacker.style.width = width;
				dimBacker.style.position = 'absolute';
				dimBacker.style.top = '0px';
				dimBacker.style.left = '0px'; 
				dimBacker.style.visibility = 'hidden';
				dimBacker.id = 'dimBacker';
				
				var dimLayer = document.createElement('div');
				dimLayer.style.backgroundColor = color;
				dimLayer.style.filter = 'alpha(opacity=' + opacity + ')';
				dimLayer.style.opacity = opacity / 100;
				dimLayer.style.MozOpacity = opacity / 100;
				dimLayer.style.zIndex = zindex; 
				dimLayer.style.height = height;
				dimLayer.style.width = width;
				dimLayer.style.backgroundRepeat = 'repeat'; 
				dimLayer.style.position = 'absolute';
				dimLayer.style.top = '0px';
				dimLayer.style.left = '0px'; 
				dimLayer.style.visibility = 'hidden';
				dimLayer.id = 'dimLayer';
			
				var bodyRef = document.getElementsByTagName("body").item(0);
				bodyRef.appendChild(dimBacker);
				bodyRef.appendChild(dimLayer);
				document.getElementById('dimBacker').style.visibility = 'visible';
				document.getElementById('dimLayer').style.visibility = 'visible';
				
			}
		}
		
	},
	
/**********************************************/

	file : {

		/*
			Object: 		file
			Author:		Ken Scott
			Creation Date: 	Wednesday, March 21, 2007
			Last Update: 	Wednesday, March 21, 2007
			Description:  	This object includes methods related to the file operations
		*/
		
		table2XLS : function(tableID) {
				
			/*
				Method: 		table2XLS
				Arguments: 		tableID: 	the id  of the table to export
				Author: 		Ken Scott
				Creation Date: 	Tuesday, March 20, 2007
				Last Update: 	Tuesday, March 20, 2007
				Description: 	Deletes the given cookie
			*/
					
			var xlsContent = '<table border=1>';
			var exportTable = document.getElementById(tableID);
				
			for (i = 0; i < exportTable.rows.length; i++) {
				xlsContent += '<tr>' + exportTable.rows[i].innerHTML + '</tr>';
			}
				
			xlsContent += "</table>";
				
			if (navigator.appName == 'Netscape') {
				alert('File export not supported on Firefox/Netscape.'); 
			} else {
				try { exportArea.document.open("txt/html", "replace"); }
				catch(err) {
					kstools.document.createHiddenIFrame('exportArea', '');
					exportArea.src = 'about:blank';
					exportArea.document.open("txt/html", "replace");
				}
				exportArea.document.write(xlsContent);
				exportArea.document.close();
				exportArea.focus();
			
				saveAsResult = exportArea.document.execCommand("SaveAs", true, "export.xls");
			}
			
			parent.focus();

			return (saveAsResult);
		}

	},
	
/**********************************************/

	google: {
	
		/*
			Object: 		google
			Author:		Ken Scott
			Creation Date: 	Wednesday, March 28, 2007
			Last Update: 	Wednesday, March 28, 2007
			Description:  	This object includes methods to interact with the Google API
		*/
		
		map : '',
		geocoder : '',
		caption : '',
		place : '',
		point : '',
		marker : '',
	
		loadMap : function(mapId, address, bubble) {
			
			/*
				Method: 		loadMap
				Arguments: 		mapID: 	the id  of the DIV tag where the map is to be placed
							address: 	a string containing the address to map
							bubble: 	a string to be displayed in the marker bubble on the map
				Author: 		Ken Scott
				Creation Date: 	Wednesday, March 28, 2007
				Last Update: 	Wednesday, March 28, 2007
				Description: 	Creates a map object with the geocoder data for the provided address
			*/

			try {
				kstools.google.caption = bubble;
				kstools.google.map = new GMap2(document.getElementById(mapId)); 
				kstools.google.geocoder = new GClientGeocoder(); 
				kstools.google.geocoder.getLocations(address, kstools.google.addToMap); 
			}
			
			catch(err) { document.getElementById(mapId).innerHTML = '<br>loadMap: Google Maps does not contain information for the location provided.<br>'; }

		},

		addToMap : function(response) {

			/*
				Method: 		addToMap
				Arguments: 		response: 	the data passed from the loadMap method
				Author: 		Ken Scott
				Creation Date: 	Wednesday, March 28, 2007
				Last Update: 	Wednesday, March 28, 2007
				Description: 	Centers the map at the given geocoder coordiantes and adds a marker 
			*/

			try {
				kstools.google.place = response.Placemark[0]; 
				kstools.google.point = new GLatLng(kstools.google.place.Point.coordinates[1], kstools.google.place.Point.coordinates[0]); 
				kstools.google.marker = new GMarker(kstools.google.point); 
			
				kstools.google.map.setCenter(kstools.google.point, 13); 
				kstools.google.map.addOverlay(kstools.google.marker); 

				if (kstools.google.caption != '') {
					if (kstools.google.caption == '[address]') {
						kstools.google.marker.openInfoWindowHtml(kstools.google.place.address); 
					} else {
						kstools.google.marker.openInfoWindowHtml(kstools.google.caption); 
					}
				}
			}
			
			catch(err) { document.getElementById(mapId).innerHTML = '<br>addMap: Google Maps does not contain information for the location provided.<br>'; }
			
		}

	},
	
/**********************************************/

	window : {

		/*
			Object: 		window
			Author:		Ken Scott
			Creation Date: 	Thursday, March 22, 2007
			Last Update: 	Thursday, March 22, 2007
			Description:  	This object includes methods to manipulate "floating windows"
		*/

		iframeBox : {
		
			/*
				Object: 		iframeBox
				Author:		Ken Scott
				Creation Date: 	Wednesday, March 21, 2007
				Last Update: 	Wednesday, March 21, 2007
				Description:  	This object includes methods related to the file operations
			*/
		
			exists : false,
		
			open : function(color, width, height, zindex, url, expand) {

				/*
					Method: 		open
					Arguments: 		none
					Author: 		Ken Scott
					Creation Date: 	Thursday, March 22, 2007
					Last Update: 	Thursday, March 22, 2007
					Description: 	"Opens" an iframeBox
				*/
					
				if (kstools.window.iframeBox.exists) {
					alert('An iframeBox already exists.');
				} else {
					
					var iframeBacker = document.createElement('iframe');
					iframeBacker.style.zIndex = zindex - 1; 
					iframeBacker.style.height = height + 5;
					iframeBacker.style.width = width + 5;
					iframeBacker.style.top = Math.round((kstools.browser.getInnerHeight() - height) / 2);
					iframeBacker.style.left = Math.round((kstools.browser.getInnerWidth() - width) / 2);
					iframeBacker.style.position = 'absolute';
					iframeBacker.id = 'iframeBacker';
					
					var iframeBoxWindow = document.createElement('div');
					iframeBoxWindow.style.zIndex = zindex; 
					iframeBoxWindow.style.height = height;
					iframeBoxWindow.style.width = width;
					iframeBoxWindow.style.backgroundRepeat = 'repeat'; 
					iframeBoxWindow.style.position = 'absolute';
					iframeBoxWindow.style.top = Math.round((kstools.browser.getInnerHeight() - height) / 2);
					iframeBoxWindow.style.left = Math.round((kstools.browser.getInnerWidth() - width) / 2);
					iframeBoxWindow.style.display = 'none';
					iframeBoxWindow.style.backgroundColor = color;
					iframeBoxWindow.style.border = '5px solid ' + color;
					iframeBoxWindow.id = 'iframeBoxWindow';
			
					if (navigator.appName == 'Microsoft Internet Explorer') {
						var iframeHeight = height - 5; 
						var iframeWidth = width - 5; 
					} else {
						var iframeHeight = height; 
						var iframeWidth = width; 
					}

					var htmlData = '<center><div style="float: right; height=18px;">';
					if ( expand ) { htmlData += '<a href="#" style="text-decoration: none; color: black; background-color: lightgrey; font-family: arial; font-size: 10px;" onClick="javascript: kstools.window.iframeBox.expand();">&nbsp;EXPAND&nbsp;</a>&nbsp;'; }
					htmlData += '<a href="#" style="text-decoration: none; color: black; background-color: lightgrey; font-family: arial; font-size: 10px;" onClick="javascript: kstools.window.iframeBox.close();">&nbsp;CLOSE&nbsp;</a>';
					htmlData += '</div>';
					htmlData += '<iframe id="iframeBoxWindowIframe" frameborder=0 bgcolor="white" src="' + url + '" width="' + iframeWidth + 'px" height="' + iframeHeight + 'px"></iframe></center>';
					iframeBoxWindow.innerHTML = htmlData;
					
					var bodyRef = document.getElementsByTagName("body").item(0);
					kstools.document.dimScreen(zindex - 2, 25, color);
					bodyRef.appendChild(iframeBacker);
					document.getElementById('iframeBacker').style.display = 'block';
					bodyRef.appendChild(iframeBoxWindow);
					document.getElementById('iframeBoxWindow').style.display = 'block';
					kstools.window.iframeBox.exists = true;
				}
			
			}, 
			
			close : function() {
				
				/*
					Method: 		close
					Arguments: 		none
					Author: 		Ken Scott
					Creation Date: 	Thursday, March 22, 2007
					Last Update: 	Thursday, March 22, 2007
					Description: 	"Closes" the open iframeBox
				*/
					
				if (kstools.window.iframeBox.exists) {
					document.getElementById('iframeBoxWindow').style.display = 'none';
					document.getElementById('iframeBacker').style.display = 'none';
					var bodyRef = document.getElementsByTagName("body").item(0);
					bodyRef.removeChild(document.getElementById('iframeBoxWindow'));
					bodyRef.removeChild(document.getElementById('iframeBacker'));
					kstools.window.iframeBox.exists = false;
					kstools.document.brightenScreen();
				} else {
					alert('No iframeBox exists.');
				}
				
			},
			
			expand : function() {
				
				/*
					Method: 		expand
					Arguments: 		none
					Author: 		Ken Scott
					Creation Date: 	Friday, May 4, 2007
					Last Update: 	Friday, May 4, 2007
					Description: 	"Closes" the open iframeBox and opens the URL in a new window
				*/
				
				var url = document.getElementById('iframeBoxWindowIframe').src;
				kstools.window.iframeBox.close();
				window.open(url);
					
			}

		}
		
	}
	
/**********************************************/
		
}



