var DHTML = (document.getElementById || document.all || document.layers);
var email_reg_exp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
var cap_reg_exp = /^([0-9]{5,})+$/;
var duecifre_reg_exp = /^([0-9]{2,})+$/;
//var data_reg_exp = /^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d+$/;
var mod = 0;
var num = 0;

function rem_disp(num) {
    if(confirm('Sei sicuro di voler eliminare questa disponibiltà?')) {
		$("#rem"+num).submit();
    }
}

function pageScroll() {
	window.scrollBy(0,800); // horizontal and vertical scroll increments
	//scrolldelay = setTimeout('pageScroll()',100); // scrolls every 100 milliseconds
}

function selectDate(myNum){
	num = myNum;
}
function getselectDate(){
	return num;
}

var dom = (document.getElementById) ? true : false; 
var ns5 = ((navigator.userAgent.indexOf("Gecko")>-1) && dom) ? true: false; 
var ie5 = ((navigator.userAgent.indexOf("MSIE")>-1) && dom) ? true : false; 
var ns4 = (document.layers && !dom) ? true : false; 
var ie4 = (document.all && !dom) ? true : false; 

function popUp(path,title,confirmation,question) {
    var popwin;
	var input=true;
	if (confirmation) input=confirm(question);
	if (input) {
	    if (title == "first"){
	        // first window
	        width = screen.width;
	        height = screen.height;
	        popwin=window.open(path, title, "width="+width+",height="+height+",top=0,left=0,toolbar=0,scrollbars=0,directories=no,location=0,statusbar=0,menubar=0,resizable=1");
	    } else if (title!="view") {
	        // small window
	        leftval=(screen.width)?(screen.width-400)/2:100;
	        topval=(screen.height)?(screen.height-500)/2:100;
	        popwin=window.open(path, title, "width=400,height=500,top=" + topval + ",left=" + leftval + ",toolbar=0,scrollbars=0,directories=no,location=0,statusbar=0,menubar=0,resizable=0");
	    } else {
	        // big window
	        leftval=(screen.width)?(screen.width-955)/2:100;
	        topval=(screen.height)?(screen.height-600)/2:100;
	        popwin=window.open(path, title, "width=955,height=600,top=" + topval + ",left=" + leftval + ",toolbar=0,scrollbars=1,directories=no,location=0,statusbar=0,menubar=0,resizable=0");
	    }
	    popwin.focus();
	}
}

function myprint(){
		 if (!window.print){
		 	alert("Browser non supportato per la stampa!")
		 	return;
		 }
		 window.print();
}

/****************************************CALENDARIO********************************************************/
var monthList = ['Gennaio','Febbraio','Marzo','Aprile','Maggio','Giugno','Luglio','Agosto','Settembre','Ottobre','Novembre','Dicembre'];
var curDate = new Date();
curDate.setDate(1);
curDate.getTheYear = curDate.getFullYear ? curDate.getFullYear : curDate.getYear;
//the form buttons don't work in NS4 because of bugs
var MWJNS4 = document.layers && !document.all && navigator.mimeTypes['*'];
//Opera 7, Opera 6 Mac/Linux, Konqueror and Safari's security means that you cannot write to about:blank as it is counted as a different domain
//Escape does not recognise about:blank (it uses its own, but it occasionally makes the popup go blank) but it does understand ''
var opOrEscape = window.opera || ( document.layers && !navigator.mimeTypes['*'] ) || navigator.vendor == 'KDE' || ( document.childNodes && !document.all && !navigator.taintEnabled ) || ( window.ScriptEngine && ScriptEngine() == 'JScript' && navigator.platform == 'Win32' && window.ActiveXObject && !navigator.__ice_version );

function openCalendar(e) {
	var sX = 0, sY = 0; if( !e ) { e = window.event; }
	if( e && ( e.screenX || e.screenY ) && screen.availWidth ) { sX = e.screenX; sY = e.screenY; }
	if( sX > screen.availWidth - 271 ) { sX = screen.availWidth - 271; }
	if( sY > screen.availHeight - 308 ) { sY = screen.availHeight - 308; }
	doReDraw(sX,sY);
}

