var reg_errors = new Array

reg_errors[0] = true;
reg_errors[1] = true;
reg_errors[2] = true;
reg_errors[3] = true;
reg_errors[4] = true;
reg_errors[5] = null;


function reg_chck_pass1(){
	if(document.getElementById('reg_pass1').value.length < 4){
		document.getElementById('reg_pass1_cll').innerHTML = '<span class="incorrect">&#x2718;</span> Password is too short';
		reg_errors[1] = true;
	}else if(document.getElementById('reg_pass1').value.length > 26){
		document.getElementById('reg_pass1_cll').innerHTML = '<span class="incorrect">&#x2718;</span> Password is too long';
		reg_errors[1] = true;
	}else{
		document.getElementById('reg_pass1_cll').innerHTML = '<span class="correct">&#x2714;</span> Valid password';
		reg_errors[1] = false;
	}
}

function reg_chck_pass2(){
	if(document.getElementById('reg_pass1').value != document.getElementById('reg_pass2').value){
		document.getElementById('reg_pass2_cll').innerHTML = '<span class="incorrect">&#x2718;</span> Passwords don\'t match';
		reg_errors[2] = true;
	}else{
		document.getElementById('reg_pass2_cll').innerHTML = '<span class="correct">&#x2714;</span> Passwords match';
		reg_errors[2] = false;
	}
}

function reg_chck_email1(){
	if(!echeck(document.getElementById('reg_email1').value)){
		document.getElementById('reg_email1_cll').innerHTML = '<span class="incorrect">&#x2718;</span> Please enter a valid e-mail';
		reg_errors[3] = true;
	}else{
		reg_emailAJAX(document.getElementById('reg_email1').value)
//		document.getElementById('reg_email1_cll').innerHTML = '';
//		reg_errors[2] = false;
	}
}



// DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
function echeck(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
//	   alert("Invalid E-mail ID")
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
//	   alert("Invalid E-mail ID")
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
//		alert("Invalid E-mail ID")
		return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
//		alert("Invalid E-mail ID")
		return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
//		alert("Invalid E-mail ID")
		return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
//		alert("Invalid E-mail ID")
		return false
	 }
	
	 if (str.indexOf(" ")!=-1){
//		alert("Invalid E-mail ID")
		return false
	 }

	 return true					
}

function reg_chck_email2(){
	if(document.getElementById('reg_email1').value != document.getElementById('reg_email2').value){
		document.getElementById('reg_email2_cll').innerHTML = '<span class="incorrect">&#x2718;</span> E-mails don\'t match';
		reg_errors[4] = true;
	}else{
		document.getElementById('reg_email2_cll').innerHTML = '<span class="correct">&#x2714;</span> Emails match';
		reg_errors[4] = false;
	}
}

function reg_submit(){
	if(reg_errors[0] == false && reg_errors[1] == false && reg_errors[2] == false && reg_errors[3] == false && reg_errors[4] == false && reg_errors[5] == false){
		alert("^_^");
		return true;
	}else{
		alert(reg_errors[0]);
		alert(reg_errors[1]);
		alert(reg_errors[2]);
		alert(reg_errors[3]);
		alert(reg_errors[4]);
		alert(reg_errors[5]);
		alert("There are errors. Correct them, biatch. D:<");
		return false;
	}
	
}

var xmlhttp_reg_user;

