﻿// JScript File//**************************code to trim space from a string
/*
==================================================================
LTrim(string) : Returns a copy of a string without trailing spaces.
==================================================================
*/
function LTrim(str)
{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1)
   {
      // We have a string with leading blank(s)...

      var j=0, i = s.length;

      // Iterate from the far left of string until we
      // don't have any more whitespace...
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;

      // Get the substring from the first non-whitespace
      // character to the end of the string...
      s = s.substring(j, i);
   }
   return s;
}

/*
==================================================================
RTrim(string) : Returns a copy of a string without trailing spaces.
==================================================================
*/
function RTrim(str)
/*
   PURPOSE: Remove trailing blanks from our string.
   IN: str - the string we want to RTrim

*/
{
   // We don't want to trip JUST spaces, but also tabs,
   // line feeds, etc.  Add anything else you want to
   // "trim" here in Whitespace
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      // We have a string with trailing blank(s)...

      var i = s.length - 1;       // Get length of string

      // Iterate from the far right of string until we
      // don't have any more whitespace...
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;


      // Get the substring from the front of the string to
      // where the last non-whitespace character is...
      s = s.substring(0, i+1);
   }
    
   return s;
}

/*
=============================================================
Trim(string) : Returns a copy of a string without leading or trailing spaces
=============================================================
*/
function Trim(str)
{
   return RTrim(LTrim(str));
}

function isDate1GreaterThanDate2(Date1,Date2) 
	{
		var dt1 = new Date(Date1);
		var dt2=new Date(Date2);
		if (dt1 > dt2)
			return true;
		else 
			return false; 
	}		

/********************************** Function to Check for Valid characted ***************************/
var dtCh= "/";
var minYear=1900;
var maxYear=2100;
function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

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++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function isDate(dtStr)
{
    //alert("inside date fucntion="+dtStr)
    dtStr=Trim(dtStr);
if (dtStr=="")
{
 return true;
}
    
    var dtCh= "/";
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=Trim(dtStr.substring(0,pos1))
	var strDay=Trim(dtStr.substring(pos1+1,pos2))
	var strYear=Trim(dtStr.substring(pos2+1))
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}
function isDateWithoutMsg(dtStr)
{
    //alert("inside date fucntion="+dtStr)
    dtStr=Trim(dtStr);
if (dtStr=="")
{
 return true;
}
    
    var dtCh= "/";
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=Trim(dtStr.substring(0,pos1))
	var strDay=Trim(dtStr.substring(pos1+1,pos2))
	var strYear=Trim(dtStr.substring(pos2+1))
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
//		alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
//		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
//		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
//		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
//		alert("Please enter a valid date")
		return false
	}
return true
}
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 DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}
function checkValidChar(str,ValidChars)
 {


   var IsChar=true;
   var Char;
   str=Trim(str);
 
   for (i = 0; i < str.length && IsChar == true; i++) 
        { 
      Char = str.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
            {   
             IsChar=false;
            }
        }
   return IsChar;
}

/*************************this function for validation of decimal umber********************/
function RegExTest(element,expression)
{
var tmp=element.value;

if(tmp.indexOf(".")==0)
{
tmp="0"+tmp;
}
return tmp.match(expression) != null;
}

function IsDecimal(element)
{
var decimalRE = "^(\\+|-)?[0-9][0-9]*(\\.[0-9]*)?$";
return RegExTest(element,decimalRE);
}

function VerifyEmail(str) 
{ 
 if (str == "") 
 {
  return true;
 }

var intx;
var strchars;
strchars = "/ ? > < , ; : | ] } [ { = + ) ( * & ^ % $ # ! ` ~";
var arrchars;
//alert(strchars);

arrchars=strchars.split(' ');
for (intx=0;intx<arrchars.length;intx++)
   {     
    if(str.indexOf("" + arrchars[intx].toString() + "")!=-1)
    {
        alert("Invalid email address. Can't use " + '"'+strchars.toString()+'"');
        return false;
    }
   }

var spd = str.indexOf(".")
 if (spd == 0) 
 {
    alert("Invalid email address")
    return false
 }
 var spa = str.indexOf("@")
 if (spa == 0) 
 {
    alert("Invalid email address")
    return false
 }
 var spdd = str.indexOf("..")
 if (spdd != -1) 
 {
    alert("Invalid email address")
    return false
 }
 var spaa = str.indexOf("@@")
 if (spaa != -1) 
 {
    alert("Invalid email address")
    return false
 }
 var spad = str.indexOf("@.")
 if (spad != -1) 
 {
    alert("Invalid email address")
    return false
 }
 var spda = str.indexOf(".@")
 if (spda != -1) 
 {
    alert("Invalid email address")
    return false
 }
 
var sp = str.indexOf(" ")
if (sp != -1) {
    alert("Invalid email address, can't use spaces!")

//    thisform.email.focus();
    return false
   }

/*
----- is there a @ ?-----
*/
var str1 = str.indexOf("@")
var c = str1+1
if (str1 == -1) {
    alert("Invalid email address, no @!")
//    thisform.email.focus();
    return false
   }
/*
----- is there a period? -----
*/
var pr = str.indexOf(".",str1)
if (pr == -1) {
    alert("Invalid email address, no period, '.', or period After the @")
//    thisform.email.focus();
    return false
   }
/*
----- are there at least 2 characters between the @ and . -----
*/
if (pr - str1 - 1 < 2) {
  alert("There must be at least 2 characters between the '@' and '.'")
   return false
}

/*
----- are there at least 2 characters after the period? -----
*/
var x = str.length - pr -1
if ( x < 2 ) {
  alert("There must be at least 2 characters after the period!")
   return false
}
 var strChk,strChk1,strChk2;
 strChk=str.split('@');
 strChk1=strChk[1].toString();
 strChk2=strChk1.split('.');
 
 if (isNaN(strChk[0].toString()))
 {
 }
 else
 {
 alert("Invalid emaill address");
 return false;
 }
 if (isNaN(strChk2[0].toString()))
 {
 }
 else
 {
 alert("Invalid emaill address");
 return false;
 }
 
 if(strChk2.length>1)
 {
     if (isNaN(strChk2[1].toString()))
     {
     }
     else
     {
     alert("Invalid emaill address");
     return false;
     }
 }

 var spd = str.indexOf(".")
 if (spd == 0) 
 {
    alert("Invalid email address")
    return false
 }
 var spd = str.indexOf("@")
 if (spd == 0) 
 {
    alert("Invalid email address")
    return false
 }

 
     var strArr;
     strArr=str.split("@");
     if (strArr.length != 2)
     {
        alert("Invalid email address")
        return false;
     }
return true  // normally this will be true or removed
}



