function trim(z){
	var str=new String(z);
	var count=0;
	var i=0;
	var j=str.length;
	var s="";
	var s1="";

	s=str.substring(i,j);

	for(var k=0;k<s.length;k++){
	if((s.charAt(k))!=" "){
	s1+=s.charAt(k);
	count=1;
	}
	else {
	if(((s.charAt(k))==" ")&&(count==1)){
	s1+=" ";
	count=-1;
	}
	     }}
	var s2="";
	var x=0;
	x=s1.length;

	if((s1.charAt(s1.length-1))!=" ")
	s1=s1.substring(0,x);
	else
	s1=s1.substring(0,x-1);

	return s1;
}


/*function validEmail(email)
{
   invalidChars="/:,;"
   if (email=="")
   {
       return false;
   }
   for(i=0;i<invalidChars.length;i++)
   {
       badChar=invalidChars.charAt(i);
       if(email.indexOf(badChar,0)!=-1)
       {
           return false;
       }
    }
    atPos=email.indexOf("@",1);
    if(atPos==-1)
    {
        return false;
    }
    if(email.indexOf("@",atPos+1)!=-1)
    {
        return false;
    }
    periodPos=email.indexOf(".",atPos);
    if(periodPos==-1)
    return false;
    if(periodPos+3>email.length)
    {
        return false;
    }
    return true;
}*/


function validEmail (emailStr) {
	/* The following pattern is used to check if the entered e-mail address
	   fits the user@domain format.  It also is used to separate the username
	   from the domain. */
	var emailPat=/^(.+)@(.+)$/
	/* The following string represents the pattern for matching all special
	   characters.  We don't want to allow special characters in the address. 
	   These characters include ( ) < > @ , ; : \ " . [ ]    */
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	/* The following string represents the range of characters allowed in a 
	   username or domainname.  It really states which chars aren't allowed. */
	var validChars="\[^\\s" + specialChars + "\]"
	/* The following pattern applies if the "user" is a quoted string (in
	   which case, there are no rules about which characters are allowed
	   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
	   is a legal e-mail address. */
	var quotedUser="(\"[^\"]*\")"
	/* The following pattern applies for domains that are IP addresses,
	   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
	   e-mail address. NOTE: The square brackets are required. */
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	/* The following string represents an atom (basically a series of
	   non-special characters.) */
	var atom=validChars + '+'
	/* The following string represents one word in the typical username.
	   For example, in john.doe@somewhere.com, john and doe are words.
	   Basically, a word is either an atom or quoted string. */
	var word="(" + atom + "|" + quotedUser + ")"
	// The following pattern describes the structure of the user
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	/* The following pattern describes the structure of a normal symbolic
	   domain, as opposed to ipDomainPat, shown above. */
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	
	
	/* Finally, let's start trying to figure out if the supplied address is
	   valid. */
	
	/* Begin with the coarse pattern to simply break up user@domain into
	   different pieces that are easy to analyze. */
	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) {
	  /* Too many/few @'s or something; basically, this address doesn't
		 even fit the general mould of a valid e-mail address. */
		alert("Email address seems incorrect (check @ and .'s)")
		return false
	}
	var user=matchArray[1]
	var domain=matchArray[2]
	
	// See if "user" is valid 
	if (user.match(userPat)==null) {
		// user is not valid
		alert("The username doesn't seem to be valid.")
		return false
	}
	
	/* if the e-mail address is at an IP address (as opposed to a symbolic
	   host name) make sure the IP address is valid. */
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
		// this is an IP address
		  for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				alert("Destination IP address is invalid!")
			return false
			}
		}
		return true
	}

	// Domain is symbolic name
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		alert("The domain name doesn't seem to be valid.")
		return false
	}
	
	/* domain name seems valid, but now make sure that it ends in a
	   three-letter word (like com, edu, gov) or a two-letter word,
	   representing country (uk, nl), and that there's a hostname preceding 
	   the domain or country. */
	
	/* Now we need to break up the domain to get a count of how many atoms
	   it consists of. */
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || 
		domArr[domArr.length-1].length>3) {
	   // the address must end in a two letter or three letter word.
	   alert("The address must end in a three-letter domain, or two letter country.")
	   return false
	}
	
	// Make sure there's a host name preceding the domain.
	if (len<2) {
	   var errStr="This address is missing a hostname!"
	   alert(errStr)
	   return false
	}
	
	// If we've gotten this far, everything's valid!
	return true;
}


