// JavaScript Document
var alpha_numb = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
var valid_usrname = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_";
var alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";
var valid_zip = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-";
var num = "0123456789";
var valid_city = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ- ";
	function res(t,v)
	{
		var w = "";
		var flag = 0;
		for (i=0; i < t.value.length; i++)
		{
			x = t.value.charAt(i);
			//indexOf has a useful return value of -1 if the character or string you searched for is not contained within the string. 
			if (v.indexOf(x,0) != -1)
			{
				w += x;
			}
			else
			{ 	
				
				flag = 1;
				//break;
			}
		}
		t.value = w;
		return flag;
		
		
	}
	
function register_check()
{
		new_user();
		if( err=="")
		{
		return true;	 
		}
		else
		{	alert(err);
			return false;
		}		
		
}

function new_user()
{

	    var form = document.newuser;
	    err=""; 

         /********email validation********/	
		 if (form.email.value == "") 
		 {
		err = err +"\n" ;
		err = err+ "Please Enter Email Address" ;
		}
		
		else{
		emailStr=form.email.value;
		var checkTLD=1;
		var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
		var emailPat=/^(.+)@(.+)$/;
		var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
		var validChars="\[^\\s" + specialChars + "\]";
		var quotedUser="(\"[^\"]*\")";
		var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
		var atom=validChars + '+';
		var word="(" + atom + "|" + quotedUser + ")";
		var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
		var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
		var matchArray=emailStr.match(emailPat);

		if (matchArray==null) {
			err=err+"\n";
			err=err+"Email address seems incorrect (check @ and .'s)";
			return false;
		}
		var user=matchArray[1];
		var domain=matchArray[2];

		for (i=0; i<user.length; i++) {
			if (user.charCodeAt(i)>127) {
				err=err+"\n";
				err=err+"Ths email name contains invalid characters";
				return false;
			   }
			}
		for (i=0; i<domain.length; i++) {
				if (domain.charCodeAt(i)>127) {
					err=err+"\n";
					err=err+"This domain name of email contains invalid characters.";
					return false;
			   }
			}
		
		if (user.match(userPat)==null) {
			err=err+"\n";
			err=err+"The email name doesn't seem to be valid";
			return false;
		}
		
		var IPArray=domain.match(ipDomainPat);
		if (IPArray!=null) {
		
			// this is an IP address
			
			for (var i=1;i<=4;i++) {
				if (IPArray[i]>255) {
					err=err+"\n";
					err=err+"Destination IP address of this email is invalid!";
					return false;
				   }
			}
			//return true;
		}

		var atomPat=new RegExp("^" + atom + "$");
		var domArr=domain.split(".");
		var len=domArr.length;
		for (i=0;i<len;i++) {
			if (domArr[i].search(atomPat)==-1) {
				err=err+"\n";
				err=err+"The domain name of this email does not seem to be valid.";
				return false;
			   }
		}
		if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) 
		{	err=err+"\n";
			err=err+"The email address must end in a well-known domain or two letter " + "country.";
			return false;
		}
		
		if (len<2) {
			
			err=err+"\n";
			err=err+"This email address is missing a hostname!";
			return false;
		}


		}
		
      /********password validation********/	
	  
	
	
	if(err !="")
	{return false;}else
	{return true;}	
	
}
