
var defaultEmptyOK = false
var reSignedInteger = /^(\d*)|([+](\d*))|([-](\d*))$/
var reInteger = /^\d+$/

function makeArray(n) {
//*** BUG: If I put this line in, I get two error messages:
//(1) Window.length can't be set by assignment
//(2) daysInMonth has no property indexed by 4
//If I leave it out, the code works fine.
//   this.length = n;
   for (var i = 1; i <= n; i++) {
      this[i] = 0
   } 
   return this
}


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 gCheckEmail(pCheckString)
{
	 var at = false;
	var dot = false;
	var atcnt=0;
	var ErrFlag=false;

	for (var i = 0; i < pCheckString.length; i++)
	{
		 ch = pCheckString.substring(i, i + 1)
		if ((ch >= "A" && ch <= "Z") || (ch >= "a" && ch <= "z") || (ch == "@") || (ch == ".") || (ch == "_") || (ch == "-") || (ch >= "0" && ch <= "9"))
         {
             if (ch == "@") 
             {
                 at=true;
				 atcnt++;
             }
             if (ch == ".") 
             {
                 dot=true;
             }
 	    }
		else 
		{
			ErrFlag=true
			break;
		}
   }
 if ((at == true) && (dot == true)&&(atcnt==1) && (ErrFlag==false)) 
 {
     return false;
 }
 else 
 {
 //lString = pCtrlName + " is not in correct format. Please input it in the following format :- abc@xyz.com";
  // window.alert (lString);
  //  pCtrl.focus();
   return true;
   
 }
}



// This function is used to check whether a date entered in mm/dd/yyyy format is correct. 
// It checks that the year should be in the yyyy format and the year should be greater 
// than 1752 (for sql server)
function gDate_Check(pDateCtrl,msg)
{
    var dd,mm,yy,chk, index_1, index_2, date
	date = pDateCtrl.value
    index_1 = date.indexOf("/") 
    index_2 = date.indexOf("/",index_1+1)
    if ((index_1 == -1) || (index_2 == -1))
     {
       alert(msg+" entered is not correct.Date should be in MM/DD/YYYY format.");
       pDateCtrl.focus();
       return false
	 }
    dd = date.substring(index_1+1,index_2)
    mm = date.substring(0,index_1)
    yy = date.substring(index_2+1,date.length)

	// check whether yy has length=4 and is greater than 1752 
    if ((yy.length != 4) || (parseInt(yy,10) < 1753))
	  {
	   alert(msg+" entered is not correct");
       pDateCtrl.focus();
	   return false
	  }
	
	// check whether the date enterd is correct or not
    chk =  isDate(yy,mm,dd)
       if (chk== false)
        {
         alert(msg+" entered is not correct");
         pDateCtrl.focus();
		 return false;
        }
       else
        {
         return true;
        }

  }
function isDate (year, month, day)
{   // catch invalid years (not 2- or 4-digit) and invalid months and days.
	if (! (isYear(year, false) && isMonth(month, false) && isDay(day, false))) return false;

    // Explicitly change type to integer to make code work in both
    // JavaScript 1.1 and JavaScript 1.2.
    var intYear = parseInt(year,10);
    var intMonth = parseInt(month,10);
    var intDay = parseInt(day,10);

    // catch invalid days, except for February
    if (intDay > daysInMonth[intMonth]) return false; 

    if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false;

    return true;
}


function isMonth (s)
{   if (isEmpty(s)) 
       if (isMonth.arguments.length == 1) return defaultEmptyOK;
       else return (isMonth.arguments[1] == true);
    return isIntegerInRange (s, 1, 12);
}



// isDay (STRING s [, BOOLEAN emptyOK])
// 
// isDay returns true if string s is a valid 
// day number between 1 and 31.
// 
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isDay (s)
{   if (isEmpty(s)) 
       if (isDay.arguments.length == 1) return defaultEmptyOK;
       else return (isDay.arguments[1] == true);   
    return isIntegerInRange (s, 1, 31);
}