function checkChar(obj){
	obj.value=trim(obj.value);
	var temp;
	var spacenum;
	var spacelet;
	temp=new String(obj.value);
	for( var k=0;k<temp.length;k++){
		if(((temp.substring(k,k+1)<"a" || "z"<temp.substring(k,k+1) ) && (temp.substring(k,k+1)<"A" || "Z"<temp.substring(k,k+1))) && temp.substring(k,k+1)!=' ' &&  temp.substring(k,k+1)!="'" ){
		//	This field accepts only letters and spaces
			obj.select();
			obj.focus();
			return false;
		}
	}
	temp=temp.substring(0,1) + temp.substring(1,temp.length);
	for( var i=1;i<=temp.length;i++)
	{
	for(var j=i;temp.substring(j,j+1) != " " && j<=temp.length;j++);
	i=j;
	if (j<=temp.length)
	      spacenum=j;
	else
	      spacenum=-1;


	if(spacenum>-1)
	{
	 spacelet=temp.substring(spacenum+1,spacenum+2);
	 temp=temp.substring(0,spacenum)+" "+spacelet+temp.substring(spacenum+2,temp.length);
	}
	}

	obj.value=temp;
	return true;
}

function checkDigit(field,str){
      field.value=trim(field.value);
      var digits="0123456789-";
      var temp;
      var num=field.value;

      for(i=0;i<num.length;i++) {
	      temp=num.substring(i,i+1);
	      if (digits.indexOf(temp)==-1)  {
    		  //alert("Please enter a proper "+str+" number here");
		      return false;
	      }
     }
     return true;
}

function strComp(obj1, obj2){
	if(trim(obj1.value) != trim(obj2.value))
		return false;
	else
		return true;
		
}

function checkDecimal(field,decimalplace){
      field.value=trim(field.value);
	  var dpoint = 0;
	  var afterdec = 0;
      var digits="0123456789-.";
      var temp;
      var num=field.value;

      for(i=0;i<num.length;i++) {
	      temp=num.substring(i,i+1);
		  if(dpoint > 0)
		  	afterdec++;	
		  if(temp == ".") {
		  	if(dpoint > 0)
				return false;
			else
			  	dpoint = i+1;
			}
	      if (digits.indexOf(temp)==-1)  {
    		  //alert("Please enter a proper "+str+" number here");
		      return false;
	      }
     }
	 if(decimalplace == 0 & num.indexOf(".") >= 0) {
	 	return false;	 
	 }
	 if(afterdec > decimalplace)
	 	return false;
	else
	     return true;
}

function checkCreditCard(field){
      field.value=trim(field.value);
      var digits="0123456789";
      var temp;
      var num=field.value;
	  if(parseInt(num.length) > 16)
	  	return false;
      for(i=0;i<num.length;i++) {
	      temp=num.substring(i,i+1);
	      if (digits.indexOf(temp)==-1)  {
    		  //alert("Please enter a proper "+str+" number here");
		      return false;
	      }
     }
	 
     return true;
}

function checkCardValid(field){
      field.value=trim(field.value);
      var digits="0123456789";
      var temp;
      var num=field.value;
      var num1=num.substring(0,2);
      var sep=num.substring(2,3);
      var num2=num.substring(3,7);
	if(num.length != 7)
		return false;
	if(sep != '/')
	  	return false;
	if(num1.length != 2)
		return false;
	if(num2.length !=4)
		return false;
	if(parseInt(num1) > 12 & parseInt(num1) < 1)
		return false;
	
    for(i=0;i<num1.length;i++) {
    	temp=num.substring(i,i+1);
    	if (digits.indexOf(temp)==-1)  {
    	    return false;
	    }
	}
    for(i=3;i<num2.length;i++) {
    	temp=num.substring(i,i+1);
    	if (digits.indexOf(temp)==-1)  {
    	    return false;
	    }
	}
     return true;
}