/**
 * @package core
 * @subpackage form_input
 * @author Lukas Makovicka <lukas_makovicka@moravia-consulting.com>
 *
 * Class with functions that are used in entire system - loaded in every file of system.
*/

var form_multilist=new Array();

/**
 * Function for making checkbox groups with one checkbox to
 * check/uncheck them all. It goes through all checkboxes with id name_count where count is from 1 to count variable.
 *
 * @param string name Name of input
 * @param integer count Number of checkboxes
 * @param object input Trigger checkbox - from it we will use the checked/unchecked value
*/
function form_multilist_group(name, count, input) {
	for (var i=1; i<=count; i++) {
		var item=document.getElementById(name+'_'+i);
		if (item) {item.checked=input.checked;}
	}
}

/**
 * Makes tab switching for multilists if they are tabbed.
 *
 * @param string name Name of the tabber (of multilist input - fro this is derived ids)
 * @param string code Code - id of current tab
*/
function form_multilist_tab(name, code, select) {
	var last_code=form_multilist[name];
	if (last_code) {
		document.getElementById(last_code).style.display='none';
		if (!select) {
			document.getElementById(last_code+'_tab').className='';
		}
	}
	
	document.getElementById(code).style.display='block';
	if (!select) {
		document.getElementById(code+'_tab').className='selected';
	}
	form_multilist[name]=code;
}


/**
 * Function for showing character count in mediumtext input with this id (name)
 *
 * @param string id Id (name) of mediumtext input
*/
function form_change_text(id) {
	document.getElementById(id+"_count").innerHTML=document.getElementById(id).value.length;
}

/**
 * Checks if text in the mediumtext input isn't longer than maximum length.
 *
 * @param string id Id (name) of mediumtext input
 * @param integer count Maximum length of input
 * @param string name Name of input (for error messages)
 * @return string "" if ok, error message, if input is longer
*/
function form_check_text(id, count, name) {
	if (document.getElementById(id).value.length>count) {return "* "+name+" - input longer than "+count+" characters\n";}
	else {return "";}
}

/**
 * Preforms regexp check of date on date input with selected name.
 * Date must be in format d.m.Y H:i where H:i isn't required.
 *
 * @param string id Id (name) of date input
 * @param string name Name of input (for error messages)
 * @return "" if ok, error message, if date isn't in proper format
*/
function form_check_date(id, name, type) {
	var re=/^\d{1,2}\.\d{1,2}\.\d{4}( (\d{1,2})?:(\d{1,2})?)?$/;
	var message=" - date not in format \"d.m.yyyy h:i\" (h:i is not required)\n";
	if (document.getElementById(id).value.search(re)!=-1) {return "";}
	else if (document.getElementById(id).value=="") {return "";}
	else {
		return "* "+name+message;
	}
}

/**
 * Checks int input for min and max values
 *
 * @param string id Id (name) of int input
 * @param string name Name of input (for error messages)
 * @param mixed min Min value, or "undefined" if no min value selected
 * @param mixed max Max value, or "undefined" if no max value selected
 * @return "" if ok, error message, if int is smaller than min or bigger than max
*/
function form_check_int(id, name, min, max) {
	var re=/^\-?\d+([\.\,]\d+)?$/;
	if (document.getElementById(id).value=="") {}
	else if (document.getElementById(id).value.search(re)==-1) {return "* "+name+" - number not in format (-)xxx[.,]xxx - (.xxx part is not required)\n";}
	if (min!="undefined") {
		if (parseFloat(document.getElementById(id).value)<min) {return "* "+name+" - number smaller than "+min+"\n";}
	}
	if (max!="undefined") {
		if (parseFloat(document.getElementById(id).value)>max) {return "* "+name+" - number bigger than "+max+"\n";}
	}
	return "";
}

/**
 * Checks password input - if the password is set twice the same
 *
 * @param string id Id (name) of int input
 * @param string name Name of input (for error messages)
 * @return "" if ok, error message, if password is different in each case
*/
function form_check_password(id, name) {
	if (document.getElementById(id).value!=document.getElementById(id+"_check").value) {return "* "+name+" - password must be twice the same\n";}
	return "";
}

/**
 * Checks if in the input is any value
 *
 * @param string id Id (name) of input
 * @param string message Error message in case of error
*/
function form_check_required(id, message) {
	if (!document.getElementById(id).value) {return "* "+message+"\n";}
	return "";
}

function show_hide(id) {
	var item=document.getElementById(id);
	if (item) {
		if (item.style.display=='block') {item.style.display='none';}
		else {item.style.display='block';}
	}
}

/**
 * Show element with this id
 *
 * @param string id Id of element
*/
function show(id) {
	document.getElementById(id).style.display='block';
}

/**
 * Hide element with this id
 *
 * @param string id Id of element
*/
function hide(id) {
	document.getElementById(id).style.display='none';
}

