
var run_createAccCheck_lastUser     = '',
	run_createAccCheck_lastUserStat = 0,
	run_createAccCheck_lastPassStat = 0,
	run_createAccCheck_lastEml_Stat = 0;


function check_create_form()
{
	var u = document.getElementById('create_user').value;
	var p = document.getElementById('create_pass').value;
	var e = document.getElementById('create_email').value;

	if (u.length && run_createAccCheck_lastUserStat != 3 && 
		p.length && run_createAccCheck_lastPassStat == 2 &&
		e.length && run_createAccCheck_lastEml_Stat == 2 &&
		document.getElementById('create_agree').checked)
	{
		if (editor_mode == "www") go_createAccount();
		else document.getElementById('create_form').submit();
	} else {
		if (run_createAccCheck_lastUserStat != 2            ) { alert("The username you entered is not correct"); document.getElementById('create_user').focus(); } else
		if (run_createAccCheck_lastPassStat != 2            ) { alert("The password you entered is not correct"); document.getElementById('create_pass').focus(); } else
		if (run_createAccCheck_lastEml_Stat != 2            ) { alert("The email address you entered is not correct"); document.getElementById('create_email').focus(); } else
		if (!document.getElementById('create_agree').checked) { alert("You must agree to our TOS before continuing"); }
	}

	return true;
}


function go_createAccount()
{
	var arr = document.getElementsByName('demo_accType');
	if (arr.length)
	{
		accMode = 'www';
		for (i=0; i<arr.length; i++)
			if (arr[i].checked) accMode = arr[i].value;
		
		var form = document.getElementById('create_form');
		form.action += "="+accMode;
		form.submit();
		return;
	}
	endModalDlg();
	var modalClient  = document.getElementById("modal-client");		if (modalClient.firstNode) return;

	modalClient.innerHTML = "<img src='images/modal_ui/modal_q.jpg' style='float:left; margin-left:10px; padding-top: 40px;'>"+
			"<p style='margin-left: 90px; padding-top: 10px;'>Select the type of account you would like to create.</p>"+
			"<div style='margin-left: 120px'><form action='#' method='POST'>"+
			"<p><input type='radio' name='demo_accType' value='www' style='margin: 0px' checked='checked'> Standard account</p>"+
			"<p><input type='radio' name='demo_accType' value='bus' style='margin: 0px'> Business account (allows creation of sales letters)</p>"+
			"</form></div>";

	addModalBtn("Start my FREE account", "go_createAccount()", 220, "go_createacc_dlgbtn");
	startModalDlg(600, 120, "BlinkWeb Create Account", 4, 1);
}

function go_startDemo()
{
	if (editor_mode != "www") 
	{
		document.location = local_server_URL+"?go_demo";
		return;
	}

	var arr = document.getElementsByName('demo_accType');
	if (arr.length)
	{
		accMode = 'www';
		for (i=0; i<arr.length; i++)
			if (arr[i].checked) accMode = arr[i].value;
		

		document.location = local_server_URL+"?go_demo="+accMode;
		return;
	}

	endModalDlg();
	var modalClient  = document.getElementById("modal-client");		if (modalClient.firstNode) return;

	modalClient.innerHTML = "<img src='images/modal_ui/modal_q.jpg' style='float:left; margin-left:10px; padding-top: 20px;'>"+
			"<p style='margin-left: 90px; padding-top: 10px;'>Select the type of account you would like to demo.</p>"+
			"<div style='margin-left: 120px'><form action='#' method='POST'>"+
			"<p><input type='radio' name='demo_accType' value='www' style='margin: 0px' checked='checked'> Standard account</p>"+
			"<p><input type='radio' name='demo_accType' value='bus' style='margin: 0px'> Business account (allows creation of sales letters)</p>"+
			"</form></div>";

	addModalBtn("Start my demo", "go_startDemo()", 220, "go_demo_dlgbtn");
	startModalDlg(600, 100, "BlinkWeb DEMO", 4, 1);
}

function run_createAccCheck(what, out)
{
	if (what == 1)
	{
		var u = document.getElementById('create_user').value;
		var to = document.getElementById('user_checked');
		var small = ((to.className == 'home_feed_back_span') ? true : false);

		if (u != run_createAccCheck_lastUser) 
		{
			run_createAccCheck_lastUserStat = 0;
			
			if (u.length <  5 || u.length > 20)
				to.innerHTML = small ? '(needs to be 5-20 chars)' : 'Username needs to be 5-20 chars in length.';
			else 
			
			if (!u.match(/^([a-zA-Z0-9_]*)$/, u))
				to.innerHTML = small ? '(only letters and digits)' : 'Username can only contain letters and digits.';
			else

			{
				run_createAccCheck_lastUser     = u;
				run_createAccCheck_lastUserStat = 1;
				to.innerHTML = small ? '(checking availability)' : 'Checking availability...';

				if (!ajaxComm) testAjax();
				sendMessage("check_username('"+escape(u)+"');", 1);
			}
		}

		if (out && u == "" && run_createAccCheck_lastUserStat == 0) to.innerHTML = '';
	}

	if (what == 2)
	{
		var p = document.getElementById('create_pass').value;
		var to = document.getElementById('pass_checked');
		var small = ((to.className == 'home_feed_back_span') ? true : false);
		run_createAccCheck_lastPassStat = 0;
		
		if (p.length < 7 || p.length > 20)
			to.innerHTML = small ? '(needs to be 7-20 chars)' :'Password needs to be 7-20 chars in length.';
		else

		if (!p.match(/([a-zA-Z]+)/, u) || !p.match(/([0-9]+)/, u))
			to.innerHTML = small ? '(both letters and digits)' : 'Password needs to contain both letters and digits.';
		else
		{
			to.innerHTML = small ? '(OK)' : 'Password is OK...';
			run_createAccCheck_lastPassStat = 2;
		}

		if (out && p == "") to.innerHTML = '';
	}

	if (what == 3)
	{
		var e = document.getElementById('create_email').value;
		var to = document.getElementById('email_checked');
		var small = ((to.className == 'home_feed_back_span') ? true : false);
		run_createAccCheck_lastEml_Stat = 0;

		if (e.length < 7)
			to.innerHTML = small ? '(must be valid)' : 'Please input a valid email address. This address will be used in case you forget your password.';

		else if (	!e.match(/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/) &&
					!e.match(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|\w{3}|\w{4})$/))
			 to.innerHTML = small ? '(not valid)' : 'Email address does not appear to be correct.'

		else {
			to.innerHTML = '';
			run_createAccCheck_lastEml_Stat = 2;
		}
		if (out && e == "") to.innerHTML = '';
	}
}

function run_createAccCheck_user(username, res)
{
	if (username == run_createAccCheck_lastUser)
	{
		var to = document.getElementById('user_checked');
		var small = ((to.className == 'home_feed_back_span') ? true : false);
		
		if (res) { to.innerHTML = small ? "(available)" : "Username is available"; run_createAccCheck_lastUserStat = 2; }
		else	 { to.innerHTML = small ? "(not available)" : "Username is reserved";  run_createAccCheck_lastUserStat = 3; }
	}
}