function trim(str)
{
    return str.replace(/^\s*|\s*$/g,"");
}          
          
var msgError = ''

function goToUrl(url){
    document.location.href = url
}

function highlight(state) {
	element=event.srcElement;
	
	if (element.tagName=='INPUT') {
		etype=element.type;
		
	if ((etype=='text'))
		{
		if(state==1){ 
		element.className="onIE"; 
		}
		 else {element.className="campoTextbox";}
		}
	//element.focus();
	}
}

function SegnalaCampo(oggetto,coloreLabel,coloreCasella){
    var NameOgg = oggetto.name
    var IDLbl = 'Lbl' + NameOgg
    document.getElementById(IDLbl).style.color = coloreLabel
    oggetto.style.background = coloreCasella
    if (msgError==''){
        oggetto.focus()      
    }    
}

function pulisciTesto(obj, word)
{
	var str = word;
	if (obj.value == str) {
		obj.value='';
	}
}

function TagIntoString(htmlText) {
    var testo = htmlText.toString();
    testo = testo.toLowerCase();

    if ((testo.indexOf('<a') != -1) ||
         (testo.indexOf('<img') != -1) ||
         (testo.indexOf('<b') != -1) ||
         (testo.indexOf('<script') != -1) ||
         (testo.indexOf('<font') != -1)) {
        return true;
    } else {
        return false;
    }
}
            
function inviaModulo(){

    var nome = document.theForm.nome.value;
    var cognome = document.theForm.cognome.value;
    var indirizzo = document.theForm.indirizzo.value;
    var cap = document.theForm.cap.value;
    var comune = document.theForm.comune.value;
    var provincia = document.theForm.provincia.value;
    var email = document.theForm.email.value;
    var telefono = document.theForm.telefono.value;
    var note = document.theForm.note.value;
    var civico = document.theForm.civico.value;
    var objCivico = document.theForm.civico;
	var stato = document.theForm.stato.value;
    var objProvincia = document.theForm.provincia;
    var objProfessione = document.theForm.professione;
    var professione = document.theForm.professione.value;
    msgError = ''

    SegnalaCampo(document.theForm.nome,'#999999','#4f4f4f')
    SegnalaCampo(document.theForm.cognome,'#999999','#4f4f4f')
    SegnalaCampo(document.theForm.indirizzo,'#999999','#4f4f4f')
    SegnalaCampo(document.theForm.cap,'#999999','#4f4f4f')
    SegnalaCampo(document.theForm.comune,'#999999','#4f4f4f')
    //SegnalaCampo(document.theForm.provincia,'#999999','#4f4f4f')
    SegnalaCampo(document.theForm.email,'#999999','#4f4f4f')
    SegnalaCampo(document.theForm.telefono,'#999999','#4f4f4f')
    SegnalaCampo(document.theForm.note,'#999999','#4f4f4f')
	
	SegnalaCampo(document.theForm.civico,'#999999','#4f4f4f')
	
  
    if (trim(nome) == "")
    {
        SegnalaCampo(document.theForm.nome,'red','red')
        msgError += "- Nome\n";		            
    }
    if (trim(cognome) == "")
    {
        SegnalaCampo(document.theForm.cognome,'red','red')
        msgError += "- Cognome\n"
    }
	if (trim(stato) == "")
	{
		//SegnalaCampo(document.theForm.professione,'red','red')
		msgError += "- Stato\n"
	}    
	
    if (trim(indirizzo) == "")
    {
        SegnalaCampo(document.theForm.indirizzo,'red','red')
        msgError += "- Indirizzo\n"
    }
    if (TagIntoString(indirizzo) == true) {
        msgError += "- Indirizzo: non è consentito inserire TAG HTML\n"
    }         
	
	var ElemStato = document.getElementById("stato")		
	if (ElemStato.options[ElemStato.selectedIndex].value == "1"){
		 if (objCivico) {
			 if (trim(civico) == "")
			 {
				 SegnalaCampo(document.theForm.civico,'red','red')
				 msgError += "- Civico\n"
			 }    
		 }
	 }
		
    if (trim(cap) == "")
    {
        SegnalaCampo(document.theForm.cap,'red','red')
        msgError += "- CAP\n"
    }
	
    if (trim(comune) == "")
    {
        SegnalaCampo(document.theForm.comune,'red','red')
        msgError += "- Comune\n"
    }	
	
	if (ElemStato.options[ElemStato.selectedIndex].value == "1"){
		if (trim(provincia) == "")
		{
			//SegnalaCampo(document.theForm.provincia,'red','red')
			msgError += "- Provincia\n"
		}
	}
    
    if (objProfessione) {
		if (trim(professione) == "")
		{
			//SegnalaCampo(document.theForm.professione,'red','red')
			msgError += "- Professione\n"
		}    
    }   	
		  	
    var testresults
    var str=trim(email)
    var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
    if (filter.test(str)){
          testresults=true
    }else{
            var testresults
            SegnalaCampo(document.theForm.email,'red','red')
            msgError += "- E-Mail\n"
            testresults=false
    }
				
    if (trim(telefono) == "")
    {
        SegnalaCampo(document.theForm.telefono,'red','red')
        msgError += "- Telefono\n"
    }

    if (TagIntoString(document.theForm.note.value) == true) {
        msgError += "- Richiesta: non è consentito inserire TAG HTML\n"
    }     
    
	if (document.theForm.note.value == 'Inserisci qui le motivazioni della tua richiesta. Le richieste verranno processate dall\'azienda in base alla rilevanza delle motivazioni da te indicate.') {
		document.theForm.note.value = '';
    }

    if (document.theForm.note.value == 'Seleziona i prodotti per i quali desideri ricevere informazioni o cataloghi e descrivi qui le motivazioni della tua richiesta') {
        document.theForm.note.value = '';
    }
	
	if (msgError!='') {      
		if (typeof(Archiproducts)  != "undefined" ){	
			window.alert("Fill all field!")
		}else{
			window.alert("ATTENZIONE!!\n\n Riempire correttamente i seguenti campi:\n\n" + msgError)
		}	
        return (false)
    }else {
        return (true)
    }    
        
}


