//===========================================================
//var row_clr_before_mover;
function changeFColor(row,mode) {
	var bgclr;
	if (mode == 1 || mode == 2) {
		bgclr = row.bgColor;
	}
	else {
		bgclr = row.parentElement.parentElement.bgColor;
	}
	if (bgclr != '#004563') {
		if (mode == 1 || mode == 3) {
			row.style.color = 'blue'
			//row_clr_before_mover = row.bgColor;
			//alert(row.bgColor);
			//row.bgColor = '#b6c7e5';
		}
		else {
			row.style.color = '#000000'
			//alert(prevColor);
			//row.bgColor = row_clr_before_mover;
		}
	}
}

//==================================================================
function ValidateEmail(email) {
  var f = eval("document.all." + email);
  if (!emailCheck (f.value)) {
	f.select();
	f.focus();
 }
}
//=================================================================================
function emailCheck (emailStr) {

/* The following variable tells the rest of the function whether or not
to verify that the address ends in a two-letter country or well-known
TLD.  1 means check it, 0 means don't. */

var checkTLD=1;

/* The following is the list of known TLDs that an e-mail address must end with. */

var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

/* The following pattern is used to check if the entered e-mail address
fits the user@domain format.  It also is used to separate the username
from the domain. */

var emailPat=/^(.+)@(.+)$/;

/* The following string represents the pattern for matching all special
characters.  We don't want to allow special characters in the address. 
These characters include ( ) < > @ , ; : \ " . [ ] */

var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

/* The following string represents the range of characters allowed in a 
username or domainname.  It really states which chars aren't allowed.*/

var validChars="\[^\\s" + specialChars + "\]";

/* The following pattern applies if the "user" is a quoted string (in
which case, there are no rules about which characters are allowed
and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
is a legal e-mail address. */

var quotedUser="(\"[^\"]*\")";

/* The following pattern applies for domains that are IP addresses,
rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
e-mail address. NOTE: The square brackets are required. */

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 + '+';

/* The following string represents one word in the typical username.
For example, in john.doe@somewhere.com, john and doe are words.
Basically, a word is either an atom or quoted string. */

var word="(" + atom + "|" + quotedUser + ")";

// The following pattern describes the structure of the user

var userPat=new RegExp("^" + word + "(\\." + word + ")*$");

/* The following pattern describes the structure of a normal symbolic
domain, as opposed to ipDomainPat, shown above. */

var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

/* Finally, let's start trying to figure out if the supplied address is valid. */

/* Begin with the coarse pattern to simply break up user@domain into
different pieces that are easy to analyze. */

var matchArray=emailStr.match(emailPat);

if (matchArray==null) {

/* Too many/few @'s or something; basically, this address doesn't
even fit the general mould of a valid e-mail address. */

alert("Email address seems incorrect (check @ and .'s)\n Please give a valid Email Address..");
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.\n Please give a valid Email Address..");
return false;
   }
}
for (i=0; i<domain.length; i++) {
if (domain.charCodeAt(i)>127) {
alert("Ths domain name contains invalid characters.\n Please give a valid Email Address..");
return false;
   }
}

// See if "user" is valid 

if (user.match(userPat)==null) {

// user is not valid

alert("The username doesn't seem to be valid.\n Please give a valid Email Address..");
return false;
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
host name) make sure the IP address is valid. */

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!\n Please give a valid Email Address..");
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.\n Please give a valid Email Address..");
return false;
   }
}

/* domain name seems valid, but now make sure that it ends in a
known top-level domain (like com, edu, gov) or a two-letter word,
representing country (uk, nl), and that there's a hostname preceding 
the domain or country. */

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.\n Please give a valid Email Address..");
return false;
}

// Make sure there's a host name preceding the domain.

if (len<2) {
alert("This address is missing a hostname!\n Please give a valid Email Address..");
return false;
}

