﻿// 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(pos1+1,pos2)) //(0,pos1))
	var strDay=Trim(dtStr.substring(0,pos1))        //(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
}

//Work: To check Numeric values excluding '.' also. This function Added by Sunil Pundir
function IsNumberOnly(sText)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;
   for (i = 0; i < sText.length && IsNumber == true; i++) 
   { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
            {
              IsNumber = false;
            }
   }
   return IsNumber;
}

//Work: To check zero string e.g. "00000". This function is Added by Sunil Pundir. Date: 7 Nov 2006
function IsZeroString(sText)
{
   var IsValid=true;
   var Char;
   for (i = 0; i < sText.length && IsValid == true; i++) 
   { 
      Char = sText.charAt(i); 
      if (Char != '0') 
      {
         IsValid = false;
      }
   }
   return IsValid;
}


function CheckInteger (thisform, lower, upper) 
 {
   var thisint = parseInt(thisform.year.value, 10)
   var thisfloat = parseFloat(thisform.year.value, 10)

   if (thisint != thisfloat) 
        {
    alert("You didn't enter an integer!")
    thisform.year.focus()
    return false
         }
    else 
        {
       if (lower <= thisint && thisint <= upper) 
               { // is integer in the range?
           alert("The value " + thisform.year.value + " is OK!")
                return false // normally this should be true but it is false so the page with not reload
               }
           else {
                  if (lower == upper && lower==thisint) {   // if range is one number and this integer then OK
                     return false // normally this should be true but it is false so the page with not reload
                         }
                    else {
                       alert("The value has to be in the range, " + lower + " <= X <= " + upper)
                         thisform.year.focus()
                         return false
                             }
                      }
              }
         }
         
    //Work: function for date checks. Added by sunil Pundir on 9 sept 2005
    function DateValidation()
	{
		
		if(isEmptyString(document.form1.datefrom.value))
		{
			//return true;
		}
		else
		{
			if(!(GreateThanCurrentDate(document.form1.datefrom.value)))
			{
				alert("From date should be less than or equal to current date.")
				document.form1.datefrom.focus();
				return false;
			}		
		}
				
		if(isEmptyString(document.form1.dateto.value))
		{
			
		}
		else
		{
			if(!(GreateThanCurrentDate(document.form1.dateto.value)))
			{
				alert("To date should be less than or equal to current date.")
				return false;
			}
		}
		
		//To check whether end date is less tha start date
		if (!(doDateCheck(document.form1.datefrom.value, document.form1.dateto.value)))
		 {
			return false;		 
		 }	
	}
	
	//Work: To compare two dates. Added by sunil Pundir on 9 sept 2006
	function doDateCheck(from, to) 
	{
		var sDate = new Date(from);
		var eDate = new Date(to);
		if (sDate > eDate)
		{
			//alert("Please select to date greater  than from date.");
			return false;
		}
		else 
		{
			return true; 
		}
	}
	
	
    //Work: To check if entered date is greater than current Date. Added by sunil Pundir on 9 sept 2006
	function GreateThanCurrentDate(ChkDate) 
	{
		var sDate = new Date(ChkDate);
		var dt=new Date();
		if (sDate > dt)
		{
			//alert("Please select a date which is less than or equal to current date.");
			return false;
		}
		else 
		{
			return true; 
		}
	}		
  //-Jasvinder 
function LessThanCurrentDate(ChkDate)  
	{
		var sDate = new Date(ChkDate);
		var dt=new Date();
    	var monthnumber = dt.getMonth();
        var monthday    = dt.getDate();
        var year        = dt.getYear();
        var now = new Date(year,monthnumber,monthday);
        
      if (sDate  >= now)
		{
			return true;
		}
		else if (sDate < now)
		{
			return false; 
		}
	}		

//Work: To open New help window. Added by: Sunil Pundir
 function openHelp(helpFileName)
 {
   //alert('helpFileName='+helpFileName)
   var filename="./help/"+helpFileName
   //alert("filename="+filename)
   window.open(filename,null,"height=300,width=500,status=yes,toolbar=no,menubar=no,location=no,titlebar=0,status=Yes,resizable=no,directories=no");
   return false;
 
 }


