
function open_spellchecker() {
	
	// are the field ids passed as an array or comma separated?
	if (is_array(arguments[0])) {
		var field_ids = arguments[0];
	} else {
		var field_ids = arguments;
	}
	
	var fields = new Array();
	
	for (var i = 0 ; i < field_ids.length; i++) {
		fields[i] = document.getElementById(field_ids[i]);
		if (!fields[i]) { continue; }
	}
	
	var speller = new spellChecker();
	speller.textInputs = fields;
	speller.openChecker();
}

function get_live_preview(the_input, the_output_id) {
	
	if (the_output_id == null || the_output_id == "") {
		var the_output_id = "the_preview";
	}
	
	var write_to = document.getElementById(the_output_id);
	
	write_to.innerHTML = the_input.value;
}


function show_guidline(field) {
	
	var tags 				= new Array("input", "textarea", "select");
	var form				= field.form;
	var elements 			= get_form_elements(tags, form);
	
	elements.push(document.getElementById('the_preview'));  // add preview div manually
	
	var guideline_id_in_focus	= field.id.replaceAll(/[0-9]+/, "") + "_guideline"; // remove numbers and append
	
	for (var i = 0; i < elements.length; i++) {
		
		var guideline_id = elements[i].id.replaceAll(/[0-9]+/, "") + "_guideline";
		var guideline = document.getElementById(guideline_id);
		if (!guideline) { continue; }
		
		if (guideline_id != guideline_id_in_focus) {
			
			//guideline.style.color = "rgb(220, 220, 220)";
			guideline.style.fontWeight = "normal";
			
		} else {
			//guideline.style.color = "rgb(80, 80, 80)";
			guideline.style.fontWeight = "bold";
		}
	}
}

function validate_form(form) {
	
	var elements 		= new Array();
	var tags 			= new Array("input", "textarea", "select");
	var errors 			= new Array();
	var error_message 	= '';
	var email_filter  		= /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	elements 			= get_form_elements(tags, form);
	
	for (var i = 0; i < elements.length; i++) {
		
		if (form.elements[i].className.indexOf("required") != -1) {
		//if (form.elements[i].type != "submit" && form.elements[i].type != "hidden" && form.elements[i].className.indexOf("optional") == -1) {	
			
			var marker_id = form.elements[i].name + "_error";
			var marker = document.getElementById(marker_id);
			if (!marker) { continue; }
			marker.style.color = "rgb(220, 220, 220)";
			
			if (form.elements[i].name.indexOf("email") != -1 && !email_filter.test(trim(form.elements[i].value))) {
				
				errors.push(form.elements[i].name);
				var marker_id = form.elements[i].name + "_error";
				var marker = document.getElementById(marker_id);
				if (!marker) { continue; }
				marker.style.color = "red";
				
			} else if (form.elements[i].name.indexOf("email") == -1 && trim(form.elements[i].value) == "") {
				
				errors.push(form.elements[i].name);
				var marker_id = form.elements[i].name + "_error";
				var marker = document.getElementById(marker_id);
				if (!marker) { continue; }
				marker.style.color = "red";
			}
		}
	}
	
	if (errors.length == 0) {
		
		return true;
		
	} else {
		
		error_message = "Please complete the following fields:\n";
		
		for (var i = 0; i < errors.length; i++) {
			error_message += "\n - " + get_titlecase(errors[i].replaceAll("_", " "));
		}
		
		alert(error_message);
		return false;
	}
}

function get_form_elements(tags, node) {
	
	if( node == undefined ) {
		node = document;
	}
	
	var elements = new Array();
	
	for( var i = 0; i < tags.length; i++ ) {
		
		elements_found = node.getElementsByTagName(tags[i]);
		for (var j = 0; j < elements_found.length; j++) {
			elements.push( elements_found.item(j) );
		}
	}
	return elements;
}

function get_titlecase(str) {
    return str.replace(/\w+/g, function(txt){ return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase(); });
}

function count_words_and_chars(the_input, the_output_id, text) {
	count_words(the_input, the_output_id, text);
	count_chars(the_input, the_output_id, text);
}

/* The function will write to an element with the id 
the_input.id + '_char_count' if no output element is specified */
function count_chars(the_input, the_output_id, text) {
	
	var read_from = the_input;
	var char_count = read_from.getAttribute("maxlength") - read_from.value.length;
	
	if (the_output_id == null || the_output_id == "") {
		var the_output_id = the_input.id + "_char_count";
	}
	
	var write_to = document.getElementById(the_output_id);
	
	if (text == null || text == "") {
		var text = "{CHAR} Characters Left";
	}
	
	if (char_count <= 0) {
		char_count = 0;
		text = '<span class="disable"> '+text+' </span>';
		read_from.value = read_from.value.substr(0, read_from.getAttribute("maxlength"));
	}
	
	write_to.innerHTML = text.replace("{CHAR}", char_count).bold();
}


/* The function will write to an element with the id 
the_input.id + '_word_count' if no output element is specified */
function count_words(the_input, the_output_id, text) {
	
	var read_from = the_input;
	var words = trim(read_from.value).replaceAll(/\s+/, " ").split(/\s/); // replace consecutive spaces with one
	var word_count = words.length;
	
	if (the_output_id == null || the_output_id == "") {
		var the_output_id = the_input.id + "_word_count";
	}
	
	var write_to = document.getElementById(the_output_id);
	
	if (text == null || text == "") {
		var text = "{WORD} Words";
	}
	
	if (read_from.value == '') {
		word_count = 0;
	}
	
	write_to.innerHTML = text.replace("{WORD}", word_count).bold();
}

String.prototype.replaceAll=function(s1, s2) {return this.split(s1).join(s2)}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}

function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}

function is_array() {
	var type =  arguments[0].constructor.toString().toLowerCase().match(/array/i);
	return (type == 'array' ? true : false);
}
