var rulesFe = new Array();
rulesFe[0]="startRate|keypress|0123456789.";
rulesFe[1]="intRate|keypress|0123456789.";
rulesFe[2]="adjust_amt|keypress|0123456789.";
rulesFe[3]="fixed_rate|keypress|0123456789.";
rulesFe[4]="adj_start_rate|keypress|0123456789.";
rulesFe[5]="adj_rate_cap|keypress|0123456789.";
rulesFe[6]="io_start_rate|keypress|0123456789.";
rulesFe[7]="io_rate_cap|keypress|0123456789.";


/*INIZIO METODI PER I COLLABORATORI*/

function saveCollaboratore(frmName) {

	var rules = new Array();
	rules[0] = "nome|required|- Inserire il nome";
	rules[1] = "cognome|required|- Inserire il cognome";
	rules[2] = "ragSoc|required|- Inserire la ragione sociale";
	rules[3] = "indirizzo|required|- Inserire un indirizzo";
	rules[4] = "citta|required|- Inserire la citta";
	rules[5] = "cap|required|- Inserire il CAP";
	rules[6] = "provincia|required|- Inserire la provincia";
	rules[7] = "telefono|required|- Inserire il telefono";
	rules[8] = "fax|required|- Inserire il fax";
	rules[9] = "email|required|- Inserire un indirizzo e-mail";
	rules[10] = "email|equal|$emailConf|- L'email e la conferma non coincidono.";
	rules[11] = "userName|required|- Specificare uno username";
	rules[12] = "userName|minlength|8|- Lo username deve essere lungo almeno 8 caratteri";
	rules[13] = "userName|maxlength|15|- Lo username deve essere lungo massimo 15 caratteri";
	rules[14] = "privacy|equal|si|- Per poter procedere alla registrazione e' necessario dare il consenso al trattamento dei dati personali."
	
	if (!performCheck(frmName, rules, 'classic')){			
		return false;
	}
	return true;

}

function checkLogin(frmName) {
	var rules = new Array();
	rules[0] = "userName|required|- Specificare lo username";
	rules[2] = "userPwd|required|- Specificare la password";	
	
	if (!performCheck(frmName, rules, 'classic')){			
		return false;
	}
	return true;
}
/*FINE METODI PER COLLABORATORI*/

/*METODO PER LA GESTIONE DEGLI IMMOBILI*/
function loadElencoImmobile(opt, id, frmName, divZona, zonaNodo) {
	
	var procedi = true;

	if (divZona != "" && $(divZona+id) != undefined) {
		if ($(divZona+id).style.display == "none") {
			$(divZona+id).style.display = 'inline';
		} else if ($(divZona+id).style.display == "inline") {
			$(divZona+id).style.display = 'none';
		}
		procedi = false;
	}

	if (procedi) {
		if (opt == 3 || opt == 5) {
			if (opt == 3) {
				$('nameLabel').value = $F('zonaA'+id);
			} else {
				$('nameLabel').value = $F('zonaB'+id);
			}
			$(frmName).action += "?opt=" + opt + "&fkZona=" + id;
			if (zonaNodo != "") {
				$(frmName).action += "&explode=true&zonaNodo="+zonaNodo;
			}
		} else if (opt == 4) {
			//sto gestendo immobili per tipologia
			$('nameLabel').value = $F('tipo'+id);
			$(frmName).action += "?opt=" + opt + "&fkTipologia=" + id;
			
		}
		$(frmName).submit();
	}
	
}

function searchImmobiliAv(frmName) {
	if ($F('fkZona') == "" && $F('fkTipologia') == "" && $F('mq') == ""
		&& $F('piano') == "" && $F('prezzo') == "") {
		alert("Attenzione! Per la ricerca avanzata e' necessario specificare elmeno un parametro.");
		return	
	} 
	$(frmName).submit();
}