// This Function added by Narendra Singh on 5-Sept=06
function DeleteRecord()
    {
      if(confirm("Are you sure you want to delete this record.?"))
      {
           return true;
      }   
      else
      {
           return false;
      }
    }


//Work: To check Numeric values including '.' also. This function Added by Sunil Pundir
//
function IsNumeric(sText)
    {
    
//   var ValidChars = "0123456789.";
//   var IsNumber=true;
//   var Char;
// 
//   for (i = 0; i < sText.length && IsNumber == true; i++) 
//        { 
//      Char = sText.charAt(i); 
//      if (ValidChars.indexOf(Char) == -1) 
//            {
//           
//            IsNumber = false;
//            
//            }
//        }
    if(isNaN(sText))
    {
      return false;
    }
    else
    {
      return true;
    }
    
    
 }

//Work: To check Numeric values excluding '.' also. This function Added by Sunil Pundir
function IsNumberOnly(sText)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;
   for (i = 0; i < sText.length && IsNumber == true; i++) 
   { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
            {
              IsNumber = false;
            }
   }
   return IsNumber;
}

//Work: To check zero string e.g. "00000". This function is Added by Sunil Pundir. Date: 7 Nov 2006
function IsZeroString(sText)
{
   var IsValid=true;
   var Char;
   for (i = 0; i < sText.length && IsValid == true; i++) 
   { 
      Char = sText.charAt(i); 
      if (Char != '0') 
      {
         IsValid = false;
      }
   }
   return IsValid;
}


function CheckInteger (thisform, lower, upper) 
 {
   var thisint = parseInt(thisform.year.value, 10)
   var thisfloat = parseFloat(thisform.year.value, 10)

   if (thisint != thisfloat) 
        {
    alert("You didn't enter an integer!")
    thisform.year.focus()
    return false
         }
    else 
        {
       if (lower <= thisint && thisint <= upper) 
               { // is integer in the range?
           alert("The value " + thisform.year.value + " is OK!")
                return false // normally this should be true but it is false so the page with not reload
               }
           else {
                  if (lower == upper && lower==thisint) {   // if range is one number and this integer then OK
                     return false // normally this should be true but it is false so the page with not reload
                         }
                    else {
                       alert("The value has to be in the range, " + lower + " <= X <= " + upper)
                         thisform.year.focus()
                         return false
                             }
                      }
              }
         }
         
         
function CheckFloating(thisform, lower, upper)
 {
     ndot = 0
     var thisfloat = thisform.floatnumber.value 
       for (i=0; i < thisfloat.length; i++) 
       {
    var ch = thisfloat.substring(i,i+1)
    if ( (ch < "0" || "9" < ch) && ch !=".") 
            {
      alert ( "Invalid Floating Point Number!")
       thisform.floatnumber.focus()
       return false
           }
		if (ch == "."){ndot = ndot + 1}    
    }
    
if (ndot > 1){
    alert ( "Only 1 period please!")
       thisform.floatnumber.focus()
       return false
	 }
if (lower == upper) 
     {
    return true
      }
  else { 
if (thisfloat < lower || upper < thisfloat) {
     alert("This number must be in the range, " + lower + " <= X <= " + upper)
      thisform.floatnumber.focus()
      return false
           }
  }

   alert("Floating Point Number Is OK!") // optional alert
return true;
}


function CheckCharacters(thisform) {
  if ( thisform.characters.value == "" ) {
    alert ( "Please Enter Data")
    thisform.characters.focus()
    return false
    }

  var str = thisform.characters.value
  var slen = str.length
  for (i=0; i < slen; i++) {
    var ch = str.substring(i,i+1)
    if ( (ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch) && ch != " ") {
     alert("The " + (i+1)  + "th Character Is Invalid!")
     thisform.characters.focus()
     return false
     }
  }
  alert("Vaild Entry") // optional alert
  return true
}
