// JavaScript Document
var mand = true;
var pword_bool = false;
var dob_ok = true;
var parent_visible = false;
var error_colour = "#ffcc00";
var field_colour = "#d49b9b";
var label_colour = "#ffffff";
var num_reg = new RegExp("([0-9])");
var iframe_height = 790;

function check_landing()
{
	email_return = IsEmailValid("email_label", "email");

	if(mand == false)
	{
		showdiv('error');
		resize_iframe("rego_frame", "30", "add");
		mand = true;
		return false;
	}
}

function check_step1()
{
	email_check = IsEmailValid("email_label", "email");
	match_password("password1_label", "password1", "password2_label", "password2");
	check_text("avatar_label", "avatar");
	check_text("fname_label", "fname");
	check_text("lname_label", "lname");
	check_select("state_label", "state");
	check_number("postcode_label", "postcode", 4, "1234567890");	
	check_number("phone_label", "phone", -1, "1234567890 ()+");
	check_checkbox("privacy_label", "privacy_optin");
	
	if(parent_visible == true) {
		check_checkbox("parent_label", "parent_optin");
	}
	
	check_dob();
	
	if(mand == false)
	{
		showdiv('error');
		resize_iframe("rego_frame", "30", "add");
		mand = true;
		return false;
	}
	
	hidediv('error');
	
	if(pword_bool == false)
	{
		resize_iframe("rego_frame", "30", "add");
		pword_bool = true;
		return false;
	}
	
	
}

function check_step2() 
{ 
	check_text("code_label", "code");
	
	if(mand == false)
	{
		showdiv('error');
		mand = true;
		return false;
	}
}

function resize_iframe(iframe_name, new_height, resize)
{
	if(resize == "add")
	{
		iframe_height += eval(new_height);
	}
	else
	{
		iframe_height = eval(new_height);
	}
	temp_string = iframe_height +"px";
	
	if(parent.document.getElementById(iframe_name))
	{
		the_iframe = parent.document.getElementById(iframe_name);
		the_iframe.style.height = temp_string;
	}
}

function check_dob()
{
	dob_ok = true;
	
	check_num2("dob_date", "0", "32");
	check_num2("dob_month", "0", "13");
	check_num2("dob_year", "1900", "2002");
	
	var the_label = document.getElementById("dob_label");
	
	if(dob_ok == true)
	{
		the_label.style.color = label_colour;
	}
	else
	{
		the_label.style.color = error_colour;
	}
}

function check_year_field()
{
	var the_field = document.getElementById("dob_year");
	var the_label = document.getElementById("dob_label");
	temp_string = the_field.value;
	
	if(temp_string.length == 4 && temp_string != "yyyy")
	{
		//if it is a number
		if(check_number("dob_label", "dob_year", 4, "1234567890"))
		{
			if(eval(temp_string) > 1993)
			{
				resize_iframe("rego_frame", "30", "add");
				parent_visible = true;
				showdiv('parent_q');
			}
			else
			{
				if(document.getElementById("parent_q").style.display == "block")
				{
					resize_iframe("rego_frame", "-30", "add");
					parent_visible = false;
					hidediv('parent_q');
				}
			}
			change_field_colour(the_label, the_field, "ok");
		}
		else
		{
			change_field_colour(the_label, the_field, "error");
		}
	}
}

function check_num2(field_name, min_value, max_value)
{
	var the_field = document.getElementById(field_name);
	var the_label = document.getElementById("dob_label");
	
	if(num_reg.test(the_field.value))
	{
		temp_number = eval(the_field.value);
		if(temp_number > min_value && temp_number < max_value)
		{
			//num OK
			change_field_colour(the_label, the_field, "ok");
			return true;
		}
		else
		{
			//out of range
			change_field_colour(the_label, the_field, "error");
			dob_ok = false;
			return false;
		}
	}
	else
	{
		//not a number
		change_field_colour(the_label, the_field, "error");
		dob_ok = false;
		return false;
	}
}

function check_checkbox(label_name, field_name)
{
	if(document.getElementById(field_name)) 
	{		//check the element exists and can be accessed 
		var the_field = document.getElementById(field_name);
		var the_label = document.getElementById(label_name);
		
		temp_bool = the_field.checked;
		
   		if(temp_bool == false)
		{
			change_field_colour(the_label, the_field, "error");
		}
		else
		{
			change_field_colour(the_label, the_field, "ok");
		}
	}
}

function check_number(label_name, field_name, req_length, ValidChars)
{
   	var Char;
	if(document.getElementById(label_name)) 
	{		//check the element exists and can be accessed 
		var the_label = document.getElementById(label_name);
		var the_field = document.getElementById(field_name);
		
		if(the_field.value.length == 0)
		{
			change_field_colour(the_label, the_field, "error");
			return false;
		}
		else if(req_length != -1 && the_field.value.length != req_length)
		{
			change_field_colour(the_label, the_field, "error");
			return false;
		}
		else
		{
			change_field_colour(the_label, the_field, "ok");
		}
		
		for (i = 0; i < the_field.value.length; i++) 
	   	{ 
			Char = the_field.value.charAt(i); 
			if (ValidChars.indexOf(Char) == -1) 
			{
				change_field_colour(the_label, the_field, "error");
				return false;
			}
	   	}
		return true;
	}
}

