/*
	Form Validation Script
	Written By: Michael Langford, CARE USA, webmaster@care.org
	Do not reuse without permission.

	General Note:  If server validation is available, add a hidden form element to the form
	with the name "javascript" and give it a value of "disabled" (actually, you can set it to anything you want based
	on your form processing script).  Then, in your processor, set a toggle that looks for this field value.  If it is
	set to "enabled" you can safely not use server-side processing because this script will have run, if it's set to
	"disabled" however, you'll need to use server-side scripts becuase it means this script wasn't allowed to run for
	whatever reason.
*/

/* Declare globals */
	var SERVER_VALIDATION_AVAILABLE = false;
	var ERROR_BGCOLOR = "#ffeeee";
	var ERROR_FONTCOLOR = "#000000";
	var BASE_BGCOLOR = "#ffffff";
	var INVALID_ERRORS = "";
	var BLANK_ERRORS = "";
	var ERROR_MESSAGE = "";
	var BLANK_MESSAGE = "";
	var INVALID_MESSAGE = "";
	var INVALID_EMAIL_ATTEMPTS = 0;
	var INVALID_EMAIL_ATTEMPTS_MAX = 3;
	var IGNORE_INVALID_EMAIL = false;

/*Instantiate and populate and array containing the field names for each required field. */
	var reqs = new Array();
		reqs[0] = "sender_name";
		reqs[1] = "sender_email";
		reqs[2] = "recip_name0";
		reqs[3] = "recip_email0";
		reqs[4] = "message";

/*
	The validate function requires the form object as input.  It depends upon two other functions:
	isBlank and isInvalid.  It returns true if both isBlank and isInvalid are false, otherwise
	it returns false and cancels form submission.  It also concats all errors and displays them
	in a single alert popup that designates whether a required value was omitted or whether a
	value entered was invalid.
*/
	function validate(obj){
		if(submitForm()){
			return HasValidData(obj);
		}
		else{
			return false;
		}
	}

	function HasValidData(obj){
		if(SERVER_VALIDATION_AVAILABLE){ obj.javascript.value = "enabled"; }
		ERROR_MESSAGE = "";
		BLANK_ERRORS = "";
		INVALID_ERRORS = "";

		for(i=0;(i<reqs.length);i++){
			element = eval(obj.elements[reqs[i]]);
			element.style.backgroundColor = BASE_BGCOLOR;
			if(isBlank(element)){
				if(element.id == "hdnmessage"){ BLANK_ERRORS += " - Your Message\n"; }
				else{ BLANK_ERRORS += " - "+element.id+"\n"; }
			}
			else{ if(isInvalid(element,i)){ INVALID_ERRORS += " - "+element.id+"\n"; } }
		}

		if(BLANK_ERRORS.length != 0 || INVALID_ERRORS.length != 0){
			if(BLANK_ERRORS.length != 0){
				BLANK_MESSAGE += "The following required fields are required,\nbut it appears you left them blank:\n" + BLANK_ERRORS;
			}
			if(INVALID_ERRORS.length != 0){ INVALID_MESSAGE += "\nThe following required fields appear to contain invalid values:\n" + INVALID_ERRORS + "\nThese invalid values have been removed,\nplease re-enter them.\n"; }
			if(INVALID_EMAIL_ATTEMPTS < INVALID_EMAIL_ATTEMPTS_MAX || IGNORE_INVALID_EMAIL){

				ERROR_MESSAGE = INVALID_ERRORS.length != 0?BLANK_MESSAGE+"\n==================================\n"+INVALID_MESSAGE:BLANK_MESSAGE;
				ERROR_MESSAGE = BLANK_ERRORS.length == 0?INVALID_MESSAGE:ERROR_MESSAGE;
				alert(ERROR_MESSAGE);
				ERROR_MESSAGE = "";
				BLANK_MESSAGE = "";
				INVALID_MESSAGE = "";
				return false;
			}
			else{
				MAXED_OUT_EMAIL = "It appears our email address validator is unable\nto recognize your address as valid.\n\nSome email address formats are more difficult\nto recognize as valid than others, and this may be\nthe problem you are having.\n\nIf you feel you have entered your address in properly,\nplease click OK below to complete the sign up process.\n\nIf you know you entered it incorrectly,\nyou may click Cancel to re-enter it.";
				if(confirm(MAXED_OUT_EMAIL)){
					IGNORE_INVALID_EMAIL = true;
					obj.validemail.value = 0;
					return false;
				}
				else{
					INVALID_EMAIL_ATTEMPTS = 0;
					return false;
				}
			}
		}
		else{ return true; }
	}

/*
	isBlank simply tests whether a value is blank or not.
	It returns true if the value is blank,
	It returns false if the value has ANY value.  The type of value does not matter.
	isBlank requires a form element object as an argument.
*/
	function isBlank(obj){
		if(obj.value == ""){ changeColor(obj); return true; }
		else{ return false; }

	}

/*
	doTrim simply chomps whitespace from boths sides of a value.
	It will leave whitespace inside of the input string alone.
	doTrim is not dependent on any other function and requires
	a form object as an arguement.  It also requires that the array
	of required elements be instantiated.
*/
	function doTrim(obj){
		if(obj.name == reqs[3]){ obj.value = obj.value.toUpperCase(); }
		obj.value = obj.value.replace(/^\s+/g,"");
		obj.value = obj.value.replace(/\s+$/g,"");
		return true;
	}


/*
	isInvalid tests whether values pass various validity checks, based on the value itself.
	The function takes two arguments, one is a form element object, the other is an integer
	that should correspond an array element in the required field array.  If a given field
	passes its validity test, or if the field name is not listed in the switch logic,
	the function will return false.  If the string fails the validity check, the function
	returns true.
*/
	function isInvalid(obj,item){
		switch(reqs[item]) {
			default: return false;
		}
		return false;
	}

	function changeColor(obj){
		obj.style.backgroundColor = ERROR_BGCOLOR;
		obj.style.color = ERROR_FONTCOLOR;
		return true;
	}

	function show_preview(img_path,image_id,obj){
//		document.images.preview_image.src = img_path;
//		document.ecard_send_form.ecard_filename.value = img_path;

//		if(document.getElementById){
//			document.ecard_send_form.card_title.value = document.getElementById(image_id).alt;
//			document.getElementById("preview_image").alt = document.getElementById(image_id).alt;
//			document.getElementById("preview_image").src = img_path;
//		}
//		else {document.ecard_send_form.card_title.value = "CARE ...where the end of poverty begins";}
	}

	function SelectThis(objid){
		THUMBS = $$(".thumbs");

		for(var i=0; i < THUMBS.length; i++){
			THUMBS[i].style.border = "0px #000 solid";
		}

		$(objid).style.border = "5px #f60 solid";
		$('ecard_filename').value = $(objid).alt;
		$('card_title').value = $(objid).title;
		$('template_file').value = $(objid).getAttribute("template");

		try{
			console.log(
				"Filename: "+$(objid).alt,
				"\nTitle: "+$(objid).title,
				"\nTemplate: " + $(objid).getAttribute("template")
			);
		}catch(e){};
	}

