/*
-------------------------------------------------------------------------------------------------

This is a validation file written in JavaScript. This file is used for validating Html files.
There are total 9 functions in this file.


functions

1. isAlpha(str,msgval,blnk,specs) - 

2. isAlreadyExists(NewItem, OldItems, msgval) - 

2. isNumeric(str,msgval,blnk,specs) - 

3. isAlphaNumeric(str,msgval,blnk,specs) -

4. isPhoneFax(str) - 

5. isAddress(str) - 

 6. isEmail(str) - 

7. isCurrency(str) - 

8. isDate(str) - 

9. isBlank(str,msgval)	-

10. areEqual(str1, str2) - 

11. String.prototype.ltrim - 

12. String.prototype.rtrim - 

13. String.prototype.trim - 

-------------------------------------------------------------------------------------------------
*/

/*
-------------------------------------------------------------------------------------------------
function isAlpha(str,msgval,blnk,specs)

Generalized function for validating alpha fields. Allowed chars are 'A to Z','a to z' and spaces.
This function accepts four parameters.
a) str - The string which we want to validate.
b) msgval - The message which we want to display.
c) blnk - If the string can be blank. Value for this parameter can be 0 and 1. 0 - can be blank and 1 - can not be blank.
d) specs - If the string can have spaces. Value for this parameter can be 0 and 1. 0 - spaces are not allowed and 1 - spaces are allowed.
*/

function isAlpha(str,msgval,blnk,specs)
{
  str=str.trim()
  if(blnk==1)
  {
    if(str.length==0)
    {
      alert("Please enter " + msgval)
      return 1
    }
  }

  for (var i = 0; i < str.length; i++)
  {
    var ch = str.substring(i, i + 1);
    if(specs==0)
    {
      if((ch < "a" || ch > "z" ) && (ch < "A" || ch > "Z") && ch != " ")
      {
        alert("Please enter valid " + msgval)
        return 1
      }
    }
    else
    {
      if((ch < "a" || ch > "z" ) && (ch < "A" || ch > "Z"))
      {
        alert("Please enter valid " + msgval)
        return 1
      }
    }
  }
  return 0
}

/*
-------------------------------------------------------------------------------------------------
*/

function isAlreadyExists(NewItem, OldItems, msgval)
{
  var arrOldItems = OldItems.split(",")
  for(i=0;i<arrOldItems.length;i++)
  {
    if(arrOldItems[i].toUpperCase()==NewItem.toUpperCase())
    {
      alert("Duplicate " + msgval)
      return 1
    }
  }
  return 0
}

/*
-------------------------------------------------------------------------------------------------

 *** Generalized function for validating Numeric fields
     Allowed chars are '0 to 9'
*/

function isNumeric(str,msgval,blnk,specs)
{
  str=str.trim()
  if(blnk==1)
  {
    if(str.length==0)
    {
      alert("Please enter " + msgval)
      return 1
    }
  }

  for (var i = 0; i < str.length; i++)
  {
    var ch = str.substring(i, i + 1);
    if(specs==0)
    {
      if( (ch < "0" || ch > "9") && ch != " ")
      {
        alert("Please enter valid " + msgval)
        return 1
      }
    }
    else
    {
      if( (ch < "0" || ch > "9"))
      {
        alert("Please enter valid " + msgval)
        return 1
      }
    }
  }
  return 0
}   

/*
-------------------------------------------------------------------------------------------------

 *** Generalized function for validating alphanumeric fields
     Allowed chars are 'A to Z' , 'a to z' and '0 to 9'
*/

function isAlphaNumeric(str,msgval,blnk,specs)
{
  str=str.trim()
  if(blnk==1)
  {
    if(str.length==0)
    {
      alert("Please enter " + msgval)
      return 1
    }
  }
  for (var i = 0; i < str.length; i++)
  {
    var ch = str.substring(i, i + 1);
    if(specs==0)
    {
      if((ch < "a" || ch > "z") && (ch < "A" || ch > "Z") && (ch < "0" || ch > "9") && ch!=" ")
      {
        alert("Please enter valid " + msgval)
        return 1
      }
    }
    else
    {
      if((ch < "a" || ch > "z") && (ch < "A" || ch > "Z") && (ch < "0" || ch > "9"))
      {
        alert("Please enter valid " + msgval)
        return 1
      }
    }
  }
  return 0
}

/*
-------------------------------------------------------------------------------------------------

 *** Generalized function for validating phone or fax kind of fields
     Allowed chars are '0 to 9' , 'Plus (+)' and 'Minus (-)'
*/

function isPhoneFax(str)
{
 	for (i=0; i<str.length; i++)
 	{
  		ch=str.substring(i, i+1)
  		if( (ch < "0" || ch > "9") && (ch!="+") && (ch!="-") && (ch!="(") && (ch!=")") )
   			return 1
 	}
 	return 0
}   

/*
-------------------------------------------------------------------------------------------------

 *** Generalized function for validating address fields
     Allowed chars are 'A to Z' , 'a to z' , '0 to 9' , 'Spaces' , 'Hash (#)' , 'Backslash (/)' , 'Coma (,)' and 'Minus (-)'
*/

function isAddress(str)
{
 	for (i=0; i < str.length; i++)
 	{
  		ch=str.substring(i, i+1)
  		if((ch < "a" || ch > "z") && (ch < "A" || ch > "Z") && (ch < "0" || ch > "9") && ch!=" " && ch!="#" && ch!="/" && ch!="," && ch!="-")
   			return 1
 	}
 	return 0
}

