//function to call the calendar "object" as a dialog.
//	sReturn - is the selected date.
//		format dd/mm/yyyy
//	sReturn == null -> no date selected, cancel, close window, esc, etc.
//
//	ControlName - is the control where the date will be assigned
//
//	Calendar.htm - is the html page that will be opened into the dialod and contain the calendar js
function modalWin(url) {
if (window.showModalDialog) {
window.showModalDialog(url,"name",
"dialogWidth:190px;dialogHeight:200px;help:no;status:no");
} else {
window.open(url,'name',
'height=190,width=200,toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no ,modal=yes');
}
} 

function ShowCalendar(ControlName)
{
	var sFeatures;
	
	//var oDate=new Date();
	var oDate = new Date(document.getElementById(ControlName).value);
	var nThisMonth=oDate.getMonth()
	var nThisYear=oDate.getFullYear()
	var nThisDate=oDate.getDate()
	
	var today = new Date()
	
	if (! isDate(document.getElementById(ControlName).value)) {
		nThisMonth = today.getMonth();
		nThisYear = today.getFullYear();
		nThisDate = today.getDate();
	}
	//alert(nThisMonth + "/" + nThisDate + "/" + nThisYear);
		

    if (window.showModalDialog) {
        var sReturn= window.showModalDialog("assets/js/Calendar.htm?m=" + nThisMonth + "&d=" + nThisDate + "&y=" + nThisYear,"name",
    "dialogWidth:190px;dialogHeight:200px;help:no;status:no");
    } else {
        var sReturn= window.open("assets/js/Calendar.htm?m=" + nThisMonth + "&d=" + nThisDate + "&y=" + nThisYear,'name',
    'height=190,width=200,toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no ,modal=yes');
    }

	if (sReturn != null)
	{
		arrDate = sReturn.split("/");
		sReturn = arrDate[0] + "/" + arrDate[1] + "/" + arrDate[2];
		
		document.getElementById(ControlName).value=sReturn
	}		
}

function isDate(dateStr) {

	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
	var matchArray = dateStr.match(datePat); // is the format ok?

	if (matchArray == null) {
		//alert("Please enter date as either mm/dd/yyyy or mm-dd-yyyy.");
		return false;
	}

	month = matchArray[1]; // p@rse date into variables
	day = matchArray[3];
	year = matchArray[5];

	if (month < 1 || month > 12) { // check month range
		//alert("Month must be between 1 and 12.");
		return false;
	}

	if (day < 1 || day > 31) {
		//alert("Day must be between 1 and 31.");
		return false;
	}

	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		//alert("Month "+month+" doesn`t have 31 days!")
		return false;
	}

	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap)) {
			//alert("February " + year + " doesn`t have " + day + " days!");
			return false;
		}
	}
	return true; // date is valid
}