function loadUtility(opt) {
	
	switch (opt) {
		case '0':
			if ($('utility').style.display == 'none') {
				$('utility').style.display = 'inline';
			} else if ($('utility').style.display == 'inline') {
				$('utility').style.display = 'none';
			}
			break;
		case '1':
		case '2':
		case '3':
		case '4':
		case '5':
			
			if ($('dettaglioUtility') != undefined) {
				//alert("ricarico div");
				//ricarico esattamente nel Div altrimenti devo caricare tutta la pagina(home page)
				var url = "/general/generalData.php";
				var params = "utility=div&pageId=" + opt;
				
				var ajax = new Ajax.Request(url, { parameters: params, onFailure: ShowError, onComplete: 
						function getResponse(request) {
							if (typeof(SymOnLoad) != 'undefined'){
								var tmp = request.responseText;
								resp = _nortonClean(tmp);		
							}else
						    	resp = request.responseText;
						
							obj = resp.evalJSON();
							obj.each( 
								function(elem) {
									d = $(elem.section);
									Element.update(d, elem.html);
								}
							)
						}
						}
					);
			} else {
				$('pageId').value = opt;
				$('frmUtility').submit();
			}
			break;
	}
}

/*FINE GESTIONE METODI IMMOBILI*/


/*METODI PER LA GESTIONE DEI CANTIERI*/

function loadDettCant(frmName, idCantiere) {
	$(frmName).action += "&idCantiere="+idCantiere;
	var link = $(frmName).action;
	window.open(link,"popupWin","width=870, height=700, toolbar=0"); 
}




/*
function loadMappa(id, idCantiere){ 
	var url = "/cantieri/manageCantieri.php";
    var eleid = id;
    var target = 'map';
    var params = "opt=6&idCantiere=" + idCantiere;
    var contenuto = "";
    var myAjax = new Ajax.Request(url, {method: 'post', 
                                asynchronous:true,
                                parameters: params, 
                                
                                onSuccess: function createModal(request) {
                                
                                	if (typeof(SymOnLoad) != 'undefined'){
					var tmp = request.responseText;
					resp = _nortonClean(tmp);		
				}else
			    	resp = request.responseText;
			obj = resp.evalJSON();
			obj.each( 
				function(elem) {
					contenuto =  elem.html;
				}
			)
			alert(contenuto);
			m = new Control.Modal(id,{
		        opacity: 0.8,
		        fade: true, 
		        contents: contenuto
		
		    });
		    m.open();
                                
                                }
                                });

}
*/

/*
function loadMappa(idCantiere) {
	var link = "/cantieri/manageCantieri.php?opt=6&idCantiere=" + idCantiere;
	window.open(link,"mappaWin","width=520, height=520, toolbar=0, scrollbars = no, resizable=no"); 
}
*/
function loadDettNews(idNews) {
	var link = '/general/generalData.php?opt=2&id=' + idNews;
	window.open(link, "popupWin", "width=620, height=600, scrollbars = yes"); 
}



function downloadListino(idCantiere) {

	//var url = "/collaboratori/callCreatePdf.php";
	var url = "/collaboratori/listinoPrezzi.php";
	var params = "opt=checkCollaboratore";
	var ajax = new Ajax.Request(url, { parameters: params, onFailure:ShowError, onComplete: 
	
			    function getResponse(request) {
			    	if (typeof(SymOnLoad) != 'undefined'){
						var tmp = request.responseText;
						resp = _nortonClean(tmp);		
					}else
				    	resp = request.responseText;
					
					obj = resp.evalJSON();
					obj.each( 
						function(elem) {
							if (elem.action == "checkCollaboratore") {
								if (elem.html == "OK") {
									//var link = "/collaboratori/listinoPrezzi.php?idCantiere="+idCantiere;
									var link = "/collaboratori/listinoPrezzi.php?idCantiere="+idCantiere;
									window.open(link,"_self","width=400, height=400, toolbar=0, scrollbars = yes, resizable=no");
								} else if (elem.html == "KO") {
									Element.hide('downloadListino');
									alert("Attenzione! La sessione e' SCADUTA.\r\nPer scaricare il listino prezzi e' "+
										  "necessario essere loggati. Pertanto si consiglia di chiudere la finestra corrente ed "+
										  "eseguire una nuova login dalla home page del sito.");
								}
							} 
						}
					)
			    }
			});
}

