/* ====================================================
//
// Client side date script utilities.
//
=====================================================*/
// Page version: v1.24, released 20 October 2003

function date_onmousewheel(x) {
	if (event.srcElement.id == document.activeElement.getAttribute('id')) {
		if (ValidDate(x.value)) { 
		
			x.value = configureDate(x.value);
			
			if (event.wheelDelta >= 120) {
				x.value = addDays(x.value, 1);
			} else if (event.wheelDelta <= -120) {
				x.value = addDays(x.value, -1);
			}
		}
		return false;
	}
}

function dateDiff(interval, date1, date2) {

	if (typeof(date1) == "string") {
		date1 = new Date(date1);
	}
	if (typeof(date2) == "string") {
		date2 = new Date(date2);
	}

	switch (interval) {
		case "m":
			/*date1.setDate(0);
			date1.setHours(0); 
			date1.setMinutes(0);
			date1.setSeconds(0);
			date1.setMilliseconds(0);

			date2.setDate(0);
			date2.setHours(0); 
			date2.setMinutes(0);
			date2.setSeconds(0);
			date2.setMilliseconds(0);

			var months = (date2.valueOf() - date1.valueOf()) / (86400000 * 28);
			return parseInt(format(months, 0));*/
			
			return (date2.getMonth() - date1.getMonth()) + (date2.getFullYear() - date1.getFullYear()) * 12;
			break;

		case "d":
			date1.setHours(0); 
			date1.setMinutes(0);
			date1.setSeconds(0);
			date1.setMilliseconds(0);

			date2.setHours(0); 
			date2.setMinutes(0);
			date2.setSeconds(0);
			date2.setMilliseconds(0);

			var days = (date2.valueOf() - date1.valueOf()) / 86400000;
			return parseInt(format(days, 0));
			break;
			
		case "h":
			date1.setMinutes(0);
			date1.setSeconds(0);
			date1.setMilliseconds(0);

			date2.setMinutes(0);
			date2.setSeconds(0);
			date2.setMilliseconds(0);

			return (date2.valueOf() - date1.valueOf()) / 3600000;
			break;
			
		case "n":
			date1.setSeconds(0);
			date1.setMilliseconds(0);

			date2.setSeconds(0);
			date2.setMilliseconds(0);
						
			return (date2.valueOf() - date1.valueOf()) / 60000;
			break;
			
		case "s":
			
			date1.setMilliseconds(0);
			
			date2.setMilliseconds(0);
			
			return (date2.valueOf() - date1.valueOf()) / 1000;
			break;
	}
}

//Pass the formatDate function either a date or time.  Currently will not accept a date-time field.
//Expects the date or time to already be validated.
function formatDate(varDate, strFormat) {
	var strNewDate = "";
	var strSubFormat;
	
	if (varDate == null || varDate.length == 0) {
		return varDate;
	}

	strSubFormat = strFormat.substr(0, 1);
	for (var i = 1; i < strFormat.length; i++) {
		if (strFormat.substr(i, 1) != strFormat.substr(i - 1, 1)) {
			strNewDate += getDatePart(varDate, strSubFormat);
			strSubFormat = strFormat.substr(i, 1);
		}else{
			strSubFormat += strFormat.substr(i, 1);
		}
	}
	strNewDate += getDatePart(varDate, strSubFormat);
	
	return strNewDate;
}

function getDatePart(varDate, strFormat) {
	var x;

	switch (strFormat) {
		case "h":
			return parseInt(getHours(varDate), 10).toString();
			break;
		case "hh":
			x = parseInt(getHours(varDate), 10);
			if (x < 10)
				return "0" + x.toString();
			else
				return x.toString();
			break;
		case "n":
			return parseInt(getMinutes(varDate), 10).toString();
			break;
		case "nn":
			x = parseInt(getMinutes(varDate), 10);
			if (x < 10)
				return "0" + x.toString();
			else
				return x.toString();
			break;
		case "s":
			return parseInt(getSeconds(varDate), 10).toString();
			break;
		case "ss":
			x = parseInt(getSeconds(varDate), 10);
			if (x < 10)
				return "0" + x.toString();
			else
				return x.toString();
			break;
		case "d":
			return parseInt(getDate(varDate), 10).toString();
			break;
		case "dd":
			x = parseInt(getDate(varDate), 10);
			if (x < 10)
				return "0" + x.toString();
			else
				return x.toString();
			break;
		case "ddd":
			return getDayName(getDay(varDate), true);
			break;
		case "dddd":
			return getDayName(getDay(varDate), false);
			break;
		case "m":
			return parseInt(getMonth(varDate), 10).toString();
			break;
		case "mm":
			x = parseInt(getMonth(varDate), 10);
			if (x < 10)
				return "0" + x.toString();
			else
				return x.toString();
			break;
		case "mmm":
			return getMonthName(getMonth(varDate), true);
			break;
		case "mmmm":
			return getMonthName(getMonth(varDate), false);
			break;
		case "yy":
			return getYear(varDate).toString().substr(2, 2);
			break;
		case "yyyy":
			return getYear(varDate);
			break;
		case "w":
			return getDay(varDate);
			break;
		case "ww":
			return getWeek(varDate);
			break;
		default:
			return strFormat;
	}
}

