/*
email : 
isblank : 
isdate : 
noquotes : 
objtrim : 
strtrim : 
*/


/* Function to check valid email : requires strim as parameter, not the object
Last Modify: 30/05/01
*/
function email(strval)
{

 if (typeof(strval) == "object")
 {
    objtrim(strval);
	emailStr = strval.value;
 }
 else
 {
    emailStr = strtrim(strval);
 }

 
 /* Whether to verify email ends in two-letter country or well-known TLD.  1 means check it, 0 means don't. */
 var checkTLD=1;

 /* List of known TLDs that email must end with. */
 var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum|shop)$/;

 /* Pattern used to check if email fits 'user@domain' format.Also is to separate the username from the domain */
 var emailPat=/^(.+)@(.+)$/;

 /* Pattern for matching all special chars.Not allowedchars includes ( ) < > @ , ; : \ " . [ ] */
 var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]'";

 /* Range of chars allowed in a user/domain name.It really states which chars aren't allowed.*/
 var validChars="\[^\\s" + specialChars + "\]";

 /* Pattern that applies if the "user" is a quoted string (in which case, anything goes).  E.g. "jiminy cricket"@disney.com is a legal e-mail address. */
 var quotedUser="(\"[^\"]*\")";

 /* Domains that are IP addresses, rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
 e-mail address,square brackets reqred. */
 var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

 /* The following string represents an atom (basically a series of non-special characters.) */
 var atom=validChars + '+';

 /* Represents one word in the typical username. Eg, in john.doe@somewhere.com, john & doe are words.
 I.e., a word is either an atom or quoted string. */
 var word="(" + atom + "|" + quotedUser + ")";

 // Pattern describes the structure of the user
 var userPat=new RegExp("^" + word + "(\\." + word + ")*$");

 /* Pattern describes structure of normal symbolic domain, as opposed to ipDomainPat, shown above. */
 var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

 /* Finally, check if the email is valid. Begin with coarse pattern to simply break up user@domain into diff pieces easy to analyze. */
 var matchArray=emailStr.match(emailPat);

 if (matchArray==null) 
 {
    /* Too many/few @'s or something; ie, this email doesn't even fit the general mould of a valid mail addr. */
    // alert("Email address seems incorrect (check @ and .'s)");
    return false;
 }
 
 var user=matchArray[1];
 var domain=matchArray[2];

 // Start by checking that only basic ASCII characters are in the strings (0-127).
 for (i=0; i<user.length; i++) 
 {
     if (user.charCodeAt(i) > 127) 
	 {
	    // alert("Ths username contains invalid characters.");
		return false;
	  }
 }
 
 for (i=0; i<domain.length; i++) 
 {
    if (domain.charCodeAt(i) > 127) 
	{
	   // alert("This domain name contains invalid characters.");
	   return false;
	}
 }

 // If valid "user"
 if (user.match(userPat) == null) 
 { 
    // alert("The username doesn't seem to be valid.");
	return false;
 }

 /* If email is at an IP addr (not symbolic hostname) make sure valid IP */
 var IPArray=domain.match(ipDomainPat);
 if (IPArray!=null) 
 {// this is an IP address
    for (var i=1;i<=4;i++) 
	{
	   if (IPArray[i]>255) 
	   {
	      // alert("Destination IP address is invalid!");
		  return false;
	   }
	}
	return true;
 }

 // Domain is symbolic name.  Check if it's valid.
 var atomPat=new RegExp("^" + atom + "$");
 var domArr=domain.split(".");
 var len=domArr.length;
 
 for (i=0;i<len;i++) 
 {
    if (domArr[i].search(atomPat)==-1) 
	{
	   // alert("The domain name does not seem to be valid.");
	   return false;
	}
 }

 /* If Dom name valid, make sure it ends in a known TLD or a 2-letter country name, and that there's a hostname preceding the TLD. */
 if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) 
 {
    // alert("The address must end in a well-known domain or two letter " + "country.");
	return false;
 }

 // Make sure there's a host name preceding the domain.
 if (len<2) 
 {
    // alert("This address is missing a hostname!");
	return false;
 }

 // If we've gotten this far, everything's valid!
 return true;
}


