// JavaScript Document
var viewWait = function(idcontainer){
	if(idcontainer){
			$("#" + idcontainer).html('<div class="Loader" align="center"><img width="32" height="31" src="struttura/swirl_anim.gif"></div>');
	}
	else
		alert("E' necessario specificare il 'container' per l'icona 'WAIT'");
}

var UnixToJSDateTime = function(unix_timestamp){
	return new Date(unix_timestamp*1000);
}

var size_format = function (filesize) {
	if (filesize >= 1073741824) {
		filesize = Math.round(sfilesize / 1073741824) + ' Gb';
	} else {
		if (filesize >= 1048576) {
			filesize = Math.round(filesize / 1048576) + ' Mb';
		} else {
			if (filesize >= 1024) {
				filesize = Math.round(filesize / 1024) + ' Kb';
			} else {
			filesize = (filesize) + ' bytes';
			};
		};
	};
	return filesize;
};

var ConvertiCaratteri = function(stringa){
	if(stringa != undefined && stringa != ''){
		stringa = stringa.replace(/\+/g, '_MIO#043;');
		stringa = stringa.replace(/\&/g, '_MIOamp;');
	}
		else return "";
	return stringa;
}

var RiprtistinaCaratteri = function(stringa){
	if(stringa != undefined && stringa != ''){
		stringa = stringa.replace(/_MIO#039;/g, "'");
		stringa = stringa.replace(/_MIO#043;/g, '+');
		stringa = stringa.replace(/_MIOamp;/g, '&');
	}
		else return "";
	return stringa;
}

var randOrd = function(){
	return (Math.round(Math.random())-0.5);
}

var strSettimana = new Array("domenica", "lunedi", "martedi", "mercoledi", "giovedi", "venerdi", "sabato");
var strMesi = new Array("gennaio", "febbraio", "marzo", "aprile", "maggio", "giugno", "luglio", "agosto", "settembre", "ottobre", "novembre", "dicembre");


//create a cookie, with expiration date in days
function setCookie(c_name,value,expiredays){
	var exdate=new Date();
	exdate.setDate(expiredays);
	document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate);
}

//return cookie value
function getCookie(c_name){
	if (document.cookie.length>0){
		c_start=document.cookie.indexOf(c_name + "=")
		if (c_start!=-1){
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
				return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return null;
}

//delete a cookie
function deleteCookie(name){
	document.cookie = name + "=" + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

//check if cookies are enable - if True, I remove testing cookie
function checkCookie(){
	setCookie("testCookie","merlinox",365);
	tmp = getCookie("testCookie");
	if (tmp != null) deleteCookie("testCookie");
		return (tmp != null);
}

function checkEmail(valore, checkVuoto){
	if(checkVuoto){
		if (obj.value==''){
			alert("Il campo email non puo' essere vuoto.");
			return false;
		}
	}
	
	if (valore.search(/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/) != -1)
		return true;
		
	alert("L'email inserita non e' valida.");
	return false;
}

function isDate(dateStr) {
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
	var matchArray = dateStr.match(datePat); // is the format ok?
	
	if (matchArray == null) {
		alert("Inserisci la data nel formato gg/mm/aaaa");
		return false;
	}
	
	day = matchArray[1]; // p@rse date into variables
	month = matchArray[3];
	year = matchArray[5];
	
	if (month < 1 || month > 12) { // check month range
		alert("Il mese deve essere compreso fra 1 e 12");
		return false;
	}
	
	if (day < 1 || day > 31) {
		alert("Il giorno deve essere compreso fra 1 e 31");
		return false;
	}
	
	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		alert(month+" non ha 31 giorni!");
		return false;
	}
	
	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap)) {
			alert("febbraio " + year + " non ha " + day + " giorni!");
			return false;
		}
	}
	return true; // date is valid
}

//alert(checkCookie());
//tmp = getCookie(’testCookie’);
//alert(tmp);
