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 ch1 = s.indexOf('@');
	var ch2 = s.lastIndexOf('.');
	var ch3 = s.lastIndexOf('@');
	if ( (ch1 < 1)||(ch2 == -1)||(ch2<(ch1+2))||(ch2>(s.length-3))||(ch1!=ch3) )
		return false;
	else
		return true;	
/*
	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;
}

function serchForm_en(objform){
	var bRtn=true;
	var ERR='';
	if(objform.q){
		if(objform.q.value==''){
			ERR += 'Search string is undefined!!';
			bRtn=false;
		}else if(String(objform.q.value).length < 2) {
			ERR += 'Search string must contain more than one character!!';
			bRtn=false;
		}
	}
	if(ERR!='') alert(ERR);
	return bRtn;
}

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; 
}
/* 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 + "=" + escape (value) +
	((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
	((path == null) ? "" : ("; path=" + path)) +
	((domain == null) ? "" : ("; domain=" + domain)) +
	((secure == true) ? "; secure" : "");
}

function tswarn(virtual) {
	var ts = GetCookie("ts");
	var warndiv;
	if (ts != null && ts != '') {
		/*
		objSlider = new SlidingContent('objSlider','tsdiv',0,0,"100%","100%");
		objSlider.updateLayer();
		*/
		objMyImage = new OpacityObject('tsdiv', virtual + 'static/css/img/back');
		objMyImage.setBackground();
		document.getElementById("tsdiv").style.display ='block';
	} else {
		document.getElementById("tsdiv").style.display ='none';
	}
}

function validateRegister(objForm){
	var szERR = "";
	var idLang = objForm.Lang.value;

	if(!objForm.rbNewsletter(0).checked)
		szERR += (idLang=="en")? "\nYour acceptance of the Privacy Policy is required\n to complete the registration." : "\nSi deve acconsentire al trattamento dei dati\nper completare la registrazione.";
	else {
		if(isEmpty(objForm.tbNome.value))
			szERR += (idLang=="en")? "\n-- required parameter \"First name\" missing" : "\n-- il campo nome e' obbligatorio";
		if(objForm.tbCognome.value=="")
			szERR += (idLang=="en")? "\n-- required parameter \"Last name\" missing" : "\n-- il campo \"cognome\" e' obbligatorio";
		if(objForm.tbEmail.value=="")
			szERR += (idLang=="en")? "\n-- required parameter \"E-mail\" missing" : "\n-- il campo \"E-mail\" e' obbligatorio";
		else if(! isEmail(objForm.tbEmail.value))
			szERR += (idLang=="en")? "\n-- parameter \"E-mail\" is not valid" : "\n-- il campo \"E-mail\" non e' corretto";
		if(objForm.tbTelefono.value=="")
			szERR += (idLang=="en")? "\n-- required parameter \"Phone Number\" missing" : "\n-- il campo \"recapito telefonico\" e' obbligatorio";
		if(objForm.lbProfessione.selectedIndex==0)
			szERR += (idLang=="en")? "\n-- required parameter \"Job function\" missing" : "\n-- il campo \"professione\" e' obbligatorio";			
	}
	
	if (szERR!=""){
		szERR = ((idLang=="en")? "Warning:" : "Attenzione:") + szERR;
		alert(szERR);
		return false;
	}else
		return true;
}

function GetDocument(nMode,bLogin,idDoc,webRoot,idLang) {
	var W = 330;
	var H = 220;
	
	var szUrl = webRoot + "/"
	var leftPos = (screen.availWidth/2) - (W/2);
	var topPos = (screen.availHeight/2) - (H/2);
	var objWin;
	
	if(!bLogin)
		szUrl += ((idLang!='en')? idLang+"/" : "") + "Login/default.content?to=" + escape(szUrl + "cntGetDoc.aspx?idDoc=" + idDoc + "&idLang=" + idLang);
	else
		szUrl += "cntGetDoc.aspx?idDoc=" + idDoc + "&idLang=" + idLang;
	
	if(nMode>0 && bLogin){
		//window.location = szUrl
		objWin = window.open(szUrl, 'getDoc','top='+topPos+',left='+leftPos+',resizable=no,location=0,statusbar=0,menubar=0,width='+W+',height='+H);
		objWin.focus();		
	}else{
		objWin = window.open(szUrl, 'getDoc','top='+topPos+',left='+leftPos+',resizable=no,location=0,statusbar=0,menubar=0,width='+W+',height='+H);
		objWin.focus();
	}
	
	
}

function downloadDoc(idDoc,idLang){
	//setTimeout("window.close()",1000);
	setTimeout("window.location = 'cntGetDoc_Stream.aspx?idDoc=" + idDoc + "&idLang=" + idLang + "'",700);	
}

function redimwin(l,h){
	if (window.opener) {
		if(window.name=="")
			window.resizeTo(l,h);
	}	
	var midW = (screen.availWidth/2) - (l/2);
	var midH = (screen.availHeight/2) - (h/2);
	window.moveTo(midW,midH);
	window.focus();
}









