//============================= Secondary functions ================================
//(They will be used to check the content of all the pages containing a formular)

function check_email ()
{
	var email = document.forms['form1'].email.value;
	var length = email.length;
	var result = true;
	var at_first_position = email.indexOf("@") + 1;
	var at_last_position = email.lastIndexOf("@") + 1;
	var dot_first_position = email.indexOf(".") + 1;
	var dot_last_position = email.lastIndexOf(".") + 1;
	if ((at_first_position == at_last_position)					/* Only one "@" */
			&& (at_first_position != 1)							/* "@" is not the first character */
			&& (dot_first_position != 1)						/* "." is not the first character  */
			&& (dot_last_position < length)						/* "." is not the last character  */
			&& ((at_first_position - dot_first_position) != 1)
					/* If there is a "." before the "@", there is an other character between both*/
			&& ((at_first_position + 1) < dot_last_position)
					/* There is a "." after the "@" and a charcater between both*/
			&& (length >= 7))									/*The email adress is 7 char. long or more*/
	{
		//The email is correct
		null;
	}
	else
	{
		//Or tell the user to solve the problem
		text = (text + "Please enter a valid mail adress. \n" );
		correct = "Error";
		return false;
	}
}



function check_filled (id)
{
	var element = document.getElementById(id);
	if ((element.value != "") && (element.value != " "))
	{
		null;
	}
	else
	{
		text = (text + "Please fill in the following field: " + id + ". \n" );
		correct = "Error";
		return false;
	}
}

function check_checkbox ()
{
	var anElementIsChecked = "no";
	var postBox = document.getElementById("post");
	var emailBox = document.getElementById("receive_by_email");

	if ((postBox.checked == true) || (emailBox.checked == true))
	{
		anElementIsChecked = "yes";
	}

	if (anElementIsChecked == "no")
	{
		text += "Please check at least one option in the checkbox. \n";
		correct = "Error";
		return false;
	}
}

//================================ End of secondary functions ==================================

	var text = "";
	var correct = "Correct";

// ------------------ Tis function checks all the fields of the Contact Us page ----------------
function check_contact_us ()
{
	//First we need to check if all the fields are filled in
	check_filled("name");
	check_filled("email");
	check_filled("comment");

	//Then we check if the email adress is correct
	check_email();
	if (correct == "Correct")
	{
		return true;
	}
	else
	{
		//We display the type of the error to the user, and we reset the variables
		alert(text);
		text = "";
		correct = "Correct";
		return false;
	}
}


// ------------------ Tis function checks all the fields of the Subscribe page ----------------
function check_subscribe ()
{
	check_filled("name");
	check_filled("address");
	check_filled("town");
	check_filled("postcode");
	check_filled("email");
	check_checkbox();

	check_email();
	if (correct == "Correct")
	{
		return true;
	}
	else
	{
		alert(text);
		text = "";
		correct = "Correct";
		return false;
	}
}