// JavaScript Document

$(document).ready(function()
{
	
	$("#username").blur(function()
	{
	 //remove all the class add the messagebox classes and start fading
	 $("#msgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");
	 document.getElementById('submit').disabled = true;
	 //check the username exists or not from ajax
	 if(document.getElementById('username').value.length > 1){
	 	if(document.getElementById('username').value.match(/^[a-zA-Z0-9_]+$/)){
			 $.post("includes/user_availability.php",{ user_name:$(this).val() } ,function(data)
			 {
			  if(data=='no') //if username not avaiable
			  {
			   $("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
			   {
				//add message and change the class of the box and start fading
				$(this).html('Unavailable').addClass('messageboxerror').fadeTo(900,1);
				document.getElementById('submit').disabled = true;
				document.getElementById('confirm').disabled = true;
			   });
			  }
			  else
			  {
			   $("#msgbox").fadeTo(200,0.1,function()  //start fading the messagebox
			   {
				//add message and change the class of the box and start fading
				$(this).html('Available').addClass('messageboxok').fadeTo(900,1);
				document.getElementById('confirm').disabled = false;
			   });
			  }
			 });
		}else{
			$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
				{
				//add message and change the class of the box and start fading
				$(this).html('Invalid Characters').addClass('messageboxerror').fadeTo(900,1);
				document.getElementById('submit').disabled = true;
				document.getElementById('confirm').disabled = true;
			   });
	   
	   }
	 }else{
		  $("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
	   {
		//add message and change the class of the box and start fading
		$(this).html('Too Short').addClass('messageboxerror').fadeTo(900,1);
		document.getElementById('submit').disabled = true;
		document.getElementById('confirm').disabled = true;
	   });
	 }
		 
	});
	$("#confirm").blur(function()
	{
	 //remove all the class add the messagebox classes and start fading
	 $("#msgbox_confirm").removeClass().addClass('messagebox_confirm').text('Checking...').fadeIn("slow");
	 //check the username exists or not from ajax
	 var pwmatch;
	 
	 if(document.getElementById('password').value.length > 4){

		 if(document.getElementById('password').value == document.getElementById('confirm').value) {
			pwmatch = true;
		 }else pwmatch = false;
		  if(!pwmatch) //if no match
		  {
		   $("#msgbox_confirm").fadeTo(200,0.1,function() //start fading the messagebox
		   {
			//add message and change the class of the box and start fading
			$(this).html('No Match').addClass('messageboxerror').fadeTo(900,1);
			document.getElementById('submit').disabled = true;
		   });
		  }
		  else
		  {
		   $("#msgbox_confirm").fadeTo(200,0.1,function()  //start fading the messagebox
		   {
			//add message and change the class of the box and start fading
			$(this).html('OK').addClass('messageboxok').fadeTo(900,1);
			document.getElementById('submit').disabled = false;
		   });
		  }
	 }else{
		  $("#msgbox_confirm").fadeTo(200,0.1,function() //start fading the messagebox
	   {
		//add message and change the class of the box and start fading
		$(this).html('Too Short').addClass('messageboxerror').fadeTo(900,1);
		document.getElementById('submit').disabled = true;
	   });
	 }
	 });
});
