var theCurDate = new Date();
var gmtDate = theCurDate.toGMTString();
var arr = gmtDate.split(' ');
var theDay   = theCurDate.getDay();
var theMonth = theCurDate.getMonth() + 1;
var theYear  = theCurDate.getYear();
var theDate  = theCurDate.getDate();
var daysOfWeek = new Array("Sunday", "Monday", "Tuesday", "Wednesday",
                           "Thursday", "Friday", "Saturday");

/*-------------------------------------------------------------
Function: makeArray
Desc	: initialise the current window array by zero
Algo	: n/a
Author	: anand@thinkbn.com ; selvan@thinkbn.com
Date	: Mar 30,2000
--------------------------------------------------------------*/
function makeArray(n) 
{
   for (var i = 1; i <= n; i++) {
      this[i] = 0;
   } 
   return this;
}

var MIN_YEAR = 1800;
var MAX_YEAR = 2099;
var MIN_EXPIRY_YEAR = 00;
var MAX_EXPIRY_YEAR = 99;
var daysInMonth = makeArray(12);


daysInMonth[1] = 31;
daysInMonth[2] = 29;   // must programmatically check this
daysInMonth[3] = 31;
daysInMonth[4] = 30;
daysInMonth[5] = 31;
daysInMonth[6] = 30;
daysInMonth[7] = 31;
daysInMonth[8] = 31;
daysInMonth[9] = 30;
daysInMonth[10] = 31;
daysInMonth[11] = 30;
daysInMonth[12] = 31;

/*-------------------------------------------------------------
Function: daysInFebruary
Desc	: ret number of days in the month Feb of that year
Algo	: check for leap year and return 29/28 accordingly
Author	: anand@thinkbn.com ; selvan@thinkbn.com
Date	: Mar 30,2000
-------------------------------------------------------------*/
function daysInFebruary (year) 
{   
    // February has 29 days in any year evenly
    // divisible by four, EXCEPT for centurial
    // years which are not also divisible by 400.
    return (((year % 4 == 0) &&
             (( !(year % 100 == 0)) ||
              (year % 400 == 0) )) ? 29 : 28 );
}

/*-------------------------------------------------------------
Function: isDate
Desc	: returns true if string arguments year, month, 
		  and day form a valid date.
Algo	: 
Author	: anand@thinkbn.com ; selvan@thinkbn.com
Date	: Mar 30,2000
-------------------------------------------------------------*/
function isDate (year, month, day, label)
{
    // Explicitly change type to integer to make code
    // work in both JavaScript 1.1 and JavaScript 1.2.
    var nYear = parseInt(year);
    var nMonth= parseInt(month);
    var nDay  = parseInt(day);
	var strError = "";

   // check for empty d/m/y fields
   if ( isWhitespace(year)  ||
        isWhitespace(month) ||
        isWhitespace(day) )
       return ("Incomplete " + label + ".\n");

	strError = isValidNumber(year,MIN_YEAR,theYear,label);
	if(strError != "")
		return strError;

      // catch invalid days, except for February

      if (nDay > daysInMonth[nMonth]) {
          return ("Invalid " + label +
                  ". Day " + day +
                  " is not valid for the Month specified.\n");
      }

      if ((nMonth == 2) && (nDay > daysInFebruary(nYear))) {
          return ("Invalid " + label +
                  ". Day " + day +
                  " is not valid for the Month specified.\n");
      }

      return "";
}

/*-------------------------------------------------------------
Function: dateComp
Desc	: returns date comparison
Algo	: 
Author	: anand@thinkbn.com 
Date	: July 26,2000
-------------------------------------------------------------*/
function dateComp(year1, month1, day1,year2, month2, day2, label)
{   
//compare first year 
// next month
// last date
    if ((year1 - year2)!= 0)
		return (year1 - year2);
	if ((month1 - month2) != 0)
		return (month1 - month2);
	if ((day1 - day2) != 0)
		return (day1 - day2);
		
	return 0; //two dates are equal;
}

// BOI, followed by one or more whitespace characters, followed by EOI.
var reWhitespace = /^\s+$/
var whitespace = /\t\n\r/

/*-------------------------------------------------------------
Function: isCharInStr
Desc	: returns true if the char is found in the given string
Algo	: 
Author	: anand@thinkbn.com ; selvan@thinkbn.com
Date	: Mar 30,2000
-------------------------------------------------------------*/
function isCharInStr (c, s) 
{   
	for (i = 0; i < s.length; i++) {   
		if (s.charAt(i) == c) return true;
	}
	return false;
}

