var type;
function RegForm(step) {

	var theform = document.regform;
	var formdata;

	if (step == 'step1') {	
		// Grab all data
		for (i=0; i<theform.elements.length; i++){
			if (theform.elements[i].name != 'fax' && theform.elements[i].name != 'billfax' && theform.elements[i].name != 'captchabox') {
				if (theform.elements[i].value.length < 1) {
					alert('Please fill in all required fields. Missing: ' + theform.elements[i].name);
					theform.elements[i].focus();
					return false;
				}
			}
		}
		
		if (theform.email.value != theform.confirmemail.value) {
			alert('Please enter your e-mail address properly in both fields.');
			theform.email.focus();
			return false;
		}
		
		if (window.pageYOffset) ScrollTop = window.pageYOffset;
		else ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
		
		document.getElementById('verdata').style.marginTop = ScrollTop + 'px';
		document.getElementById('vercode').style.height = window.document.body.scrollHeight + 'px';
		show('vercode');
	}
	
	if (step == 'step2') {
		if (document.getElementById('captchabox').value.length < 3) {
			alert('Please enter the the letters and digits you see in the image.');
			return false;
		}
		for (i=0; i<theform.elements.length; i++){
			if (theform.elements[i].name != 'billfax') {
				if (theform.elements[i].value.length < 1) {
					alert('Please fill in all required fields.');
					hide('vercode');
					theform.elements[i].focus();
					return false;
				}
			}
			formdata = formdata + '&' + theform.elements[i].name + '=' + encode(theform.elements[i].value);		
		}
		hide('verinput');
		show('processing');
		type = 'reg';
		new AJAXRequest('POST','processReg.php','session=' + globalsession + formdata,processRegform);
		return false;
	}

}

function processRegform(myAJAX) {
	if (myAJAX.readyState == 4) {
		var block = false;
		var msg = '';
		if (myAJAX.status == 200) {
			if (myAJAX.responseText.length > 0) {
				msg = myAJAX.responseText;
			} else {
				shownMsg = 'Unable to complete task. Please try again.';
			}
		} else {
			if(myAJAX.status == '404') shownMsg = 'The requested resource is no longer available. Try refreshing the page or reporting the error to us.';
			else showMsg = 'An error occured. Try again later. HTTP Error code: ' + myAJAX.status;
		}
		if (msg.length > 0) {
			var response = msg.split('\n');
			if (response[0] == 'false') {
				shownMsg = response[1];
			} else {
				if (response[0] == 'true') {
					if (type == 'reg') document.location = 'regdone.php';
					if (type == 'edit') document.location = 'summary.php';
					if (type == 'pass') {
						document.getElementById('procmsg').innerHTML = 'Your password was changed. Click on Continue to login again.';
						show('continuebutt');
					}
					block = true;
				} else {
					shownMsg = 'An unknown reply was received. Reload this page and try again.';
				}
			}
		}
		if (block === false) {
			alert(shownMsg);
			hide('processing');
			if (type == 'reg') {
				hide('vercode'); show('verinput');
				refreshCaptcha();
				retry = 0;
			}
		}
	}
}

var retry = 1;
function refreshCaptcha() {
	if (retry > 3) {
		alert('You have retried a lot of times. Reload this page and try again later.');
	} else {
		document.getElementById('captcha').src = 'captcha.php?rand=' + Math.random();
		document.getElementById('captchabox').value = '';
		retry++;
	}
}

function SendEditForm() {
	var theform = document.editform;
	var formdata;
	for (i=0; i<theform.elements.length; i++){
		if (theform.elements[i].name != 'fax' && theform.elements[i].name != 'billfax') {
			if (theform.elements[i].value.length < 1) {
				alert('Please fill in all required fields.');
				theform.elements[i].focus();
				return false;
			}
		}
		formdata = formdata + '&' + theform.elements[i].name + '=' + encode(theform.elements[i].value);		
	}
	if (window.pageYOffset) ScrollTop = window.pageYOffset;
	else ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
	
	document.getElementById('processingtext').style.marginTop = ScrollTop + 'px';
	document.getElementById('processing').style.height = window.document.body.scrollHeight + 'px';
	show('processing');
	type = 'edit';
	new AJAXRequest('POST','processEdit.php','session=' + globalsession + formdata,processRegform);
	return false;
}
function ChangePass() {
	var theform = document.editform;
	var formdata;
	for (i=0; i<theform.elements.length; i++){
		if (theform.elements[i].value.length < 1) {
			alert('Please fill in all required fields.');
			theform.elements[i].focus();
			return false;
		}
		formdata = formdata + '&' + theform.elements[i].name + '=' + encode(theform.elements[i].value);		
	}
	if (window.pageYOffset) ScrollTop = window.pageYOffset;
	else ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
	
	document.getElementById('processingtext').style.marginTop = ScrollTop + 'px';
	document.getElementById('processing').style.height = window.document.body.scrollHeight + 'px';
	show('processing');
	type = 'pass';
	new AJAXRequest('POST','changePass.php','session=' + globalsession + formdata,processRegform);
	return false;
}
