var xmlHttp

function Validate(str)
{ 
	if(str.length>3){
		Regex = new RegExp ("[a-zA-Z]([A-Za-z0-9]{2})");
		Regtest = Regex.test(str);
			if(Regtest==false){
				populate("myLayer","Nickname Validation","Must be alphanumeric beginning with a letter",4500);
  				StyleField(document["registrationForm"]["Nickname"],false);
			}
			else{
				
			xmlHttp=GetXmlHttpObject()
				if (xmlHttp==null){
 		
 				alert ("Browser does not support HTTP Request")
 				return;
 		
 				}

			var url="RegValidation.php"
			url=url+"?Nickname="+str
			url=url+"&sid="+Math.random()
			xmlHttp.onreadystatechange=stateChanged 
			xmlHttp.open("GET",url,true)
			xmlHttp.send(null)
			}
	}

	else{

   	populate("myLayer","Nickname Validation","Nickname must be longer than or equal to 4 characters!",4500);
  	StyleField(document["registrationForm"]["Nickname"],false);
   
	}

}

var NickValidation=true;

function ValidateAll(){

var errmsg="";


//Check For Required Fields
if(document["registrationForm"]["Name"].value=="") errmsg=errmsg+"Name Field Empty!<br/>";
if(document["registrationForm"]["SurName"].value=="") errmsg=errmsg+"SurName Field Empty!<br/>";
if(document["registrationForm"]["Nickname"].value=="") errmsg=errmsg+"Nickname not entered!<br/>";
if(document["registrationForm"]["Password"].value=="") errmsg=errmsg+"Password not entered!<br/>";
if(document["registrationForm"]["Retype"].value=="") errmsg=errmsg+"Password not entered twice!<br/>";
if(document["registrationForm"]["Email"].value=="") errmsg=errmsg+"Email Field Empty!<br/>";
if(document["registrationForm"]["phone"].value!="" && document["registrationForm"]["code"].value=="") 
	errmsg=errmsg+"Country Code is Empty!<br/>";


if(errmsg!="")
	populate("myLayer","Data Validation",errmsg,5000);
var NameValidation=ValidateText(document['registrationForm']['Name']);
var SurnameValidation=ValidateText(document['registrationForm']['SurName']);
var EmailValidation=ValidateEmail();
var PasswordValidation=ValidatePassword();
var PictureValidation=ValidatePicture();
var phoneValidation;
if(document['registrationForm']['phone'].value!="" && document['registrationForm']['code'].value=="")
{
	phoneValidation=false;
	StyleField(document['registrationForm']['phone'],false);
	StyleField(document['registrationForm']['code'],false);
}

else
{
	phoneValidation=true;
	StyleField(document['registrationForm']['phone'],true);
	StyleField(document['registrationForm']['code'],true);
}

Validate(document['registrationForm']['Nickname'].value);
if((errmsg=="") && (phoneValidation==true) && (NickValidation==true) && (NameValidation==true) && (SurnameValidation==true) && (EmailValidation==true) && (PasswordValidation==true)&& (PictureValidation==true) ) 
	document['registrationForm'].submit();
else
	{
	return false;
	}

}

function StyleField(field,status){
	
	if(status==true){
	field.style.color="#00aa00";
	field.style.backgroundColor="#eeffdd";
	field.style.background='url(../images/t_header.jpg)';
	}
	else{
	field.style.color="#ff0000";
	field.style.backgroundColor="#ffeedd";
	field.style.background='url(../images/t_header2.jpg)';
	}
	
}

function ValidateText(field){
//Check For Valid Name/Surname (each of minimum 2 characters)
  	Regex = new RegExp ("[a-zA-Z]([a-zA-Z0-9]{1})");
  	Regtest = Regex.test(field.value);
	
	if(Regtest==false) {
	errmsg="It must be a minimum 2-letter alphanumeric beginning with a letter!";
	populate("myLayer","Name/Family Validation",errmsg,4500);
	StyleField(field,false);
	return false;
}

else{

	StyleField(field,true);
	return true;
}


}



/*function ValidateDate(){

//Check For ~Valid Date
  	Regex = new RegExp ("^((0[1-9]|1[012])/(0[1-9]|[12][0-9]|3[01])/(19|20)[0-9][0-9])$");
  	Regtest = Regex.test(document["registrationForm"]["DateOfBirth"].value);
if(Regtest==false) {
errmsg=errmsg+"Date Of Birth WRONG!<br/>";
StyleField(document["registrationForm"]["DateOfBirth"],false);	
}
else {
	StyleField(document["registrationForm"]["DateOfBirth"],false);
	errmsg="";
}

}
*/

function ValidatePassword(){

Regex = new RegExp ("[a-zA-Z]([A-Za-z0-9]{5})");
var password=document["registrationForm"]["Password"].value;
Regtest = Regex.test(password);

//Check For Password Matching
if((password!=document["registrationForm"]["Retype"].value)&&(document["registrationForm"]["Retype"].value!="")){
errmsg="Passwords Do Not Match";
StyleField(document["registrationForm"]["Password"],false);
StyleField(document["registrationForm"]["Retype"],false);
populate("myLayer","Password Validation",errmsg,4500);
return false;
}

else if (password.length<=5)
{
errmsg="Password Too Short, it must be a minimum of 6 characters!";
StyleField(document["registrationForm"]["Password"],false);
populate("myLayer","Password Validation",errmsg,4500);
return false;
}

else if(Regtest==false) {
errmsg="Password must use only alphanumeric characters!";
StyleField(document["registrationForm"]["Password"],false);
populate("myLayer","Password Validation",errmsg,4500);
return false;
}

else{
	
StyleField(document["registrationForm"]["Password"],true);
StyleField(document["registrationForm"]["Retype"],true);
return true;
	
}

}

function ValidateEmail(){

//Check For Email Validation

  	Regex = new RegExp ("^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$");
  						 
  	Regtest = Regex.test(document["registrationForm"]["Email"].value);
if(Regtest==false) {
errmsg="Wrong e-mail entry!";
populate("myLayer","Email Validation",errmsg,4500);
StyleField(document["registrationForm"]["Email"],false);
return false;
}

else {

StyleField(document["registrationForm"]["Email"],true);
return true;

}

}

function ValidatePicture(){

//Check For Email Validation
var tmp=document['registrationForm']['ProfilePicture'].value;
tmp=tmp.toLowerCase();
var ext=tmp.substring(tmp.length-4)
if(tmp.length==0) return true;
if(ext!=".jpg" && ext!=".gif" && ext!=".png") {
errmsg="Please use only .JPG/.GIF/.PNG extension!";
populate("myLayer","Picture Validation",errmsg,4500);
StyleField(document["registrationForm"]["ProfilePicture"],false);
return false;
}

else {

StyleField(document["registrationForm"]["ProfilePicture"],true);
return true;

}
	

//Reg Email => 

}


function stateChanged() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 

 if(xmlHttp.responseText=='valid'){
 	
  StyleField(document["registrationForm"]["Nickname"],true);
 NickValidation=true;
 } 
 else if(xmlHttp.responseText=='invalid') {
 NickValidation=false;
 //alert(xmlHttp.responseText);
 StyleField(document["registrationForm"]["Nickname"],false);
   populate("myLayer","Name/Family Validation","<font color='#ff0000'>Nickname already taken, kindly pick another</font>",4500);
 
 }
 
 } 
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 //Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}

