/*	-- JavaScript Library -- 			
function checktextempty(textbox,charlen,name) -->  checktextempty(document.Form.Field, minLength, "FieldName");
function checktext(textbox,charlen,name)      -->  checktext(document.Form.Field, minLength, "FieldName");
function checktextarea(textbox,charlen,name)  -->  checktextarea(document.Form.Field, minLength, "FieldName");
function chknull(txtbox, txtname)	          -->  chknull(document.Form.Field, "FieldName");
function chknull(txtbox, txtname)             -->  chknull(document.Form.Field, "FieldName");
function chkurl(txtbox,txtname)               -->  chkurl(document.Form.Field, "FieldName");
function checknumber(textbox,charlen,name)    -->  chknumber(document.Form.Field, minLength, "FieldName"); 
function checkfloat(textbox,charlen,name)     -->  checkfloat(document.Form.Field, minLength, "FieldName"); 
function checkemail(textbox,name)             -->  checkemail(document.Form.Field, "FieldName");
function isEmail(str)                         -->  isEmail(String);
function checkzip(textbox,name)				  -->  checkzip(document.Form.Field, "FieldName");
function lengthchk(textbox,charlen,name)      -->  lengthchk(document.Form.Field, minLength, "FieldName");    
function alphanumchk(textbox,name)		      -->  alphanumchk(document.Form.Field, "FieldName");
function alphanumchk2(textbox,name)	          -->  alphanumchk2(document.Form.Field, "FieldName");
function chkblank(textbox,name)				  -->  chkblank(document.Form.Field, "FieldName");
function date_validate(dd, mm , yyyy)		  -->  date_validate(01,12, 2002) 
function checkimage(textbox,name)		  -->  checkimage(document.Form.Field, "Filed name");
*/

function checktextempty(textbox,charlen,name)
 {var t;
  
  if (textbox.value != "")
   {
     var str=textbox.value;
      var ch="";
      var chk=0;
   
        for (t=0;t<textbox.value.length;t++)
         {
         
          if (str.charCodeAt(t)!=32)
           {
            chk=1;
             if (ch=="")
              {
               ch=str.charAt(t);
              }  
             else
              {
               ch=ch + str.charAt(t);
              } 
           }
          else 
           {
            if (chk==1)
             {
              ch=ch + " ";
              chk=0;
             }
           } 
         }
     
       if (ch=="")
        {
         textbox.select();
         textbox.focus();
         alert("Spaces are not allowed in " + name +".");
         return 0;
        }
  if ( charlen > 0 )
   {
     if (textbox.value.length < charlen )
      { 
       textbox.select();
       textbox.focus();
       alert( name + " cannot be less than "+ charlen +" characters");
       return 0;
      }
     if (textbox.value.length == charlen )
      {
       for(t=0;t<textbox.value.length;t++)
        {
         if ( (textbox.value.charCodeAt(t)>=48) && (textbox.value.charCodeAt(t)<=57) )
          {
           textbox.select();
           textbox.focus();
           alert("Digits are not allowed in " + name + ".");
           return 0;
          }
        }  
      }
   }
  else 
   {
  
   }
 }
}


function checktext(textbox,charlen,name)
 {
  var i;
  
  if (textbox.value == "")
   {
    textbox.select();
    textbox.focus();
    alert("Please enter " + name +".");
    return 0;
   }
  if (textbox.value != "")
   {
     var str=textbox.value;
      var ch="";
      var chk=0;
   
        for (i=0;i<textbox.value.length;i++)
         {
        /* if (textbox.value.charCodeAt(i)==44)
          {
			textbox.select();
			textbox.focus();
			alert("Comma (,) is not allowed in " + name +".");
			return 0; 
          }*/
          if (str.charCodeAt(i)!=32)
           {
            chk=1;
             if (ch=="")
              {
               ch=str.charAt(i);
              }  
             else
              {
               ch=ch + str.charAt(i);
              } 
           }
          else 
           {
            if (chk==1)
             {
              ch=ch + " ";
              chk=0;
             }
           } 
         }
     
       if (ch=="")
        {
         textbox.select();
         textbox.focus();
         alert("Spaces are not allowed in " + name +".");
         return 0;
        }
  if ( charlen > 0 )
   {
     if (textbox.value.length < charlen )
      { 
       textbox.select();
       textbox.focus();
       alert( name + " cannot be less than "+ charlen +" characters");
       return 0;
      }
     if (textbox.value.length == charlen )
      {
       for(i=0;i<textbox.value.length;i++)
        {
         if ( (textbox.value.charCodeAt(i)>=48) && (textbox.value.charCodeAt(i)<=57) )
          {
           textbox.select();
           textbox.focus();
           alert("Digits are not allowed in " + name + ".");
           return 0;
          }
          /*if (textbox.value.charCodeAt(i)==44)
          {
			textbox.select();
			textbox.focus();
			alert("Comma (,) is not allowed in " + name +".");
			return 0; 
          }*/
        }  
      }
   }
  else 
   {
       for(i=0;i<textbox.value.length;i++)
        {
        if ( (textbox.value.charCodeAt(i)>=48) && (textbox.value.charCodeAt(i)<=57) )
          {
           textbox.select();
           textbox.focus();
           alert("Digits are not allowed in " + name +".");
           return 0; 
          }
          /*if (textbox.value.charCodeAt(i)==44)
          {
			textbox.select();
			textbox.focus();
			alert("Comma (,) is not allowed in " + name +".");
			return 0; 
          }*/
        }  
   }
 }


     mystr =textbox.value;     
     sp = mystr.split("'");
     ln = sp.length;
     total=new String();
     
     for(i=0;i<=ln-2;i++)
       {
        sp[i]= sp[i]+"''";        
        total = total+sp[i];
       } 
       total = total+sp[ln-1];
       textbox.value = total;     

/****************** end changes ********************************/ 

 return 1;
}

