// JavaScript Document

var error_msg;
var strEmail;

function checkForm(which_form,url) {
/*	if(navigator.appName == "Microsoft Internet Explorer")
	{
		for (i = 0; i < document.getElementsByTagName("select").length; i++)
		{
			document.getElementsByTagName("select")[i].style.visibility = "hidden";
		}
	}
*/
	error_msg = "";
if (status != 0)
{
	error_msg = "Sorry, you've already registered for our workshop.";
	document.getElementById("error_message").innerHTML = "<center>" + error_msg + "<br><br><b><a href=\"javascript:hide('error_message_container');\"><span style=\"color:#ffffff\">OK</span></b></center>";
	document.getElementById("error_message").style.backgroundColor = box_colour;
	document.getElementById("error_message").style.color = error_msg_colour;
	document.getElementById("error_message_container").style.backgroundColor = overlay_colour;
	document.getElementById("error_message_container").style.display = "block";
	document.getElementById("error_message_container").style.visibility = "visible";
}
else
{
	document.getElementById("error_message").innerHTML = "<b>Please fill in the following fields:</b><br><br>";
	for ( i = 0; i < document.forms[which_form].elements.length; i++)
	{
		for ( j = 0; j < mandatory_fields.length; j++)
		{
			if (String(document.forms[which_form].elements[i].name) == String(mandatory_fields[j]) && document.forms[which_form].elements[i].value.length == 0 && String(mandatory_fields[j]) != 'tm_email1' && String(mandatory_fields[j]) != 'tm_email2' && String(mandatory_fields[j]) != 'tm_email3' && String(mandatory_fields[j]) != 'f_email1' && String(mandatory_fields[j]) != 'f_email2' && String(mandatory_fields[j]) != 'f_email3')
			{
				error_msg += mandatory_field_names[j] + "<br>";
				//error_msg += document.forms[which_form].elements[i].name + "<br>";
				document.forms[which_form].elements[i].style.backgroundColor = field_error_colour;
			}
			else if (String(document.forms[which_form].elements[i].name) == String(mandatory_fields[j]) && document.forms[which_form].elements[i].value.length != 0 && !validateEmail(document.forms[which_form].elements[i].value) && String(mandatory_fields[j]) == 'tm_email1' && String(mandatory_fields[j]) == 'tm_email2' && String(mandatory_fields[j]) == 'tm_email3' && String(mandatory_fields[j]) == 'f_email1' && String(mandatory_fields[j]) == 'f_email2' && String(mandatory_fields[j]) == 'f_email3')
			{

				error_msg += mandatory_field_names[j] + "<br>";
				//error_msg += document.forms[which_form].elements[i].name + "<br>";
				document.forms[which_form].elements[i].style.backgroundColor = field_error_colour;
			}
			else if (String(document.forms[which_form].elements[i].name) == String(mandatory_fields[j]) && document.forms[which_form].elements[i].value.length != 0)
			{
				document.forms[which_form].elements[i].style.backgroundColor = "#ffffff";
			}
		}
		//alert(document.forms[which_form].elements[i].name + " " + document.forms[which_form].elements[i].value);
	}

	//if ((strEmail.indexOf('@hotmail.com') != -1) || (strEmail.indexOf('@yahoo.com') != -1) || (strEmail.indexOf('@gmail.com') != -1) || (strEmail.indexOf('@msn.com') != -1) || (strEmail.indexOf('@') == 0))
	//{
	//	error_msg += "<br><b>Your email address is invalid.</b><br><br>";
	//}
	document.getElementById("error_message").innerHTML += error_msg;

	if (error_msg.length > 1)
	{
		document.getElementById("error_message").innerHTML += "<br><center><b><a href=\"javascript:hide('error_message_container');\"><span style=\"color:#ffffff\">OK</span></b></center>";
		document.getElementById("error_message").style.backgroundColor = box_colour;
		document.getElementById("error_message").style.color = error_msg_colour;
		document.getElementById("error_message_container").style.backgroundColor = overlay_colour;
		document.getElementById("error_message_container").style.display = "block";
		document.getElementById("error_message_container").style.visibility = "visible";
		
		return false;
	}
	else
	{
		//submit form
		//document.signup.action = url;
		//document.signup.submit();
		
		return true;
	}
}
}

function hide(what)
{
	document.getElementById("error_message_container").style.visibility = "hidden";
	document.getElementById("error_message_container").style.display = "none";
}

//IE position fixer
function addEvent(obj, evType, fn, useCapture){
	if(navigator.appName == "Microsoft Internet Explorer")
	{
		document.getElementById("error_message_container").style.position = "absolute";
		document.getElementById("error_message_container").style.width = "100%";
		document.getElementById("error_message_container").style.textAlign = "center";
		document.getElementById("error_message_container").style.height = screen.height + "px";
		document.getElementById("error_message").style.textAlign = "left";

		if (obj.addEventListener){
			obj.addEventListener(evType, fn, useCapture);
			return true;
		} else if (obj.attachEvent){
			var r = obj.attachEvent("on"+evType, fn);
			return r;
		} else {
			alert("Handler could not be attached");
		}
	}
}

func = function()
{
	document.getElementById("error_message_container").style.top = document.body.scrollTop + "px";
}

function validateEmail(email)
{
    var splitted = email.match("^(.+)@(.+)$");
    if(splitted == null) return false;
    if(splitted[1] != null )
    {
      var regexp_user=/^\"?[\w-_\.]*\"?$/;
      if(splitted[1].match(regexp_user) == null) return false;
    }
    if(splitted[2] != null)
    {
      var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
      if(splitted[2].match(regexp_domain) == null) 
      {
	    var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
	    if(splitted[2].match(regexp_ip) == null) return false;
      }// if
      return true;
    }
return false;
}