//script.js

var abilitato_js=0;
var ms=0; //e' ie microsoft?

//IDENTIFICAZIONE BROWSER
function identifica_browser(){
	//alert(navigator.appName);
	//analizza se il browser e' compatibile con Javascript
	browserName=navigator.appName.charAt(0);
	browserVer=parseInt(navigator.appVersion);
	if (browserName=="M" && browserVer >= 3) {abilitato_js=1; ms=1;}
	if (browserName=="N" && browserVer >= 3) abilitato_js=2;
	if (browserName=="N" && browserVer >= 4) abilitato_js=3;
	if (browserName=="M" && browserVer >= 4) {abilitato_js=4; ms=1;}
}
identifica_browser();

var arImages=new Array();
function Preload() {
 var temp = Preload.arguments; 
 for(x=0; x < temp.length; x++) {
  arImages[x]=new Image();
  arImages[x].src=Preload.arguments[x];
 }
}

function preload_my_images(){
	
					Preload("images/it_IT/menu_azienda.png");
							Preload("images/it_IT/menu_azienda_ro.png");
							Preload("images/it_IT/menu_azienda_md.png");
							Preload("images/it_IT/menu_contatti.png");
							Preload("images/it_IT/menu_contatti_ro.png");
							Preload("images/it_IT/menu_contatti_md.png");
							Preload("images/it_IT/menu_listino.png");
							Preload("images/it_IT/menu_listino_ro.png");
							Preload("images/it_IT/menu_listino_md.png");
							Preload("images/it_IT/menu_riservata.png");
							Preload("images/it_IT/menu_riservata_ro.png");
							Preload("images/it_IT/menu_riservata_md.png");
							Preload("images/it_IT/menu_servizi.png");
							Preload("images/it_IT/menu_servizi_ro.png");
							Preload("images/it_IT/menu_servizi_md.png");
				for(img in document.images){
		Preload(img.src);
	}
}

//RICONOSCIMENTO TASTO PREMUTO
function keyDown(e) {
       	if (abilitato_js==3) {
		//var ns4=1;
		var nKey=e.which; var ieKey=0;
		var realkey = String.fromCharCode(e.which);
		//strin="Codice tasto in Netscape: "+nKey+". Premuto tasto "+realkey
	}
       	if (abilitato_js==4) {
		//var ns4=0;
		var ieKey=event.keyCode; var nKey=0;
		var realkey = String.fromCharCode(event.keyCode);
		//strin="Codice tasto in Explorer: " + ieKey+". Premuto 		
		//strin="Codice tasto in Explorer: " + ieKey+". Premuto tasto "+realkey
		//strin="Codice tasto in Explorer: " + ieKey+". Premuto tasto "+realkey
		//strin="Codice tasto in Explorer: " + ieKey+". Premuto tasto "+realkey 
	}
	//alert(strin)
}

//CONTROLLO DATE
function controlla_data(nome,dt,k){
	var mil='19';
	if(k==2){
		mil='20';
	}
	var rit=false;
	if(dt.match(/^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/) && dt.length==10){ // gg/mm/aaaa
		rit=true;
	}else
	if(dt.match(/^[0-9]{2}\/[0-9]{2}\/[0-9]{2}$/) && dt.length==8){ // gg/mm/aa in +
		document.form[nome].value=dt.substr(0,6)+mil+dt.substr(6,2);
		rit=true;
	}else
	if(dt.match(/^[0-9]{8}$/) && dt.length==8){						 //	ggmmaaaa
		document.form[nome].value=dt.substr(0,2)+"/"+dt.substr(2,2)+"/"+dt.substr(4,4);
		rit=true;
	}else
	if(dt.match(/^[0-9]{6}$/) && dt.length==6){						 //	ggmmaa in +
		document.form[nome].value=dt.substr(0,2)+"/"+dt.substr(2,2)+"/"+mil+dt.substr(4,2);
		rit=true;
	}else
	if(dt.match(/^[0-9]{2}\.[0-9]{2}\.[0-9]{4}$/) && dt.length==10){ // gg.mm.aaaa
		dt=dt.replace('.','/');
		dt=dt.replace('.','/');
		document.form[nome].value=dt;
		rit=true;
	}else
	if(dt.match(/^[0-9]{2}\.[0-9]{2}\.[0-9]{2}$/) && dt.length==8){ // gg.mm.aa
		dt=dt.replace('.','/');
		dt=dt.replace('.','/');
		document.form[nome].value=dt.substr(0,6)+mil+dt.substr(6,2);
		rit=true;
	}else
	if(dt.match(/^[0-9]{2}\-[0-9]{2}\-[0-9]{2}$/) && dt.length==8){ // gg-mm-aa
		dt=dt.replace('-','/');
		dt=dt.replace('-','/');
		document.form[nome].value=dt.substr(0,6)+mil+dt.substr(6,2);
		rit=true;
	}else
	if(dt.match(/^[0-9]{2}\-[0-9]{2}\-[0-9]{4}$/) && dt.length==10){ // gg-mm-aaaa in +
		dt=dt.replace('-','/');
		dt=dt.replace('-','/');
		document.form[nome].value=dt;
		rit=true;
	}	
	return rit;
}
//CONTROLLO CAMPI
//controllo email
function controlla_input_email(){
	if(this.value.indexOf('@')==-1){
		alert("NO OK");
		this.value="tuamail@tuodominio.it";
		//this.focus();
	}
}

