function LeapYear(intYear) 
{
  if (intYear % 100 == 0)
  {
    if (intYear % 400 == 0) { return true; }
  }
  else 
  {
    if ((intYear % 4) == 0) { return true; }
  }
  
  return false;
}


function isValidDate(d,convert) 
{
  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 strSeparatorArray = new Array("/");
  var intElementNr;
  var err = 0;
  var strMonthArray = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
  strDate = d;
  if (strDate.length < 1)
  {
    return false;
  }


  for(intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++)
  {
    if(strDate.indexOf(strSeparatorArray[intElementNr]) != -1)
    {
      strDateArray = strDate.split(strSeparatorArray[intElementNr]);
      if (strDateArray.length != 3) 
      {
	err = 1;
	return false;
      }
      else 
      {
	strDay = strDateArray[0];
	strMonth = strDateArray[1];
	strYear = strDateArray[2];
      }
      booFound = true;
    }
  }

  if (booFound == false)
  {
    return false;
  }
  if (isNaN(strMonth))
  {
    return false;
  }
	
  //If number and has . then say invalid
  if (strMonth.indexOf(".") >= 0 || strMonth.indexOf(' ') != -1)
  {
    return false;
  }
	
  // verify year part	2 or 4 digits
  if (strYear.length != 4)
  {
    return false;
  }
  if (isNaN(strYear))
  {
    return false;
  }
	
  //If number and has . then say invalid
  if (strYear.indexOf(".") >= 0)
  {
    return false;
  }
	
  // verify 1 or 2 digit integer day
  if (strDay.length != 2 || strMonth.length != 2)
  {
    return false;
  }
  
  if (isNaN(strDay))
  {
    return false;
  }
	
  //If number and has . then say invalid
  if (strDay.indexOf(".") >= 0 || strDay.indexOf(' ') != -1)
  {
    return false;
  }
	
  // month may be digits of characters, hence following check
  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;
    }
  }

  intDay=parseInt(strDay,10);
  intYear = parseInt(strYear, 10);
	
  if (intMonth>12 || intMonth<1)
  {
    err = 5;
    return false;
  }
	
  // day in month check
  if (intDay < 1 || intDay > 31)
  {
    return false;
  }
		
  if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intDay > 30))
  {
    return false;
  }
	
  if (intMonth == 2)
  {
    if (LeapYear(intYear))
    {
      if (intDay > 29)
      {
        return false;
      }
    }
    else 
    {
      if(intDay > 28)
      {
        return false;
      }
    }
  }	
  
  if (intYear<1900 || intYear>2200)
  {
    return false;	
  }
	
  if (!convert)
    return true;
  else
  {
    return intDay+"/"+intMonth+"/"+intYear;
  }
}

function compareDateWithToday(lstrDate)
{
  var dparts= lstrDate.split("/");
  var JDate = new Date(dparts[2]+"/"+dparts[1]+"/"+dparts[0]);
  var curDate = new Date();
  if(JDate < curDate) return -1;
  else if(JDate == curDate) return 0;
  else if(JDate > curDate) return 1;

}


