// ===================================================================
// Author: Matt Kruse <matt@mattkruse.com>
// WWW: http://www.mattkruse.com/
//
// NOTICE: You may use this code for any purpose, commercial or
// private, without any further permission from the author. You may
// remove this notice from your final code if you wish, however it is
// appreciated by the author if at least my web site address is kept.
//
// You may *NOT* re-distribute this code in any way except through its
// use. That means, you can include it in your product, or your web
// site, or any other form where the code is actually being used. You
// may not put the plain javascript up on your site for download or
// include it in your javascript libraries for download. 
// If you wish to share this code with others, please just point them
// to the URL instead.
// Please DO NOT link directly to my .js files from your site. Copy
// the files to your server and use them there. Thank you.
// ===================================================================

// -------------------------------------------------------------------
// autoComplete (text_input, select_input, ["text"|"value"], [true|false])
//   Use this function when you have a SELECT box of values and a text
//   input box with a fill-in value. Often, onChange of the SELECT box
//   will fill in the selected value into the text input (working like
//   a Windows combo box). Using this function, typing into the text
//   box will auto-select the best match in the SELECT box and do
//   auto-complete in supported browsers.
//   Arguments:
//      field = text input field object
//      select = select list object containing valid values
//      property = either "text" or "value". This chooses which of the
//                 SELECT properties gets filled into the text box -
//                 the 'value' or 'text' of the selected option
//      forcematch = true or false. Set to 'true' to not allow any text
//                 in the text box that does not match an option. Only
//                 supported in IE (possible future Netscape).
// -------------------------------------------------------------------
function autoComplete (field, select, property, forcematch) {
	var found = false;
	for (var i = 0; i < select.options.length; i++) {
		if (select.options[i][property].toUpperCase().indexOf(field.value.toUpperCase()) == 0) {
			found=true; break;
		}
	}
	if (found) { 
		select.selectedIndex = i; 
	}
	else { 
		select.selectedIndex = -1; 
	}
	if (field.createTextRange) {
		if (forcematch && !found) {
			field.value=field.value.substring(0,field.value.length-1); 
			return;
		}
		var cursorKeys ="8;46;37;38;39;40;33;34;35;36;45;";
		if (cursorKeys.indexOf(event.keyCode+";") == -1) {
			var r1 = field.createTextRange();
			var oldValue = r1.text;
			var newValue = found ? select.options[i][property] : oldValue;
			if (newValue != field.value) {
				field.value = newValue;
				var rNew = field.createTextRange();
				rNew.moveStart('character', oldValue.length) ;
				rNew.select();
			}
		}
	}
}

function isDate(source, args){
	if(args.Value==""){
		args.IsValid = true;
	}
	else {
		var m_strDate = FormatDate(args.Value);
		if(m_strDate==""){
			args.IsValid = false;
		}
		else {
			var m_arrDate = m_strDate.split("/");
			var m_DAY = m_arrDate[0];
			var m_MONTH = m_arrDate[1];
			var m_YEAR = m_arrDate[2];
			if(m_YEAR.length != 4 ){
				args.IsValid = false;
			}
			else {
				m_strDate = m_DAY + "/" + m_MONTH + "/" + m_YEAR;
				var testDate=new Date(m_strDate);
				if(testDate.getMonth()+1==m_MONTH){
					var DateField = document.getElementById(source.controltovalidate);
					DateField.value = m_strDate;
					args.IsValid = true;
				} 
				else{
					args.IsValid =  false;
				}
			}
		}
	}
}  