function checktextarea(textbox,charlen,name)
 {
  var i;
  
  if (textbox.value == "")
   {
    textbox.select();
    textbox.focus();
    alert("Please enter " + name +".");
    return 0;
   }
  if (textbox.value != "")
   {
     var str=textbox.value;
      var ch="";
      var chk=0;
   
        for (i=0;i<textbox.value.length;i++)
         {
          if (str.charCodeAt(i)!=32)
           {
            chk=1;
             if (ch=="")
              {
               ch=str.charAt(i);
              }  
             else
              {
               ch=ch + str.charAt(i);
              } 
           }
          else 
           {
            if (chk==1)
             {
              ch=ch + " ";
              chk=0;
             }
           } 
         }
     
       if (ch=="")
        {
         textbox.select();
         textbox.focus();
         alert("Spaces are not allowed in " + name +".");
         return 0;
        }
  if ( charlen > 0 )
   {
     if (textbox.value.length < charlen )
      { 
       textbox.select();
       textbox.focus();
       alert( name + " cannot be less than "+ charlen +" characters");
       return 0;
      }
   }
   if  ( !(isNaN(textbox.value)) )   
   {
   alert('Number are not allowed in '+ name +'.')
   return 0;
   }
  
 }
 return 1;
}

//*********************************** Check Null ******************************

function chknull(txtbox, txtname)
{
  if (txtbox.value == '')
  {
    return 0;	
  }
  return 1;
}

//******************************** URL Format **********************************

function chkurl(txtbox,txtname)
{

  if (!chknull(txtbox, txtname)) return 1;


  if (txtbox.value.length > 0)
  {
		for (i=0;i<txtbox.value.length;i++)
		{
			cca = txtbox.value.charCodeAt(i);

			if ((cca>=65 && cca<=90) || (cca>=97 && cca<=122) || (cca>=45 && cca<=58) || cca==95)
			{
			//
			}	
			else
			{
				alert("Invalid Characters are not allowed in " + txtname + ".");
				txtbox.focus();
				return 0;	
			}
		}	
  }         
  if(txtbox.value.indexOf('http://') + txtbox.value.indexOf('HTTP://') == -2 || txtbox.value.indexOf('http://') +      txtbox.value.indexOf('HTTP://') > 0 || txtbox.value.charCodeAt(0) == 64 || txtbox.value.indexOf("..") != -1 ||    txtbox.value.indexOf("__") != -1 || txtbox.value.indexOf("--") != -1 || txtbox.value.indexOf(".") == -1) 

  {
    alert("Invalid " + txtname + " Address. \n e.g. http://www.yourdomain.com");
    txtbox.focus();
    return 0;	
  }	

  cca = txtbox.value.charCodeAt(txtbox.value.length-1);
  ccb = txtbox.value.charCodeAt(7);
  if (((cca>=65 && cca<=90) || (cca>=97 && cca<=122) || (cca>=48 && cca<=57)) && ((ccb>=65 && ccb<=90) || (ccb>=97 && ccb<=122) || (ccb>=48 && ccb<=57)))
  {
	//
  }	
  else
  {
    alert("Invalid " + txtname + " Address. \n e.g. http://www.yourdomain.com");
    txtbox.focus();
    return 0;	
  }

  return 1;
}



