/**-----------------------------------------------------------------------------
  Module "ML Mass Mail"
  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 Mass Mail module.

  ML Mass Mail 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 Mass Mail 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 Mass Mail module. If not, see "http://www.gnu.org/licenses".

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

  ML Mass Mail 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; 14/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_massmail_hideblock_f(moduledirectory, blockname)
mod_ml_massmail_showblock_f(moduledirectory, blockname)

    Form management
mod_ml_massmail_checkemail_f(moduledirectory, email_value, image_prefixe)
mod_ml_massmail_checktext_f(moduledirectory, text_value, image_prefixe, text_min_length)

    ML Mass Mail functions
mod_ml_massmail_checkandsubmit_f(form, use_mail, use_name, email_error, name_error, mail_error)
-------------------------------------------------------------------------------*/


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

function mod_ml_massmail_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_massmail_showblock_f(moduledirectory, blockname) {
/*
This function is the reciprocal function of "mod_ml_massmail_hideblock_f" to show the same block.
*/
	document.getElementById("mod_" + moduledirectory + "_" + blockname + "_hiddenblock" ).style.display='block';
}


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

function mod_ml_massmail_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_massmail_showblock_f(moduledirectory, image_prefixe+ 'ok');
		mod_ml_massmail_hideblock_f(moduledirectory, image_prefixe+ 'ko');
		mod_ml_massmail_hideblock_f(moduledirectory, image_prefixe+ 'default');
		return true;
	}
	else {
		mod_ml_massmail_showblock_f(moduledirectory, image_prefixe+ 'ko');
		mod_ml_massmail_hideblock_f(moduledirectory, image_prefixe+ 'ok');
		mod_ml_massmail_hideblock_f(moduledirectory, image_prefixe+ 'default');
		return false;
	}
}


function mod_ml_massmail_checktext_f(moduledirectory, text_value, image_prefixe, text_min_length) {
/*
This function is similar to "mod_ml_massmail_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_massmail_showblock_f(moduledirectory, image_prefixe+ 'ok');
		mod_ml_massmail_hideblock_f(moduledirectory, image_prefixe+ 'ko');
		mod_ml_massmail_hideblock_f(moduledirectory, image_prefixe+ 'default');
		return true;
	}
	else {
		mod_ml_massmail_showblock_f(moduledirectory, image_prefixe+ 'ko');
		mod_ml_massmail_hideblock_f(moduledirectory, image_prefixe+ 'ok');
		mod_ml_massmail_hideblock_f(moduledirectory, image_prefixe+ 'default');
		return false;
	}
}


/*-------------------------------------------------------------------------------
    ML Mass Mail functions
-------------------------------------------------------------------------------*/

function mod_ml_massmail_checkandsubmit_f(form, use_mail, use_name, email_error, name_error, mail_error, do_submit) {
/*
This function check that all fields are OK and submit the form (in the ml_massmail frontend).
Argument:
	- form: the form to check
	- use_mail: true is mail is used, false otherwise
	- use_name: true is name is used, false otherwise
	- email_error: error to display if email is not valide
	- name_error: error to display if name is not valide
	- mail_error: error to display if mail is not valide
	- 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 e-mail field
	if(!mod_ml_massmail_checkemail_f(document.getElementById('text_email').value, 'email')) {
		alert(email_error);
		return false;
	}
	
	// If it is a subscription
	if(document.getElementById('radio_subscribe').checked) {
		// Check name field, if necessary
		if(use_name) {
			if(!mod_ml_massmail_checktext_f(document.getElementById('text_name').value, 'name')) {
				if(do_submit){
					alert(name_error);
				}
				return false;
			}
		}
		
		// Check mail field, if necessary
		if(use_mail) {
			if(!mod_ml_massmail_checktext_f(document.getElementById('textarea_mail').value, 'mail')) {
				if(do_submit){
					alert(mail_error);
				}
				return false;
			}
		}
	}
	
	// All is fine
	if(do_submit){
		return true;
	}
	else{
		return false;
	}
}