/*-------------------------------------------------------------
Function: stripInitialWhitespace
Desc	: Removes initial (leading) whitespace characters from s.
		  Global variable whitespace (see above)
		  defines which characters are considered whitespace.
Algo	: 
Author	: anand@thinkbn.com ; selvan@thinkbn.com
Date	: Mar 30,2000
-------------------------------------------------------------*/
function stripInitialWhitespace (s) 
{   
	var i = 0;

	while ((i < s.length) && isCharInStr(s.charAt(i), whitespace))
       		i++;
    
	return s.substring (i, s.length);
}

/*-------------------------------------------------------------
Function: isWhitespace
Desc	: check for whitespaces 
Algo	: 
Author	: anand@thinkbn.com ; selvan@thinkbn.com
Date	: Mar 30,2000
-------------------------------------------------------------*/
function isWhitespace (s) 
{ 
    // Is s empty?
    return (isEmpty(s) || reWhitespace.test(s));
}


/*-------------------------------------------------------------
Function: isEmpty 
Desc	: check whether the string is empty
Algo	: 
Author	: anand@thinkbn.com ; selvan@thinkbn.com
Date	: Mar 30,2000
-------------------------------------------------------------*/
// Check whether string s is empty.
function isEmpty(s)
{   
	return ((s == null) || (s.length == 0))
}

// BOI, followed by one or more digits, followed by EOI.
var reNumber = /^(\d+|\d)$/

/*-------------------------------------------------------------
Function: isNumber
Desc	: check if the given Str is a valid number
Algo	: 
Author	: anand@thinkbn.com ; selvan@thinkbn.com
Date	: Mar 30,2000
-------------------------------------------------------------*/
function isNumber (s, label)
{   
	var i;

    if (isWhitespace(s))
    {
        return (label + ".\n");
    }
    else {
        s = stripInitialWhitespace(s);
        if (reNumber.test(s))
            return "";
        else 
		    return ( "Invalid " + label + ". This is a NUMERIC field.\n");
    }
       
}

/*-------------------------------------------------------------
Function: isValidNumber
Desc	: check if the given Str is a valid number and 
	  	  in given range (sVal - eVal)
Algo	: 
Author	: anand@thinkbn.com ; selvan@thinkbn.com
Date	: Mar 30,2000
-------------------------------------------------------------*/
function isValidNumber (s,sVal,eVal, label)
{   
	if( (isNumber(s,label) != "") ||  s < sVal || s > eVal)
		return ("Invalid " + label + 
				". Valid range : " + sVal + " - " + eVal + ".\n");
	else
		return "";
}


// BOI, followed by one or more characters, followed by @,
// followed by one or more characters, followed by .,
// followed by one or more characters, followed by EOI.
//(..+|..)

//Regular Expression Modified on 21 July,2000 
//var reEmail = /^.+\@.+\...+$/

//to check for more than one @ char 
//to check for the email ending with period
//to check for all other default e-mail validations
//var reEmail = /^[^\@]+\@[^\@\.]+(\.[^\@\.]+)+$/

//Modified again on Apr 6, 2001 by Rohit
//var reEmail = /^[a-z][a-z_0-9\.\-]+@[a-z_0-9\.\-]+\.[a-z]+$/i
var reEmail = /^[a-z_0-9\.\-]+@[a-z_0-9\.\-]+\.[a-z]+$/i

/*-------------------------------------------------------------
Function: isEmail
Desc	: check if the given str is a valid EMail ID
			Email address must be of form a@b.c -- in other words:
				* there must be at least one character before the @
				* there must be at least one character before and after the .
				* the characters @ and . are both required
Algo	: 
Author	: anand@thinkbn.com ; selvan@thinkbn.com
Date	: Mar 30,2000
-------------------------------------------------------------*/
function isEmail (s, label)
{ 
    if (isWhitespace(s))
    {
        return (label + ".\n");
    }
    else {
        if (reEmail.test(s))
		    return "";
        else 
		    return ( "Invalid " + label + ".\n" );
    }
}

/*-------------------------------------------------------------
Function: checkLength 
Desc	: check if the given string is atleast of length 'size'
Algo	: 
Author	: anand@thinkbn.com ; selvan@thinkbn.com
Date	: Mar 30,2000
-------------------------------------------------------------*/
function checkLength(s, size , label)
{
	var str="";
	str = checkField(s, label);
	if ( "" != str) {
		return (label + ".\n");
	}
	else {
		if (s.length < size)
			return (label + " should be atleast " + size + " characters long.\n");
	}
	return "";
}

/*-------------------------------------------------------------
Function: checkField
Desc	: check if the field has only whitespaces 
Algo	: 
Author	: anand@thinkbn.com ; selvan@thinkbn.com
Date	: Mar 30,2000
-------------------------------------------------------------*/
function checkField(s, label)
{
	if (isWhitespace(s) )
		return (label + ".\n");
	else 
		return "";
}