/*
-------------------------------------------------------------------------------------------------

 *** generalized function for validating e-mail fields for valid chars
     Allowed chars are 'A to Z' , 'a to z' , '0 to 9' , 'Unserscore (_)' , 'Minus (-)' , 'At the rate (@)' , 'Full Stop (.)'
 *** Generalized function for checking the valid emails
     this function checks two chars '@' and "." in email. if existed, the email is valid therewise not.
*/

function isEmail(str,blnk)
{
  var aa=0
  var aa1=0
  str=str.trim()
  if(blnk==1)
  {
    if(str.length==0)
    {
      alert("Please enter Email Id")
      return 1
    }
  }

  for (i=0; i<str.length; i++)
  {	
    ch=str.substring(i, i+1)
    if( (ch < "a" || ch > "z" )&&(ch < "A" || ch > "Z")&&(ch  < "0" || ch > "9")&&(ch != "_")&&(ch != "-")&&(ch != "@")&&(ch != "."))
    {
      alert("Please enter valid Email Id")
      return 1
    }

    if(ch=="@") 
      aa=1

    if(ch==".")
      aa1=1
  } 

  if(aa!=1 || aa1!=1)
  {
    alert("Please enter valid Email Id")
    return 1
  }
  return 0
} 

/*
-------------------------------------------------------------------------------------------------

 *** generalized function for validating currency fields
*/

function isCurrency(str,msgval,blnk,specs)
{
  var NoOfDot=0
  str=str.trim()
  if(blnk==1)
  {
    if(str.length==0)
    {
      alert("Please enter " + msgval)
      return 1
    }
  }

  for (var i = 0; i < str.length; i++)
  {
    var ch = str.substring(i, i + 1);

    if(ch==".")
      NoOfDot=NoOfDot+1

    if(NoOfDot>=2)
    {
      alert("Please enter valid " + msgval)
      return 1
    }

    if(specs==0)
    {
      if( (ch < "0" || ch > "9") && ch != " " && (ch!="."))
      {
        alert("Please enter valid " + msgval)
        return 1
      }
    }
    else
    {
      if( (ch < "0" || ch > "9") && (ch!="."))
      {
        alert("Please enter valid " + msgval)
        return 1
      }
    }
  }
  return 0
/*
	var NoOfDot=0
	var stpos=0
	var ifstop=0
	for(i=0; i<str.length; i++)
	{
		ch=str.substring(i,i+1)
		if(ch==".")
			NoOfDot=NoOfDot+1

		if((ch  < "0" || ch > "9") && (ch!="."))
			return -1

		if(NoOfDot>=2)
			return -2

		if(ch!="0" && ifstop==0)
		{
			stpos=i
			ifstop=1
		}
	}

	str=str.substring(stpos,str.length)
	return str
*/
}

/*
-------------------------------------------------------------------------------------------------

 *** generalized function for validating date fields
*/

function isDate(str,blnk)
{
  var dd,mm,yy,days_in_month
  var msg="Please enter valid Date. (dd/mm/yyyy format)"

  str=str.trim()
  if(blnk==1)
  {
    if(str.length==0)
    {
      alert("Please enter Date")
      return 1
    }
  }

  for (i=0; i<str.length; i++)
  {
    ch=str.substring(i, i+1)
    if( (ch < "0" || ch > "9") && (ch!="/") )
    {
      alert(msg)
      return 1
    }

    if((i==2 || i==5) && ch!='/')
    {
      alert(msg)
      return 1
    }
  }


  if(str !="")
  {
    dd=str.substring(0,2)
    mm=str.substring(3,5)
    yy=str.substring(6,str.length)

    if(dd>31 || mm>12 || yy.length !=4)
    {
      alert(msg)
      return 1
    }


    if((yy%100)==0)
    {
      if((yy%40)==0)
        days_in_month = "312931303130313130313031"
      else
        days_in_month = "312831303130313130313031"
    }
    else
    {  
      if((yy%4)==0) 
        days_in_month = "312931303130313130313031"
      else
        days_in_month = "312831303130313130313031"
    }

    if(dd > (days_in_month.substring(2*(mm-1),(2*(mm-1)+2))) )
    {
      alert(msg)
      return 1
    }
  }

    return 0
}

/*
-------------------------------------------------------------------------------------------------
*/

function isBlank(str,msgval)
{ 
  str=str.trim()
  if(str.length==0)
  {
    alert("Please enter " + msgval)
    return 1
  }
  else
  {
    return 0
  }
}

/*
-------------------------------------------------------------------------------------------------
*/

function areEqual(str1, str2)
{
	if(str1 == str2)
		return 1
	else
		return 0
}

/*
-------------------------------------------------------------------------------------------------
*/

String.prototype.ltrim = function()
{
  return this.replace(/^\s*/, "");
}

/*
-------------------------------------------------------------------------------------------------
*/

String.prototype.rtrim = function()
{
  return this.replace(/\s*$/, "");
}

/*
-------------------------------------------------------------------------------------------------
*/

String.prototype.trim = function() 
{
  return this.replace(/^\s*(\b.*\b|)\s*$/, "$1");
}

/*
********** END *************
-------------------------------------------------------------------------------------------------
*/


function ArraySearch(NewItem, OldItems, Delimiter)
{
  var arrOldItems = OldItems.split(Delimiter)
  for(i=0;i<arrOldItems.length;i++)
  {
    if(arrOldItems[i].toUpperCase()==NewItem.toUpperCase())
    {
      return 1
    }
  }
  return 0
}