

//--To check whether the value(szValue) has any special characters passed in the 
//--second argument(bag)
//--Usage bfnIsvalidChar('Your Value','%*#$@)(!\'\"')
//--Returns False - if the value has any spl char. else returns true
function bfnIsvalidChar(szValue, szBag) {
var i;
	    
for (i = 0; i <szValue.length;i++)
{ 
	var c =szValue.charAt(i);
	if (szBag.indexOf(c) != -1) return false;
 }
  return true
}

// -- To check for any spaces in a field --
function bfnIsNoSpaces(szFieldName) 
{	
	if (!bfnIsvalidChar(szFieldName.value, ' ')) {		
		return false;
	} else {		
		return true;
	}	
}


// -- To set focus to the input box --
function SetFocusSelect(szFieldName)
{
      szFieldName.focus();
      szFieldName.select();
      return true;
}


// -- To check that input box is not empty --

function padin(number)
{
 	return (number < 10) ? ("0" + number) : number;
}


function IsEmpty(szFieldName)
{
      var i;
      var ch;
      
  
   
   if (parseInt(szFieldName.value.length) == 0)
         return true;

      for (i=0; i<parseInt(szFieldName.value.length); i++) {
            ch = szFieldName.value.charAt(i);
            if (ch != ' ' && ch != '\t')
                  return false;
      }
      return true;
}


// -- To check whether two strings are the same

function EqualField(szFieldName1, szFieldName2){
	if(szFieldName1.value == szFieldName2.value)
		return false;
	else 
		return true;
 }
 
 
// -- To check that string1 is same as string2 --
function IsIdentical(szFieldName1,szFieldName2)
{
   var i;
   var ch1;
   var ch2;

   if (parseInt(szFieldName1.value.length) != parseInt(szFieldName2.value.length))
       return false;
   for (i=0; i<parseInt(szFieldName1.value.length); i++) {
       ch1 = szFieldName1.value.charAt(i);
       ch2 = szFieldName2.value.charAt(i);
       if (ch1 != ch2)
             return false;
   }
   return true;
}

// -- Check that the mandatory checkbox is selected--
function IsChecked(szFieldName)
{
	  var j;
      var nValue= null;
      var nLen = parseInt(szFieldName.length);

      if (isNaN(nLen))  // -- if there is only 1 radio button --
         {
            if (szFieldName.checked)
               return true;
         }
      for (j = 0; j <nLen; j++) {
         if (szFieldName[j].checked) {
               return true;
         }
      }
      return false;
}


// -- To get the value of radio button that is being checked --
function CheckedVal(szFieldName)
{
      var j;
      var nValue= null;
      var nLen = parseInt(szFieldName.length);

      if (isNaN(nLen))  // -- if there is only 1 radio button --
         {
            if (szFieldName.checked)
               nValue = szFieldName.value
         }
      for (j = 0; j <nLen; j++) {
         if (szFieldName[j].checked) {
               nValue = szFieldName[j].value;
               break;
         }
      }
      return nValue;
}

function bfnRadioChecked(objRadio){
	var i;
	var RetValue = false;
	if(objRadio.length > 0){
		for(i=0;i<objRadio.length;i++)
			RetValue = RetValue || objRadio[i].checked;
	} else {
		RetValue = objRadio.checked;
	}		
	return RetValue;
}

// -- check whether the length is not less than min --
function CheckMinLen(szValue,nLen)
{
   if (parseInt(szValue.length) < parseInt(nLen))
      return false;
   else return true;
}

function CheckMaxLen(szValue,nLen)
{ 
 if (parseInt(szValue.length) > parseInt(nLen))
      return false;
   else return true;
}

// -- To get the total length of input --
function TotalLength(szFieldName)
{
      return parseInt(szFieldName.value.length);
}

// -- Check that mandatory picklist is selected --
function IsSelected(szFieldName)
{

      if (szFieldName.options[szFieldName.selectedIndex].value == "")
         return false;
      else return true;
}


// -- Check that the mandatory checkbox is selected--
function IsChecked(szFieldName)
{
	  var j;
      var nValue= null;
      var nLen = parseInt(szFieldName.length);

      if (isNaN(nLen))  // -- if there is only 1 radio button --
         {
            if (szFieldName.checked)
               return true;
         }
      for (j = 0; j <nLen; j++) {
         if (szFieldName[j].checked) {
               return true;
         }
      }
      return false;
}