/* Last Update: 20/05/01   */

function isdate(val) 
{

 if (typeof(val) == "object")
 {
    objtrim(val);
    objName = val;
 }
 else
 {
    objName = strtrim(val);
 }

 var datefield = objName;
 if (chkdate(objName) == false) 
 {
    datefield.select();
	alert("That date is invalid. Try again and please ensure that it is in 'dd/mm/yy' format");
	datefield.focus();
	return false;
 }
 else 
 {
    return true;
 }
}

function chkdate(objName) 
{
// var strDatestyle = "US"; //United States date style
 var strDatestyle = "EU";  //European date style
 var strDate;
 var strDateArray;
 var strDay;
 var strMonth;
 var strYear;
 var intday;
 var intMonth;
 var intYear;
 var booFound = false;
 var datefield = objName;
 var strSeparatorArray = new Array("-"," ","/",".");
 var intElementNr;
 var err = 0;
 var strMonthArray = new Array(12);

 // Create and array of months with their names
 strMonthArray[0] = "Jan";
 strMonthArray[1] = "Feb";
 strMonthArray[2] = "Mar";
 strMonthArray[3] = "Apr";
 strMonthArray[4] = "May";
 strMonthArray[5] = "Jun";
 strMonthArray[6] = "Jul";
 strMonthArray[7] = "Aug";
 strMonthArray[8] = "Sep";
 strMonthArray[9] = "Oct";
 strMonthArray[10] = "Nov";
 strMonthArray[11] = "Dec";
 strDate = datefield.value;
 
 // Nulls are allowed
 if (strDate.length < 1) 
 {    return true; }
 
 for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) 
 {
    // Separate dd & mm & yy by splitting on common delimiters (- / . & space)
	if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) 
	{  strDateArray = strDate.split(strSeparatorArray[intElementNr]);

	   // If componentd are not enough
	   if (strDateArray.length != 3) 
	   {
	      err = 1;
		  return false;
	   }
       else 
	   {
	      // Separate date components and put them into separate vars for day, month and year		  
		  strDay = strDateArray[0];
		  strMonth = strDateArray[1];
		  strYear = strDateArray[2];
       }
	   booFound = true;
	}
 }
 
 if (booFound == false) 
 {
    if (strDate.length>5) 
	{
	   strDay = strDate.substr(0, 2);
	   strMonth = strDate.substr(2, 2);
	   strYear = strDate.substr(4);
    }
 }
 
 if (strYear.length == 2) 
 {  strYear = '20' + strYear; }

 // US style
 if (strDatestyle == "US") 
 {
    strTemp = strDay;
	strDay = strMonth;
	strMonth = strTemp;
 }
 
 intday = parseInt(strDay, 10);
 
 if (isNaN(intday)) 
 {
    err = 2;
	return false;
 }

 intMonth = parseInt(strMonth, 10);
 if (isNaN(intMonth)) 
 {
    for (i = 0;i<12;i++) 
	{
	   if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) 
	   {
	      intMonth = i+1;
		  strMonth = strMonthArray[i];
		  i = 12;
       }
    }
    
	if (isNaN(intMonth)) 
	{
	   err = 3;
	   return false;
    }
 }

 intYear = parseInt(strYear, 10);
 if (isNaN(intYear)) 
 {
    err = 4;
	return false;
 }

 // Invalid month number 
 if (intMonth>12 || intMonth<1) 
 {
    err = 5;
	return false;
 }

 // These months are allowed 31 days, invalidate any entries greater that 31 and lesser than 1
 if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) 
 {
    err = 6;
	return false;
 }

 // These months are allowed 30 days, invalidate any entries greater that 30 and lesser than 1
 if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) 
 {
    err = 7;
	return false;
 }

 // Check days for feb now
 if (intMonth == 2) 
 {
    if (intday < 1) 
	{
	   err = 8;
	   return false;
    }
	
	// For leap yrs, feb gets 29 days
	if (LeapYear(intYear) == true) 
	{
	   if (intday > 29) 
	   {
	      err = 9;
		  return false;
	   }
	 }
	 else 
	 {
	    if (intday > 28) 
		{
		   err = 10;
		   return false;
		}
	 }
 }
 