function checknumber(textbox,charlen,name)
 {
  var i;
  
  if (textbox.value == "")
   {
    textbox.select();
    textbox.focus();
    alert("Please enter " + name +".");
    return 0;
   }

  if (textbox.value != "")
   {
     	var str=textbox.value;
   
        if ( isNaN(textbox.value) )
        {
           textbox.select();
           textbox.focus();
           alert("Characters are not allowed in " + name +".");
           return 0;  
        }

         for (i=0;i<textbox.value.length;i++)
         {
          if (str.charCodeAt(i)==32)
           {
            textbox.select();
            textbox.focus();
            alert("Spaces are not allowed in " + name +"."); 
            return 0;
           } 
         } 
     	
     	if ( charlen > 0 ) 
       	{
        if (textbox.value.length < charlen )
         { 
           textbox.select();
           textbox.focus();
           alert(name +" cannot be of less than "+ charlen +" digits.");
           return 0;
         }
        else
         {
           if ( isNaN(textbox.value) )
            {
             textbox.select();
             textbox.focus();
             alert("Characters are not allowed in " + name +".");
             return 0;
            }
         }
       }
      else
       {
           if ( textbox.value < 0 )
            {
             textbox.select();
             textbox.focus();
             alert("Negative values are not allowed in " + name + ".");
             return 0;  
            }
       } 
      if ( textbox.value < 0 )
            {
             textbox.select();
             textbox.focus();
             alert("Negative values are not allowed in " + name + " .");
             return 0;  
            }
 
      for (i=0;i<textbox.value.length;i++)
      {
     	   if (str.charCodeAt(i)==46)
     	   {
     	     textbox.select();
     	     textbox.focus();
     	     alert("Decimal values are not allowed in " + name + "."); 
     	     return 0;
     	   } 
      } 
    
 } 
  return 1; 
 }




function checkfloat(textbox,charlen,name)
 {
  var i;
  
  if (textbox.value == "")
   {
    textbox.select();
    textbox.focus();
    alert("Please enter " + name +".");
    return 0;
   }
  if (textbox.value != "")
   {
     var str=textbox.value;
   
           if ( isNaN(textbox.value) )
            {
             textbox.select();
             textbox.focus();
             alert("Characters are not allowed in " + name +".");
             return 0;  
            }

         for (i=0;i<textbox.value.length;i++)
         {
          if (str.charCodeAt(i)==32)
           {
            textbox.select();
            textbox.focus();
            alert("Spaces are not allowed in " + name +"."); 
            return 0;
           } 
         } 
      if ( charlen > 0 ) 
       {
        if (textbox.value.length < charlen )
         { 
           textbox.select();
           textbox.focus();
           alert(name +" cannot be of less than "+ charlen +" digits.");
           return 0;
         }
        else
         {
           if ( isNaN(textbox.value) )
            {
             textbox.select();
             textbox.focus();
             alert("Characters are not allowed in " + name +".");
             return 0;
            }
         }
       }
      else
       {
           if ( textbox.value < 0 )
            {
             textbox.select();
             textbox.focus();
             alert("Negative Values are not allowed in " + name + ".");
             return 0;  
            }
       } 
      if ( textbox.value < 0 )
            {
             textbox.select();
             textbox.focus();
             alert("Negative Values are not allowed in " + name + ".");
             return 0;  
            }
 
   
 } 
  return 1; 
 }