function FormatDate(DateToFormat,FormatAs){
	if(DateToFormat==""){return"";}
	if(!FormatAs){FormatAs="dd/mm/yyyy";}
	var strReturnDate;
	FormatAs = FormatAs.toLowerCase();
	DateToFormat = DateToFormat.toLowerCase();
	var arrDate
	var arrMonths = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
	var strMONTH;
	var Separator;
	while(DateToFormat.indexOf("st")>-1){
		DateToFormat = DateToFormat.replace("st","");
	}
	while(DateToFormat.indexOf("nd")>-1){
		DateToFormat = DateToFormat.replace("nd","");
	}
	while(DateToFormat.indexOf("rd")>-1){
		DateToFormat = DateToFormat.replace("rd","");
	}
	while(DateToFormat.indexOf("th")>-1){
		DateToFormat = DateToFormat.replace("th","");
	}
	if(DateToFormat.indexOf(".")>-1){
		Separator = ".";
	}
	if(DateToFormat.indexOf("-")>-1){
		Separator = "-";
	}
	if(DateToFormat.indexOf("/")>-1){
		Separator = "/";
	}
	if(DateToFormat.indexOf(" ")>-1){
		Separator = " ";
	}
	arrDate = DateToFormat.split(Separator);
	DateToFormat = "";
	for(var iSD = 0;iSD < arrDate.length;iSD++){
		if(arrDate[iSD]!=""){
			DateToFormat += arrDate[iSD] + Separator;
		}
	}
	DateToFormat = DateToFormat.substring(0,DateToFormat.length-1);
	arrDate = DateToFormat.split(Separator);
	if(arrDate.length < 3){
		return "";
	}
	var DAY = arrDate[0];
	var MONTH = arrDate[1];
	var YEAR = arrDate[2];
	if(parseFloat(arrDate[1]) > 12){
		DAY = arrDate[1];
		MONTH = arrDate[0];
	}
	if(parseFloat(DAY) && DAY.toString().length==4){
		YEAR = arrDate[0];
		DAY = arrDate[2];
		MONTH = arrDate[1];
	}
	for(var iSD = 0;iSD < arrMonths.length;iSD++){
		var ShortMonth = arrMonths[iSD].substring(0,3).toLowerCase();
		var MonthPosition = DateToFormat.indexOf(ShortMonth);
		if(MonthPosition > -1){
			MONTH = iSD + 1;
			if(MonthPosition == 0){
				DAY = arrDate[1];
				YEAR = arrDate[2];
			}
			break;
		}
	}
	var strTemp = YEAR.toString();
	if(strTemp.length==2){
		if(parseFloat(YEAR)>40){
			YEAR = "19" + YEAR;
		}
		else{
			YEAR = "20" + YEAR;
		}
	}
	if(parseInt(MONTH)< 10 && MONTH.toString().length < 2){
		MONTH = "0" + MONTH;
	}
	if(parseInt(DAY)< 10 && DAY.toString().length < 2){
		DAY = "0" + DAY;
	}
	switch (FormatAs){
	case "dd/mm/yyyy":
		return DAY + "/" + MONTH + "/" + YEAR;
	case "mm/dd/yyyy":
		return MONTH + "/" + DAY + "/" + YEAR;
	case "dd/mmm/yyyy":
		return DAY + " " + arrMonths[MONTH -1].substring(0,3) + " " + YEAR;
	case "mmm/dd/yyyy":
		return arrMonths[MONTH -1].substring(0,3) + " " + DAY + " " + YEAR;
	case "dd/mmmm/yyyy":
		return DAY + " " + arrMonths[MONTH -1] + " " + YEAR;	
	case "mmmm/dd/yyyy":
		return arrMonths[MONTH -1] + " " + DAY + " " + YEAR;
	}
	return DAY + "/" + strMONTH + "/" + YEAR;;
}

function DisplayMessage(Msg){
	alert(Msg);
}

//Print only whats between DIV tags
var gAutoPrint = true; // Flag for whether or not to automatically call the print function
function printSpecial()
{
	if (document.getElementById != null)
	{
		var html = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n<html xmlns="http://www.w3.org/1999/xhtml">\n<HEAD>\n<link href="../styles/linkstyles.css" type="text/css" rel="stylesheet" />\n<script language="Javascript" src="../Common/Common.js"></script>\n<link href="../App_Themes/Default/StyleSheet.css" type="text/css" rel="stylesheet" />';

		if (document.getElementsByTagName != null)
		{
			var headTags = document.getElementsByTagName("head");
			if (headTags.length > 0)
				html += headTags[0].innerHTML;
		}
		
		html += '\n</HE' + 'AD>\n<BODY bgcolor="#ffffff" leftmargin="0" topmargin="0">\n<div>\n';
		
		var printReadyElem = document.getElementById("printReady");
		
		if (printReadyElem != null)
		{
				html += printReadyElem.innerHTML;
		}
		else
		{
			alert("Could not find the printReady section in the HTML");
			return;
		}
			
		html += '\n</div>\n</BO' + 'DY>\n</HT' + 'ML>';
		
		var printWin = window.open("","printSpecial");
		printWin.document.open();
		printWin.document.write(html);
		printWin.document.close();
		if (gAutoPrint)
			printWin.print();
	}
	else
	{
		alert("Sorry, the print ready feature is only available in modern browsers.");
	}
}