// if (strDatestyle == "US") 
// {  datefield.value = strMonthArray[intMonth-1] + " " + intday+" " + strYear; }
// else 
// {  datefield.value = intday + " " + strMonthArray[intMonth-1] + " " + strYear; }

 return true;
}

// Function that checks if the year is a leap year
function LeapYear(intYear) 
{
   if (intYear % 100 == 0) 
   {
      if (intYear % 400 == 0) { return true; }
   }
   else 
   {
      if ((intYear % 4) == 0) { return true; }
   }
 return false;
}

// func isblank()parameter is string to be compared. returns true if it does not contain any 
// non-whitespace character
function isblank(strval)
{
   if (typeof(strval) == "object")
   {  objtrim(strval);
	  str = strval.value;
   }
   else
   {  str = strtrim(strval);   }

   var len = str.length;
   for (var i = 0; i < len; i++)
   {
      if (str.charAt(i) != " ")
      {
	     return false;				// If the is any non-space character, isblank() returns false
      }
   }
   return true;
}

// Alltrim function meant only for string value. Returns trimmed value
// Last Modify : 28/05/01
function strtrim(val)
{
   while('' + val.charAt(0)==' ')
   {
      val = val.substring(1,val.length);
   }

   while('' + val.charAt(val.length-1)==' ')
   {
      val = val.substring(0,val.length-1)
   }

   return val;
}

// Alltrim func when object is passed. 
// last Modify : 28/05/01
function objtrim(box)
{
   while('' + box.value.charAt(0)==' ')
   {
      box.value = box.value.substring(1,box.value.length);
   }

   while(''+box.value.charAt(box.value.length-1)==' ')
   {
      box.value = box.value.substring(0,box.value.length-1)
   }

}

// Function to return false if there are any single quotes in 
function noquotes(str)
{
   for (var i=0; i < str.length ; i++ )
   {
      if (str.charAt(i) == "'")
      {
	     return false;				// If the is any non-space character, isblank() returns false
      }
   }
   return true;
}

//14/02/03 - ES(4)
function isdropdate(ctrlname)
{
	var dtcorrect;
	dtcorrect = true;
	
	day = ctrlname(0).value;
	mon = ctrlname(1).value;
	year = ctrlname(2).value;

	if ((day == '') || (mon == '') || (year == ''))
	{
			dtcorrect = false;
	}
	else
	{
		if ((year % 4) == 0)
		{
			if (mon == 'Feb')
			{
				if (day > 29)
				{
					dtcorrect = false;
				}
			}
		}
		else
		{
			if (mon == 'Feb')
			{
				if (day > 28)
				{
					dtcorrect = false;
				}
			}
		}
		switch (mon)
		{
			case 'Apr':
			case 'Jun':
			case 'May':
			case 'Sep':
			case 'Nov': if (day > 30)
						{
							dtcorrect = false;
						}
		}
	}
	return dtcorrect;
}

function getdropdate(ctrl)
{
  var arr_months = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
  var this_month = 0;
  var this_dt = new Date();
  
  for (var i = 0; i<= arr_months.length; i++)
  {
     if (ctrl[1].value == arr_months[i])
     {
        this_month = i;
     }
  }

  this_dt.setDate(parseInt(ctrl[0].value));
  this_dt.setMonth(this_month);
  this_dt.setYear(parseInt(ctrl[2].value));

  return this_dt;
}

function get_Date(dt)
{
dt=dt.substr(dt.indexOf("/")+1,dt.lastIndexOf("/")-dt.indexOf("/")-1)+"/"+dt.substr(0,dt.indexOf("/"))+"/"+dt.substr(dt.lastIndexOf("/")+1);
return dt;
}

function getutctime()
{
   var d, s ="";
   var y = "";
   d = new Date();
   y += d.getUTCHours()+ ":";
   y += d.getUTCMinutes()+ ":";
   y += d.getUTCSeconds();
   return (y);
}   

function getutcdate()
{
   var d, s ="";
   var y = "";
   d = new Date();
   s += (d.getUTCMonth() + 1) + "/";
   s += d.getUTCDate() + "/";
   s += d.getUTCFullYear();
   return (s);
   
}   