// The function that validated the Email fields
function checkEmail(lstrEmail)
{
  var ch;
  var lstrNext;
  var lstrPrev;
  var lintLoopIndex = 0;
  var lintSymbolAt = 0;
  var lintSymbolDot = 0;

  // Checking for the first character.
  ch = lstrEmail.charAt(0);
  if(!((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')|| (ch >= '0' && ch <= '9')))
  {
    return false;
  }

  // Checking for other characters in the mail Id.
  for(lintLoopIndex=1;lintLoopIndex<lstrEmail.length-1;lintLoopIndex++)
  {
    ch = lstrEmail.charAt(lintLoopIndex);

    if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
    {
      continue;
    }

    if (!((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || ch == '@' || ch == '.' || ch == '-' || ch == '_' ))
    {
      return false;
    }

    if(ch == '@' || ch == '.' || ch == '-' || ch == '_')
    {
      if(ch == '.') // If '.' is found, count it.
      {
 	lintSymbolDot++;
      }

      // If '@' is found and its count is more than zero, then the mail Id is invalid.
      if(ch == '@' && lintSymbolAt > 0)
      {
 	return false;
      }
      else
      {
	if(ch == '@')
      	{
      	  lintSymbolAt++;
      	}

      	// Checking for the '@'s before and after '@'.
      	lstrNext = lstrEmail.charAt(lintLoopIndex+1);
      	lstrPrev =lstrEmail.charAt(lintLoopIndex-1);

      	if (!((lstrNext >= 'a' && lstrNext <= 'z' || lstrNext >= 'A' && lstrNext <= 'Z'	||	lstrNext >= '0' && lstrNext <= '9') && (lstrPrev >= 'a' && lstrPrev <= 'z' || lstrPrev >= 'A' && lstrPrev <= 'Z'	||	lstrPrev >= '0' && lstrPrev <= '9')&&(lstrPrev >= 'a' && lstrPrev <= 'z' || lstrPrev >= 'A' && lstrPrev <= 'Z'	||	lstrPrev >= '0' && lstrPrev <= '9') && (lstrPrev >= 'a' && lstrPrev <= 'z' || lstrNext >= 'A' && lstrPrev <= 'Z' || lstrPrev >= '0' && lstrPrev <= '9')))
      	{
      	  return false;
      	}
      }
    } //if of lstrprev
  } //end of for

  ch = lstrEmail.charAt(lintLoopIndex);
  if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' || ch >= '0' && ch <= '9')
  {
    //do nothing ...
  }
  else
  {
    return false;
  }
  if ( lintSymbolDot >0 && lintSymbolAt  == 1)
  {
    return true;
  }
  else
  {
    return false;
  }
}
      
// Function to check the numeric fields.
function NumericCheck(str)
{
  var ch;
  for(i = 0;i<str.length;i++)
  {
    ch = str.charAt(i);
    if(ch >=0 || ch<=9)
    {
      //No operation Performed
    }
    else
    {
      return 1;
    }
    if(ch == ' ')
    {
      return 1;
    }
  }
}

// Trimming functions
function ltrim(str)
{
  for (var i=0; str.charAt(i)==" "; i++);
    return str.substring(i,str.length);
}

function rtrim(str)
{
  for (var i=str.length-1; str.charAt(i)==" "; i--);
      return str.substring(0,i+1);
}

function trim(str)
{
  return ltrim(rtrim(str));
}

// Function to check the special characters.
function splCharsCheck(str)
{
  if(str.indexOf('\t') != -1)
  {
    return 0;
  }
  if(str.indexOf('\n') != -1)
  {
    //do nothing
  }

  var ch;
  for(i = 0; i < str.length; i++)
  {
    ch = str.charAt(i);  
	    
    if(ch >= '0' && ch <= '9' || ch >= 'a' && ch <= 'z' || 
       ch >= 'A' && ch <= 'Z' || ch == '_' || ch == '.' || 
       ch == '\'' || ch == '"' || ch == '&' || ch == '©' || 
       ch == '®' || ch == '-' || ch == '#' || ch == ',' || 
       ch == '/' || ch == '@' || ch == ':' || ch == '(' || 
       ch == ')' || ch == ' ' || ch == '\r\n' || ch == '\t')
    {
      //do nothing
    }
    else
    {
      return 0;
    }
  }
  return 1;
}

// Function to validate the addresses for special characters
function splCharsCheckAddr(str)
{
  var ch;
  for(i = 0; i < str.length; i++)
  {
    ch = str.charAt(i);  
	    
    if(ch >= 0 || ch <= 9 || ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' || ch == '_' || ch == '.' || ch == '\'' || ch == '"' || ch == '&' || ch == '©' || ch == '®' || ch == ',' || ch == '(' || ch == ')' || ch == '%' || ch == '$' || ch == '#' || ch == '@' || ch == '-' || ch == '/')
    {
      //do nothing
    }
    else
    {
      return 0;
    }
  }
  
  return 1;
}


/**  Method Name	:checkNull
*    Description	:Would Check the required string for null or blank or spaces.
*    @param   		:value.
*    @return  		:True/False
*    @Author
*
**/
function checkNull(val)
{
  var lblnFlag = true;
  k = 0;
  for (i=0; i<val.length; i++)
  {
    if(val.substring(i,i+1) != " ")
    {
      k++;
    }
  }

  if(val == null || val == "" || k == 0)
  {
    lblnFlag = false;
  }
  else
  {
    lblnFlag = true;
  }

  k = 0;
  if (lblnFlag == true)
  {
    for (i=0; i<val.length; i++)
    {
      if(val.substring(i, i+1) == "\t")
      {
	k++;
      }
    }
  }

  if(k == val.length)
  {
    lblnFlag = false;
  }
	
  if(val.indexOf('\t') != -1)
  {
    lblnFlag = false;
  }

  return lblnFlag;
}


function isValidPhoneNo(str)
{
  var ch;
  for(lintLoopIndex=0;lintLoopIndex<str.length;lintLoopIndex++)
  {
    ch 	= str.charAt(lintLoopIndex);
    if	(!((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')|| ch >= '0' && ch <= '9' || ch	== '-' || ch	== '+' || ch ==	'#' || ch == ' ' || ch == '(' || ch == ')'))
    {
	return false;
    }
  }
  return true;
}