function ToggleCheck (ElemImmagine,IDCheck,IDLi,ImmagineON,ImmagineOFF) {
     var ElemCheck, ElemLI, myFx
     
     ElemCheck      = document.getElementById(IDCheck)
     ElemLI         = $(IDLi)

     myFx            = new Fx.Tween(ElemLI)
     myFx.onComplete = function (){
                             if (ElemCheck.checked == false)
                                   ElemLI.style.borderColor = ""                     
                        }                      

     if (ElemCheck.checked == true) {
         ElemCheck.checked = false
         ElemImmagine.src  = ImmagineOFF         
         myFx.start('borderColor', '#666666');  
     }else{     
         ElemCheck.checked = true
         ElemImmagine.src  = ImmagineON         
         myFx.start('borderColor', '#FF0000');
     }   

     myFx = null                                               
}


var OnScroll = false

function Scroll (Elemento,Pixel) {
     var Elemento, FxScroll
     
     FxScroll = new Fx.Tween(Elemento)
     FxScroll.start('margin-top',Pixel).chain(function(){OnScroll = false})           
}

function SetStateButton(){
    var BtnDown, BtnUp
    BtnDown = $("down")
    BtnUp   = $("up")    
    
    // verifica se uno dei pulsanti è presente nel DOM (in questo caso "BtnDown")
    // e imposta il loro stato
	if (typeof(Archiproducts)  != "undefined"){
		if (BtnDown) { 
			if ((Row == NumRow)){    
				BtnDown.setStyle('background', 'url(immagini/btn_updown_ap.png) -42px -32px no-repeat')
				BtnUp.setStyle('background', 'url(immagini/btn_updown_ap.png) 0 0 no-repeat')
			}else{   
				if ((Row == NumMinRow)){   
					BtnDown.setStyle('background','url(immagini/btn_updown_ap.png) -42px 0 no-repeat')
					BtnUp.setStyle('background','url(immagini/btn_updown_ap.png) 0 -32px no-repeat')    
				}else{
					BtnDown.setStyle('background','url(immagini/btn_updown_ap.png) -42px 0 no-repeat')
					BtnUp.setStyle('background', 'url(immagini/btn_updown_ap.png) 0 0 no-repeat') 				
				}
			}    
		}	
	}else{
		if (BtnDown) { 
			if ((Row == NumRow)){    
				BtnDown.setStyle('background', 'url(immagini/btn_updown.png) -42px -32px no-repeat')
				BtnUp.setStyle('background', 'url(immagini/btn_updown.png) 0 0 no-repeat')
			}else{   
				if ((Row == NumMinRow)){   
					BtnDown.setStyle('background','url(immagini/btn_updown.png) -42px 0 no-repeat')
					BtnUp.setStyle('background','url(immagini/btn_updown.png) 0 -32px no-repeat')    
				}
			}    
		}    
	}
}

function ScrollUp (IDElemento) {
    var NewPos
    var Offset = 174
    var Elemento = $(IDElemento)
    
    NewPos = parseInt(Elemento.getStyle('margin-top').replace('px','')) - Offset
            
    if ((Row < NumRow) && (OnScroll==false)) {
        OnScroll = true
        Scroll(IDElemento,NewPos)     
        Row += 1
    } 
    
    SetStateButton()
}

function ScrollDown (IDElemento) {
    var NewPos
    var Offset = 174
    var Elemento = $(IDElemento)
        
    NewPos = parseInt(Elemento.getStyle('margin-top').replace('px','')) + Offset          

    if ((Row > NumMinRow) && (OnScroll==false)) {
        OnScroll = true
        Scroll(IDElemento,NewPos)    
        Row -= 1
    }  
    
    SetStateButton()  
}


window.onload = function() {
    SetStateButton()
}




