/**-----------------------------------------------------------------------------
  Module "ML Contact"
  File "frontend.js"
  Copyright (C) 2008-2009, Nicolas Cardoso De Castro
(nicolas.cardoso(at)altaum.com or "http://www.altaum.com")
--------------------------------------------------------------------------------
  This file is part of ML Contact module.

  ML Contact module is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option) any
later version.

  ML Contact module is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.

  You should have received a copy of the GNU General Public License along with
ML Contact module. If not, see "http://www.gnu.org/licenses".

--------------------------------------------------------------------------------
  ML Contact module has been developed on the base of the "HelloWorld" tutorial 
module available at 
"http://addons.websitebaker.org/pages/modules/miscellaneous.php".

  ML Contact module is developed for the Open Source Content Management System
Website Baker (http://websitebaker.org, copyright (C) 2004-2006, Ryan
Djurovich).

--------------------------------------------------------------------------------
  MODIFICATION HISTORY:
    Nicolas Cardoso De Castro; 12/06/2009
      + initial release
-----------------------------------------------------------------------------**/


/*
NOTE:
This file is automatically loaded by Website Baker when invoking "modify.php" or "tool.php" files.
All Javascript functions have to stick to the following naming conventions:
	mod_MODULE_DIRECTORY_XXXX_f

	mod:	shows that the Javascript routine is defined within a module
	MODULE_DIRECTORY:	unique identifier to prevent that Javascript routines are overwritten by other modules
	XXXX:	your function name
	f:	identifier for the frontend
*/


/*-------------------------------------------------------------------------------
  Functions list
--------------------------------------------------------------------------------
    Misc
mod_ml_contact_hideblock_f(moduledirectory, blockname)
mod_ml_contact_showblock_f(moduledirectory, blockname)

    Form management
mod_ml_contact_checkemail_f(moduledirectory, email_value, image_prefixe)
mod_ml_contact_checktext_f(moduledirectory, text_value, image_prefixe, text_min_length)

    ML Contact functions
mod_ml_contact_checkandsubmit_f(moduledirectory, form, use_firstname, use_name, use_email, firstname_error, name_error, email_error, subject_error, comment_error, do_submit)
-------------------------------------------------------------------------------*/


/*-------------------------------------------------------------------------------
    Misc
-------------------------------------------------------------------------------*/

function mod_ml_contact_hideblock_f(moduledirectory, blockname) {
/*
This function hides a block. The block should be called (I mean have as IDs) "mod_MODULE_DIRECTORY_XXXX_hiddenblock".
Argument:
	- moduledirectory: the name of the module directory (the string "MODULE_DIRECTORY" from the example)
	- blockname: the name of the block (the string "XXXX" from the example).
*/
	document.getElementById("mod_" + moduledirectory + "_" + blockname + "_hiddenblock" ).style.display='none';
}  


function mod_ml_contact_showblock_f(moduledirectory, blockname) {
/*
This function is the reciprocal function of "mod_ml_contact_hideblock_f" to show the same block.
*/
	document.getElementById("mod_" + moduledirectory + "_" + blockname + "_hiddenblock" ).style.display='block';
}

/*-------------------------------------------------------------------------------
    Form management
-------------------------------------------------------------------------------*/

function mod_ml_contact_checkemail_f(moduledirectory, email_value, image_prefixe) {
/*
This function checks if the email provided by the user is correct. If so, it shows an "OK" image, if not, it shows a "KO" image. This function assumes that 3 blocks exist, one with a "default" image, one with an "OK" image and the last with a "KO" image, named as follows "mod_MODULE_DIRECTORY_XXXX_ok", "mod_MODULE_DIRECTORY_XXXX_ko" and "mod_MODULE_DIRECTORY_XXXX_default"
Argument:
	- moduledirectory: the name of the module directory (the string "MODULE_DIRECTORY" from the example)
	- email_value: the e-mail provided by the user
	- image_prefixe: the prefixe of the block containing the images (the string "XXXX" from the example)
*/
	var at = email_value.indexOf("@", 1);
	var dot = email_value.indexOf(".", at + 2);
	
	if ((at > 1) && (email_value.length > dot + 2) && (dot > at + 2)) {
		mod_ml_contact_showblock_f(moduledirectory, image_prefixe+ 'ok');
		mod_ml_contact_hideblock_f(moduledirectory, image_prefixe+ 'ko');
		mod_ml_contact_hideblock_f(moduledirectory, image_prefixe+ 'default');
		return true;
	}
	else {
		mod_ml_contact_showblock_f(moduledirectory, image_prefixe+ 'ko');
		mod_ml_contact_hideblock_f(moduledirectory, image_prefixe+ 'ok');
		mod_ml_contact_hideblock_f(moduledirectory, image_prefixe+ 'default');
		return false;
	}
}


