//<!--
//  Developed by Roshan Bhattarai 
//  Visit http://roshanbh.com.np for this script and more.
//  This notice MUST stay intact for legal use
// -->
$(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");
		//check the username exists or not from ajax
		$.post("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('This Username Already exists').addClass('messageboxerror').fadeTo(900,1);
			});
		  }
		  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('Username available to register').addClass('messageboxok').fadeTo(900,1);
			});
			document.register.validUser.value = '1';
		  }
        });

	});

	$("#confirm").blur(function()
	{
		  if($(this).attr('value') != $("#password").attr('value')) //if passwords don't match
		  {
		  	alert('Passwords do not match!');
			$("#password").focus();
			$("#password").select();
		  }

	});
});

function validate_required(field,alerttxt)
{
	with (field)
	{
		if (value==null||value=="")
		{
			alert(alerttxt);return false;
		}
		else
		{
			return true;
		}
	}
}

function validate_email(field,alerttxt)
{
	with (field)
	{
		apos=value.indexOf("@");
		dotpos=value.lastIndexOf(".");
		if (apos<1||dotpos-apos<2) 
		  {alert(alerttxt);return false;}
		else {return true;}
	}
}

function validate_username()
{
	if(document.getElementById("msgbox").innerHTML == "This Username Already exists")
	{
		alert("This Username already exists! Please choose a different Username");
		return false;
	}
	else if (document.getElementById("msgbox").innerHTML == "Checking...")
	{
		alert("Please check to verify that the username is available");
		return false;
	}
	else
	{
		return true;
	}
}

function validate_form(thisform)
{
	with (thisform)
	{
		if (validate_required(username,"User Name is Required!")==false)
		  {username.focus();return false;}
		if (validate_username()==false)
		  {username.focus();return false;}
		if (validate_required(password,"Password is Required!")==false)
		  {password.focus();return false;}
		if (validate_required(confirm,"Password Confirmation Required!")==false)
		  {password.focus();return false;}
		if (validate_required(firstName,"First Name is Required!")==false)
		  {firstName.focus();return false;}
		if (validate_required(lastName,"Last Name is Required!")==false)
		  {lastName.focus();return false;}
		if (validate_required(email,"E-mail Address is Required!")==false)
		  {email.focus();return false;}
		if (validate_email(email,"Not a valid E-mail Address!")==false)
		  {email.focus();return false;}
		if (validate_required(zipCode,"Zipcode is Required!")==false)
		  {zipCode.focus();return false;}
	}
}

function apply()
{
  document.forms[0].register.disabled=true;
  if((document.forms[0].eula.checked==true)&&(document.forms[0].privacy.checked==true))
  {
    document.forms[0].register.disabled=false;
  }
  if((document.forms[0].eula.checked==false)||(document.forms[0].privacy.checked==false))
  {
    document.forms[0].register.enabled=false;
  }
}

function addElement()
{
	  var ni = document.getElementById('myDiv');
	  var numi = document.getElementById('theValue');
	  var num = (document.getElementById('theValue').value -1)+ 2;
	  numi.value = num;
	  var newdiv = document.createElement('div');
	  var divIdName = 'my'+num+'Div';
	  newdiv.setAttribute('id',divIdName);
	  newdiv.innerHTML = 'Ingredient '+num+': <input type="text" name="ing'+num+'" id="ing'+num+'" /> <a href="javascript:;" onclick=\'removeElement("'+divIdName+'")\'>Remove</a>';
	  ni.appendChild(newdiv);
}
function removeElement(divNum)
{
	  var d = document.getElementById('myDiv');
	  var olddiv = document.getElementById(divNum);
	  d.removeChild(olddiv);
}