function managePdfImmobile(idImmobile) {
	var url = "/immobili/stampaSchedaImmobile.php";
	var params = "opt=checkCollaboratore";
	var ajax = new Ajax.Request(url, { parameters: params, onFailure: ShowError, onComplete: 
	
			    function getResponse(request) {
			    	if (typeof(SymOnLoad) != 'undefined'){
						var tmp = request.responseText;
						resp = _nortonClean(tmp);		
					}else
				    	resp = request.responseText;
					
					obj = resp.evalJSON();
					obj.each( 
						function(elem) {
							if (elem.action == "checkCollaboratore") {
								if (elem.html == "OK") {
									//var link = "/collaboratori/listinoPrezzi.php?idCantiere="+idCantiere;
									var link = "/immobili/stampaSchedaImmobile.php?idImmobile="+idImmobile;
									window.open(link,"_self","width=400, height=400, toolbar=0, scrollbars = yes, resizable=no");
								} else if (elem.html == "KO") {
									Element.hide('downloadListino');
									alert("Attenzione! La sessione e' SCADUTA.\r\nPer scaricare la planimetria dell'immobile e' "+
										  "necessario essere loggati. Pertanto si consiglia di chiudere la finestra corrente ed "+
										  "eseguire una nuova login dalla home page del sito.");
								}
							} 
						}
					)
			    }
			});
}

function loadDettImmobile(valSel) {

	if (valSel != "" ) {

		var url = "/cantieri/manageCantieri.php";
		var params = "opt=2&idImmobile=" + valSel;
		
		var ajax = new Ajax.Request(url, { parameters: params, onFailure: ShowError, onComplete: 
				function getResponse(request) {
					if (typeof(SymOnLoad) != 'undefined'){
						var tmp = request.responseText;
						resp = _nortonClean(tmp);		
					}else
				    	resp = request.responseText;
					
					obj = resp.evalJSON();
					
					obj.each( 
						function(elem) {
						
							d = $(elem.section);
							Element.update(d, elem.html);
							
						}
					)
				}
				}
			);
	}	
}
function loadDettagioAjax(valSel, opt) {

	if (valSel != "") {
		var url = "/cantieri/manageCantieri.php";
		var params = "opt=" + opt;
		if (opt == 2) {
			params +="&idImmobile=" + valSel;
		} else if (opt == 4) {
			params +="&idCantiere=" + valSel;
		}
		
		var ajax = new Ajax.Request(url, { parameters: params, onFailure: ShowError, onComplete: getResponse});
	}	  
}

function loadImmobileRicercato(idImmobile, fkCantiere) {
	var link = "/cantieri/manageCantieri.php?opt=5&idImmobile=" + idImmobile + "&fkCantiere=" + fkCantiere;
	window.open(link, "popupWin","width=870, height=700, toolbar=0"); 
}



function loadDettagioCapitAjax(valSel, fkCantiere) {

	if (valSel != "") {

		var url = "/cantieri/manageCantieri.php";
		var params = "opt=7&fkCantiere=" + fkCantiere + "&fkData=" + valSel;

		var ajax = new Ajax.Request(url, { parameters: params, onFailure: ShowError, onComplete: getResponse});
	}	
}

function getResponse(request) {
	if (typeof(SymOnLoad) != 'undefined'){
		var tmp = request.responseText;
		resp = _nortonClean(tmp);		
	}else
    	resp = request.responseText;
	
	obj = resp.evalJSON();
	
	obj.each( 
		function(elem) {
		
			d = $(elem.section);
			Element.update(d, elem.html);
			
			/*if (elem.action == "loadDettImmobile") {
				Element.show($('tdBottoni'));
			} else if (elem.action == "loadDettCantiere") {
				Element.hide($('tdBottoni'));
			} */
		}
	)
}

function loadFotoCapitolato(valSel, tipologia) {
	if (valSel != "") {
		var strImage = '<img src="/getImage.php?type=capitolato&nome=' + valSel + '" width="480" height="360" >';
		Element.update('fotoCapitolato', strImage);
		Element.update('tipologia', tipologia);
	}
}

