// Basic data validation
// Created by Mehran Ahmad
// Version 2.5
// December 29, 2005

// verify's that the text is in dd/mm/yyyy format
function verifyUNDateFormat(txt)
{
	var regexp = new RegExp("^[0-9]{1,2}/[0-9]{1,2}/[0-9]{4}$");
	if(txt != txt.match(regexp)) return false;

	var testDate = new Date('1/2/1982');
	var monthPos = testDate.getMonth();

	var a_Date = txt.split("/");
	var monthVal = a_Date[1];
	if(monthPos == 0)
	{
		if(a_Date[1] > 31 || a_Date[1] < 1) return false;
		testDate = new Date("" + a_Date[1] + "/" + a_Date[0] + "/" + a_Date[2]);
	}
	else
	{
		if(a_Date[0] > 31 || a_Date[0] < 1) return false;
		testDate = new Date("" + a_Date[0] + "/" + a_Date[1] + "/" + a_Date[2]);
	}

	if(testDate.getMonth()+1 != monthVal) return false;
	
	return true;
}

// verify's time format hh:mm
function verifyTimeFormat(txt)
{
	var regexp = new RegExp("^[0-9]{1,2}:[0-9]{2}$");
	if (txt != txt.match(regexp))
		return false;

	var a_Time = txt.split(":");
	if(a_Time[0] > 12 || a_Time[0] < 1) return false;
	if(a_Time[1] > 59) return false;
	
	return true;
}

// verify's field contains numbers only
function verifyNumbersOnly(txt)
{
	var regexp = new RegExp(/^\d+$/);
	return (txt == txt.match(regexp));
}

// Strips HTML and returns text.  Also trims the text afterwards
function stripHTML(txt)
{
	var tempString = "";
	tempString = txt.replace(/(<([^>]+)>)/ig,""); 
	tempString = tempString.replace(/^\s*|\s*$/g,"");

	return tempString;
}

// Function TrimText
function trimText(txt)
{
	return txt.replace(/^\s*|\s*$/g,"");
}

// Compares two dates (dates ONLY. not times):
// Returns: 0 = match, -1 = first date < second date, 1 = first date > second date
function compareUNDates(dt1, dt2)
{
	var testDate1 = new Date('1/2/1982');
	var testDate2;
	var monthPos = testDate1.getMonth();
	
	var a_Date;
	if(monthPos == 0)
	{
		a_Date = dt1.split("/");
		testDate1 = new Date("" + a_Date[1] + "/" + a_Date[0] + "/" + a_Date[2]);
		a_Date = dt2.split("/");
		testDate2 = new Date("" + a_Date[1] + "/" + a_Date[0] + "/" + a_Date[2]);
	}
	else
	{
		a_Date = dt1.split("/");
		testDate1 = new Date("" + a_Date[0] + "/" + a_Date[1] + "/" + a_Date[2]);
		a_Date = dt2.split("/");
		testDate2 = new Date("" + a_Date[0] + "/" + a_Date[1] + "/" + a_Date[2]);
	}

	testDate2.setHours(testDate1.getHours());
	testDate2.setMinutes(testDate1.getMinutes());
	testDate2.setSeconds(testDate1.getSeconds());
	testDate2.setMilliseconds(testDate1.getMilliseconds());
	
	if (testDate1 < testDate2)
		return -1;
	else if(testDate1 > testDate2)
		return 1;
	else
		return 0;
}


// returns a julian date based on year, month and day
function julDate(y, m, d)
{
   var uh = 12;
   var um = 0;
   var us = 0;

   var extra = 100.0*y + m - 190002.5;
   var rjd = 367.0*y;

   rjd -= Math.floor(7.0*(y+Math.floor((m+9.0)/12.0))/4.0);
   rjd += Math.floor(275.0*m/9.0) ;
   rjd += d;
   rjd += (uh + (um + us/60.0)/60.)/24.0;
   rjd += 1721013.5;
   rjd -= 0.5*extra/Math.abs(extra);
   rjd += 0.5;

   return rjd
}

// Adds/Subtracts a given number of days (numDays) from myDate (date type: Date)
// and returns a Date.  If you want to subtracts days, specify numDays as a negative
// value
function offsetDays(myDate, numDays)
{
	return new Date(myDate.getTime() + numDays*24*60*60*1000);
}