function addDays(varDate, daysToAdd) {
	var dt = new Date(varDate);
	dt.setDate(dt.getDate() + daysToAdd);
	return dt.getDate() + " " + getMonthName(dt.getMonth() + 1) + " " + dt.getFullYear();
}

function addMonths(varDate, monthsToAdd) {
	var dt = new Date(varDate);
	dt.setMonth(dt.getMonth() + monthsToAdd);
	return dt.getDate() + " " + getMonthName(dt.getMonth() + 1) + " " + dt.getFullYear();
}

function addYears(varDate, yearsToAdd) {
	var dt = new Date(varDate);
	dt.setYear(dt.getFullYear() + yearsToAdd);
	return dt.getDate() + " " + getMonthName(dt.getMonth() + 1) + " " + dt.getFullYear();
}

function month_onblur(x, blnAbbreviate) {
   
	if(x.value.length == 0) {
		return false;
	}

	if(isNaN(x.value)) {
	
		if(x.value.length >= 3) {
			// check the text entered to see if valid month
			var month = getMonthNumber(x.value);
			
			if(month != -1) {
				x.value = getMonthName(month, blnAbbreviate);
				return true;
			}
		}
	} else {
		// check to see if number entered is a valid month and return the valid month name
		var month = parseInt(x.value);
		
		if(month >= 1 && month <= 12){
			x.value = getMonthName(month, blnAbbreviate);
			return true;
		}   
	}

	alert("Invalid month entered.\n\nYou can enter the month in any of the following formats;\n\n1 to 12\njan to dec\njanuary to december.");
	x.focus();
	x.select();
	return false;
}

function date_onblur(x, strFormat) {
	if (x.value != "") {
		if (typeof(strFormat) == "undefined")
			strFormat = "d mmmm yyyy";

		if (x.value.toUpperCase() == "T") {
			x.value = formatDate(getCurrentDate(), strFormat);
			return true;
		}else if (x.value.toUpperCase() == "Y") {
			x.value = formatDate(addDays(getCurrentDate(), -1), strFormat);
			return true;
		}else if (ValidDate(x.value)) {
			x.value = formatDate(configureDate(x.value), strFormat);
			return true;
		}else{
			alert("Invalid Date");
			x.focus();
			x.select();
		}
	}
	return false;
}

function time_onblur(x, strFormat) {
	if (x.value != "") {
		if (typeof(strFormat) == "undefined")
			strFormat = "hh:nn:ss";
			
		if (x.value.toUpperCase() == "N" || x.value.toUpperCase() == "T") {
			x.value = formatDate(getCurrentTime(), strFormat);
			return true;
		}else if (ValidTime(x.value)) {
			x.value = formatDate(myTime(x.value), strFormat);
			return true;
		}else{
			alert("Invalid Time");
			x.focus();
			x.select();
		}
	}
	return false;
}

function shorttime_onblur(x) {
	return time_onblur(x, "hh:nn");
}

function datetime_onblur(x) {
	if(x.value != ""){
	
		//get date part.
		var datebreak = x.value.lastIndexOf(' ');
		
		if(datebreak == -1)
			datebreak = x.value.length;
				
		var datepart = x.value.slice(0,datebreak);
		var timepart = x.value.slice(datebreak + 1);
		
		if(datepart.length > 0 && timepart.length==0)
			timepart = getCurrentTime();
			
		if(ValidDate(datepart)){
			
			if(ValidTime(timepart)){
				x.value = myDate(datepart) + " " + myTime(timepart);				
				return true;				
			}
			else{
				alert("Invalid Time");
				x.focus();
				x.select();
			}			
			
		}else{
			alert("Invalid Date");
			x.focus();
			x.select();
		}

	}
	
	return false;
}