// -- To ensure that value is alphanumeric --
function IsAlphaNum(szFieldName)
{
      var i;
      var IsAlphaN;
      var ch;

      IsAlphaN=true;
      TrimField(szFieldName);
      if (parseInt(szFieldName.value.length) == 0)
         IsAlphaN= true;

      for(i=0; i<parseInt(szFieldName.value.length); i++) {
            ch=szFieldName.value.charAt(i);
            if (((ch >= "A") && (ch <= "Z")) || ((ch >= "a") && (ch <= "z")) || ((ch >= "0") && (ch <= "9")) || (ch == " "))
                  IsAlphaN= true;
            else
                  return false;
      }
      return IsAlphaN;
}

function IsAlphaNum1(szFieldValue)
{
      var i;
      var IsAlphaN;
      var ch;

      IsAlphaN=true;

      for(i=0; i<parseInt(szFieldValue.length); i++) {
            ch=szFieldValue
            if (((ch >= "A") && (ch <= "Z")) || ((ch >= "a") && (ch <= "z")) || ((ch >= "0") && (ch <= "9")) || (ch == " ")) 
                  IsAlphaN= true;
            else
                  return false;
      }
      return IsAlpha;
}

function IsNumeric(szFieldName)
{
      var i;
      var IsNum;
      var ch;

      IsNum=true;
      TrimField(szFieldName);
      for(i=0; i<parseInt(szFieldName.value.length); i++) {
            ch=szFieldName.value.charAt(i);
            if ((ch >= "0") && (ch <= "9"))
               IsNum= true;
            else
                  return false;
      }
      return IsNum;
}

function IsDouble(szFieldName)
{
      //This function checks the input for 0 - 9 digits and period (.)
      var i;
      var IsDouble;
      var ch;

      IsDouble = true;
      TrimField(szFieldName);
      for(i=0; i<parseInt(szFieldName.value.length); i++) {
            ch=szFieldName.value.charAt(i);
            if (((ch >= "0") && (ch <= "9")) || (ch == "."))
               IsDouble = true;
            else
                  return false;
      }
      return IsDouble;
}


function IsValidEmail(szFieldName)
{
      var IsEmail;
      var ch;
      var checkAT;
      var checkPERIOD;

      checkAT = 0;
      checkPERIOD = 0;

      if (IsEmpty(szFieldName)) {
            alert("Please enter your email address.");
            SetFocusSelect(szFieldName);
            return false;
      }

      if (szFieldName.value.indexOf("@")==-1) {
            alert("Sorry, your email address format is not acceptable or you may have entered invalid characters.");
            SetFocusSelect(szFieldName);
            return false;
      }
	
   
      if (szFieldName.value.indexOf("@")==0) {
            alert("Sorry, your email address format is not acceptable or you may have entered invalid characters.");
            SetFocusSelect(szFieldName);
            return false;
      }

      if (szFieldName.value.indexOf(":")>=0) {
     	    alert("Sorry, your email address format is not acceptable or you may have entered invalid characters.");
     	    SetFocusSelect(szFieldName);
     	    return false;
      }

      if ((szFieldName.value.indexOf("."))-(szFieldName.value.indexOf("@"))==1){
     	    alert("Sorry, your email address format is not acceptable or you may have entered invalid characters.");
     	    SetFocusSelect(szFieldName);
     	    return false;
      }
     	    
      if (szFieldName.value.indexOf("@")==(parseInt(szFieldName.value.length)-1)) {
            alert("Sorry, your email address format is not acceptable or you may have entered invalid characters.");
            SetFocusSelect(szFieldName);
            return false;
      }

      if (szFieldName.value.charAt(parseInt(szFieldName.value.length)-1)==".") {
            alert("Sorry, your email address format is not acceptable or you may have entered invalid characters.");
            SetFocusSelect(szFieldName);
            return false;
      }

      if (szFieldName.value.indexOf(" ") != -1) {
            alert("Sorry, your email address format is not acceptable or you may have entered invalid characters.");
            SetFocusSelect(szFieldName);
            return false;
      }

      for(i=0; i<parseInt(szFieldName.value.length); i++) {
            ch= szFieldName.value.charAt(i)

			//Check for more than 1 '@' character
			if (ch == "@") {
				checkAT = checkAT + 1;
				if (checkAT >= 2) {
					IsEmail = false;
					break;
				}
			}
			
			//Check for more than 1 '.' character
			if (ch == ".") {
				checkPERIOD = checkPERIOD + 1;
				
			}

            if ((( ch >= "A") && (ch <= "Z")) || ((ch >= "a") && (ch <= "z")) || ((ch >= "0") && (ch <= "9")) ||
                  (ch == "$") || (ch == "-") || (ch == ".") || (ch == "&") || (ch == "+") || (ch == "!") ||
                  (ch == "*") || (ch == "`") || (ch == "(") || (ch == ")") || (ch == ",") || (ch == "@") ||
                  (ch == "_")) {
                  IsEmail= true;
            }
            else {
                  IsEmail= false;
                  break;
            }
      }
	  
	  if (checkPERIOD == 0) {
	       	alert("Sorry, your email address format is not acceptable or you may have entered invalid characters.");
		SetFocusSelect(szFieldName);		
            	return false;
	  }

      if (!IsEmail) {
            alert("Sorry, your email address format is not acceptable or you may have entered invalid characters.");
            SetFocusSelect(szFieldName);
            return false;
      }

   return true;
}

