/** This function allows only numbers to be allowed as input characters for the event onkeypress **/
function isNumberKey(evt)
{
   var charCode = (evt.which) ? evt.which : evt.keyCode;
   if (charCode > 31 && (charCode < 48 || charCode > 57))
      return false;

   return true;
}

function isCharacter(evt){

 var charCode = (evt.which) ? evt.which : evt.keyCode;
 if(charCode > 31 && (charCode < 64 || charCode > 92) && (charCode < 96 || charCode > 122))
{
   return false;
}
return true;

}

function isCharacter_names(evt){

 var charCode = (evt.which) ? evt.which : evt.keyCode;
 if(charCode > 31 && (charCode < 64 || charCode > 92) && (charCode < 96 || charCode > 122) && charCode != 45)
{
   return false;
}
return true;

}

function isCharacter_state(evt){

 var charCode = (evt.which) ? evt.which : evt.keyCode;
 if(charCode != 32 && charCode > 31 && (charCode < 64 || charCode > 92) && (charCode < 96 || charCode > 122) && charCode != 45)
{
   return false;
}
return true;

}

function isCharacterLastname(evt){
 var charCode = (evt.which) ? evt.which : evt.keyCode;
 if(charCode!=39 && charCode != 45 && charCode > 31 && (charCode < 64 || charCode > 92) && (charCode < 96 || charCode > 122))
{
   return false;
}
return true;

}
	
function trim(string) {
	return string.replace(/^\s+|\s+$/g,"");
	}	
	
		// validate special chars...
function isSpclChar(evt){

 var charCode = (evt.which) ? evt.which : evt.keyCode;
   if (charCode > 32 && charCode<48)
      return false;

   return true;   
}

//Function to compare if given two dates are valid  
function compareDates(date1,date2,seperator){

  var arrD1= new Array();
  arrD1=date1.split(seperator); //will be split as day, month ,year
  var arrD2= new Array();
  arrD2=date2.split(seperator);
  
  //Set the two dates
  var startingDate=new Date(arrD1[2], arrD1[1]-1, arrD1[0]); //Month is 0-11 in JavaScript
  var endingDate=new Date(arrD2[2], arrD2[1]-1, arrD2[0]);
  //alert(startingDate+" ---- "+endingDate);
  //Set 1 day in milliseconds
  var one_day=1000*60*60*24

//Calculate difference btw the two dates, and convert to days
 var diff=(endingDate.getTime()-startingDate.getTime())/(one_day);
 if(diff<0){
  return 0; //dates are invalid, if negative diff
 }else{
  return 1; //dates are valid
 }

}
//Function to compare if given two dates are valid  
function compare_currentDates(date2,seperator){
  var month = today.getMonth();
  var day = today.getDate();
  var year = today.getFullYear(); 
  var curdate=day+'-'+month+'-'+year;
  alert(curdate);
  var arrD1= new Array();
  arrD1=curdate.split(seperator); //will be split as day, month ,year
  var arrD2= new Array();
  arrD2=date2.split(seperator);
  
  //Set the two dates
  var startingDate=new Date(arrD1[2], arrD1[1]-1, arrD1[0]); //Month is 0-11 in JavaScript
  var endingDate=new Date(arrD2[2], arrD2[1]-1, arrD2[0]);
  //alert(startingDate+" ---- "+endingDate);
  //Set 1 day in milliseconds
  var one_day=1000*60*60*24

//Calculate difference btw the two dates, and convert to days
 var diff=(endingDate.getTime()-startingDate.getTime())/(one_day);
 if(diff<0){
  return 0; //dates are invalid, if negative diff
 }else{
  return 1; //dates are valid
 }

}

function aftermsg(div){
  $(div).fadeOut(fadeoutInterval);
}