function getCurrentDateTime(){
	var dt = new Date();
	return dt.getDate() + " " + getMonthName(dt.getMonth() + 1) + " " + dt.getFullYear() + " " + myTime(dt.getHours() + ":" + dt.getMinutes() + ":" + dt.getSeconds());
}

function getCurrentDate(){
	var dt = new Date();
	return dt.getDate() + " " + getMonthName(dt.getMonth() + 1) + " " + dt.getFullYear();
}

function getCurrentTime(){
	var dt = new Date();
	return myTime(dt.getHours() + ":" + dt.getMinutes() + ":" + dt.getSeconds());
}

function myLongDate(varDate){
	if(varDate == "null" || varDate == null || varDate == "")
		return varDate;
		
	varDate = configureDate(varDate);
	
	return getDayName(getDay(varDate)) + ", " + getDate(varDate) + " " + getMonthName(getMonth(varDate)) + " " + getYear(varDate);
}

function myDate(varDate){
	if(varDate == "null" || varDate == null || varDate == "")
		return varDate;
		
	varDate = configureDate(varDate);
	
	return getDate(varDate) + " " + getMonthName(getMonth(varDate)) + " " + getYear(varDate);
}

function myMedDate(varDate){
	if(varDate == "null" || varDate == null || varDate == "")
		return varDate;
		
	varDate = configureDate(varDate);
	
	return getDate(varDate) + " " + getMonthName(getMonth(varDate), true) + " " + getYear(varDate);
}

function myShortDate(varDate){
	if(varDate == "null" || varDate == null || varDate == "")
		return varDate;
		
	varDate = configureDate(varDate);
	
	return getDate(varDate) + "/" + getMonth(varDate) + "/" + getYear(varDate);
}

function myTime(varDate){
	var h, m, s;

	if(varDate == "null" || varDate == null || varDate == "")
		return varDate;
		
	varDate = configureTime(varDate);
	
	h = getHours(varDate);
	if (h.length < 2)
		h = "0" + h;

	m = getMinutes(varDate);
	if (m.length == 0)
		m = "00";
	else if (m.length < 2)
		m = "0" + m;

	s = getSeconds(varDate);
	if (s.length < 2)
		s = "0" + s;
	
	return h + ":" + m + ":" + s;
}

function myShortTime(varDate){
	var h, m, s;

	if(varDate == "null" || varDate == null || varDate == "")
		return varDate;

	varDate = configureTime(varDate);

	h = getHours(varDate);
	if (h.length < 2)
		h = "0" + h;

	m = getMinutes(varDate);
	if (m.length == 0)
		m = "00";
	else if (m.length < 2)
		m = "0" + m;
	
	return h + ":" + m;
}

function getWeekNo(varDate, blnAbbreviate){
    var y;
    var w;
    var m;
    var weekNo;
	
    varDate = configureDate(varDate);

    y = getYear(varDate);
    m = getMonth(varDate);
    w = getWeek(varDate);
    
    switch(w){
        case 1:
            if (m == 12)
				y = y + 1;
			break;
        case 52, 53:
            if (m == 1)
				y = y - 1;
			break;        
    }

    weekNo = (y * 100) + w;
    weekNo = weekNo.toString();

	if (blnAbbreviate)
		return weekNo.substring(4,6);
	else
		return weekNo;
}

function configureDate(varDate){

	varDate = varDate.replace(/[-\.\/ ]+/g," ");

	if (varDate.length == 6 && !isNaN(varDate))
			varDate = varDate.substring(0,2) + " " + varDate.substring(2,4) + " " + varDate.substring(4,6);
	else if (varDate.length == 4 && !isNaN(varDate))
			varDate = varDate.substring(0,2) + " " + varDate.substring(2,4);

	varDate = getDate(varDate) + " " + getMonthName(getMonth(varDate)) + " " + getYear(varDate);
	//next two lines convert 32 Jan to 1 Feb, etc.
	varDate = new Date(varDate);
	varDate = formatUTCDate(varDate);
	
	if (getYear(varDate) > 9995){
		return "";
	}
	else{
		return getDate(varDate) + " " + getMonthName(getMonth(varDate)) + " " + getYear(varDate);
	}
}

