var curPage = 0;
var statusImg = "<div align=\"center\"><img src=\"./register/ajax-loader.gif\" title=\"Please Wait...\" /></div>";
var maxPage = 3;


function getRegPage(page,parm) {
	var pagebox = window.document.getElementById('register');
	pagebox.innerHTML = statusImg;
	var ajax = new GLM.AJAX();
	ajax.callPage(PATH+"/getpage.php?unique="+parm+"&page="+page, getRegPageCallback);
	curPage = page;
}
function getRegPageCallback(response) {
	var page = window.document.getElementById('register');
	page.innerHTML = response;

	if(curPage == 3)
		getSecureQuestion();
}
function regError(msg) {
	alert(msg);
}
function registerCallback(response) {
	if(response == 1 || response == "1") {
		if (curPage == 9){
			getRegPage(curPage + 1);
		}else{
			// Success, call next page
			if((curPage + 1) > maxPage)
				finishRegistration();
			else
				getRegPage(curPage + 1);
		}
	} else {
		//alert('Response error. Reponse is ('+response+')');
		error = response.split(':');
		regError(error[1]);
	}
	hideStatus();
}
function finishRegistration() {
//	hideStatus();
// comment this out; will generate Error: could not get the display property on IE7
	window.location = './?done=true';
}
function showStatus() {
	var statusIcon = window.document.getElementById('status');
	statusIcon.innerHTML = statusImg;
 	statusIcon.style.display = 'block';
}
function hideStatus() {
	// MALEK256:  this looks to be throwing a JS error?
	// IE - cannot get the display property
	/*
	var statusIcon = window.document.getElementById('status');
	statusIcon.innerHTML = '';
	statusIcon.style.display = 'hidden'; */
}

function getSecureQuestion() {
	var ajax = new GLM.AJAX();
	ajax.callPage(PATH+"/securequestion.php?show=1", secureQuestion);
}
function secureQuestion(response) {
	parts = response.split(':');

	window.document.getElementById("q1").innerHTML = parts[0];
}

function CheckEmailForm(form){
	showStatus();
	var email = form.email.value;
	if (email == ""){
		alert("Provide your email address punk!");
		hideStatus();
		return false;
	}
	curPage = 9;
	args = 'regpage=9&email='+email;
	var ajax = new GLM.AJAX();
	ajax.callPage(PATH+"/register.php?"+args, registerCallback);
	return false;
}
function newRegPage(theform) {
	// Validate and handle input...
	try {

		switch(curPage) {
			case 1:
					showStatus();
					// Get values
					var age = theform.age.value;
					var gender = theform.gender.value;
					var country = theform.country.value;
					// Validate
					if(age == "age") {
						alert('Please fill in your age');
						hideStatus();
						return;
					 }
					if(gender == "gender")
					{
						alert('Please fill in your gender');
						hideStatus();
						return;
					}

					// Create args
					args = 'regpage=1&age='+age+'&country='+country+'&gender='+gender;
					// Post data to register file
					var ajax = new GLM.AJAX();
					ajax.callPage(PATH+"/register.php?"+args, registerCallback);
				break;
			case 2:
					showStatus();
					// Get values
					var user	= theform.username.value;
					var pass	= theform.password.value;
					var cpass	= theform.cpass.value;
					var email	= theform.email.value;
					var cemail	= theform.cemail.value;
					// Validate
					if(user == "" || pass == "" || email == "") {
						alert('Please fill in all fields completely');
						hideStatus();
						return;
					}
					if(user.length > 16) {
						alert('Your username is too long. Max 16 characters');
						hideStatus();
						return;
					}
					if(pass != cpass) {
						alert('Your passwords do not match');
						hideStatus();
						return;
					}
					if(email != cemail) {
						alert('Your emails do not match');
						hideStatus();
						return;
					}
					// Create args
					args = 'regpage=2&user='+user+'&pass='+pass+'&email='+email;
					// Post data to register file
					var ajax = new GLM.AJAX();
					ajax.callPage(PATH+"/register.php?"+args, registerCallback);
				break;
			case 3:
					showStatus();
					// Get values
					var code	= theform.code.value;
					var question= 2; //theform.question.value;
					var q		= 0; // theform.q2.value;
					var tos		= theform.tos.value;
					var mailings= theform.mailings.value;
					// Validate
					if(tos != 1) {
						alert('You must agree to the TOS');
						hideStatus();
						return;
					}
					if(code == "" || question == "") {
						alert('Please enter all fields completely');
						hideStatus();
						return;
					}

					// Create args
					args = 'regpage=3&code='+code+'&question='+question+'&q='+q+'&tos='+tos+'&mailings='+mailings;
					// Post data to register file
					var ajax = new GLM.AJAX();
					ajax.callPage(PATH+"/register.php?"+args, registerCallback);
				break;
			default:
					alert('Unknown page');
					hideStatus();
					return;
				break;
		}
	} catch(ex) {
		// Failure...
		alert('JS error: '+ex);
	}
}
