/***********************************************************************************************************
* date.js
* 
* ICT Services, Web Transactional Team
*
* Common Javascript validation to be used in conjuction with webtrans.css
*
***********************************************************************************************************/

// toDate accepts an element and converts it's value to dd/mm/yyyy format, returns true if successful or false otherwise
function wt_toDate(theElement)
{
	var CorrectDate;
	var SplitDate;
	var thisDay, strDay;
	var thisMonth;
	var thisMonthN
	var inpDate = theElement.value;	
	var thisYear;
	var DayArray   = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	var MonthArray = new Array("JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC");
	var CockUp     = 0;
	var today      = new Date();

	// kill if nothing there
	if (inpDate.length == 0 ) return 0;
	
	if (inpDate.indexOf("day") != -1) inpDate = inpDate.substr(inpDate.length-10);

	// numerical dates first for DDMMYYYY OR DDMMYY OR DDMM
	if (/^[0-9]{3}$/.test(inpDate.substr(1,3))) 
	{
		if (/^[0-9]{8}/.test(inpDate)) // ddmmyyyy
		{
			thisDay = inpDate.substr(0,2);
			thisMonth = inpDate.substr(2,2);
			thisYear = inpDate.substr(4,4);
		} 
		else if (/^[0-9]{6}/.test(inpDate)) // ddmmyy
		{
			thisDay = inpDate.substr(0,2);
			thisMonth = inpDate.substr(2,2);
			thisYear = inpDate.substr(4,2);
		}
		else if (/^[0-9]{4}/.test(inpDate)) // ddmm
		{
			thisDay = inpDate.substr(0,2);
			thisMonth = inpDate.substr(2,2);
			thisYear = new String(today.getFullYear());
		}
		else 
		{
			CockUp = 1;
		}
	}

	// now alpha numeric dates for DDMONYYYY, DMONYYYY, DDMONYY, DMONYY, DDMONY, DMONY or MONYY	
    else if (/^[a-zA-Z|0-9]{3}$/.test(inpDate.substr(1,3))) 
	{

		if (/^[0-9|a-zA-Z]{9}/.test(inpDate)) {
			thisDay   = inpDate.substr(0,2);
			thisMonth = inpDate.substr(2,3).toUpperCase();
			thisYear  = inpDate.substr(5,4);
		} else if (/^[0-9|a-zA-Z]{8}/.test(inpDate)) {
			thisDay   = inpDate.substr(0,1);
			thisMonth = inpDate.substr(1,3).toUpperCase();
			thisYear  = inpDate.substr(4,4);
		} else if (/^[0-9|a-zA-Z]{7}/.test(inpDate)) {
			thisDay   = inpDate.substr(0,2);
			thisMonth = inpDate.substr(2,3).toUpperCase();
			thisYear  = inpDate.substr(5,2);
		} else if (/^[0-9|a-zA-Z]{6}/.test(inpDate)) {
			if (/^[a-zA-Z]{1}/.test(inpDate.substr(1,1))) {
				thisDay = inpDate.substr(0,1);
				thisMonth = inpDate.substr(1,3).toUpperCase();
				thisYear = inpDate.substr(4,2);
			} else {
				thisDay = inpDate.substr(0,2);
				thisMonth = inpDate.substr(2,3).toUpperCase();
				thisYear  = inpDate.substr(3,2);
			}
		} else {
			CockUp = 2;
		}
    }
	else     // lastly pick up every conceivable special character or kick out with error
	{
		if      (/-/.test(inpDate))  { SplitDate = inpDate.split("-"); }
		else if (/\//.test(inpDate)) { SplitDate = inpDate.split("/"); }
		else if (/\./.test(inpDate)) { SplitDate = inpDate.split("."); }
		else if (/,/.test(inpDate))  { SplitDate = inpDate.split(","); }
		else if (/\;/.test(inpDate)) { SplitDate = inpDate.split(";"); }
		else if (/\:/.test(inpDate)) { SplitDate = inpDate.split(":"); }
		else if (/\*/.test(inpDate)) { SplitDate = inpDate.split("*"); }
		else if (/_/.test(inpDate))  { SplitDate = inpDate.split("_"); }
		else if (/%/.test(inpDate))  { SplitDate = inpDate.split("%"); }
		else if (/ /.test(inpDate))  { SplitDate = inpDate.split(" "); }
		else                         { SplitDate = false; }
		
		if (SplitDate) 
		{
			thisDay     = SplitDate[0];
			thisMonth   = SplitDate[1].toUpperCase();
			thisYear    = SplitDate[2];			
		}
		else 
		{		
			CockUp = 3;			
		}
    }

	// request reinput data if nonexistent	
	if (!thisYear)  { thisYear = "" + today.getFullYear(); }
	if (!thisMonth) { CockUp = 4; }
	if (!thisDay)   { CockUp = 5; }

	// kick out any errors before we do any formatting etc
	if (CockUp != 0) 
	{
//		alert(CockUp);
		return -1;
	}

	// change from numeric to alpha-numeric
	if (/^[0-9]{1}$/.test(thisMonth.substr(0,1))) 
	{
		thisMonthN = thisMonth - 1;
	} 
	else 
	{
		switch (thisMonth)
		{
			case "JAN" : thisMonthN = '0'; break;
			case "FEB" : thisMonthN = '1'; break;
			case "MAR" : thisMonthN = '2'; break;
			case "APR" : thisMonthN = '3'; break;
			case "MAY" : thisMonthN = '4'; break;
			case "JUN" : thisMonthN = '5'; break;
			case "JUL" : thisMonthN = '6'; break;
			case "AUG" : thisMonthN = '7'; break;
			case "SEP" : thisMonthN = '8'; break;
			case "OCT" : thisMonthN = '9'; break;
			case "NOV" : thisMonthN = '10'; break;
			case "DEC" : thisMonthN = '11'; break;
			default    : return false;
		}
		thisMonth = new String(new Number(thisMonthN) + 1);
	}

	// sort variable lengths out and build date
	if (thisDay.length  == 1) { thisDay = "0" + thisDay; }

	if (thisMonth.length == 1){ thisMonth = "0" + thisMonth;}
	if (thisYear.length == 1) { thisYear = ("" + today.getFullYear()).substr(0, 3) + thisYear; }
	if (thisYear.length == 2) 
	{
	   i = thisYear.substr(0,2);
	   if  (i < 50) 
	   {
	   	   thisYear = (""+today.getFullYear()).substr(0,2) + thisYear;
	   } 
	   else 
	   {
   	   	   thisYear = (""+today.getFullYear()).substr(0,2) + thisYear;
		   thisYear -= 100;
	   } 
	}

	var testDate = new Date(new Number(thisYear), new Number(thisMonthN), new Number(thisDay));
	if ((thisDay == testDate.getDate()) & (thisMonthN == testDate.getMonth()) & (thisYear == testDate.getFullYear()))
	{
		theElement.value = thisDay + "/" + thisMonth + "/" + thisYear;
		return 1;
	}
	return -1;
}

// isDate - accepts an element and returns true if the value is a valid date (dd/mm/yyyy) or false otherwise
function wt_isDate(theElement)
{
	var str = theElement.value;
	
	// check date format
	var regex = /^([0-2][0-9]|3[0-1])\/(0[0-9]|1[0-2])\/[1-3][0-9]{3}$/;
	if  (!regex.test(str)) return false;

	// create javascript date
	var a = str.split("/");
	var d = new Number(a[0]);
	var m = new Number(a[1] - 1);  // javascript stores months as 0 - 11
	var y = new Number(a[2]);
	var da = new Date(y, m, d);
	
	// check date exists
	if (da.getDate() != d || da.getMonth() != m || da.getYear() != y) return false;
	
	// all gone ok
	return true;
}

// isTime - accepts an element and returns true if it is a valid time (hh24:mi) or false otherwise
function wt_isTime(theElement)
{
	if (/^$/.test(theElement.value)) return 0;
	if (!(/^([0-1][0-9]|2[0-3]):[0-5][0-9]$/).test(theElement.value)) return -1;
	return 1;
}

// toTime - accepts an element and converts it's value to hh24:mi format, returns true if successful or false otherwise
function wt_toTime(theElement)
{;
	var str = theElement.value;
	var SplitTime, hr, mn;
	
	if (/^$/.test(theElement.value)) return 0;	
	
	// test correct format - hh24:mi
	if (/^([0-1][0-9]|2[0-3]):[0-5][0-9]$/.test(str)) return 1;
	
	// test missing colon - hh24mi
	if (/^([0-1][0-9]|2[0-3])[0-5][0-9]$/.test(str)) 
	{
		theElement.value = str.substr(0,2) + ":" + str.substr(2,2);
		return 1;
	}

	// test single digit
	if (/^[0-9]$/.test(str)) 
	{
		theElement.value = "0" + str + ":00";
		return 1;
	}

	// test missing minutes - hh24? or hh24
	if (/^(([0-1][0-9])|(2[0-3]))(:|.)?$/.test(str))
	{
		theElement.value = str.substr(0,2) + ":00";
		return 1;
	}

	if      (/-/.test(str))  { SplitTime = str.split("-"); }
	else if (/\//.test(str)) { SplitTime = str.split("/"); }
	else if (/\./.test(str)) { SplitTime = str.split("."); }
	else if (/,/.test(str))  { SplitTime = str.split(","); }
	else if (/\;/.test(str)) { SplitTime = str.split(";"); }
	else if (/\:/.test(str)) { SplitTime = str.split(":"); }
	else if (/\*/.test(str)) { SplitTime = str.split("*"); }
	else if (/_/.test(str))  { SplitTime = str.split("_"); }
	else if (/%/.test(str))  { SplitTime = str.split("%"); }
	else if (/ /.test(str))  { SplitTime = str.split(" "); }
	else                     { SplitTime = false; }		

	if (SplitTime) 
	{
		hr = SplitTime[0];
		mn = SplitTime[1];
		if (hr.length < 2) hr = "0" + hr;
		if (mn.length < 2) mn = "0" + mn;			
		str = hr + ":" + mn
		if (/^([0-1][0-9]|2[0-3]):[0-5][0-9]$/.test(str))
		{
			theElement.value = str;
			return 1;
		} 
	}
	return -1;
}