function isYear (s)
{   if (isEmpty(s)) 
       if (isYear.arguments.length == 1) return defaultEmptyOK;
       else return (isYear.arguments[1] == true);
    if (!isNonnegativeInteger(s)) return false;
    return ((s.length == 2) || (s.length == 4));
}
 //returns true if date1 is greater than date2
function gDateCompSpl(pDateCtrl,pDateCtr2) 
{
var Date1;
var Date2;
	Date1 = pDateCtrl;
	Date2 = pDateCtr2;

	SplitDate1 = Date1.split("/");
	SplitDate2 = Date2.split("/");

		alert(Date1);
		alert(Date2);
//		alert(parseInt(SplitDate1[i],10))
	for(i=2;i>=0;i--)
	{
		alert(parseInt(SplitDate2[i], 10));
		alert(parseInt(SplitDate1[i],10))
		if(parseInt(SplitDate2[i], 10) < parseInt(SplitDate1[i],10)) 
		{			
			return true;
		}
		if(parseInt(SplitDate2[i],10) == parseInt(SplitDate1[i],10))
				continue;
		else
				return false;
	}
	return false;
}

//returns true if date1 is greater than date2 in mm/dd/yyyy
function gDateCompUSFormat(pDateCtrl,pDateCtr2) 
{
var Date1;
var Date2;
var temp
	Date1 = pDateCtrl;
	Date2 = pDateCtr2;

	SplitDate1 = Date1.split("/");
	SplitDate2 = Date2.split("/");

	temp = SplitDate2[0];
	SplitDate2[0] = SplitDate2[1];
	SplitDate2[1] = temp;
	
	temp = SplitDate1[0];
	SplitDate1[0] = SplitDate1[1];
	SplitDate1[1] = temp;
		
	for(i=2;i>=0;i--)
	{
		if(parseInt(SplitDate2[i], 10) < parseInt(SplitDate1[i],10)) 
		{			
			return true;
		}
		if(parseInt(SplitDate2[i],10) == parseInt(SplitDate1[i],10))
				continue;
		else
				return false;
	}
	return false;
}
function gDateCompUSFormat2(pDateCtrl,pDateCtr2) 
{
var Date1;
var Date2;
var temp
	var newDate1;
var newDate2
	newDate1=pDateCtrl.split(" ");
	newDate2=pDateCtr2.split(" ");
	
	Date1 = newDate1[0];
	Date2 =newDate2[0];

	SplitDate1 = Date1.split("/");
	SplitDate2 = Date2.split("/");
//alert(Date1);
//alert(Date2);

//a/lert(SplitDate1[0])
//alert(SplitDate2[0],2))
//alert(parseInt(SplitDate1[1],2))
//alert(parseInt(SplitDate2[1],2))
//alert(parseInt(SplitDate1[2],4))
//alert(parseInt(SplitDate2[2],4))


if ((SplitDate1[0] == SplitDate2[0]) && (SplitDate1[1] == SplitDate2[1]) && (SplitDate1[2]==SplitDate2[2]))
	{
//	alert("if");

	return true;	
	}	
else

	{
	//alert("else");
	temp = SplitDate2[0];
	SplitDate2[0] = SplitDate2[1];
	SplitDate2[1] = temp;
	
	temp = SplitDate1[0];
	SplitDate1[0] = SplitDate1[1];
	SplitDate1[1] = temp;		
	for(i=2;i>=0;i--)
	{
//		alert(parseInt(SplitDate2[i], 10))	
	//	alert(parseInt(SplitDate1[i], 10))	
		if(parseInt(SplitDate1[i], 10) < parseInt(SplitDate2[i],10)) 
		{			
			return true;
		}
		if(parseInt(SplitDate2[i],10) == parseInt(SplitDate1[i],10))
				continue;
		else
				return false;
	}

}

}
function gDateCompUSFormat1(pDateCtrl,pDateCtr2) 
{
//alert(pDateCtrl)
//alert(pDateCtr2)
var Date1;
var Date2;
var temp;
var time1;
var time2;
	
	Date1 = pDateCtrl.split(" ");
	Date2 = pDateCtr2.split(" ");
	SplitDate1 = Date1[0].split("/");
	SplitDate2 = Date2[0].split("/");
	SplitTime1 = Date1[1].split(":");
	SplitTime2 = Date2[1].split(":");
	time1=SplitTime1[0]+SplitTime1[1]
	time2=SplitTime2[0]+SplitTime2[1]
	
	if(parseInt(SplitDate1[0])==parseInt(SplitDate2[0]) && parseInt(SplitDate1[1])==parseInt(SplitDate2[1]) && parseInt(SplitDate1[2])==parseInt(SplitDate2[2]))
		{
/*
	alert("=");
	alert(Date1[1]);
	alert(Date2[1]);
	alert(SplitTime1[0])
	alert(SplitTime2[0])
		//alert(time2);
	alert(SplitDate1[0])
	alert(SplitTime2[0])
	alert(SplitTime1[1])
	alert(SplitTime2[1])
*/
		if (SplitTime1[0]==SplitTime2[0])
			{
				if(SplitTime2[1] < SplitTime1[1])
				return false;
			}
		if(SplitTime2[0] < SplitTime1[0])
			{
				return false;
			}
		}
return true;
}

