function tmp () {
	//$('im').innerHTML = '<span id="alImg9"><img name="ImageName2" src="/ajax/images/wizard/age4.png" border="0" alt=""></span>';
}

// called for each wizard page
function checkform()
{
	var finish=false;
	//call the relevant step 
	switch (document.theform.current_step.value)
	{
	case "1":		
	  step1();
	  break
	case "2":
	  step2();
	  break
	case "3":
	  step3();
	  break
	}

	return finish;
}

function sendData (url, data , action) {
	//var pars = Form.serialize('frm');
	//var nocache = "1234";
	//var pars = "action="+action + "&data="+$F(fld) + "&nocache="+nocache;   ! the $F seems not to support FF
	
	var dt = new Date();   
	var nocache = dt.getTime();
	
	var pars = "action="+action + "&data="+data + "&nocache="+nocache;

	// show the relevent load div
	//$('load_'+action).style.display = 'block';
	$('status_'+action).innerHTML = $('load_'+action).innerHTML;
	var myAjax = new Ajax.Request( url, {method: 'get', parameters: pars, onLoading: showLoad, onComplete: showResponse} );

}

function showLoad () {
//	$('load').style.display = 'block';
}

function showResponse (http_request) {
	if (http_request.readyState == 4) {
		var decoded = eval("(" + http_request.responseText + ")");

		if (decoded.action == "check_mail")
		{
				if ( decoded.result == "ok"  )
				{
					$('status_'+decoded.action).innerHTML = "<font color=green>Valid</font>";
					$('status_'+decoded.action).tagname = "ok";
					step1_validate();
				}
				else
				{
					$('status_'+decoded.action).innerHTML = "<font color=red>" + decoded.result + "</font>";					
					document.theform.email.focus();
				}
		}
		else if (decoded.action == "check_username")
		{
				if ( decoded.result == "ok"  )
				{
					$('status_'+decoded.action).innerHTML = "<font color=green>Valid</font>";
					$('status_'+decoded.action).tagname = "ok";
					step2_validate();
				}
				else
				{
					$('status_'+decoded.action).innerHTML = "<font color=red>" + decoded.result + "</font>";
				}
		}
		else
		{
			//$('status').style.display = 'none';
		}		
	}
}


function switch_slide(new_slide_num)
{
	//Effect.SlideUp( $('step_div'+document.theform.current_step.value) );
	Effect.Puff( $('step_div'+document.theform.current_step.value) );
	Effect.Appear($('step_div'+new_slide_num));
	document.theform.current_step.value = new_slide_num;
}

function set_status( fld , msg , color )
{
	$(fld).innerHTML = "<font color="+color+">" + msg + "</font>";
}

/*
****************************************************************************************************
WIZARD STEPS
****************************************************************************************************
*/


// perform checks for step
function step1()
{	
	// this step checks email, and passwords.
	// pre-checks		
	if ( 	(checkEmail(document.theform.email.value) == "" ) && 
		(checkPassword(document.theform.manufacturers_password1.value)=="") &&
		(matchPassword(document.theform.manufacturers_password1.value,document.theform.manufacturers_password2.value)=="")
	    )  	{
			// local checks ok, send ajax request 
			sendData('/ajax_check_email.php',document.theform.email.value,'check_mail');
		}
}

// this is called when ajax respond was OK
function step1_validate()
{
		if  ( ( $('status_check_mail').tagname == "ok" ) )
		{
			// All is good! we go on
			switch_slide(2);
			setTimeout("document.theform.manufacturers_username.focus()", 1000);
		}
}

// perform checks for step
function step2()
{
	// validate 	
	sendData('/tmp_ajax_check_email.php',document.theform.manufacturers_username.value,'check_username');
}

// this is called when ajax respond was OK
function step2_validate()
{
		if  ( ( $('status_check_username').tagname == "ok" ) )
		{
			// All is good! we go on
			switch_slide(3);
		}
}

function step_gender(val)
{
	if ( val == 1 )
		document.theform.gender.value = "M";	
	else
		document.theform.gender.value = "F";	
	
	switch_slide(4);
}


function step_age_range(val)
{
	document.theform.age_range.value = val;	
	$('step_div4_load').style.display = 'block';	
	document.theform.submit();
}

