/**
 * JavaScript Library
 *
 * @author		Peter Chapman
 * @version		1.0.09.07.22
 *
 * These are generic JavaScript Functions
 *
 * Functions: (bracketed parameters are optional)
 *  - bookmarkSite(Title, URL) - Bookmarks the specified site
 *  - isEmailAddressValid(String) - A JavaScript implementation of lib_mail:isEmailAddressValid()
 *  - ltrim(String) - Trim spaces from the left of a string
 *  - popupWindow(URL, Width, Height) - Displays a pop-up window
 *  - printCurrentPage(ID, CSS File) - Prints the contents of the specified element id, using the specified stylesheet
 *  - rtrim(String) - Trim spaces from the right of a string
 *  - trim(String) - Trim spaces from both sides of a string
 *  - updateDate(Widget ID, (Date)) - Date Selection Widget Logic
 *  - urldecode(String) - JavaScript version of PHP's urldecode
 *  - urlencode(String) - JavaScript version of PHP's urlencode
 *  - highlightText(id) - Highlights the text in the input box that has the id that's passed.
 */
function bookmarkSite(title, url) {
	if (window.sidebar) { // firefox
        window.sidebar.addPanel(title, url, '');
	} else if (window.opera && window.print) { // opera
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	} else if (document.all) {
		window.external.AddFavorite(url, title);
	} else {
        alert('Press Ctrl + D to bookmark this site');
    }
}
function isEmailAddressValid(email) {
	return (email.indexOf("@") > 0 && email.indexOf(".", email.indexOf("@")) != -1 && email.indexOf(":") == -1 && email.indexOf(" ") == -1);
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^[\s|\xA0]+/, "");
}
function popupWindow(url, width, height) {
	var popup=window.open(url,"_blank","scrollbars=yes,width="+width+",height="+height+",top="+(screen.height/2 - height/2)+",left="+(screen.width/2 - width/2));
	if (popup) popup.focus();
}
function printCurrentPage(div, css) {
	var width	= 800;
	var height	= 650;
	var pp = window.open("", "_print_page", "scrollbars=yes,width=" + width + ",height=" + height + ",top=" + (screen.height/2 - height/2) + ",left=" + (screen.width/2 - width/2));
	pp.document.writeln("<html><head>");
	pp.document.writeln("<link rel='stylesheet' type='text/css' href='" + css + "'>");
	pp.document.writeln("<style type='text/css'> .noprint { display:none; } body { background-color: #ffffff; } </style>");
	pp.document.writeln("</head><body>");
	pp.document.writeln(document.getElementById(div).innerHTML);
	pp.document.writeln("</body></html>");
	pp.document.close();
	pp.print();
	pp.close();
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/[\s|\xA0]+$/, "");
}
function trim(stringToTrim) {
	return stringToTrim.replace(/^[\s|\xA0]+|[\s|\xA0]+$/g, "");
}
function updateDate(widget, date) {
	if (date) {
		date = date.split("-");
		if (date[1] > 12) date[1] = 12;
		if (date[2] > 31) date[2] = 31;
		for(var i=0;i<document.getElementById(widget + "_year").options.length;i++)document.getElementById(widget + "_year").options[i].selected=(document.getElementById(widget + "_year").options[i].value==date[0]);
		document.getElementById(widget + "_month").selectedIndex=parseInt(date[1]) - 1;
		document.getElementById(widget + "_day").selectedIndex=parseInt(date[2]) - 1;
	}
	if (document.getElementById(widget + "_year").value % 4 == 0 && (document.getElementById(widget + "_year").value % 100 != 0 || (document.getElementById(widget + "_year").value % 400 == 0)) && document.getElementById(widget + "_month").value == '02' && document.getElementById(widget + "_day").value >= '29') document.getElementById(widget + "_day").value = '29'; else if (document.getElementById(widget + "_month").value == '02' && document.getElementById(widget + "_day").value > '28') document.getElementById(widget + "_day").value = '28'; else if((document.getElementById(widget + "_month").value == '04' || document.getElementById(widget + "_month").value == '06' || document.getElementById(widget + "_month").value == '09' || document.getElementById(widget + "_month").value == '11') && document.getElementById(widget + "_day").value > '30') document.getElementById(widget + "_day").value = '30'; document.getElementById(widget).value = document.getElementById(widget + "_year").value + '-' + document.getElementById(widget + "_month").value + '-' + document.getElementById(widget + "_day").value;
}
function urldecode(encodedString) {
	var output = encodedString;
	// Turn all +'s into spaces
	var intIndexOfMatch = output.indexOf('+');
	// Loop over the string value replacing out each matching substring.
	while (intIndexOfMatch != -1){
		// Relace out the current instance.
		output = output.replace('+', ' ');
		// Get the index of any next matching substring.
		intIndexOfMatch = output.indexOf('+');
	}
	// Replace the fancy characters
	var binVal, thisString;
	var myregexp = /(%[^%]{2})/;
	while ((match = myregexp.exec(output)) != null && match.length > 1 && match[1] != '') {
		binVal = parseInt(match[1].substr(1),16);
		thisString = String.fromCharCode(binVal);
		output = output.replace(match[1], thisString);
	}
	return output;
}
function urlencode(clearString) {
	var output = '';
	var x = 0;
	clearString = clearString.toString();
	var regex = /(^[a-zA-Z0-9_.]*)/;
	while (x < clearString.length) {
		var match = regex.exec(clearString.substr(x));
		if (match != null && match.length > 1 && match[1] != '') {
			output += match[1];
			x += match[1].length;
		} else {
			if (clearString[x] == ' ') {
				output += '+';
			} else {
				var charCode = clearString.charCodeAt(x);
				var hexVal = charCode.toString(16);
				output += '%' + (hexVal.length < 2 ? '0' : '') + hexVal.toUpperCase();
			}
			x++;
		}
	}
	return output;
}
function highlightText(id) {
	document.getElementById(id).focus();
	document.getElementById(id).select();
}
function wordcount(input, output) {
	var words = input.split(/\s/);
	var ele = document.getElementById(output).value = words.length;
}
function numbersOnly(evt) {
	evt = (evt) ? evt : window.event
	var charCode = (evt.which) ? evt.which : evt.keyCode
	if (charCode > 31 && (charCode < 48 || charCode > 57)) {
		return false
	}
	return true
}