function reg_user2(username)
{
	if(username.length < 1){
		document.getElementById('reg_user_cll').innerHTML = '<span class="incorrect">&#x2718;</span> Username is too short';
		return;
	}
	if(username.length > 26){
		document.getElementById('reg_user_cll').innerHTML = '<span class="incorrect">&#x2718;</span> Username is too long';
		return;
	}
	
	
xmlhttp_reg_user = null;
if (window.XMLHttpRequest)
  {// code for all new browsers
  xmlhttp_reg_user = new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {// code for IE5 and IE6
  xmlhttp_reg_user = new ActiveXObject("Microsoft.XMLHTTP");
  }
if (xmlhttp_reg_user!=null)
  {
  url = "js/AJAX/reg_username.php?user="+username;
  xmlhttp_reg_user.onreadystatechange=state_Change_reg_user;
  xmlhttp_reg_user.open("GET",url,true);
  xmlhttp_reg_user.send(null);
  }
else
  {
  alert("Your browser does not support XMLHTTP.");
  }
}


function state_Change_reg_user()
{
if (xmlhttp_reg_user.readyState==4)
  {// 4 = "loaded"
  if (xmlhttp_reg_user.status==200)
    {// 200 = "OK"
	if(xmlhttp_reg_user.responseText == "true"){
		document.getElementById('reg_user_cll').innerHTML = '<span class="incorrect">&#x2718;</span> Username is already in use';
		reg_errors[0] = true;
	}else{
		document.getElementById('reg_user_cll').innerHTML = '<span class="correct">&#x2714;</span> Username is not in use';
		reg_errors[0] = false;
	}
    }
  else
    {
    alert("Problem retrieving XML data:" + xmlhttp_reg_user.statusText);
    }
  }
}

var xmlhttp_reg_email;

function reg_emailAJAX(email)
{
xmlhttp_reg_email = null;
if (window.XMLHttpRequest)
  {// code for all new browsers
  xmlhttp_reg_email = new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {// code for IE5 and IE6
  xmlhttp_reg_email = new ActiveXObject("Microsoft.XMLHTTP");
  }
if (xmlhttp_reg_email!=null)
  {
  url = "js/AJAX/reg_email.php?email="+email;
  xmlhttp_reg_email.onreadystatechange=state_Change_reg_email;
  xmlhttp_reg_email.open("GET",url,true);
  xmlhttp_reg_email.send(null);
  }
else
  {
  alert("Your browser does not support XMLHTTP.");
  }
}


function state_Change_reg_email()
{
if (xmlhttp_reg_email.readyState==4)
  {// 4 = "loaded"
  if (xmlhttp_reg_email.status==200)
    {// 200 = "OK"
	if(xmlhttp_reg_email.responseText == "true"){
		document.getElementById('reg_email1_cll').innerHTML = '<span class="incorrect">&#x2718;</span> E-mail is already in use';
		reg_errors[3] = true;
	}else{
//		document.getElementById('reg_user_cll').innerHTML = '<span class="correct">&#x2714;</span> Username is not in use';
		document.getElementById('reg_email1_cll').innerHTML = '<span class="correct">&#x2714;</span> Valid E-mail';
		reg_errors[3] = false;
	}
    }
  else
    {
    alert("Problem retrieving XML data:" + xmlhttp_reg_email.statusText);
    }
  }
}

var xmlhttp_reg_register;

function reg_submitAJAX(captcha)
{
	
xmlhttp_reg_register = null;
if (window.XMLHttpRequest)
  {// code for all new browsers
  xmlhttp_reg_register = new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {// code for IE5 and IE6
  xmlhttp_reg_register = new ActiveXObject("Microsoft.XMLHTTP");
  }
if (xmlhttp_reg_register!=null)
  {
	user = document.getElementById('reg_user').value;
	pass1 = document.getElementById('reg_pass1').value;
	pass2 = document.getElementById('reg_pass2').value;
	email1 = document.getElementById('reg_email1').value;
	email2 = document.getElementById('reg_email2').value;
	capthca = document.getElementById('reg_captcha').value;
	tz = document.getElementById('reg_tz').value;
	skin = document.getElementById('reg_skin').value;
	  
	  
  url = "js/AJAX/reg_register.php?user="+user+"&pass1="+pass1+"&pass2="+pass2+"&email1="+email1+"&email2="+email2+"&capthca="+capthca+"&tz="+tz+"&skin="+skin;
  xmlhttp_reg_register.onreadystatechange=state_Change_reg_register;
  xmlhttp_reg_register.open("GET",url,true);
  xmlhttp_reg_register.send(null);
  
  return false;
  }
else
  {
  alert("Your browser does not support XMLHTTP.");
  }
}


function state_Change_reg_register()
{
if (xmlhttp_reg_register.readyState==4)
  {// 4 = "loaded"
  if (xmlhttp_reg_register.status==200)
    {// 200 = "OK"
		eval(xmlhttp_reg_register.responseText);
    }
  else
    {
    alert("Problem retrieving XML data:" + xmlhttp_reg_register.statusText);
    }
  }
}

function reg_refresh_captcha(img){
	img.src = 'captcha.php?j='+Math.random(); 
}

function reg_confirmRegsiter(){
	if(document.getElementById('reg_con').checked){
		return true;
	}else{
		alert("You must agree to the terms before registering");
		return false;
	}
}