function beforemsg(div){
  $(div).fadeIn(0);
}

  /**
   * Loads in a URL into a specified divName, and applies the function to
   * all the links inside the pagination div of that page (to preserve the ajax-request)
   * @param string href The URL of the page to load
   * @param string divName The name of the DOM-element to load the data into
   * @return boolean False To prevent the links from doing anything on their own.
   */
  function loadPiece(href,divName) {  
      $(divName).load(href, {}, function(){
          var divPaginationLinks = divName+" #pagination a";
          $(divPaginationLinks).click(function() {     
              var thisHref = $(this).attr("href");
              loadPiece(thisHref,divName);
              return false;
          });
      });
  } 

function count_char(e,totalchar,spanid,id){
   var len=document.getElementById(id).value; 
    var key;     
                           
   if(window.event){
        key = window.event.keyCode;     //IE
    }
    else{
        key = e.which;     //firefox
    }

    if((len.length+1) <= totalchar){
         if(key!=8){ 
          document.getElementById(spanid).innerHTML="&nbsp;&nbsp;[ "+(totalchar-(len.length+1))+" ] characters";
         }else{
             document.getElementById(spanid).innerHTML="&nbsp;&nbsp;[ "+((totalchar-1)-(len.length-1))+" ] characters";
         } 
    }else{
     if(key!=8){ // check for backspaces
      return false;
     }
    }
}

function checkfileext(name){
      var imgname=name;
      var ext = imgname.split('.');
      var e=ext[1].toLowerCase();
      if(e!='gif' && e!='jpg' && e!='jpeg' && e!='png' && e!='bmp'){
        return false;
      }else 
        return true;
}

function checkAll(divid,v)
{
  
    var s=document.getElementById(v);
    var field=document.getElementById(divid);
    var c=field.getElementsByTagName("input").length;
    
    for(var i=0;i<=c-1;i++)
    {
    
      var ch=field.getElementsByTagName("input").item(i);
       
      if(s.checked==true)
      {
        ch.checked=true;
      }
      else
      {
        ch.checked=false;
      }
    }
}

//email validation
/*
function isValidEmailAddress(emailAddress) {
var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}
*/
function isValidEmailAddress(stremail)
{ 
  var err;
  var str=stremail;
  var ValidChars = "0123456789-_.";
  var validdom   = "0123456789";
  var illegalchars="#$!%^&*();:<>+/\?|";
  var i;
  var c;
  var l;
  var n;
  var at="@";
  var dot=".";
  var lat=str.indexOf(at);
  var lstr=str.length;
  var ldot=str.indexOf(dot);
  
    i=0;
   c = str.charAt(i); 
  
   if (ValidChars.indexOf(c) !=-1) 
   { return false; }
 
    //commented on 29 08 09
    // l=str.indexOf(dot)+1;  
    // n=str.charAt(l);
  
   if(n=="")
   { return false; }
   
   for(i=l;i<lstr;i++)
   {
     l=str.charAt(i); 
		 
	 if (validdom.indexOf(l)!=-1) 
	 { return false; }
   }
   
   	clen=illegalchars.length;
 	for(j=0;j<clen;j++)
	{
		c=illegalchars.charAt(j);
		if(str.indexOf(c)>0)
		{
		 return false;	
		}
	}
   
  if (str.indexOf(at)==-1){
     return false;
  }
  if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
     return false;
  }
  if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
      return false;
  }
  
   if (str.indexOf(at,(lat+1))!=-1){
      return false;
   }
   if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
      return false;
   }
   
   if (str.indexOf(dot,(lat+2))==-1){
      return false;
   }
   
   if (str.indexOf(" ")!=-1){
      return false;
     
   }
    
      return true ;        
}

//file extension type
function checkfileType(name){
     var filename=name;
     ext = filename.split('.');
     strext = ext.toString();
     var count = file_type.length;
     var match = 0;
   
      for(var i=0; i<count; i++){
     //  alert('ext[1]'+ext[1]+'file_type[i]'+file_type[i]);
      
        if(strext.toLowerCase() == file_type[i]){
         match++;
        }
      }

      if(match == 0){
        return false;
      }else 
        return true;
}
function SubMe(){document.forms[0].submit();}