function stampaSchedaImmobile(idImmobile) {
	//var link = "/immobili/createPdf.php?idImmobile="+idImmobile;
	var link = "/immobili/stampaSchedaImmobile.php?idImmobile="+idImmobile;
	window.open(link, "_self","width=400, height=400, toolbar=0, scrollbars = yes, resizable=no");
}

function ShowError() {
	alert("Attenzione ! Si � verificato un errore. Contattare l'amministratore di sistema");
}

function loadImageDiv(imageName) {

	var strToLoad = '<img src="/getImage.php?type=fotoCantiere&nome='+imageName+'" width="480" height="360" > ';

	Element.update('bigImage', strToLoad);

}

function loadImageDivCap(imageName) {

	var strToLoad = '<img src="/getImage.php?type=capitolato&nome='+imageName+'" width="480" height="360" > ';

	Element.update('bigImage', strToLoad);

}
function checkFlag(fieldCheck, filedApp, eleObj) {

	var read = true;
	
	if ($(fieldCheck).checked) {
		$(filedApp).value = "S";
		read = false;	
	} else {
		$(filedApp).value = "N";
	}
	
	for (var i = 0; i < eleObj.length; i++) {
		$(eleObj[i]).readOnly = read;
	}
}

function deselezionaCombo(idCombo, totali) {

	for (var i = 1; i <= totali; i++) {
		nomeCombo = eval("combo" + i);
		if (i != idCombo) {
			$(nomeCombo).selectedIndex = "-1"; 
		}
	}
}

function _nortonClean(tmp){
	// Elimino la porzione di codice in testa al codice html
	var idx_start_top = tmp.indexOf("<script");
	var idx_end_top = tmp.indexOf("</script>");
	
	if(idx_start_top > 0 && idx_end_top > 0) {
		tmp = tmp.substring(0,idx_start_top-1) + tmp.substring(idx_end_top+9); 
	}

	// Elimino la porzione di codice in fondo al codice html
	var idx_start_bottom = tmp.lastIndexOf("<script");
	var idx_end_bottom = tmp.lastIndexOf("</script>");

	if(idx_start_bottom > 0 && idx_end_bottom > 0)
		tmp = tmp.substring(0,idx_start_bottom-1) + tmp.substring(idx_end_bottom+9);

	return tmp.gsub(String.fromCharCode(10), '');
}

/*
Funzione che restituisce la data e l'ora corrente nel seguente formato di esempio:
Marted� 02 gennaio 2006 - ore 09:05
*/
function getFullDateTime(dtVar) {

	if (dtVar == undefined) {
    	var time_str    = new Date();
    } else {
   		var time_str = new Date(dtVar.substring(0, 4), dtVar.substring(5, 7)-1, dtVar.substring(8, 10),
   								dtVar.substring(11, 13), dtVar.substring(14, 16));
    }
     

	time_month      = time_str.getMonth();
	time_date       = time_str.getDate();
	time_day        = time_str.getDay();
	time_year       = time_str.getFullYear();   
	time_hours      = time_str.getHours();
	time_min        = time_str.getMinutes();
	
	if(time_day == 0) time_day = "Domenica"
    if(time_day == 1) time_day = "Lunedi'"
    if(time_day == 2) time_day = "Martedi'"
    if(time_day == 3) time_day = "Mercoledi'"
    if(time_day == 4) time_day = "Giovedi'"
    if(time_day == 5) time_day = "Venerdi'"
    if(time_day == 6) time_day = "Sabato"
	
	if(time_month == 0) time_month = "Gennaio"
    if(time_month == 1) time_month = "febbraio"
    if(time_month == 2) time_month = "Marzo"
    if(time_month == 3) time_month = "Aprile"
    if(time_month == 5) time_month = "Giugno"
    if(time_month == 6) time_month = "Luglio"
    if(time_month == 7) time_month = "Agosto"
    if(time_month == 8) time_month = "Settembre"
    if(time_month == 9) time_month = "Ottobre"
    if(time_month == 10) time_month = "Novembre"
    if(time_month == 11) time_month = "Dicembere"
	
		
	if(time_date < 10) {
        time_date = "0" + time_date;
    }
	
	if(time_hours.length == 1) {
        time_hours = "0" + time_hours;
    }
	
    if(time_min < 10) {
        time_min = "0" + time_min;
    }
 	var dateFull = time_day + " " + time_date + " " + time_month + " " + time_year + " - ore " + time_hours + ":" + time_min;
	return dateFull;
	
}

