var xmlHttp
var Process=new String() 
var arr;
var content=new String();

function regpi(retrievetype)
{ 
	xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null){
		  alert ("Your browser does not support AJAX!");
		  return;
		}
		Process=retrievetype;
		arr=Process.split("~");
		if(arr[0]=="reg") {
			var url="reg.php?retrievetype=" + retrievetype + "&n="+ parseInt(Math.random()*999999999);			
		}
		xmlHttp.onreadystatechange=stateChanged;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
}		
function stateChanged() { 
	if (xmlHttp.readyState==4){ 
	content=xmlHttp.responseText;
		if(arr[0]=="reg"){
				if(content=="OK"){
					if(confirm("Are you sure that all the necessary information you have encoded are correct?")){
						document.signupform.allow.value="YES";
						document.signupform.action="register_proc.php";
						document.signupform.submit()
					}
				}else if(content=="LOCKED"){
					window.location="register.php";					
				}else{
					alert(content);
				}
		}
	}
}
function GetXmlHttpObject()
{var xmlHttp=null;try{xmlHttp=new XMLHttpRequest();}catch (e){try{xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}catch (e){xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}
}return xmlHttp;
}

function validate_member()
{
	var errorstr = "";
	errorstr += checkitem(document.signupform.firstname,"First Name");	
	errorstr += checkitem(document.signupform.lastname,"Last Name");	
	errorstr += checkitem(document.signupform.password,"Password");	
	errorstr += checkitem(document.signupform.confirm_password,"Confirm Password");	
	if(document.signupform.password.value!=document.signupform.confirm_password.value) {
		errorstr += "Password and confirmed password must be the same.\n";
	}
	errorstr += checkemail(document.signupform.email);		
	errorstr += checkitem(document.signupform.mobile,"Mobile Number");				
	errorstr += checkitem(document.signupform.address,"Address");			
	errorstr += checkitem(document.signupform.email,"Email");			
	errorstr += checkitem(document.signupform.sponsor_code,"Sponsor ID");	
	
	if(errorstr=="") {
			errorstr="reg~"+ document.signupform.registration_code.value+"~"+document.signupform.password_code.value + "~" + document.signupform.sponsor_code.value + ""
			regpi(errorstr);
	}else{
			alert(errorstr);
	}
}
function checkitem(item, fdesc) { 
	var errorstr = "";
	if (item.value == "" || checkblanks(item.value) == true)  {
   		if (fdesc != "") { 
   			errorstr = "'" + fdesc + "' is required.\n";
		}
   }
   return errorstr;
 }

// following looks for all blanks in a string - typical of checking//
function checkblanks(item)  {
  var isblank = true;
  for (i = 0; i < item.length; i++) {
    if (item.charAt(i) != " ") {
      isblank = false;  }
  } 
  return isblank;
}

// check for valid emails....
function checkemail(item) {
  var goodmail = true;
  var addr = item.value;
  var invchar = " /:,;";
  var errorstr = "";
  var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
  var emailFilter=/^.+@.+\..{2,3}$/;
  for (i=0; i<invchar.length; i++)  {
	badchar = invchar.charAt(i);
	if (addr.indexOf(badchar,0) > -1)  {
   		goodmail = false;
	}
  }
  atpos = addr.indexOf("@",1);
  if (atpos == -1) {
	goodmail = false;
  }
  else  {
	perpos = addr.indexOf(".",atpos);
	if (perpos == -1)  {
		goodmail = false;
	} else if (perpos + 3 > addr.length)  {
		goodmail = false;
	} else if (addr.match(illegalChars)) {
		goodmail = false;
	} else if (!(emailFilter.test(addr))) { 
		goodmail = false;
    }
  }
  if (goodmail == false) {
    errorstr = "Email address is not valid.\n";
  }
  return errorstr;
}
function NewWindow(mypage, myname, w, h, scroll) {
var winl = (screen.width - w) / 2;
var wint = (screen.height - h) / 2;
winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable=no,titlebar=no,toolbar=no,status=no,menubar=no'
win = window.open(mypage, myname, winprops)
if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}