/*
****************************************************************************************************
Standard JS validations
****************************************************************************************************
*/

function NOT_USED_checkformRegister()
{
	var why = "";
	if ( (document.theform.manufacturers_email.value=="") || (document.theform.manufacturers_username.value=="") || (document.theform.manufacturers_password1.value=="") || (document.theform.manufacturers_password2.value=="") )
	{
		why += "Please fill in all required fields";
	}
	else
	{
		why += checkEmail(document.theform.manufacturers_email.value);
		why += checkUsername(document.theform.manufacturers_username.value);
		why += checkPassword(document.theform.manufacturers_password1.value);
		why += matchPassword(document.theform.manufacturers_password1.value,document.theform.manufacturers_password2.value);
	}

    if (why != "")
	{
		alert(why);
		return false;
    }
return true;
}

function checkEmail (str) {
	var error="";
	if (str == "") {
		error = "Please enter Email address";
	}

	var emailFilter=/^.+@.+\..{2,3}$/;
	if (!(emailFilter.test(str))) {
		error = "Please use Email format name@site.com";
	}
	else {
		//test email for illegal characters
		var illegalChars= /[\(\)\<\>\,\;\:\\\"\'\[\]]/
		if (str.match(illegalChars)) {
			error = "Trying to inject? we want you even more! : )";
		}
	}
	
	if (error!="")
		document.theform.email.focus();
	
	set_status( "status_check_mail" , error , "red" );
		
	return error;
}

function checkPassword (str)
{
	var error = "";
	var illegalChars = /[\W_]/; // allow only letters and numbers
	
	if (str == "")
	{
		error = "Please enter password";
	}
	else if (str.length < 6)
	{
		error = "Password must contain at least six characters";
	}	
	else if (str.length > 20)
	{
		error = "Password can not contain more then 20 characters";
	}	
	else if (illegalChars.test(str))
	{
		error = "Sorry, password contains illegal characters";
	}

	if (error!="")
		document.theform.manufacturers_password1.focus();

	set_status( "status_check_password1" , error , "red" );

	return error;
}

function matchPassword (str1,str2)
{
	var error = "";
	if (str1 != str2)
	{
		error = "Passwords doesn't match";
	}

	if (error!="")
		document.theform.manufacturers_password2.focus();

	set_status( "status_check_password2" , error , "red" );
	
	return error;
}

function checkNotEmpty (str,field)
{
	var error = "";
	if (str == "")
	{
		error = "You didn't enter a " + field + ".";
	}
	return error;
}

function checkSelection (str,field)
{
	var error = "";
	if (str == "")
	{
		error = "You didn't enter your " + field + ".";
	}
	return error;
}

function checkDay (str)
{
	var error = "";
	if (str == "")
	{
		error = "You didn't enter your day of birth.";
	}
	else if (isNaN(str))
	{
   		error = "The day of birth should be a number.";
	}
	else if (str > 31 || str<1)
	{
		error = "The day of birth needs to be a number between 1 and 31.";
	}
	return error;
}

function checkMonth (str)
{
	var error = "";
	if (str == "")
	{
		error = "You didn't enter your month of birth.";
	}
	else if (isNaN(str))
	{
   		error = "The month of birth should be a number.";
	}
	else if (str > 12 || str<1)
	{
		error = "The month of birth needs to be a number between 1 and 12.";
	}
	return error;
}

function checkYear (str)
{
	var error = "";
	if (str == "")
	{
		error = "You didn't enter your year of birth.";
	}
	else if (isNaN(str))
	{
   		error = "The year of birth should be a number.";
	}
	else if (str > 2006 || str<1920)
	{
		error = "The year of birth needs to be a number between 1920 and 2006.";
	}
	return error;
}

function checkUsername (str)
{
	var error = "";
	if (str == "")
	{
		error = "You didn't enter a username.";
	}
	if ((str.length < 4) || (str.length > 10))
	{
		error = "The username is the wrong length.Please keep it 4 to 10 characters long.";
	}
	
	var illegalChars = /\W/;
	// allow only letters, numbers, and underscores
	if (illegalChars.test(str))
	{
		error = "The username contains illegal characters.";
	}
	return error;
}

