/**
 * JavaScript Library
 *
 * @author		Peter Chapman
 * @version		1.0.08.11.21
 *
 * These are generic JavaScript Functions
 *
 * Functions: (bracketed parameters are optional)
 *  - popupWindow(URL, Width, Height) - Displays a pop-up window
 *  - updateDate(Widget ID, (Date)) - Date Selection Widget Logic
 *  - printCurrentPage(ID, CSS File) - Prints the contents of the specified element id, using the specified stylesheet
 *  - urldecode(String) - JavaScript version of PHP's urldecode
 *  - urlencode(String) - JavaScript version of PHP's urlencode
 */
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 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;
}