function mod_ml_contact_checktext_f(moduledirectory, text_value, image_prefixe, text_min_length) {
/*
This function is similar to "mod_ml_contact_checkemail_f" but with a simple text.
Argument:
	- moduledirectory: the name of the module directory (the string "MODULE_DIRECTORY" from the example)
	- text_value: the text provided by the user
	- image_prefixe: the prefixe of the block containing the images (the string "XXXX" from the example)
	- text_min_length: the minimal length the text should have
*/
	if (text_value.length >= text_min_length) {
		mod_ml_contact_showblock_f(moduledirectory, image_prefixe+ 'ok');
		mod_ml_contact_hideblock_f(moduledirectory, image_prefixe+ 'ko');
		mod_ml_contact_hideblock_f(moduledirectory, image_prefixe+ 'default');
		return true;
	}
	else {
		mod_ml_contact_showblock_f(moduledirectory, image_prefixe+ 'ko');
		mod_ml_contact_hideblock_f(moduledirectory, image_prefixe+ 'ok');
		mod_ml_contact_hideblock_f(moduledirectory, image_prefixe+ 'default');
		return false;
	}
}


/*-------------------------------------------------------------------------------
    ML Contact functions
-------------------------------------------------------------------------------*/

function mod_ml_contact_checkandsubmit_f(moduledirectory, form, use_firstname, use_name, use_email, firstname_error, name_error, email_error, subject_error, comment_error, do_submit) {
/*
This function check that all fields are OK and submit the form (in the ml_contact frontend).
Argument:
	- moduledirectory: the name of the module directory (the string "MODULE_DIRECTORY" from the example)
	- form: the form to check
	- use_firstname: true is the first name is used, false otherwise
	- use_name: true is the name is used, false otherwise
	- use_email: true is the email is used, false otherwise
	- firstname_error: error to display if the first name is not valide
	- name_error: error to display if the name is not valide
	- email_error: error to display if the email is not valide
	- subject_error: error to display if the subject is not empty
	- comment_error: error to display if the comment is not empty
	- do_submit: true is the form should be submited if it is correct, false if one only want to force a check, but not submit afterwards
*/
	// Check firstname field, if necessary
	if(use_firstname) {
		if(!mod_ml_contact_checktext_f(moduledirectory, document.getElementById('first_name').value, 'firstname', 1)) {
			if(do_submit){
				alert(firstname_error);
			}
			return false;
		}
	}
	
	// Check name field, if necessary
	if(use_name) {
		if(!mod_ml_contact_checktext_f(moduledirectory, document.getElementById('name').value, 'name', 1)) {
			if(do_submit){
				alert(name_error);
			}
			return false;
		}
	}
	
	// Check email field, if necessary
	if(use_email) {
		if(!mod_ml_contact_checkemail_f(moduledirectory, document.getElementById('email').value, 'email')) {
			if(do_submit){
				alert(email_error);
			}
			return false;
		}
	}
	
	// Check that subject is not empty
	if(moduledirectory, moduledirectory, document.getElementById('subject').value.length == 0) {
		if(do_submit){
			alert(subject_error);
		}
		return false;
	}
	
	// Check that subject is not empty
	if(moduledirectory, moduledirectory, document.getElementById('comment').value.length == 0) {
		if(do_submit){
			alert(comment_error);
		}
		return false;
	}
	
	// All is fine
	if(do_submit){
		return true;
	}
	else{
		return false;
	}
}
