 /****************************************************************************\
	 Creation date :-> 27-06-2006 [dd-mm-yyyy]
	 Last update   :-> 16-04-2009 [dd-mm-yyyy]

	 Author        :-> Antonio Magdic  <antonio.magdic@fsb.hr>
 \****************************************************************************/

var my_form       = "";
var submitted     = false;
var error         = false;
var error_message = "";

function check_input(field_name, field_size, message) {
	if(my_form.elements[field_name] && (my_form.elements[field_name].type != "hidden")) {
		var field_value = my_form.elements[field_name].value;
		if(field_value == '' || field_value.length < field_size) {
			error_message = error_message + "* " + message + "\n";
			error = true;
		}
	}
}

function check_radio(field_name, message) {
	var isChecked = false;
	if(my_form.elements[field_name] && (my_form.elements[field_name].type != "hidden")) {
		var radio = my_form.elements[field_name];

		for (var i=0; i<radio.length; i++) {
			if (radio[i].checked == true) {
				isChecked = true;
				break;
			}
		}

		if(isChecked == false) {
			error_message = error_message + "* " + message + "\n";
			error = true;
		}
	}
}

function check_select(field_name, field_default, message) {
	if(my_form.elements[field_name] && (my_form.elements[field_name].type != "hidden")) {
		var field_value = my_form.elements[field_name].value;

		if(field_value == field_default) {
			error_message = error_message + "* " + message + "\n";
			error = true;
		}
	}
}

function check_password(field_name_1, field_name_2, field_size, message_1, message_2) {
	if(my_form.elements[field_name_1] && (my_form.elements[field_name_1].type != "hidden")) {
		var password = my_form.elements[field_name_1].value;
		var confirmation = my_form.elements[field_name_2].value;

		if(password == '' || password.length < field_size) {
			error_message = error_message + "* " + message_1 + "\n";
			error = true;
		} else if(password != confirmation) {
			error_message = error_message + "* " + message_2 + "\n";
			error = true;
		}
	}
}

function check_password_new(field_name_1, field_name_2, field_name_3, field_size, message_1, message_2, message_3) {
	if(my_form.elements[field_name_1] && (my_form.elements[field_name_1].type != "hidden")) {
		var password_current = my_form.elements[field_name_1].value;
		var password_new = my_form.elements[field_name_2].value;
		var password_confirmation = my_form.elements[field_name_3].value;

		if(password_current == '' || password_current.length < field_size) {
			error_message = error_message + "* " + message_1 + "\n";
			error = true;
		} else if(password_new == '' || password_new.length < field_size) {
			error_message = error_message + "* " + message_2 + "\n";
			error = true;
		} else if(password_new != password_confirmation) {
			error_message = error_message + "* " + message_3 + "\n";
			error = true;
		}
	}
}

function check_form(form_name) {
	if(submitted == true) {
		alert("This form has already been submitted. Please press Ok and wait for this process to be completed.");
		return false;
	}

	error = false;
	my_form = form_name;
	var my_action = my_form.elements['_action_'].value;
	error_message = "Errors have occured during the process of your form.\n\nPlease make the following corrections:\n\n";

	// Sign Up Form OR Update Personal Data Form OR Members Admin -> Edit
	if(my_action == 'sign_up' || my_action == 'update_personal_data' || my_action == 'user_edit' || my_action == 'user_activate') {
		if(my_action == 'user_activate') {
			check_input("password", 5, "Password must contain a minimum of 5 characters.");
		}
		
		if(my_action == 'update_personal_data') {
			check_input("username", 4, "Username must contain a minimum of 4 characters.");
		}
		
		check_input("lastname", 2, "Your Last Name must contain a minimum of 2 characters.");
		check_input("firstname", 2, "Your First Name must contain a minimum of 2 characters.");
		check_input("title", 2, "Your Title must contain a minimum of 2 characters.");
		check_radio("gender", "Please select your Gender.");

		check_input("email", 6, "Your E-Mail Address must contain a minimum of 6 characters.");
		check_input("address", 5, "Your Street Address must contain a minimum of 5 characters.");
		check_input("town", 3, "Your Town must contain a minimum of 3 characters.");
		check_input("postcode", 4, "Your Post Code must contain a minimum of 4 characters.");
		check_select("country", "", "Please select a country from the Countries pull down menu.");
	}

	// Change Password Form
	else if(my_action == 'change_password') {
		check_password_new("old_password", "new_password", "new_password_verify", 5, "Your Password must contain a minimum of 5 characters.", "Your new Password must contain a minimum of 5 characters.", "The Password Confirmation must match your new Password.");
	}

	// Admin: Link
	else if(my_action == 'link_add' || my_action == 'link_edit') {
		check_select("link_topic", "", "Please select a topic from the Topic pull down menu.");
		check_input("link_title", 2, "Link title must contain a minimum of 2 characters.");
		check_input("link_url", 10, "Link url must contain a minimum of 10 characters.");
	}
	
	// Admin: News
	else if(my_action == 'news_edit' || my_action == 'news_add') {
		check_input("news_title", 2, "News title must contain a minimum of 2 characters.");
		//check_input("news_url", 10, "News url must contain a minimum of 10 characters.");
		check_input("news_text", 5, "News text must contain a minimum of 5 characters.");
	}
	
	// My Documents: Add
	else if(my_action == 'my_document_add') {
		check_input("document_title", 2, "Document title must contain a minimum of 2 characters.");
		check_input("document_file", 2, "Please select a document.");
	}
	
	// Admin: Edit Document; My Documents
	else if(my_action == 'document_edit' || my_action == 'my_document_edit') {
		check_input("document_title", 2, "Document title must contain a minimum of 2 characters.");
	}
	
	// Admin: Publication
	else if(my_action == 'publication_add' || my_action == 'publication_edit') {
		check_select("publication_type", "", "Please select a type from the Type pull down menu.");
		check_input("publication_title", 2, "Publication title must contain a minimum of 2 characters.");
		check_input("publication_authors", 2, "Publication authors must contain a minimum of 2 characters.");
		//check_input("publication_description", 5, "Publication description must contain a minimum of 5 characters.");
	}
	
	// SIGs Administration: Add Member
	else if(my_action == 'sig_member_add') {
		check_input("member", "", "Please select a new SIG member from the Member pull down menu.");
	}
	
	// Events
	else if(my_action == 'event_add' || my_action == 'event_edit') {
		check_select("event_type", "", "Please select a type from the Type pull down menu.");
		check_input("event_title", 2, "Event title must contain a minimum of 2 characters.");
		check_select("event_country", "", "Please select a country from the Countries pull down menu.");
	}

	// Private Messages
	else if(my_action == 'send_message') {
		if(getCheckedValue(document.Messages.elements['to']) == '4') {
			check_select("member", "", "Please select a recipient.");
		}
		if(getCheckedValue(document.Messages.elements['to']) == '6') {
			check_select("SIG", "", "Please select a SIG.");
		}
		check_input("subject", 2, "Message subject must contain a minimum of 2 characters.");
		check_input("message_body", 5, "Message body must contain a minimum of 5 characters.");
	}
	
	if(error == true) {
		alert(error_message);
		return false;
	} else {
		submitted = true;
		return true;
	}
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "0";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "0";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "0";
}