function MWJ_dropYear() {
	curDate.setYear( curDate.getTheYear() - 1 ); doReDraw();
}
function MWJ_raiseYear() {
	curDate.setYear( curDate.getTheYear() + 1 ); doReDraw();
}
function MWJ_dropMonth() {
	if( curDate.getMonth() ) {
		curDate.setMonth( curDate.getMonth() - 1 );
	}
	else {
		curDate.setMonth(11); curDate.setYear( curDate.getTheYear() - 1 );
	}
	doReDraw();
}
function MWJ_raiseMonth() {
	if( curDate.getMonth() < 11 ){
		curDate.setMonth( curDate.getMonth() + 1 );
	}
	else {
		curDate.setMonth(0); curDate.setYear( curDate.getTheYear() + 1 );
	}
	doReDraw();
}
function resetDate() {
	curDate.setTime((new Date()).getTime());
}

function doReDraw(sX,sY) {
	var theContent = 
		'<html><head><title>Calendario</title><style type="text/css"><!--\n'+
		'th { background-color: #c33939; }\n'+
		'a { text-decoration: none; }\n'+
		'td, th { font-family: \'Times New Roman\', Times, Serif; }\n'+
		'a.MWJCAL { display: block; width: 100%; font-weight: bold; }\n'+
		'a.MWJCAL:hover { background-color: #99ff99; }\n--></style></head>'+
		'<body bgcolor="#dbdbff" text="#ffffff" link="#00007f" alink="#00007f" vlink="#00007f">'+
		'<form onsubmit="return false;"><table border="1" cellpadding="2" cellspacing="1" width="233">'+
		'<tr><td bgcolor="#ffffff" colspan="7"><table border="0" cellpadding="2" cellspacing="1" width="100%">'+
		'<tr><td bgcolor="#323c76">'+(MWJNS4?'<a href="javascript:if( window.opener && window.opener.MWJ_dropYear ) { window.opener.MWJ_dropYear(); } else { window.alert( \'Error:\\nCould not communicate with opening window.\' ); }"><font color="#ffffff">&lt;&lt;</font></a>':'<input type="button" value="<<" onclick="if( window.opener && window.opener.MWJ_dropYear ) { window.opener.MWJ_dropYear(); } else { window.alert( \'Error:\\nCould not communicate with opening window.\' ); }">')+'</td><td bgcolor="#323c76" align="center"><b>'+curDate.getTheYear()+'</b></td><td bgcolor="#323c76" align="right">'+(MWJNS4?'<a href="javascript:if( window.opener && window.opener.MWJ_raiseYear ) { window.opener.MWJ_raiseYear(); } else { window.alert( \'Error:\\nCould not communicate with opening window.\' ); }"><font color="#ffffff">&gt;&gt;</font></a>':'<input type="button" value=">>" onclick="if( window.opener && window.opener.MWJ_raiseYear ) { window.opener.MWJ_raiseYear(); } else { window.alert( \'Error:\\nCould not communicate with opening window.\' ); }">')+'</td></tr>'+
		'<tr><td bgcolor="#323c76">'+(MWJNS4?'<a href="javascript:if( window.opener && window.opener.MWJ_dropMonth ) { window.opener.MWJ_dropMonth(); } else { window.alert( \'Error:\\nCould not communicate with opening window.\' ); }"><font color="#ffffff">&lt;&lt;</font></a>':'<input type="button" value="<<" onclick="if( window.opener && window.opener.MWJ_dropMonth ) { window.opener.MWJ_dropMonth(); } else { window.alert( \'Error:\\nCould not communicate with opening window.\' ); }">')+'</td><td bgcolor="#323c76" align="center"><b>'+monthList[curDate.getMonth()]+'</b> <font size="-2">[<a href="javascript:if( window.opener && window.opener.resetDate ) { window.opener.resetDate(); window.opener.doReDraw(); } else { window.alert( \'Error:\\nCould not communicate with opening window.\' ); }"><font color="#bbbbff">Go to now</font></a>]</font></td><td bgcolor="#323c76" align="right">'+(MWJNS4?'<a href="javascript:if( window.opener && window.opener.MWJ_raiseMonth ) { window.opener.MWJ_raiseMonth(); } else { window.alert( \'Error:\\nCould not communicate with opening window.\' ); }"><font color="#ffffff">&gt;&gt;</font></a>':'<input type="button" value=">>" onclick="if( window.opener && window.opener.MWJ_raiseMonth ) { window.opener.MWJ_raiseMonth(); } else { window.alert( \'Error:\\nCould not communicate with opening window.\' ); }">')+'</td></tr>'+
		'</table></td></tr>'+
		'<tr><th>Lun</th><th>Mar</th><th>Mer</th><th>Gio</th><th>Ven</th><th>Sab</th><th>Dom</th></tr>'
	;
	for( var x = 1; x <= [31,((!( curDate.getTheYear() % 4 ) && ( ( curDate.getTheYear() % 100 ) || !( curDate.getTheYear() % 400 ) ))?29:28),31,30,31,30,31,31,30,31,30,31][curDate.getMonth()]; x++ ) {
		curDate.setDate(x);
		if( x == 1 && curDate.getDay() ) { theContent += '<tr><td colspan="'+curDate.getDay()+'"></td>'; }
		theContent += ( ( !curDate.getDay() ) ? '<tr>' : '' ) + '<td align="center" bgcolor="#'+((curDate.getMonth()==(new Date()).getMonth()&&curDate.getYear()==(new Date()).getYear()&&x==(new Date()).getDate())?'abc4f1':((!curDate.getDay()||curDate.getDay()==6)?'ffffbb':'ffffff'))+'"><a class="MWJCAL" href="javascript:if( window.opener && window.opener.datePickerReturn ) { void(window.opener.setTimeout(\'datePickerReturn('+x+','+(curDate.getMonth()+1)+','+curDate.getTheYear()+');\',1)); window.close(); } else { window.alert( \'Error:\\nCould not communicate with opening window.\' ); }">'+x+'</a></td>';
	} curDate.setDate(1);
	theContent += '</table></form></body></html>';
	var oDoc = window.open( ( opOrEscape ? '' : 'about:blank' ), 'MWJ_TOFR', 'width=259,height=277'+(sY?(',top='+sY):'')+(sX?(',left='+sX):'')+',resizable=1,location=0,menubar=0,scrollbars=0,status=0,toolbar=0' );
	oDoc.document.open(); oDoc.document.write(theContent); oDoc.document.close(); window.MWJwinStore = oDoc;
	//these must be done after open-write-close or the window cannot be written to in IE4 Mac
	if( oDoc.focus ) { oDoc.focus(); } if( ( sX || sY ) && oDoc.moveTo ) { oDoc.moveTo(sX,sY); }
}