// -- To remove leading and trailing spaces, and returns the shortened string --
function TrimString(szString)
{
      var i = 0;
      var j = 0;

      for (i=0; i<parseInt(szString.length); i++) {
            if(szString.charAt(i) != " ") {
                  for (j=parseInt(szString.length) - 1; j > i; j--) {
                        if (szString.charAt(j) != " ") {
                              break;
                        }
                  }
                  break;
            }
      }

      if (i > j)
            i = j;

      if (szString.length > 0 && szString.charAt(j) != " ")
            j++;

      return szString.substring(i, j);
}

// -- To set the shortened value as the input field new value--
function TrimField(szFieldName)
{
  var szNewStr="";
  if (szFieldName.value == "")
  	return;
  if (szFieldName.value.length > 0) {
	szNewStr = TrimString(szFieldName.value);
	szFieldName.value = szNewStr;
	}
}

function ClearInp(nFormNum)
{
   window.document.forms[nFormNum].reset();
}

function IsValidDate(dateStr) {
// Checks for the following valid date formats:
// DD/MM/YY   DD/MM/YYYY   DD-MM-YY   DD-MM-YYYY
// Also separates date into month, day, and year variables

var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;

var matchArray = dateStr.match(datePat); // is the format ok?
if (matchArray == null) {
alert("Date is not in a valid format.")
return false;
}
month = matchArray[3]; // parse date into variables
day = matchArray[1];
year = matchArray[4];
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!")
alert("Please enter a valid date.");
return false
}
if (month == 2) { // check for february 29th
var g = parseInt(year / 4);
if (day > 29 || (day == 29 && (year / 4) != g)) {
//alert("February in "+year+" doesn't have "+day+" days!")
alert("Please enter a valid date.");
return false;
   }
}
return true;  // date is valid
}

function padout(number) 
{
	 return (number < 10) ? number.substring(1,2) : number; 
}
	
// functions for Calendar Window Start
function padin(number)
{
 	return (number.length<2) ? ("0" + number) : number;
}

/*function padin(number)
{
 	return (number < 10) ? ("0" + number) : number;
}*/

function y2k(number)
{
	return (number < 1000) ? number + 1900 : number; 
}

function makeArray() 
{
 	this[0] = makeArray.arguments.length;
	for (i = 0; i<makeArray.arguments.length; i++)
        this[i+1] = makeArray.arguments[i];
}
	
var daysofmonth    = new makeArray( 31, 28, 31, 30, 31, 30,
                                    31, 31, 30, 31, 30, 31);
var daysofmonthLY  = new makeArray( 31, 29, 31, 30, 31, 30,
                                    31, 31, 30, 31, 30, 31);

var monthsofyear   = new makeArray('Jan','Feb','Mar',
		                                 'Apr','May','Jun',
		                                  'Jul','Aug','Sep',
			                             'Oct','Nov','Dec');
// the function return 2 digits for month
function GetMthNum1(szMthName)
{
	var szName;
	var i;

	szName = szMthName.toUpperCase();

	if (szName == "JAN" || szName == "JANUARY")
		return "01";
	if (szName == "FEB" || szName == "FEBRUARY")
		return "02";
	if (szName == "MAR" || szName == "MARCH")
		return "03";
	if (szName == "APR" || szName == "APRIL")
		return "04";
	if (szName == "MAY" || szName == "MAY")
		return "05";
	if (szName == "JUN" || szName == "JUNE")
		return "06";
	if (szName == "JUL" || szName == "JULY")
		return "07";
	if (szName == "AUG" || szName == "AUGUST")
		return "08";
	if (szName == "SEP" || szName == "SEPTEMBER")
		return "09";
	if (szName == "OCT" || szName == "OCTOBER")
		return "10";
	if (szName == "NOV" || szName == "NOVEMBER")
		return "11";
	if (szName == "DEC" || szName == "DECEMBER")
		return "12";
}