/*-------------------------------------------------------------
Function: isSplFieldEmpty
Desc	: check if the field has only whitespaces 
Algo	: 
Author	: anand@thinkbn.com 
Date	: July 25,2000
-------------------------------------------------------------*/
function isSplFieldEmpty(a,b,c,label)
{
	//alert((f.ssn1.value).length)
	var fldValue = a + b + c
	var fldError = ""
	fldError = checkField(fldValue,label);
	return fldError;
}

/*-------------------------------------------------------------
Function: isSplFieldValid
Desc	: check if the field has only whitespaces 
Algo	: 
Author	: anand@thinkbn.com 
Date	: July 25,2000
-------------------------------------------------------------*/
function isSplFieldValid(a,b,c,nSize,label)
{
	//alert((f.ssn1.value).length)
	var fldValue = a + b + c
	var fldError = ""
	
	if (fldValue.length != nSize || fldValue.indexOf(" ") >= 0)
			fldError = "Invalid " + label + ".\n";
	
	return fldError;
}

/*-------------------------------------------------------------
Function: checkPassword
Desc	: check the p1 and p2 are same and valid passwords
Algo	: 
Author	: anand@thinkbn.com ; selvan@thinkbn.com
Date	: Mar 30,2000
-------------------------------------------------------------*/
function checkPassword( l1, p1, p2, label1, label2, label3)
{
	var str = "";
    var flag = 0;
    
	str += checkLength(p1, 4 , label2);
	if ( "" != str ) 
		flag = 1;
		
	str += checkLength(p2, 4, label3);
	if ( "" != str ) 
		flag = 1;
	
	if ( p1 != p2 ) {
		flag = 1;
		str += "The 2 passwords do not match\n";
	}
	
/*	if ( ! flag )
		if ( (p1 == l1) || (p2 == l1) )
			str += "Your Username cannot be the password\n";
*/
/* RAnand commented on July 19, 2000
	if ( ! flag )
		if ( (p1 ==	l1.toLowerCase()) ||
			 (p1 ==	l1.toUpperCase())    )
			str += "Your Username cannot be the password\n";
*/	
	if ( "" != str )
		return str;
	else 
		return "";
}


/*-------------------------------------------------------------
Function: disable
Desc	: sets the diabled stye of a control
Algo	: n/a
Author	: anand@thinkbn.com ; selvan@thinkbn.com
Date	: Mar 30,2000
--------------------------------------------------------------*/
function disable( control, flag)
{
	control.disabled = flag;
}

/*-------------------------------------------------------------
Function: verify
Desc	: Query the user with a question
Algo	: n/a
Author	: anand@thinkbn.com ; selvan@thinkbn.com
Date	: Mar 30,2000
--------------------------------------------------------------*/
function verify(mesg) 
{
	return confirm(mesg);
}


/*-------------------------------------------------------------
Function: doNewWin
Desc	: Open a new popup window
Algo	: n/a
Author	: anand@thinkbn.com 
Date	: July 18,2000
--------------------------------------------------------------*/
function doNewWin(path) {
	var posX , posY
	var button_left , button_top
	button_left = event.screenX - event.offsetX
	button_top  = event.screenY - event.offsetY
	if ((button_left + 280) > window.screen.availWidth )
 		posX = button_left - 280
	else 
		posX = button_left
	
	if ((button_top + 130) > window.screen.availHeight)
 		posY =  button_top - 130
	else
 		posY =  button_top

	window.open(path, "openWin", "width=280,height=130,resizable=0,screenx=0,screeny=0,left=" + 
	posX + ",top=" + posY);
}

/*-------------------------------------------------------------
Function: isSelected
Desc	: returns true if at least one radio button/checkbox in a 
group is selected, false otherwise
Algo	: n/a
Author	: selvan@thinkbn.com 
Date	: April 18,2001
--------------------------------------------------------------*/
function isSelected(field)
{
	// only one radio button
	if (!field.length) {
		if (field.checked == true)
			return true;
		return false;
	}
	// multiple radio buttons
	for (var i = 0; i < field.length ; ++i) {
		if (field[i].checked == true) {
			return true;
		}
	}
	return false;
}

function validateLogin(f) {
	var errStr = isEmail (f.login.value, "Login ID");
	errStr += checkField(f.password.value, "Password");
	if ( errStr == "" ) {
		return true;
	} else {
		errStr = "The sign-in information is incomplete or incorrect:\n\n" + errStr;
		alert (errStr);
		//f.login.focus();
		return false;
	}
}

function IsHtmlDocument(s) {
	if ( s.substring(((s.length)-5), s.length) == ".html")
		return true;
	else if ( s.substring(((s.length)-4), s.length) == ".htm")
		return true;
	else if ( s.substring(((s.length)-6), s.length) == ".dhtml")
		return true;
	else if ( s.substring(((s.length)-6), s.length) == ".shtml")
		return true;
	else
		return false;
}

