/*	core	*/
function trim(str) {
	var ret;
	if(typeof(str) != "string") str = str + "";
	return str.replace(/(^\s+)|(\s+$)/gi, ""); 
}

function isNonEmpty(ctrl) {
	var empty = true;
	var msg;
	if(typeof(ctrl) == "undefined") {
		alert("checkNonEmpty error: the field does not exists [" + ctrl.name + "]\n");
		return false;
	}
	if(ctrl.type == "select-one" || ctrl.type == "select-multiple") //select control
		empty = (trim(ctrl.options[ctrl.selectedIndex].text) == "");
	else //text control
		empty = (trim(ctrl.value)=="");
	if(empty) return false;
	else return true;
}

function checkNonEmpty(ctrl, errorMsg) {
	var empty = true;
	var msg;
	if(typeof(ctrl) == "undefined") {
		alert("checkNonEmpty error: the field does not exists [" + ctrl.name + "]\n");
		return false;
	}
	if(ctrl.type == "select-one" || ctrl.type == "select-multiple") //select control
		empty = (ctrl.options[ctrl.selectedIndex].text == "");
	else //text control
		empty = (trim(ctrl.value)=="");
	if(empty) {
		alert(errorMsg);
		ctrl.focus();
		return false;
	}
	else
		return true;
}

function checkNumeric(ctrl, errorMsg) {
	var i;
	var numeric = true;
	if(typeof(ctrl) == "undefined") {
		alert("checkNumeric error: the field does not exists [" + ctrl.name + "]\n");
		return false;
	}
	if(ctrl.type != "text") {
		alert("checkNumeric error: the field is not a textbox [" + ctrl.name + "]\n");
		return false;
	}
	for(i=0; i<ctrl.value.length; i++)
		if(ctrl.value.charAt(i) > "9" || ctrl.value.charAt(i) <"0")
			numeric = false;
			
	if(!numeric) {
		alert(errorMsg);
		ctrl.focus();
		return false;
	}
	else
	{
		return true;
	}
}

function checkDate(ctrl, errorMsg)
{
	var i;
	var isdate = true;
	var val;
	var dateparts;
	var intYear, intMonth, intDay;
	var daysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	var nDays;
	if(typeof(ctrl) == "undefined") {
		alert("checkDate error: the field does not exists [" + ctrl.name + "]\n");
		return false;
	}
	if(ctrl.type != "text") {
		alert("checkDate error: the field is not a textbox [" + ctrl.name + "]\n");
		return false;
	}
	val = ctrl.value;
	dateparts = val.split("/");
	if(dateparts.length != 3) {
		isdate = false;
	}
	else
	{
		intYear = dateparts[2];
		intMonth = dateparts[1];
		intDay = dateparts[0];
		
		if(!isNumeric(intYear) || !isNumeric(intMonth) || !isNumeric(intDay)) {
			isdate = false;
		}
		else
		{
			intYear = parseInt(intYear, 10);
			intMonth = parseInt(intMonth, 10);
			intDay = parseInt(intDay, 10);
			
			if(intYear <= 50)
				intYear += 2000;
				
			if(intYear >= 51 && intYear < 100)
				intYear += 1900;
			if(intMonth == 0 || intMonth > 12 || intDay == 0 || intYear > 9999)
			{
				isdate = false;
			}
			else
			{
				if (intMonth==2) //february
				{
					nDays = ((intYear%4==0) && (!(intYear%100==0) || (intYear%400==0)))?29:28;
				}
				else
					nDays = daysInMonth[intMonth-1];
				if(intDay > nDays)
					isdate = false;
			}
		}
	}
	if(!isdate)
	{
		alert(errorMsg);
		ctrl.focus();
		return false;
	}
	else
	{
		return true;
	}
}

function isNumeric(str) {
	var i;
	var numeric = true;
	for(i=0; i<str.length; i++)
		if(str.charAt(i) > "9" || str.charAt(i) <"0")
			numeric = false;
	return numeric;
}

function isNumericSigned(str) {
	var i;
	var start = 0;
	var numeric = true;
	if (str.charAt(0)=="-" || str.charAt(0)=="+") start = 1;
	for(i=start; i<str.length; i++)
		if(str.charAt(i) > "9" || str.charAt(i) <"0")
			numeric = false;
	return numeric;
}

function setCookie(name, value, days) {
	var today = new Date();
	var expire = new Date();
	if (days==null || days==0) days = 1;
	expire.setTime(today.getTime() + 3600000*24*days);
	document.cookie = name + "=\"" + escape(value) + "\";expires=" + expire.toGMTString() + ";path=/";
}

function getCookie(name) {
	var cookie = "" + document.cookie;
	var ind = cookie.indexOf(name);
	if (ind==-1 || name=="") return "";
	var ind1 = cookie.indexOf(';', ind);
	if (ind1==-1) ind1 = cookie.length;
	return unescape(cookie.substring(ind + name.length+2, ind1-1));
}

function delCookie(name) {
	setCookie(name, "", -1);
}

/*	app	*/
function fSetupWin(url, name, width, height) {
	w = window.open(url, name, "width=" + width + ",height=" + height + ",status=no,resizable=yes,menubar=no,location=no,scrollbars=yes");
	w.focus();
}

var isSelected = false;
function fToggleSelect(frm)
{	var i;
	if (isSelected == false)
	{	for(i=0; i<frm.elements.length; i++)
			if (frm.elements[i].type == "checkbox" && frm.elements[i].name == "ids")
				frm.elements[i].checked = true;
		isSelected = true;}
	else
	{	for(i=0; i<frm.elements.length; i++)
			if (frm.elements[i].type == "checkbox" && frm.elements[i].name == "ids")
				frm.elements[i].checked = false;
		isSelected = false;}
}
function fCheckSelection(frm)
{	var i;
	check = false;
	for(i=0; i<frm.elements.length; i++)
		if (frm.elements[i].type == "checkbox" && frm.elements[i].name == "ids" && frm.elements[i].checked)
		{	check = true;
			break;}
	return check;}

function toggleMenu(submenu) {
	menu_state = getCookie('fourhooks.menu').toString();
	if (document.getElementById(submenu).style.display == 'none') {
		document.getElementById(submenu).style.display = 'block';
		if (submenu.substr(0, 3)=='sub') {
			document.getElementById('img' + submenu).src = 'http://media.fourhooks.ro/admin/ico_up.gif';
		} else {
			document.getElementById('img' + submenu).src = 'http://media.fourhooks.ro/admin/ico_tit_up.gif';
		}
		menu_state += '#' + submenu;
	} else {
		document.getElementById(submenu).style.display = 'none';
		if (submenu.substr(0, 3)=='sub') {
			document.getElementById('img' + submenu).src = 'http://media.fourhooks.ro/admin/ico_down.gif';
		} else {
			document.getElementById('img' + submenu).src = 'http://media.fourhooks.ro/admin/ico_tit_down.gif';
		}
		var menu_state_items = menu_state.split('#');
		menu_state = '';
		for(var i=0; i<menu_state_items.length; i++) {
			if (menu_state_items[i]!=submenu && menu_state_items[i] != '')
				menu_state += menu_state_items[i] + '#';
		}
		menu_state = menu_state.substr(0, menu_state.length-1);
	}
	setCookie('fourhooks.menu', menu_state, 365);
}