if( !window.onunload ) { window.onunload = function () { if( window.MWJwinStore && !window.MWJwinStore.closed ) { window.MWJwinStore.close(); } }; }


function datePickerReturn( oDay, oMonth, oYear , num) {
	//this function will be called when the user finishes picking a date
	//you will want to do something with the day, month and year
	if(oDay < 10) oDay = '0' + oDay;
	if(oMonth < 10) oMonth = '0' + oMonth;
	
	switch(getselectDate()){
		case 1:
			$('#giorno').val(oDay);
			$('#mese').val(oMonth);
			$('#anno').val(oYear);
			break;
		case 2:
			$('#giorno2').val(oDay);
			$('#mese2').val(oMonth);
			$('#anno2').val(oYear);
			break;
	}
	
}


function checkUserData(){
	if($('input[name=cognome]').val()==''){
		$('input[name=cognome]').css("background-color","red");
		alert("Inserire il cognome  per proseguire");
		return false;
	}
	if($('input[name=nome]').val()==''){
		alert("Inserire il nome  per proseguire");
		return false;
	}
	if($('input[name=indirizzo]').val()==''){
		alert("Inserire l'indirizzo per proseguire");
		return false;
	}
	if($('input[name=qualifica]').val()==''){
		alert("Inserire la qualifica  per proseguire");
		return false;
	}
	if($('input[name=codicefiscale]').val()==''){
		alert("Inserire il codicefiscale  per proseguire");
		return false;
	}
	if($('input[name=email]').val()==''){
		alert("Inserire l'email per proseguire");
		return false;
	}
	if($('input[name=cap]').val()=='' && $('input[name=citta]').val()==''){
		alert("Inserire la città per proseguire");
		return false;
	}
	if($('input[name=cellulare]').val()=='' && $('input[name=telefono]').val()==''){
		alert("Inserire un recapito telefonico per proseguire");
		return false;
	}
	if(!$('input[name=privacy]').attr("checked")){
		alert("Selezionare l'assenso al trattamento dei dati personali  per proseguire");
		return false;
	}
	else return true;
}