// functions for Calendar Window End

function LeapYear(year) {
    if ((year/4)   != Math.floor(year/4))   return false;
    if ((year/100) != Math.floor(year/100)) return true;
    if ((year/400) != Math.floor(year/400)) return false;
    return true;
}

function ValidDate(day,month,year) {
    
    if ( (LeapYear(year) && (day > daysofmonthLY[month])) ||
         (!LeapYear(year) && (day > daysofmonth[month])) )
        return false;
    else
        return true;
}
	
function SelNewVal(szFieldName,szValue)
{
	var nLen;
	var i;
	nLen = parseInt(szFieldName.length);
for (i=0 ; i < nLen ; i++) {
         
		if (szFieldName.options[i].value == szValue) {
			szFieldName.selectedIndex = i;
			
			break;
		       
		}
	}
	return true;		
}

// the function return 1 digit for month
function GetMthNum(szMthName)
{
	var szName;
	var i;

	szName = szMthName.toUpperCase();

	if (szName == "JAN" || szName == "JANUARY")
		return 1;
	if (szName == "FEB" || szName == "FEBRUARY")
		return 2;
	if (szName == "MAR" || szName == "MARCH")
		return 3;
	if (szName == "APR" || szName == "APRIL")
		return 4;
	if (szName == "MAY" || szName == "MAY")
		return 5;
	if (szName == "JUN" || szName == "JUNE")
		return 6;
	if (szName == "JUL" || szName == "JULY")
		return 7;
	if (szName == "AUG" || szName == "AUGUST")
		return 8;
	if (szName == "SEP" || szName == "SEPTEMBER")
		return 9;
	if (szName == "OCT" || szName == "OCTOBER")
		return 10;
	if (szName == "NOV" || szName == "NOVEMBER")
		return 11;
	if (szName == "DEC" || szName == "DECEMBER")
		return 12;
}


function GetMthName(nNum)
{
	nNum = parseInt(nNum);
	
	if (nNum == 1)
		return "JAN";
	if (nNum == 2)
		return "FEB";
	if (nNum == 3)
		return "MAR";
	if (nNum == 4)
		return "APR";
	if (nNum == 5)
		return "MAY";
	if (nNum == 6)
		return "JUN";
	if (nNum == 7)
		return "JUL";
	if (nNum == 8)
		return "AUG";
	if (nNum == 9)
		return "SEP";
	if (nNum == 10)
		return "OCT";
	if (nNum == 11)
		return "NOV";
	if (nNum == 12)
		return "DEC";
}

function DateDiff(dFrom,dTo) 
{
	var nMsPerDay, nDiff;

	alert(dFrom.getTime());
	alert(dTo.getTime());
	nMsPerDay = 24 * 60 * 60 * 1000 // Number of milliseconds per day
	nDiff = (dTo.getTime() - dFrom.getTime()) / nMsPerDay
	//nDiff=Math.round(nDiff);
	return nDiff;
}

function szfnGetFileName(szFilePath) {
	var szBackslash = "\\";
	var nLength = szFilePath.length;
	var nSlashIndex = szFilePath.lastIndexOf(szBackslash);
	var szFileName = szFilePath.slice(nSlashIndex+1,nLength);
	return szFileName;
}

function bfnIsValidFileName(szFilePath) {
	var szFileName = szfnGetFileName(szFilePath);
	if (!bfnIsvalidChar(szFileName, ' \'')) {
		return false;
	} else {
		return true;
	}
}

function bfnIsvalidChar(szValue, szBag) {
var i;

for (i = 0; i <szValue.length;i++)
{
	var c =szValue.charAt(i);
	if (szBag.indexOf(c) != -1) return false;
 }
  return true
}

//--To check whether atlease one of the radio or checkbox is checked or not
function IsRadioChecked(objRadio){
	var i;
	var RetValue = false;
	if(objRadio.length > 0){
		for(i=0;i<objRadio.length;i++)
			RetValue = RetValue || objRadio[i].checked;
	} else {
		RetValue = objRadio.checked;
	}		
	return RetValue;
}