function configureTime(varDate){
	var blnPM = false;
	
	if (varDate.indexOf("pm") > -1 || varDate.indexOf("p.m") > -1 || varDate.indexOf("p m") > -1)
		blnPM = true;
		
	varDate = varDate.replace(/pm|p.m|p m|am|a.m|a m/g, "");
	varDate = varDate.replace(/[-\.\/ ]+/g, ":");

	if (isNaN(varDate.replace(/[:]+/g, "")))
		return "NaN";
	
	varDate = getHours(varDate) + ":" + getMinutes(varDate) + ":" + getSeconds(varDate);
	varDate = new Date("1 Jan 2000 " + varDate);
	
	if (blnPM && varDate.getHours() < 12)
		varDate.setHours(varDate.getHours() + 12);
	varDate = formatUTCDate(varDate);
	//subtract 1 January 2000 from the front to leave the time
	varDate = varDate.substr(15, varDate.length - 15);

	return getHours(varDate) + ":" + getMinutes(varDate) + ":" + getSeconds(varDate);
}

function getHours(varDate){	
	if (varDate.indexOf(":") > -1)
		return varDate.substring(0, varDate.indexOf(":"));
	else
		return varDate;
}

function getMinutes(varDate){
	var varRight;

	if (varDate.indexOf(":") > -1){	
		varRight = varDate.substring(varDate.indexOf(":") + 1, varDate.length);
		
		if (varRight.indexOf(":") > -1)
			varRight = varRight.substring(0, varRight.indexOf(":"));
		else
			varRight = varRight;
			
		if (varRight != "")	
			return varRight;
		else
			return "0";
	}else{
		return "0";
	}
}

function getSeconds(varDate){
	var varRight;

	if (varDate.indexOf(":") > -1){	
		varRight = varDate.substring(varDate.indexOf(":") + 1, varDate.length);

		if (varRight.indexOf(":") > -1){	
			varRight = varRight.substring(varRight.indexOf(":") + 1, varRight.length);

			if (varRight.indexOf(":") > -1 )
				return varRight.substring(0, varRight.indexOf(":"));
			else
				return varRight;
				
			if (varRight != "")	
				return varRight;
			else
				return "0";
		}else
			return "0";
	}else{
		return "0";
	}
}

// gets the day of the month
function getDate(varDate){
	if (varDate.indexOf(" ") > -1)
		return varDate.substring(0, varDate.indexOf(" "));
	else
		return varDate;
}

// gets the weekno based on the first four day week
function getWeek(varDate){
	var dayofWeek;
	var startofyear;
	var timedifference;
	var dayofyear;
	var weekofyear;
	var newDate;
	
	startofyear = new Date("1 January " + getYear(varDate));
	
	dayofWeek = startofyear.getDay()
	if (dayofWeek == 0)
		dayofWeek = 7;
	dayofWeek--;
	startofyear = startofyear.valueOf();
	
	newDate = new Date(varDate);
	newDate = newDate.valueOf();
	
	timedifference = newDate - startofyear;
	dayofyear = timedifference / 86400000 + 1;
	dayofyear = Math.floor(dayofyear);
	dayofyear += dayofWeek;
	weekofyear = dayofyear / 7;
	weekofyear = Math.ceil(weekofyear);
		
	if (dayofWeek >= 4){
		if (weekofyear == 1)
			weekofyear = getWeek("31 December " + (getYear(varDate) - 1));
		else
			weekofyear--;
	}	

	if (weekofyear == 53){
		startofyear = new Date("1 January " + (getYear(varDate) + 1));
		dayofWeek = startofyear.getDay();	
		if (dayofWeek == 0)
			dayofWeek = 7;
		dayofWeek--;

		if (dayofWeek < 4)
			weekofyear = 1;
	}
	
	return weekofyear;
}

// gets the month of the year 1-12
function getMonth(varDate){
	var varRight;
	var varMonth;

	if (varDate.indexOf(" ") > -1){	
		varRight = varDate.substring(varDate.indexOf(" ") + 1, varDate.length);

		if (varRight.indexOf(" ") > -1 )
			varRight = varRight.substring(0, varRight.indexOf(" "));
		else
			varRight = varRight;
		
		if (varRight != ""){	
			varMonth = parseInt(varRight, 10);
			
			if (isNaN(varMonth))
				return getMonthNumber(varRight);
			else
				return varMonth;
		}else{
			varDate = new Date();
			return varDate.getMonth() + 1;
		}
	}else{
		varDate = new Date();
		return varDate.getMonth() + 1;
	}
}