function check_text(label_name, field_name)
{
	if(document.getElementById(label_name)) 
	{		//check the element exists and can be accessed 
		var the_label = document.getElementById(label_name);
		var the_field = document.getElementById(field_name);
		
		if(the_field.value == "")
		{
			change_field_colour(the_label, the_field, "error");
		}
		else
		{
			change_field_colour(the_label, the_field, "ok");
		}
	}
}

function IsEmailValid(label_name, field_name)
{
	var EmailOk  = true;
	
	if(document.getElementById(label_name)) 
	{		
		var the_label = document.getElementById(label_name);
		var the_field = document.getElementById(field_name);

		var AtSym    = the_field.value.indexOf('@')
		var Period   = the_field.value.lastIndexOf('.')
		var Space    = the_field.value.indexOf(' ')
		var Length   = the_field.value.length - 1   // Array is from 0 to length-1
	
		if ((AtSym < 1) ||                     // '@' cannot be in first position
			(Period <= AtSym+1) ||             // Must be atleast one valid char btwn '@' and '.'
			(Period == Length ) ||             // Must be atleast one valid char after '.'
			(Space  != -1))                    // No empty spaces permitted
		   {  
				change_field_colour(the_label, the_field, "error");
		   }
		   else
		   {
		   		change_field_colour(the_label, the_field, "ok");
		   }
	}
}

function check_text(label_name, field_name)
{
	if(document.getElementById(label_name)) 
	{		//check the element exists and can be accessed 
		var the_label = document.getElementById(label_name);
		var the_field = document.getElementById(field_name);
		
		if(the_field.value == "")
		{
			change_field_colour(the_label, the_field, "error");
		}
		else
		{
			change_field_colour(the_label, the_field, "ok");
		}
	}
}

function change_field_colour(the_label, the_field, change)
{
	if(change == "error")
	{
		the_label.style.color = error_colour;
		the_field.style.backgroundColor = error_colour;
		mand = false;
	}
	else
	{
		the_label.style.color = label_colour;
		the_field.style.backgroundColor = field_colour;
	}
}

function clear_field(field_name, temp_string)
{
		var the_field = document.getElementById(field_name);
		if(the_field.value == temp_string)
		{
			the_field.value = "";
		}
}

function match_password(label_1, password_1, label_2, password_2)
{
	if(document.getElementById(password_1)) 
	{		
		var the_label_1 = document.getElementById(label_1);
		var the_field_1 = document.getElementById(password_1);
		var the_label_2 = document.getElementById(label_2);
		var the_field_2 = document.getElementById(password_2);
		
		if(the_field_1.value != the_field_2.value || the_field_1.value == "")
		{  
			the_label_1.style.color = error_colour;
			the_field_1.style.backgroundColor = error_colour;
			the_label_2.style.color = error_colour;
			the_field_2.style.backgroundColor = error_colour;
			pword_bool = false;
			showdiv('password_error');
		}
		else
		{
			the_label_1.style.color = label_colour;
			the_field_1.style.backgroundColor = field_colour;
			the_label_2.style.color = label_colour;
			the_field_2.style.backgroundColor = field_colour;
			pword_bool = true;
			hidediv('password_error');
		}
	}
}

function check_select(label_name, field_name)
{
	if(document.getElementById(label_name)) 
	{		//check the element exists and can be accessed 
		var the_label = document.getElementById(label_name);
		var the_field = document.getElementById(field_name);
		
		if(the_field.selectedIndex == 0)
		{
			change_field_colour(the_label, the_field, "error");
		}
		else
		{
			change_field_colour(the_label, the_field, "ok");
		}
	}
}

/*function popwin(aPage, aTarget, w, h, var1, var2)
{
	child = window.open(aPage,aTarget,'status=no,top=0,left=0,scrollbars=' +((var1=='scroll' || var2=='scroll')? 'yes' : 'no')+ ',resizable=' +((var1=='resize' || var2=='resize')? 'yes' : 'no')+ ',width='+w+',height='+h +',left = 0,top = 0');

	if (window.focus) 
	{
		child.focus();
	}
	return false;
}*/

function showdiv(id) 
{ 
	if(document.getElementById(id)) 
	{		//check the element exists and can be accessed 
		var ele = document.getElementById(id);//get hold of the element 
		ele.style.display="block";
	}
}

function hidediv(id) 
{ 
	if(document.getElementById(id)) 
	{		//check the element exists and can be accessed 
		var ele = document.getElementById(id);//get hold of the element 
		ele.style.display="none";
	}
}