/*  Copyright TechMA s.r.l. - Alessandro Riggi, 2008  |  www.techma.it
 * -----------------------------------------------------------
 */
 
//alert("online");
var myRequest = null;
var idCampoShare='ID0';
var lastObjectOpen = null;

function CreateXmlHttpReq(handler) {
  var xmlhttp = null;
  xmlhttp = new XMLHttpRequest();
  xmlhttp.onreadystatechange = handler;
  return xmlhttp;
}

function CreateXmlHttpReq2(handler) {
  var xmlhttp = null;
  try {
    xmlhttp = new XMLHttpRequest();
  } catch(e) {
    try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  xmlhttp.onreadystatechange = handler;
  return xmlhttp;
}

function myHandler() {
	//alert(idCampoShare);
	document.getElementById('calendario'+idCampoShare).innerHTML="<div style=text-align:center;padding:10px;background-color:white;width:100%;height:100%;><img src=../images/attesa.gif border=0 class=IMG_CALENDARIO_preload></div>";
    if (myRequest.readyState == 4 && myRequest.status == 200) {
        document.getElementById('calendario'+idCampoShare).innerHTML=myRequest.responseText;
    }
}

function apriCalendarioAjax(id,y,m,d,ds,hh,mm,ss) {
	if((lastObjectOpen)&&lastObjectOpen!=document.getElementById('calendario'+id)) lastObjectOpen.style.display='none';
	idCampoShare=id;
	//alert(id+'-'+y+'-'+m+'-'+d+'-'+ds+'-'+hh+'-'+mm+'-'+ss);
    myRequest = CreateXmlHttpReq2(myHandler);
    //alert("calendario.asp?9668=012&id="+idCampo+"&y="+y+"&m="+m+"&d="+d);
    myRequest.open("GET","/asp/datario.asp?9668=012&id="+id+"&y="+y+"&m="+m+"&d="+d+"&hh="+hh+"&mm="+mm+"&ss="+ss+"&ds="+ds+"&dc="+document.getElementById(''+id).value);
    myRequest.send(null);
    lastObjectOpen=document.getElementById('calendario'+id);
}

function aggiornaCalendario(id,ds){
	//alert(id+'-'+ds);
	idCampoShare=id;
	var hh='';
	var mm='';
	var ss='';
	if(document.getElementById('hh'+id)) hh=document.getElementById('hh'+id).options[document.getElementById('hh'+id).selectedIndex].value;
	if(document.getElementById('mm'+id)) mm=document.getElementById('mm'+id).options[document.getElementById('mm'+id).selectedIndex].value;
	if(document.getElementById('ss'+id)) ss=document.getElementById('ss'+id).value;
	apriCalendarioAjax(id,document.getElementById('yearnum'+id).options[document.getElementById('yearnum'+id).selectedIndex].value,document.getElementById('mesenum'+id).options[document.getElementById('mesenum'+id).selectedIndex].value,'',ds,hh,mm,ss)
}

function startCalendario(id,ds){
	hh = '';
	mm = '';
	ss = '';

	//try{
	var DateTimeString = document.getElementById(''+id).value.replace(/\./g,':');
	var matchDateTime;
	var tmpDate;
	var datajs=new Date();
	var validFormat = false;
	var	nuovaDataValida = datajs.getDate()+'/'+((datajs.getMonth())+1)+'/'+datajs.getFullYear();
	if (ds!=''&&ds!='20'){
		nuovaDataValida += ' '+datajs.getHours()+':'+datajs.getMinutes()+':'+datajs.getSeconds();
	}
	if(DateTimeString==null|DateTimeString==''){DateTimeString = nuovaDataValida;}
	var DateTimeString=DateTimeString.toString(19);
	if(DateTimeString.length>0){
		arrDataOra=DateTimeString.split(' ');
	
		if(arrDataOra.length==1){
			matchDateTime = DateTimeString.match(/^([0-3]?\d)[\/\.]([01]?\d)[\/\.](\d{4})$/);
			if (matchDateTime != null) {
				var dataValida = matchDateTime[1]+'/'+matchDateTime[2]+'/'+matchDateTime[3].substr(0,4)
				tmpDate = new Date(matchDateTime[3], matchDateTime[2] - 1, matchDateTime[1]);
				validFormat = ((tmpDate.getMonth()==matchDateTime[2]-1) && (tmpDate.getDate()==matchDateTime[1]));
			}
		}else{
			matchDateTime = DateTimeString.match(/^([0-3]?\d)[\/\.]([01]?\d)[\/\.](\d{4})[\ ](\d?\d)[:\.](\d\d)[:\.](\d\d)$/);
			if (matchDateTime != null) {
				var dataValida = matchDateTime[1]+'/'+matchDateTime[2]+'/'+matchDateTime[3].substr(0,4)+' '+matchDateTime[4]+':'+matchDateTime[5]+':'+matchDateTime[6]
				tmpDate = new Date(matchDateTime[3], matchDateTime[2] - 1, matchDateTime[1], matchDateTime[4], matchDateTime[5], matchDateTime[6]);
				validFormat = ((tmpDate.getMonth()==matchDateTime[2]-1) && (tmpDate.getDate()==matchDateTime[1] && tmpDate.getHours()==matchDateTime[4]) && (tmpDate.getMinutes()==matchDateTime[5]));

			}
		}
	}

	//alert(id+'-'+y+'-'+m+'-'+d+'-'+ds);
	//if(validFormat==true){return dataValida;}else{return nuovaDataValida};
	
	if(validFormat==true){strDataOra=dataValida;}else{strDataOra=nuovaDataValida;document.getElementById(''+id).value=nuovaDataValida;};
	arrDataOra=strDataOra.split(' ')
	strData=arrDataOra[0];
	if(arrDataOra.length>1){
		strOra=arrDataOra[1];
		strOra=strOra.replace(/\./g,':');
		hh = strOra.split(':')[0];
		mm = strOra.split(':')[1];
		ss = strOra.split(':')[2];
	}
	y = strData.split('/')[2];
	m = strData.split('/')[1];
	d = strData.split('/')[0];
	//	alert(id+'-'+y+'-'+m+'-'+d+'-'+ds);
	apriCalendarioAjax(id,y,m,d,ds,hh,mm,ss);
	//}catch(err){
	//	alert('data non valida!');
	//}
}



var http = creaXMLHTTP();
var objDiv;
var popAperto=0;
	
function creaXMLHTTP() {
	var ro;
	if(navigator.appName == "Microsoft Internet Explorer"){
		ro = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		ro = new XMLHttpRequest();
	}
	return ro;
}

function apriSceltaPopUp() {
	if(http.readyState == 4){
		var response = http.responseText;
		document.getElementById('pop'+objDiv).innerHTML=response; 
		//document.getElementById('pop'+objDiv).innerText=response; 
		//alert(response);
	}
}

function sceltaValorePopUp(ctrl,id,tab,fltr,cmpvis,opz,typ,modo,valfltr,onoff,sel,pag,size){
	if(pag==null) pag='1';
	if(size==null|size=='') size='15';
	//sceltaValorePopUp('controllo','§N','§S','but§V','§H','§Z','§L','1/2/3/n',(testo filtro),0/1,(valori selezionati),pagina,size)
        //prompt('test','');
		objDiv = id;
        if(document.getElementById('pop'+objDiv).style.display=='none'|onoff==1){
	        popAperto=1;
	        document.getElementById('pop'+objDiv).innerHTML='<img src=/images/loader_big.gif border=0 style=margin:20px>'
	        document.getElementById('pop'+objDiv).style.background='#FFF';
	        document.getElementById('pop'+objDiv).style.display='';
	        http.open('get', '/asp/popscelta.asp?9668=' + ctrl+ '&id=' + id + '&tabpop=' + tab + '&fltr=' + fltr+ '&cmpvis=' + cmpvis+ '&opz=' + opz+ '&typ=' + typ +'&modo=' + modo + '&val='+valfltr+'&sel='+sel+'&pag='+pag+'&size='+size);
			http.setRequestHeader("Content-Type", "text/plain; charset=windows-1252");
			http.onreadystatechange = apriSceltaPopUp;
			http.send(null);
		}else{
			popAperto=0;
			document.getElementById('pop'+objDiv).style.display='none';
		}
}

function printVersion(id,revOf,rev){
		objDiv=id;
        document.getElementById('pop'+objDiv).innerHTML='<img src=/images/loader_big.gif border=0 style=margin:20px>'
        http.open('get', '/anagrafiche0000/lista0000003.asp?id=' + id + '&revOf=' + revOf+'&rev='+rev);
		http.setRequestHeader("Content-Type", "text/plain; charset=windows-1252");
		http.onreadystatechange = apriSceltaPopUp;
		http.send(null);
}

