
var request = makeObject();
var focusSet = 0 ;



function varFocus(inputVar)
{
	
	if(focusSet == 0)
	{
		inputVar.focus();
		focusSet = 1 ;
	}	
	
}
function check_username()
{
	
	username = document.getElementById('username')  ;
	
	if(username.value.length < 3)
	{
		username.value = "" ;
		username.focus();
		print_error("username_hint","Invalid Username");
		reg_disable("disabled");

	}
	else
	{
		print_ok("username_hint","Checking " + username.value);
		var now = new Date();
		request.open('get','remote.os?section=register&cmd=check_username&username='+username.value+'&tm='+now);
		request.onreadystatechange = parseReg; 
		request.send('');	
		
	}


}


function parseReg()
	{
		username = document.getElementById('username')  ;
		if(request.readyState == 1)
			{
				print_ok("username_hint","Verifying " + username.value );
			}
		
		if(request.readyState == 4)
			{ 
		
				var answer = request.responseText;
				
				//alert(answer);
				if(answer == "OK")
				{
					print_ok("username_hint","<strong>" + username.value + "</strong> is available");
					reg_disable(false);
					focusSet = 0 ;
				}
				else
				{
					
					print_error("username_hint",answer );
					reg_disable("disabled");
					username.value = "" ;
					varFocus(username);

				}
			}
	}



function check_password()
{
	var password ;
	password = document.getElementById('password') ;

	if(password.value.length < 6)
		{
			print_error("password_hint","Password too short") ;
			password.value = "" ;
			varFocus(password);	
			reg_disable("disabled");
		}
	else
	{
		
		document.getElementById('password_hint').innerHTML = "" ;
		focusSet = 0 ;
	}
}


function check_password2()
{
	
	var password ;
	var password2;
	
	password = document.getElementById('password') ;
	password2 = document.getElementById('password2') ;

	if(password.value != password2.value)
	{
			print_error("password_hint","Password does not match") ;
			password2.value = "" ;
			password.value = "" ;
			varFocus(password);		
					
	}
	else
		{
			document.getElementById('password2_hint').innerHTML = "" ;
			reg_disable(false);
			focusSet = 0 ;
		}
	
}




function check_email() {
	
  var email ;
  var email_id ;
  
  	email = document.getElementById('email').value ;
  	email_id = document.getElementById('email');
	
  var at="@"
  var dot="."
  var lat=email.indexOf(at)
  var lstr=email.length
  var ldot=email.indexOf(dot)
  if (email.indexOf(at)==-1){
    print_error("email_hint","Invalid E-mail ID")
      	print_error("email_hint","Invalid E-mail ID")
		email_id.value = "" ;
		varFocus(email_id);	
	  	return false ;
  }
  if (email.indexOf(at)==-1 || email.indexOf(at)==0 || email.indexOf(at)==lstr){
      	print_error("email_hint","Invalid E-mail ID")
		email_id.value = "" ;
		varFocus(email_id);	
	  	return false ;
  }
  if (email.indexOf(dot)==-1 || email.indexOf(dot)==0 || email.indexOf(dot)==lstr){
      	print_error("email_hint","Invalid E-mail ID")
		email_id.value = "" ;
		varFocus(email_id);	
	  	return false ;
  }
   if (email.indexOf(at,(lat+1))!=-1){
      	print_error("email_hint","Invalid E-mail ID")
		email_id.value = "" ;
		varFocus(email_id);	
	  	return false ;
   }
   if (email.substring(lat-1,lat)==dot || email.substring(lat+1,lat+2)==dot){
      	print_error("email_hint","Invalid E-mail ID")
		email_id.value = "" ;
		varFocus(email_id);	
	  	return false ;;
   }
   if (email.indexOf(dot,(lat+2))==-1){
      	print_error("email_hint","Invalid E-mail ID")
		email_id.value = "" ;
		varFocus(email_id);	
	  	return false ;
   }
   if (email.indexOf(" ")!=-1){
      	print_error("email_hint","Invalid E-mail ID")
		email_id.value = "" ;
		varFocus(email_id);	
	  	return false ;
   }
   
	print_ok("email_hint","Checking " + email_id.value);
	var now = new Date();
	request.open('get','remote.os?section=register&cmd=check_email&email='+email+'&tm='+now);
	request.onreadystatechange = parseEmail; 
	request.send('');	
		         
}


function parseEmail()
	{
		email_id = document.getElementById('email')  ;
		if(request.readyState == 1)
			{
				print_ok("email_hint","Verifying " + email_id.value );
			}
		
		if(request.readyState == 4)
			{ 
		
				var answer = request.responseText;
				
				//alert(answer);
				if(answer == "OK")
				{
					print_ok("email_hint","<strong>" + email_id.value + "</strong> is available");
					reg_disable(false);
					focusSet = 0 ;
				}
				else
				{
					
					print_error("email_hint",answer );
					reg_disable("disabled");
					email_id.value = "" ;
					varFocus(email_id);	
				}
			}
	}





function reg_disable(status)
{
	document.getElementById('btn_register').disabled = status ;	
}


function print_error(id,error)
{
	document.getElementById(id).innerHTML = "<span class=js_error>" + error + " </span>" ;
}

function print_ok(id,msg)
{
	document.getElementById(id).innerHTML = "<span class=js_ok>" + msg + "</span> ";
}

function reg_clear(id)
{
	document.getElementById(id).innerHTML = " Waiting ... " ;
	
}
