//<![CDATA[
function check_email(fieldvalue) {
	var goodEmail = fieldvalue.search(/^[^\.]*[A-Za-z0-9_\-\.]*[^\.]\@[^\.][A-Za-z0-9_\-\.]+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\.info)|(\.biz)|(\.us)|(\.bizz)|(\.coop)|(\..{2,2}))[ ]*$/gi);
	if (goodEmail != -1)
		return true;
	else
		return false;
}

function signup_step1() {
	var error_flag = false;
	$('#errorfirstname').remove();
	$('#errorlastname').remove();
	$('#erroraddress').remove();
	$('#errorcity').remove();
	$('#errorcounty').remove();
	$('#errorpostcode').remove();
	$('#errorphone').remove();
	$('#erroremail1').remove();
	$('#erroremail2').remove();
	$('#errorpassword1').remove();
	$('#errorpassword2').remove();
	
	if ($('#firstname').val() == '') {
		errorfirstnamehtml = '<div id="errorfirstname" class="error_text">Please enter your First Name.</div>';
		$('#firstname').after(errorfirstnamehtml);
		error_flag = true;
	} else if ($('#lastname').val() == '') {
		errorlastnamehtml = '<div id="errorlastname" class="error_text">Please enter your Last Name.</div>';
		$('#lastname').after(errorlastnamehtml);
		error_flag = true;
	} else if ($('#address').val() == '') {
		erroraddresshtml = '<div id="erroraddress" class="error_text">Please enter your Correspondence Address.</div>';
		$('#address').after(erroraddresshtml);
		error_flag = true;
	} else if ($('#city').val() == '') {
		errorcityhtml = '<div id="errorcity" class="error_text">Please enter your Town or City.</div>';
		$('#city').after(errorcityhtml);
		error_flag = true;
	} else if ($('#county').val() == '') {
		errorcountyhtml = '<div id="errorcounty" class="error_text">Please enter your County.</div>';
		$('#county').after(errorcountyhtml);
		error_flag = true;
	} else if ($('#postcode').val() == '') {
		errorpostcodehtml = '<div id="errorpostcode" class="error_text">Please enter your Post Code.</div>';
		$('#postcode').after(errorpostcodehtml);
		error_flag = true;
	} else if ($('#phone').val() == '') {
		errorphonehtml = '<div id="errorphone" class="error_text">Please enter your Phone Number.</div>';
		$('#phone').after(errorphonehtml);
		error_flag = true;
	} else if ($('#email1').val() == '') {
		erroremail1html = '<div id="erroremail1" class="error_text">Please enter your Email Address.</div>';
		$('#email1').after(erroremail1html);
		error_flag = true;
	} else if ($('#email2').val() == '') {
		erroremail2html = '<div id="erroremail2" class="error_text">Please confirm your Email Address.</div>';
		$('#email2').after(erroremail2html);
		error_flag = true;
	} else if ($('#email1').val() != '' && $('#email2').val() != '' && $('#email1').val() != $('#email2').val()) {
		erroremail2html = '<div id="erroremail2" class="error_text">Your email addresses do not match. Please re-enter.</div>';
		$('#email2').after(erroremail2html);
		error_flag = true;
	} else if ($('#password1').val() == '') {
		errorpassword1html = '<div id="errorpassword1" class="error_text">Please enter your Password.</div>';
		$('#password1').after(errorpassword1html);
		error_flag = true;
	} else if ($('#password2').val() == '') {
		errorpassword2html = '<div id="errorpassword2" class="error_text">Please confirm your Password.</div>';
		$('#password2').after(errorpassword2html);
		error_flag = true;
	} else if ($('#password1').val() != '' && $('#password2').val() != '' && $('#password1').val() != $('#password2').val()) {
		errorpassword2html = '<div id="errorpassword2" class="error_text">Your passwords do not match. Please re-enter.</div>';
		$('#password2').after(errorpassword2html);
		error_flag = true;
	}
	if ($('#email1').val() != '' && !check_email($('#email1').val())) {
		$('#erroremail2').remove();
		erroremail2html = '<div id="erroremail2" class="error_text">Your email address is not valid. Please re-enter.</div>';
		$('#email2').after(erroremail2html);
		error_flag = true;
	}
	if ($('#email1').val() != '' && $('#password1').val() != '') {
		$.get('/cms/check_login_details.php', { email: $('#email1').val(), password: $('#password1').val() },
			function(data) {
				var check_result = data;
				if (check_result == 'NOK') {
					$('#duplicatedetails').remove();
					erroremail2html = '<div id="duplicatedetails" class="error_text">Duplicate login details found. Please re-enter.</div>';
					$('#email2').after(erroremail2html);
					error_flag = true;
				}
				if (!error_flag) {
					params = 'action=step1post&firstname='+$('#firstname').val()+'&lastname='+$('#lastname').val()+'&address='+$('#address').val()+'&city='+$('#city').val()+'&county='+$('#county').val()+'&postcode='+$('#postcode').val()+'&phone='+$('#phone').val()+'&mobile='+$('#mobile').val()+'&email1='+$('#email1').val()+'&email2='+$('#email2').val()+'&password1='+$('#password1').val()+'&password2='+$('#password2').val();
					post('/cms/signup.php', params, 1, 'signup_step1_returnbit');
				}
			}
		);
	}
}

function signup_init() {
	$('#signup_content').load('/cms/signup.php', {action: 'start'}, function() {});
}

function goback_step1() {
	$('#signup_content').load('/cms/signup.php', {action: 'start'}, function() {});
}

function goback_step2() {
	$('#signup_content').load('/cms/signup.php', {action: 'step1'}, function() {
		$('#captcha').attr('src', '/cms/libs/captcha/securimage_show.php?' + Math.random());
	});
}

function signup_step1_returnbit() {
	if (myreqXML.readyState == 4 && myreqXML.status == 200) {
		var changeid = $('#changeid').val();
		if (changeid != 0) {
			$.get('/cms/signup.php', { action: 'changeadvertowner' }, function(data) {
				if (data == 'OK') {
					document.location='/cms/admin/adverts.php?advertid='+changeid;
				}
			});
		} else {
			$('#signup_content').load('/cms/signup.php', {action: 'step1'}, function() {});
		}
	}
}

function signup_step2() {
	params = 'action=step2&captcha_code='+$('#captcha_code').val();
	post('/cms/signup.php', params, 1, 'signup_step2_returnbit');
}

function send_details() {
	params = 'action=send_details';
	post('/cms/signup.php', params, 1, 'signup_send_details_returnbit');
}

function signup_send_details_returnbit() {
	if (myreqXML.readyState == 4 && myreqXML.status == 200) {
		var signup_type = $('#signup_type').text();
		if (signup_type == 'manual') {
			var custid = myreqXML.responseText;
			document.location='/cms/admin/adverts.php?cust='+custid;
		} else {
			document.location='/cms/advert/create.php';
		}
	}
}

function signup_step2_returnbit() {
	if (myreqXML.readyState == 4 && myreqXML.status == 200) {
		$('#captchastatus').remove();
		var captcha_response = myreqXML.responseText;
		if (captcha_response == 'OK') {
			send_details();
		} else {
			captchastatushtml = '<div id="captchastatus" class="error_text">The code you entered is invalid. Please try again.</div>';
			$('#captcha_area').after(captchastatushtml);
			$('#captcha').attr('src', '/cms/libs/captcha/securimage_show.php?' + Math.random());
		}
	}
}

function signup_step3_returnbit() {
	if (myreqXML.readyState == 4 && myreqXML.status == 200) {
		document.location='/cms/advert/create.php';
	}
}
//]]>