function checkemail(textbox,name)
{
  var i;
  
   if (textbox.value == "")
   {
	textbox.select();
    textbox.focus();
    alert("Please enter " + name + ".");
    return 0;
   }
  
	if (textbox.value != "")
	{
		var str2 = textbox.value;
		var isSpace = 0;
		
		for (j=0; j<textbox.value.length; j++)
		{
			if (str2.charCodeAt(j) == 32)
				isSpace++;
		}
		
		if (parseInt(isSpace) >= 1)
		{
         textbox.select();
         textbox.focus();
         alert("Spaces are not allowed in " + name + ".");
         return 0; 
		}
	}
		
	
	if (textbox.value != "")
	{
     var str=textbox.value;
      var ch="";
      var chk=0;
   
        for (i=0; i<textbox.value.length; i++)
        {
          if (str.charCodeAt(i)!=32)
           {
            chk=1;
             if (ch=="")
              {
               ch = str.charAt(i);
              }  
             else
              {
               ch = ch + str.charAt(i);
              } 
           }
          else 
           {
            if (chk==1)
             {
              ch=ch + " ";
              chk=0;
             }
           } 
        }
       
       if (ch=="")
        {
         textbox.select();
         textbox.focus();
         alert("Spaces are not allowed in " + name + ".");
         return 0; 
        }
    }
    
     ch=0;
     ch2=0;
     
      for (i=1;i<textbox.value.length ; i++)
	   {
		if (textbox.value.charAt(i)=='@' )
			 {
			   ch=ch+1
			 }
		if (textbox.value.charAt(i)=='.' )
			 {
			   ch2=ch2+1
			 }
	   }

    for (i=1;i<textbox.value.length ; i++)
		if (textbox.value.charAt(0) == '@')
		{
			textbox.select();
			textbox.focus();	   
			alert(name + " is not correct. Special characters are not allowed other than @ sign.\ne.g. username@domain.com");
			return 0;
		}
	
	   
  	 if (ch==0 || ch > 1)
	  {
        textbox.select();
        textbox.focus();	   
		alert(name + " is not correct. @ mark not found or occurred more than once\ne.g. username@domain.com");
        return 0;
   	  }
   	  
     if (ch>1)
	  {
        textbox.select();
        textbox.focus();
		alert(name + " is not correct. please use one @ mark \ne.g. username@domain.com");
        return 0;  
   	  }
   	  
	 if (ch2==0)
	  {
        textbox.select();
        textbox.focus();
        alert(name + " is not correct \ne.g. username@domain.com");
        return 0; 
  	  }
  	  
  	return 1;
  	  
   }



function isEmail(str) 
{
	var specialchar = 0;
	
	str = new String(str);
	for (i=0; i<str.length; i++)
	{
		cca = str.charCodeAt(i);
		if ((cca>=33 && cca<=45) || (cca>=58 && cca<=63) || (cca>=91 && cca<=94) || (cca>=123) || (cca==47) || (cca==32) || (cca==96))
		{
			specialchar = 1;
		}
	}
	
	if (specialchar == 1)
		return 0;

  // are regular expressions supported?  
  var supported = 0;
  
  if (window.RegExp) 
  {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) 
		supported = 1;
  }
  if (!supported) 
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
    
	var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
	var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  
	return (!r1.test(str) && r2.test(str));

}



function checkzip(textbox,name)
 {
  var i;
  
  if (textbox.value == "")
   {
    textbox.select();
    textbox.focus();
    alert("Please enter "  + name + ".");
    return 0;
   }
  if (textbox.value != "")
   {
     var str=textbox.value;
   
        if ( isNaN(textbox.value) )
         {
           textbox.select();
           textbox.focus();
           alert("Characters are not allowed in " + name + ".");
           return 0;  
        }

         for (i=0;i<textbox.value.length;i++)
         {
          if (str.charCodeAt(i)==32)
           {
            textbox.select();
            textbox.focus();
            alert("Spaces are not allowed in " + name + "."); 
            return 0;
           } 
         } 
        if  ( textbox.value.length < 5 ) //|| ( textbox.value.length < 9 ) || ( textbox.value.length < 12 ) )
         { 
           textbox.select();
           textbox.focus();
           alert(name + " cannot be of less than 5 , 9 or 12 characters");
           return 0;
         }
	if((textbox.value.length > 5 ) && ( textbox.value.length < 9 ))
	 {
           textbox.select();
           textbox.focus();
           alert(name + " cannot be of less than 5 , 9 or 12 characters");
           return 0;
	 }
	if((textbox.value.length > 9) && ( textbox.value.length < 12 ))
	 {
           textbox.select();
           textbox.focus();
           alert(name +"  cannot be of less than 5 , 9 or 12 characters");
           return 0;
	 }
	if (parseInt(str) < 0)
	{
           textbox.select();
           textbox.focus();
           alert(name +"  cannot be less than zero");
           return 0;
	}
	for (i=0;i<textbox.value.length;i++)
	{
	   if (str.charCodeAt(i)==46)
	   {
	     textbox.select();
	     textbox.focus();
	     alert("Decimal values are not allowed in " + name + "."); 
	     return 0;
	   } 
        } 
   } 
  return 1; 
 }

/************************************length Checking **********************/
function lengthchk(textbox,charlen,name)
{
//alert(textbox.value);
 var nv = textbox.value;

   if (nv.length<charlen)
	{     textbox.select();
	      textbox.focus();
	      alert( name + " cannot be less than "+ charlen +" characters");
              return 0;
	}
return 1;
}