var spcChar="`~!@#$%^&*=|\\{}[]'\":;/?<>_"

var spcCharSSN="`~!@#$%^&*()+=|\\{}[]'\":;/?<>_-,"

var lowercaseLetters = "abcdefghijklmnopqrstuvwxyz"

var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
var CheckAllChar
//CheckAllChar=lowercaseLetters+uppercaseLetters+spcChar
CheckAllChar=spcChar

var checkAllCharSSN=lowercaseLetters+uppercaseLetters+spcCharSSN

function CheckSSN(pString)
{   

var i;
var j;
var ctr;
ctr = false;
 for (i=0; i < pString.length; i++)
 {   
	for(j=0;j<checkAllCharSSN.length;j++)
	{
		if(pString.charAt(i) == checkAllCharSSN.charAt(j))
		{
			ctr = true;
			break;
		}
	}
	if (ctr)
	 return true;
 }
 return false;
}

function CheckPhone(pString)
{   
var i;
var j;
var ctr;
ctr = false;
 for (i=0; i < pString.length; i++)
 {   
	for(j=0;j<CheckAllChar.length;j++)
	{
		if(pString.charAt(i) == CheckAllChar.charAt(j))
		{
			ctr = true;
			break;
		}
	}
	if (ctr)
	 return true;
 }
 return false;
}


function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}



function isNonnegativeInteger (s)
{   var secondArg = defaultEmptyOK;

    if (isNonnegativeInteger.arguments.length > 1)
        secondArg = isNonnegativeInteger.arguments[1];

    // The next line is a bit byzantine.  What it means is:
    // a) s must be a signed integer, AND
    // b) one of the following must be true:
    //    i)  s is empty and we are supposed to return true for
    //        empty strings
    //    ii) this is a number >= 0

    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) >= 0) ) );
}



function isSignedInteger (s)

{   if (isEmpty(s)) 
       if (isSignedInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isSignedInteger.arguments[1] == true);

    
    else {
       return reSignedInteger.test(s)
    }
}
function isIntegerInRange (s, a, b)
{   if (isEmpty(s)) 
       if (isIntegerInRange.arguments.length == 1) return defaultEmptyOK;
       else return (isIntegerInRange.arguments[1] == true);

    // Catch non-integer strings to avoid creating a NaN below,
    // which isn't available on JavaScript 1.0 for Windows.
    if (!isInteger(s, false)) return false;

    // Now, explicitly change the type to integer via parseInt
    // so that the comparison code below will work both on 
    // JavaScript 1.2 (which typechecks in equality comparisons)
    // and JavaScript 1.1 and before (which doesn't).
    var num = parseInt (s,10);
    return ((num >= a) && (num <= b));
}

function isInteger (s)

{   var i;

    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);

    return reInteger.test(s);
}