//FINE CONTROLLO CAMPI


//GESTIONE ROLLOVER
function img_omover(){	
	//this.src=this.src + '&ro=1';
	this.src=this.src.substring(0,this.src.length-4)+"_ro.png";
	//alert(this.src);
}
function img_omout(){
	/*
	var cp=this.src.substring(this.src.length-5);
	if(cp.indexOf('&ro=1')!=-1 || cp.indexOf('&ro=2')!=-1){
		this.src=this.src.substring(0,this.src.length-5);
	}
	*/
	var cp=this.src.substring(this.src.length-7,this.src.length-4);
	//alert(cp);
	if(cp.indexOf('_ro')!=-1 || cp.indexOf('_md')!=-1){
		this.src=this.src.substring(0,this.src.length-7)+".png";
		//alert(this.src);
	}
}
function img_omdown(){
	//this.src=this.src.replace('&ro=1','&ro=2');
	this.src=this.src.replace('_ro.png','_md.png');

}
function img_omup(){
	//this.src=this.src.replace('&ro=2','&ro=1');
	this.src=this.src.replace('_md.png','_ro.png');
}

//press flag
function img_omdown_flag(){
	this.src=this.src.replace(".png","_press.png");
}
function img_omup_flag(){
	this.src=this.src.replace("_press.png",".png");
}

//press rollover
function img_omdown_rollover(){
	this.src=this.src.replace(".png","_md.png");
}
function img_omup_rollover(){
	this.src=this.src.replace("_md.png",".png");
}


function set_ovr_out_img(){
	for(i=0;i<document.images.length;i++){
		if(document.images[i].className=='menu_img'){
			document.images[i].onmouseover=img_omover;
			document.images[i].onmouseout=img_omout;
			document.images[i].onmousedown=img_omdown;
			document.images[i].onmouseup=img_omup;
		}
		if(document.images[i].className=='flag_img'){
			document.images[i].onmousedown=img_omdown_flag;
			document.images[i].onmouseup=img_omup_flag;
		}
		if(document.images[i].className=='rollover_img'){
			document.images[i].onmousedown=img_omdown_rollover;
			document.images[i].onmouseup=img_omup_rollover;
		}
	}
}
//FINE GESTIONE ROLLOVER


//GESTIONE ROLLTEXT
function input_onf(){
	this.style.backgroundColor='#F0F0FF';
	this.style.fontWeight="bold";
}
function input_onb(){
	this.style.backgroundColor='#FFFFFF';
	this.style.fontWeight="normal";
}

function input_numero_onb(){
	this.style.backgroundColor='#FFFFFF';
	this.style.fontWeight="normal";
	if(isNaN(this.value) && this.value!="") {
		alert("Non è un numero valido");
		this.value="0";
	}
	return true;
}

function input_valuta_onb(){
	this.style.backgroundColor='#FFFFFF';
	this.style.fontWeight="normal";
	this.value=this.value.replace(".",",");
	if(isNaN(this.value.replace(",",".")) && this.value!="") {
		alert("Non è un numero valido");
		this.value="0";
	}
	return true;
}

function input_data_onb(){
	this.style.backgroundColor='#FFFFFF';
	this.style.fontWeight="normal";
	if(!controlla_data(this.name,this.value,2) && this.value!=""){
		alert("Formato data errato deve essere (gg/mm/aaaa)");
	}
	return true;
}