/*************************************Alpha Numeric Checkin**************/
function alphanumchk(textbox,name)
{
 var nv = textbox.value;
 var ok = 0;
    for(var i=0;i<=textbox.value.length-1;i++)
      {
      	cca = nv.charCodeAt(i);
		if ((cca>=48 && cca<=57) || (cca>=65 && cca<=90) || (cca>=97 && cca<=122))
			{}
		else
			ok=1;
      }
   		if (ok==1) 
   		{       textbox.select();
		        textbox.focus();
			alert("Spaces & special characters are not allowed in "+name+".");
			return 0;		
		}
return 1;
}

function chkblank(textbox,name)
{
 var nv = textbox.value;
 var ok = 0; 
 if (textbox.value.length==0)
 {
	alert("Please enter "+name+".")
	textbox.select();
	textbox.focus();
	return 0;
	
 }
 else
 {
    for(var i=0;i<=textbox.value.length-1;i++)
      {
      	cca = nv.charCodeAt(i);
      	if (cca==32)
      	{
      		Isblank=1
      		break;
      	}
      	else
      	{
      		Isblank=0
      		break;
      		
      	}
     }
     
     if (Isblank==1)
     {		textbox.select();
		    textbox.focus();
			alert("Spaces are not allowed in "+name+".")
			
			return 0;
     }
     
  }    	
      	
return 1;
}


/****************************Alpha Numeric Checkin (Don't allow only numbers) **************/
function alphanumchk2(textbox,name)
{
 var nv = textbox.value;
 var ok = 0;
 var x = 0;
 var y = 0;
 var a = 0;
 var b = 0;

	if (textbox.value == '')
	{
		alert("Please enter " + name);
		textbox.focus();
		return 0;
	}
	
// Checking for only Spaces 
	for(var i=0;i<=textbox.value.length-1;i++)
	{
		n = nv.charCodeAt(i);
		if (n==32)
			a = 1;
		else
			b = 1;
	}
	
	if (a==1 && b==0)
	{   
		textbox.select();
		textbox.focus();
		alert("Only spaces are not allowed in " +name);
		return 0;		
	}


// Checking for only Numbers	
	for(var i=0;i<=textbox.value.length-1;i++)
	{
		n = nv.charCodeAt(i);
		if (n>=48 && n<=59)
			x = 1;
		else
			y = 1;
	}
	
	if (x==1 && y==0)
	{   
		textbox.select();
		textbox.focus();
		alert(name+" cannot be a numeric");
		return 0;		
	}

	
    for(var i=0;i<=textbox.value.length-1;i++)
      {
      	cca = nv.charCodeAt(i);
		if ((cca>=48 && cca<=57) || (cca>=65 && cca<=90) || (cca>=97 && cca<=122) || (cca==32))
			{}
		else
			ok=1;
      }
   		if (ok==1) 
   		{   textbox.select();
		    textbox.focus();
			alert("Special Characters not allowed in " + name);
			return 0;		
		}
	return 1;
}

//************** Date validation function ***********************// 
function date_validate(dd, mm , yyyy)
{
   var chk    = 0;
   var maxDay = 0;

	// calling function to get maximum day for this month
	if((mm == 4) || (mm == 6) || (mm == 9) || (mm == 11))
	{	
		maxDay = 30;
	}
	else if(mm == 2)
	{
	//calling leap year function	
		maxDay = (yyyy % 4 == 0) ? 29 : 28;    
	}
	else
	{
		maxDay = 31;
	}
	
	if((dd <= 0) || (dd > maxDay))
	{	chk = 1;}
	else if((mm <= 0) || (mm > 12))
	{	chk = 1;}
	else if((yyyy <= 0))
	{	chk = 1;}	
   	
   return chk;
}

//****************************** check for image file ******************************//
 
/*function checkimage(textbox,name)
{
   var imgp  = 0;
   var exPos = 0;

   if (textbox.value != "")
   {
	imgp = textbox.value;
	exPos  = imgp.lastIndexOf('.'); 
     
        if ((imgp.substring(exPos) != '.jpg') && (imgp.substring(exPos) != '.JPG') && (imgp.substring(exPos) != '.gif') && (imgp.substring(exPos) != '.GIF') && (imgp.substring(exPos) != '.bmp') && (imgp.substring(exPos) != '.BMP'))
	{
	    textbox.select();
	    textbox.focus();
	    alert("Uploaded image must be a correct image file.");
	    return 0;	
	}
	else
	{ 
	   return 1;	
	} 
   }
   else
   {
       textbox.focus();
       alert("Select " + name +" before uploading.");
       return 0;	
   }
}*/