﻿function checkRegister(id)
{
	if (document.register.username.value=="")
	{
		alert("Please enter your username!");
		document.register.username.focus();
		return false;
	}
   
	if (InvalidChar(document.register.username.value))
	{
		alert ("Username contains invalid characters. Please verify your username!");
		document.register.username.focus();
		return false;
	}
   
	if (id == '0') {	/*First registration --> need password */
		if (document.register.password.value=="")
		{
			alert("Please enter your password");
			document.register.password.focus();
			return false;
		}
   
		if (InvalidChar(document.register.password.value))
		{
			alert ("Your password contains invalid characters. Please verify your password.");
			document.register.password.focus();
			return false;
		}
		
		if (document.register.password.value != document.register.rePassword.value)
		{
			alert("Retyped password does not match. Please verify your password!");
			document.register.rePassword.focus();
			return false;
		}
	}
	
	if (!isEmail(document.register.email.value))	
	{
		alert("Your email is not valid. Please verify your email!");
		document.register.email.focus();
		return false;
	}
	
	if (document.register.reEmail.value != document.register.email.value)	
	{
		alert("Retyped email does not match. Please verify your email!");
		document.register.reEmail.focus();
		return false;
	}
	
	if (id == '0') {	/*First registration --> need agreement of term of services */
		if (document.register.condition.checked==false)
		{
			alert("Condition is not ticked!");
			document.register.condition.focus();
			return false;
		}
	}
	
    return true;
}

function isEmail(s)
{   
  if (s=="") return false;
  if(s.indexOf(" ")>0) return false;
  if(s.indexOf("@")==-1) return false;
  var i = 1;
  var sLength = s.length;
  if (s.indexOf(".")==-1) return false;
  if (s.indexOf("..")!=-1) return false;
  if (s.indexOf("@")!=s.lastIndexOf("@")) return false;
  if (s.lastIndexOf(".")==s.length-1) return false;
  var str="0123456789ABCDEFGHIKJLMNOPQRSTUVWXYZabcdefghikjlmnopqrstuvwxyz-@._"; 
  for(var j=0;j<s.length;j++)
	if(str.indexOf(s.charAt(j))==-1)
		return false;
   return true;
}

function InvalidChar(s)
{
	var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>? ";
	for (var i = 0; i < s.length; i++) {
		if (iChars.indexOf(s.charAt(i)) != -1) {
		return true;
  	}
  }
  return false;
}