/*FINE METODI USATI*/

function load(long, lat, data, elementName) {

    if (GBrowserIsCompatible()) {
      
     var map = new GMap2(document.getElementById(elementName));
	 map.addControl(new GLargeMapControl());
	 map.addControl(new GMapTypeControl());

  	 map.setCenter(new GLatLng(long, lat), 13);
	
	 if (data != "") {
		 var txt = "<strong>Cantiere:</strong><br><span class='txt12'>" + data[0] + "</span><br>" + 
		 	    "<strong>Zona:</strong><br><span class='txt12'>" + data[1] + "</span><br>" + 
		 	    "<strong>Indirizzo:</strong><br><span class='txt12'>" + data[2] + "</span><br>";
	
		 map.openInfoWindowHtml(map.getCenter(), txt);
	 } else {
	 	map.addOverlay(new GMarker(new GLatLng(long, lat)));
        
	 	
	 }
	 map.setZoom(15);

    }
}




function ApriMappa(id){
	var confWin  = $('FinestraWindow');
	//messaggio = "<iframe width='425' height='350' frameborder='0' scrolling='no' marginheight='0' marginwidth='0' src='http://maps.google.it/maps?f=q&amp;hl=it&amp;geocode=&amp;q=Via+S.+Zeno,+2+%E2%80%93+56127+Pisa&amp;sll=41.442726,12.392578&amp;sspn=9.089915,17.62207&amp;ie=UTF8&amp;s=AARTsJrPD9eRnzIDHKXfymG3sH0ThNrp_A&amp;ll=43.729181,10.408344&amp;spn=0.00428,0.008605&amp;t=h&amp;z=14&amp;output=embed'></iframe><br />";
	messaggio = "<iframe width='425' height='350' frameborder='0' scrolling='no' marginheight='0' marginwidth='0' src='http://maps.google.it/maps?f=q&amp;hl=it&amp;geocode=&amp;q=Via+S.+Zeno,+2+%E2%80%93+56127+Pisa&amp;sll=41.442726,12.392578&amp;sspn=9.089915,17.62207&amp;ie=UTF8&amp;s=AARTsJrPD9eRnzIDHKXfymG3sH0ThNrp_A&amp;ll=43.729181,10.408344&amp;spn=0.00428,0.008605&amp;t=h&amp;z=14&amp;output=embed'></iframe><br />";
	var html = "<div id='layer'></div><div id='FinestraMsgBox' style='width:435px;'>"+
			   "<div class='contenitore'><br>" + messaggio + "<div align='center'>"+
			   "<br><input type='Button' class='txt_puls' onClick='ChiudiMsgBox()' align='center' style='cursor:pointer;'/>"+
			   "</div></div></div>";
	Element.update(confWin, html);   
	        
	var layer= $('layer');
	var finestra= $('FinestraMsgBox');
    altez =document.body.clientHeight / 5;
    larg = document.body.clientWidth / 2;
    finestra.style.left = larg + "px";
    finestra.style.top = altez + "px";
	finestra.setStyle({ opacity: 1.0 }); 
	layer.setStyle({ opacity: 0.5 }); 
	layer.style.display='inline';
	finestra.style.display='inline';   

	new Draggable('FinestraWindow',{ scroll:window,
								      handle: 'intestazione',
								       starteffect:function(){},
								       endeffect:function(){}

                                               });         
}
function ChiudiMsgBox() {
	
	var layer= $('layer');
  	var finestra= $('FinestraMsgBox');	
	layer.style.display='none';
	finestra.style.display='none';
}