function checkDot(pCheckString)
{
	var dot = false;
	var atcnt=0;
	var ErrFlag=false;
//alert(pCheckString.length);
	for (var i = 0; i < pCheckString.length; i++)
	{
		 ch = pCheckString.substring(i, i + 1)
//		alert(ch);
		if ((ch >= "A" && ch <= "Z") || (ch >= "a" && ch <= "z") || (ch == ".") || (ch == "_") || (ch >= "0" && ch <= "9"))
         {
             if (ch == ".") 
             {
                 dot=true;
				 atcnt++; 	
			 }
 	    }
		else 
		{
			ErrFlag=true
			break;
		}
   }
 if ((dot == true)&&(atcnt==1) && (ErrFlag==false)) 
 {
     return false;
 }
 else 
 {
   return true;
   
 }
}



function checkFirstCharDot(pString, pCtrlName)
{ 
    var c = pString.charAt(0);
    if(c == ".")
 	{	
	var lString;
 	 lString = pCtrlName + " cannot have the first character as a dot(.)" + c ;
 	window.alert(lString);
 	   
 	return true;
 	}
 }

//function to check space
var whitespace = " \t\n\r";
function stripWhitespace (s)

{   return stripCharsInBag (s, whitespace)
}

function stripCharsInBag (s, bag)

{   var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
      
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

// function to check special character
function gChkPassString(pString, pCtrl, blnCheck)
{   
var i;
var j;
var ctr;
ctr = false;
 for (i=0; i < pString.length; i++)
 {   
	for(j=0;j<pCtrl.length;j++)
	{
		if(pString.charAt(i) == pCtrl.charAt(j))
		{
			ctr = true;
			break;
		}
	}
	if (ctr)
	 return true;
 }
 return false;
}



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 );
}

//end here "to be included in js file"


function CheckDateDiff( start, end, interval, rounding ) {

    var iOut = 0;
    
    // Create 2 error messages, 1 for each argument. 
    var startMsg = "Check the Start Date and End Date\n"
        startMsg += "must be a valid date format.\n\n"
        startMsg += "Please try again." ;
		
    var intervalMsg = "Sorry the dateAdd function only accepts\n"
        intervalMsg += "d, h, m OR s intervals.\n\n"
        intervalMsg += "Please try again." ;

    var bufferA = Date.parse( start ) ;
    var bufferB = Date.parse( end ) ;
    	
    // check that the start parameter is a valid Date. 
    if ( isNaN (bufferA) || isNaN (bufferB) ) {
        alert( startMsg ) ;
        return null ;
    }
	
    // check that an interval parameter was not numeric. 
    if ( interval.charAt == 'undefined' ) {
        // the user specified an incorrect interval, handle the error. 
        alert( intervalMsg ) ;
        return null ;
    }
    
    var number = bufferB-bufferA ;
    
    // what kind of add to do? 
    switch (interval.charAt(0))
    {
        case 'd': case 'D': 
            iOut = parseInt(number / 86400000) ;
            if(rounding) iOut += parseInt((number % 86400000)/43200001) ;
            break ;
        case 'h': case 'H':
            iOut = parseInt(number / 3600000 ) ;
            if(rounding) iOut += parseInt((number % 3600000)/1800001) ;
            break ;
        case 'm': case 'M':
            iOut = parseInt(number / 60000 ) ;
            if(rounding) iOut += parseInt((number % 60000)/30001) ;
            break ;
        case 's': case 'S':
            iOut = parseInt(number / 1000 ) ;
            if(rounding) iOut += parseInt((number % 1000)/501) ;
            break ;
        default:
        // If we get to here then the interval parameter
        // didn't meet the d,h,m,s criteria.  Handle
        // the error. 		
        alert(intervalMsg) ;
        return null ;
    }
    
    return iOut ;
}



function showSize(strName,strValue,strLength)
{
if(strValue.length>strLength)
{
alert("You have entered " + strValue.length+" characters. Maximum characters you can add in " + strName+" is " + strLength)
return false
}
else
return true;
}