// gets the yyyy formatted year
function getYear(varDate){
	var varRight;
	var varYear;
	var varCurrentDate = new Date();
	var varCurrentYear;

	varRight = varDate.substring(varDate.indexOf(" ") + 1, varDate.length);

	if (varRight.indexOf(" ") > -1 ){
		varRight = varRight.substring(varRight.indexOf(" ") + 1, varRight.length);
		
		if (varRight.indexOf(" ") > -1 )
			varRight = varRight.substring(0, varRight.indexOf(" "));
		else
			varRight = varRight;
		
		if (varRight != ""){
			if (varRight.length <= 2){
				varYear = parseInt(varRight, 10);
				varCurrentYear = varCurrentDate.getFullYear().toString();
				varCurrentYear = parseInt(varCurrentYear.substr(2, 2), 10);
				
				if (varYear < (30 + varCurrentYear))
					return 2000 + varYear;
				else
					return 1900 + varYear;
			}else{
				varYear = parseInt(varRight, 10);
				return varYear;
			}
		}else{
			return varCurrentDate.getFullYear();
		}
	}else{
		return varCurrentDate.getFullYear();
	}
}

// validates a date
function ValidDate(varDate){
	varDate = configureDate(varDate);
	
	varDate = new Date(varDate);
	
	if (isNaN(varDate))
		return false;
	else
		return true;
}

// validates a time
function ValidTime(varDate){
	varDate = configureTime(varDate);
	
	varDate = new Date("1 Jan 2000 " + varDate);
	
	if (isNaN(varDate))
		return false;
	else
		return true;
}

function getMonthName(lngMonth, blnAbbreviate) {
	var strMonthNames;
	
	if (blnAbbreviate)
		strMonthNames = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
	else
		strMonthNames = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
	
	return strMonthNames[lngMonth - 1];
}


// returns the month of the year 1-12 based on the inputed month name
function getMonthNumber(varMonth){
	var strMonthNames;
	
	varMonth = varMonth.toLowerCase();
	
	if (varMonth.length == 3)
		strMonthNames = new Array("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec");
	else
		strMonthNames = new Array("january","february","march","april","may","june","july","august","september","october","november","december");
	
	for (var i = 0; i < strMonthNames.length; i++)
		if (varMonth == strMonthNames[i])
			return i + 1;
			
	return -1;
}

// formats a UTC formatted date, to a "dd monthname yyyy hh:nn:ss" formatted date.
function formatUTCDate(varDate) {
	var newDate;

	if(varDate == "null" || varDate == null || varDate == "")
		return varDate;

	varDate = new Date(varDate);

	newDate = varDate.getDate() + " ";
	newDate += getMonthName(varDate.getMonth() + 1) + " ";
	newDate += varDate.getFullYear() + " ";
	if (varDate.getHours() < 10)
		newDate += "0" + varDate.getHours() + ":";
	else
		newDate += varDate.getHours() + ":";
	if (varDate.getMinutes() < 10)
		newDate += "0" + varDate.getMinutes() + ":";
	else
		newDate += varDate.getMinutes() + ":";
	if (varDate.getSeconds() < 10)
		newDate += "0" + varDate.getSeconds();
	else
		newDate += varDate.getSeconds();
		
	return newDate;
}

// formats a UTC formatted date, to a "hh:nn:ss" formatted date.
function formatUTCTime(varDate) {
	var newDate;

	if(varDate == "null" || varDate == null || varDate == "")
		return varDate;

	varDate = new Date(varDate);

	if (varDate.getHours() < 10)
		newDate = "0" + varDate.getHours() + ":";
	else
		newDate = varDate.getHours() + ":";
	if (varDate.getMinutes() < 10)
		newDate += "0" + varDate.getMinutes() + ":";
	else
		newDate += varDate.getMinutes() + ":";
	if (varDate.getSeconds() < 10)
		newDate += "0" + varDate.getSeconds();
	else
		newDate += varDate.getSeconds();
		
	return newDate;
}

// returns the day of the week where Sunday = 0
function getDay(varDate) {
	var newDate = new Date(varDate);
	return newDate.getDay();
}

function getDayName(lngDay, blnAbbreviate) {
	var strDayNames;
	
	if (blnAbbreviate)
		strDayNames = new Array("Sun","Mon","Tue","Wed","Thr","Fri","Sat");
	else
		strDayNames = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
	
	return strDayNames[lngDay];
}