// If we've gotten this far, everything's valid!
return true;
}
//====================================================
function CheckLengthOfField(mode,fld,len) {
	if(fld.value) {
		var f = fld;
	}
	else {
		var f = eval("document.all." + fld.name);
	}
	if(!mode || mode == 1) {
		if (f.value.length > len) {
			alert("The length of the entry must not exceed " + len + ".!!\n The length of the entry now is " + f.value.length + ".")
			f.select();
			f.focus();
		}
	}
	else if(mode == 2) {
		if (TRIM(f.value) != "") {
			if (!isDate(TRIM(f.value),2)) {
				f.select();
				f.focus();
			}
		}
		else {
			alert("Please give the date");
			f.select();
			f.focus();
		}
	}
	else if(mode == 3) {
		if (TRIM(f.value) != "") {
		//if (TRIM(fldvalue) != "") {
			if (isNaN(f.value))  {
			//if (isNaN(fldvalue))  {
				//alert("Please give a numeric value");
				f.value = 0
				//f.select();
				//f.focus();
			}
		}
		else {
			//alert("Please give a numeric value");
			f.value = 0;
			//f.select();
			//f.focus();
		}
	}
}

//========================================================================
function isDate(dateStr,arg) {
	if (arg) {
		var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{2,4})$/;
	}
	else {
		var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
	}
    var matchArray = dateStr.match(datePat); // is the format ok?

    if (matchArray == null) {
		if (arg) { alert("Please enter a valid date");}
		else {alert("The Date must be in the format [mm/dd/yyyy]");	}       
		return false;
    }

    month = matchArray[1]; // parse 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;
        }
    }
    //alert(year.toString().length);
    //if (!arg) {
    if(year.toString().length == 4){
		if (year > 2099 || year < 1900) {
			alert("The year must be between 1900 and 2099")
			return false;
		}
    }
    return true; // date is valid
}
//=====================================================
// Using  Global Variables -  "FIELD" (Indicates the current field) , "FORMAT" - format of the date.


var Cal = 0;
var FIELD;
var FORMAT;
var SelectedYear , SelectedMonth , SelectedDay;
var DOW = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");

