function isEmpty (s) { return ((s==null) || (s.length==0)) }

function isDigit (c) { return ((c>="0") && (c<="9")) }

function isWhitespace (s) {
	var whitespace="\t\r\n";
	var i;
	if(isEmpty(s))
		return true;
	for (i=0;i<s.length;i++) {
		var c = s.charAt(i);
		if (whitespace.indexOf(c)==-1)
			return false;
	}

	return true;
}

function noWhitespace (s) {
	var whitespace="\t\r\n ";
	var i;
	if(isEmpty(s))
		return true;
	for (i=0;i<s.length;i++) {
		var c = s.charAt(i);
		if (whitespace.indexOf(c)!=-1)
			return false;
	}
	return true;
}

function isEmail (s) {
	var i = 1;
	var sLength = s.length;
	if (noWhitespace(s)) {
		while ((i<sLength) && (s.charAt(i) != "@")) { i++ }
		if ((i>=sLength) || (s.charAt(i) != "@"))
			return false;
		while ((i<sLength) && (s.charAt(i) != ".")) { i++ }
		if ((i>=sLength-2) || (s.charAt(i) != "."))
			return false;
		return true;
	}
	return false;
}

function isAllDigit (s) {
	var i;
	if(isEmpty(s) || isWhitespace(s))
		return false;
	for (i=0;i<s.length;i++) {
		var c = s.charAt(i);
		if (!isDigit(c))
			return false;
	}
	return true;
}

function isDate(szData){
	var retValue = false;
	var bLongFormat = true;
	var arr_date;
	var re_date;
	re_date = /^(\d{1,2})[-\/](\d{1,2})[-\/](\d{4})\s+(\d{1,2})[:\.](\d{1,2})[:\.]{0,1}(\d{1,2}){0,1}$/;
		arr_date = re_date.exec(szData);
	if(!arr_date){
		bLongFormat = false;
		re_date = /^(\d{1,2})[-\/](\d{1,2})[-\/](\d{4})$/;
		arr_date = re_date.exec(szData);
	}
	if(!arr_date) {return retValue;}

	//*** Controlla il mese
	var tmpMonth = Number(arr_date[2]);
	if ((tmpMonth >12) || (tmpMonth < 1)) {	return retValue; }
	//*** Controlla l'anno
	var tmpYear = String(arr_date[3]);
	if (tmpYear.length == 2) tmpYear = "20" + tmpYear;
	tmpYear	= Number(tmpYear);
	if ((tmpYear > 9999) || (tmpYear < 1763)) {	return retValue; }
	//*** Controlla il giorno
	var numDayMonth = GetDaysInMonth(tmpMonth,tmpYear);
	var tmpDay = Number(arr_date[1]);
	if ((tmpDay > numDayMonth) || (tmpDay < 1))  {	return retValue; }

	if(bLongFormat){
		//*** Controlla l'ora
		var tmpHH = Number(arr_date[4])
		if ((tmpHH > 23) || (tmpHH < 0)) return retValue;
		//*** Controlla i minuti
		var tmpMM = Number(arr_date[5])
		if ((tmpMM > 59) || (tmpMM < 0)) return retValue;
		//*** Controlla i secondi
		if (arr_date.length == 7) {
			var tmpSS = Number(arr_date[6])
			if ((tmpSS > 59) || (tmpSS < 0)) return retValue;
		}
	}
	return true;
}

function GetDaysInMonth(tmpMonth, tmpYear){
	var szOut;
	var dPrevDate = new Date(tmpYear,tmpMonth,1);
	var MinMilli = 1000 * 60;
	var HrMilli = MinMilli * 60;
	var DyMilli = HrMilli * 23;
	var lastDayMonth = dPrevDate.getTime();
	lastDayMonth = lastDayMonth - DyMilli;
	dPrevDate.setTime(lastDayMonth);
	szOut = dPrevDate.getDate();
	return szOut;
}

function whichIsChecked (radio) {
	for (var i=0;i<radio.length;i++) {
		if (radio[i].checked) {
			return radio[i].value;
		}
	}
	return "";
 }

function ConfirmToGo(szConfirm,szPage) {
	if(confirm(szConfirm))
		window.location=szPage;
	else	
		return;
}

function o(url,name,options) {
	var wnd = window.open(url, name, options);
}

function setQs(sf,sff){
	var s, a, a1, q;
	s = decodeURI(location.search);
	a = s.split("?");
	if (a != null){
		if (a.length > 1){
			a1 = a[1];
			a = a1.split("&");
			if (a != null){
				for (i=0;i<a.length;i++){
					if (a[i].substr(0,2)==(sff+"=")){
						if (typeof(eval("document."+sf)) != "undefined" )
							eval("document."+sf+"."+sff).value=a[i].substr(2).replace(/\+/ig," ");
					}
				}
			}
		}
	}
	return;
}

function serchForm(objform){
	var bRtn=true;
	var ERR='';
	if(objform.q){
		if(objform.q.value==''){
			ERR += 'Specificare una stringa di ricerca!!';
			bRtn=false;
		}else if(String(objform.q.value).length < 2) {
			ERR += 'La stringa di ricerca deve contenere piu\' di un carattere!!';
			bRtn=false;
		}
	}
	if(ERR!='') alert(ERR);
	return bRtn;
}
/*  Accessible Pop-up Links
 *	http://www.alistapart.com/articles/popuplinks/
 */
var _POPUP_FEATURES = 'resizable=yes,location=0,statusbar=0,menubar=0,width=800,height=500';

function raw_popup(url, target, features) {
    // pops up a window containing url optionally named target, optionally having features
    if (isUndefined(features)) features = _POPUP_FEATURES;
    if (isUndefined(target  )) target   = '_blank';
    var theWindow = window.open(url, target, features);
    theWindow.focus();
    return theWindow;
}

function link_popup(src, features) {
    // to be used in an html event handler as in: <a href="..." onclick="link_popup(this,...)" ...
    // pops up a window grabbing the url from the event source's href
    return raw_popup(src.getAttribute('href'), src.getAttribute('target') || '_blank', features);
}

function isUndefined(v) {
    var undef;
    return v===undef;
}

// ------------------ COOKIES ------------------ //
// cookie expire date
var expdate = new Date ();

expdate.setTime(expdate.getTime() + (24 * 60 * 60 * 1000 * 31 * 9));

/* returns the given cookie value */
function getCookieVal (offset) {
	var endstr = document.cookie.indexOf (";", offset); //; signifies end of value
	if (endstr == -1)
	endstr = document.cookie.length; //if -1 then take it to the end
	return unescape(document.cookie.substring(offset, endstr)); //convert to readable text
}
/* Get a cookie */
function GetCookie (name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
	var j = i + alen;
	if (document.cookie.substring(i, j) == arg)
		return getCookieVal(j);
	i = document.cookie.indexOf(" ", i) + 1;
	if (i == 0) break; }
	return null;
}
function RemoveCookie (name) {
	document.cookie = name+"=; path=/";
}

/* Set a cookie */
function SetCookie (name, value) {
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	document.cookie = name + "=" +
	((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
	((path == null) ? "" : ("; path=" + path)) +
	((domain == null) ? "" : ("; domain=" + domain)) +
	((secure == true) ? "; secure" : "");
}