function select_onf(){
	this.style.fontWeight='normal';
}
function select_onb(){
	this.style.fontWeight='normal'; 
}
function btn_onmovr(){
	this.style.color='#FF0000';
}
function btn_onmout(){
	this.style.color='#0000FF';
}



function set_focusblur(){
	//alert(document.forms);
	for(var i=0;i<document.forms.length;i++){
		var frm=document.forms[i];
		for(var j=0 ; j<frm.length; j++){ 			
			//myform_text
			if(frm[j].tagName=='INPUT' && frm[j].className=='myform_text' && (frm[j].type=='text' || frm[j].type=='password')){
				frm[j].onfocus=input_onf;
				frm[j].onblur=input_onb;
			}
			//myform_text_numero
			if(frm[j].tagName=='INPUT' && frm[j].className=='myform_text_numero' && (frm[j].type=='text' || frm[j].type=='password')){
				frm[j].onfocus=input_onf;
				frm[j].onblur=input_numero_onb;
			}
			//myform_valuta_numero
			if(frm[j].tagName=='INPUT' && frm[j].className=='myform_text_valuta' && (frm[j].type=='text' || frm[j].type=='password')){
				frm[j].onfocus=input_onf;
				frm[j].onblur=input_valuta_onb;
			}
			//myform_text_data
			if(frm[j].tagName=='INPUT' && frm[j].className=='myform_text_data' && (frm[j].type=='text' || frm[j].type=='password')){
				frm[j].onfocus=input_onf;
				frm[j].onblur=input_data_onb;
			}
			//myform_select
			if(frm[j].tagName=='SELECT' && frm[j].className=='myform_select'){
				frm[j].onfocus=select_onf;
				frm[j].onblur=select_onb;
			}
			//myform_btn
			if(frm[j].tagName=='INPUT' && frm[j].className=='myform_btn' &&  (frm[j].type=='submit' || frm[j].type=='reset' || frm[j].type=='button' )){
				frm[j].onmouseover=btn_onmovr;
				frm[j].onmouseout=btn_onmout;
			}
			//myform_email
			if(frm[j].tagName=='INPUT' && frm[j].className=='myform_email' &&  frm[j].type=='text'){
				frm[j].onblur=controlla_input_email;
			}
			
		}
	}
}

//FINE GESTIONE ROLLTEXT


function vedi_annuncio(id){
	if(id!=""){
		var frm=document.form;
		frm.pag.value="";
		frm.cmd.value="vedi_annuncio";
		frm.id.value=id;
		frm.submit();
	}
}

function vedi_immagine(nome){
	if(nome!=""){
		//alert("nome="+nome);
		var box=document.getElementById('floating_box');
		document.getElementById('id_img').src="/"+nome;
		var l=(screen.width-600)/2;
		var t=((screen.height-450)/2)-100;
		box.style.left=l+"px";
		box.style.top=t+"px";
		box.style.display='block';
	}
	return false;
}	
function richiedi_info(id){
	var frm=document.form;
	if(id!=""){
		frm.pag.value='8cae4ec85d836a997e9d32160d9f89fcad13a8e6';
		frm.id.value=id;
		frm.submit();
	}
}
	

//conteggio caratteri
function conta_caratteri(frm_elem,id_num_show,limit){
	var l=document.form[frm_elem].value.length;
	if(l>limit){
		document.form[frm_elem].value=document.form[frm_elem].value.substr(0,limit);
		l=limit;
	}
	document.getElementById(id_num_show).innerHTML=l;
}

function cebanner(){
	if(document.form.id_scroll){
		banner();
	}
}

function associa(){
	if(document.getElementById('contatti_comune')){
		document.getElementById('contatti_comune').onkeyup=suggerisci;
	}
}


function body_onload(){
	set_ovr_out_img();
	set_focusblur();
	cebanner();
	associa();
}


function rimuovi_html(nome){
	frm=document.form;
	frm[nome].value=frm[nome].value.replace(/[^-Za-z0-9_\,\.\;\:\s\à\è\é\ì\ò\ù\@\?\!\€\%\(\)\[\]\{\}\=\/\+\-\*\"\']/gi,'');
}

function pulisci_email(nome){
	frm=document.form;
	frm[nome].value=frm[nome].value.replace(/[^-Za-z0-9_\,\.\@]/gi,'');
}