//===========================
function Cal_Open(F,Format) {
	
  FORMAT = Format;
  var F1=eval("document.all." + F);
  //var F1=eval("document.all.search." + F);
  FIELD=F1;
  var NOW = new Date(FIELD.value);
  if (isNaN(NOW)) NOW = new Date();
  var Y,M;
  SelectedYear= NOW.getYear();
  if (SelectedYear < 2000){SelectedYear = SelectedYear + 1900;}
  SelectedMonth=NOW.getMonth() + 1;
  SelectedDay= NOW.getDate();
  if (Cal!=0) WriteDiv("D"+Cal,"");
  Cal=F;
  PCal(SelectedYear , SelectedMonth , SelectedDay);
  Over();
  var TIMER = setTimeout("Close_Cal()",1000); // Timer to close cal
} 
//===========================
function Set_Date(V) {
	//alert(V);
	var SetDate = new Date(V);
	if (FORMAT == "dow dd/mm/yy") V = DOW[SetDate.getDay()] + " " + V;
	else if (FORMAT == "dd/mm") V = (SetDate.getMonth() + 1) + "/" + SetDate.getDate();
	FIELD.value=V;
	WriteDiv("D"+Cal,"");
}  
//===========================
function PCal(Y,M) {
  var DT1= new Date(Y,M-1,1,0,0,0);
  //alert(Y);
  var FIRST=DT1.getDay(); // DOW of 1st day of the month
  var MON = new Array("JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC");
  var FEB = (leapYear(Y)) ? 29 : 28;
  var All_DAYS= new Array(31,FEB,31,30,31,30,31,31,30,31,30,31);
  var DAYS = All_DAYS[M-1];
  //Start
  var T;
  T="<table border=0 class = \"CalStyle\" border=0 cellpadding = 1 cellspacing = 1";
  T=T+" onmousemove=Over() onmouseout=Out()>";
  var LastY;
  var LastM;
  var NextY;
  var NextM;
  LastY = Y;
  LastM = M-1;
  if (LastM<1) {LastM=12;LastY=LastY-1;}
  NextY = Y;
  NextM = M+1;
  if (NextM>12) {NextM=1;NextY=NextY+1;}
  T=T+"<tr>";
  T=T+"<td><a class=CalHREF href=\"Javascript:PCal("+LastY+","+LastM+")\"><<</a></td>";
  T=T+"<td colspan=5 bgcolor= black align=center>";
  T=T+"<font style=\"font-family:Arial;font-size:7pt;color:white;\">" + Y + " " + MON[M-1] + "</font></td>";
  T=T+"<td><a class=CalHREF href=\"Javascript:PCal("+NextY+","+NextM+")\">>></a></td>";
  T=T+"</tr>";
  T=T+"<tr bgcolor='#00438C'><td><font style=\"font-size:7pt;font-family:Arial;color:white\">S</font></td><td><font style=\"font-size:7pt;font-family:Arial;color:white\">M</font></td><td><font style=\"font-size:7pt;font-family:Arial;color:white\">T</font></td><td><font style=\"font-size:7pt;font-family:Arial;color:white\">W</font></td><td><font style=\"font-size:7pt;font-family:Arial;color:white\">T</font></td><td><font style=\"font-size:7pt;font-family:Arial;color:white\">F</font></td><td><font style=\"font-size:7pt;font-family:Arial;color:white\">S</font></td></tr>"
  T=T+"<tr>";
  var START=1;
  START = START + FIRST;
  if (START >1) T=T+"<td colspan=" + FIRST + "></td>"
  for (i=START,DAY=1;DAY<=DAYS;i++,DAY++) {
    if (Y==SelectedYear && M==SelectedMonth && DAY==SelectedDay) {BGC="#00438C";TXTC="#c0c0c0";}
    else {BGC="#c0c0c0";TXTC="black";}
    T=T+"<td onmouseover=DayOver(this) onmouseout=DayOut(this,'"+BGC+"','"+TXTC+"')";
    T=T+" onclick=Set_Date('" + M + "/" + DAY + "/" + Y + "') ";
    T=T+" style=\"color:" + TXTC + ";background-color:" + BGC + "\">";
    T=T+"<font style=\"font-size:7pt;font-family:Arial\">" + DAY + "</font></td>";
    if (i%7 == 0) T=T+"</tr><tr>";
  }
  T=T+"</tr>";
  T=T + "</table>";
  //var cd = new Date(eval("document.all.search." + Cal + ".value"));
  var cd = new Date(eval("document.all." + Cal + ".value"));
  if(isNaN(cd.getDate())) {
	cd = new Date();
  }  
  Set_Date(eval("'" + M + "/" + cd.getDate() + "/" + Y + "'"));
  WriteDiv("D"+Cal,T);
}
//===========================
function leapYear(Y) {
if (Y % 4 == 0 && (Y % 100 != 0 || Y % 400 == 0))return true;
//  if (Y%4 ==0) return true;
  else return false;
}
//===========================
function Over() {
  if (Cal != 0) {var D=setdivA("D"+Cal); D.isOn=true;}
}
//===========================
function Out() {
  if (Cal != 0) {var D=setdivA("D"+Cal); D.isOn=false;}
  var TIMER = setTimeout("Close_Cal()",500); // Timer to close cal
}
//===========================
function DayOver(D) {
  D.style.color = 'white';
  //D.style.backgroundColor='black';
  D.style.backgroundColor='#00438C';
}
//===========================
function DayOut(D,BackColor,TextColor) {
  D.style.color = TextColor;
  D.style.backgroundColor=BackColor;
}
//===========================
function Close_Cal() {
  if (Cal != 0) {
    var D=setdivA("D"+Cal);
    if (D.isOn!=true) {
     D.isOn=false;
     WriteDiv("D"+Cal,"");
     Cal=0;
    }
  }
}

//===========================================================================

//Setting Layer for IE and Netscape..
function setdiv(arg) {
if (document.all) {return eval("document.all." + arg + ".style");}
else if (document.layers) {return eval("document.layers." + arg);}
else {return document.getElementById(arg).style;}
}

function setdivA(arg) {
 if (document.all) {return eval("document.all." + arg);} 
 else if (document.layers) {return eval("document.layers." + arg);} 
 else {return document.getElementById(arg);} } 
 
 
//==============================

//Write to  Layer for IE and Netscape..
function WriteDiv(arg,T) {
	var D;
	if (document.all) {D = eval("document.all." + arg);}
	else if (document.layers) {D = eval("document.layers." + arg);}
	else {D = document.getElementById(arg);}
	D.innerHTML = T;
}

//Getting Elements By ID
function getElement(arg) {
  if (document.all) {return eval("document.all." + arg);}
  else if (document.layers) {return eval("document." + arg);}
  else {return document.getElementById(arg);}
}

//==================================================
function LTRIM(STR) {
  STR = STR.toString();
  return STR.replace( /^\s*/, "" )
}
//==================================================
function RTRIM(STR) {
  STR = STR.toString();
  return STR.replace( /\s*$/, "" );
}
//==================================================
function TRIM(STR) {
  STR = STR.toString();
  return RTRIM(LTRIM(STR));
}
 