/*   Trovobarche.it JavaScript 
 *  (c) 2006-2008 Liveshape Srl
 *
 * http://www.trovobarche.it http://www.liveshape.it
 *
 *--------------------------------------------------------------------------*/

// <![CDATA[ 

function getLabel(label,val)
{
    switch(label)
    {  
        case "error":
          return "Errore ... ";
        case "error1":
         return "MESSAGGIO NON INVIATO \n Il codice digitato non corrisponde a quello visualizzato. \n Verifica il codice, inseriscilo correttamente e premi il tasto \"Invia\" \n";
        case "error11":
	    return "\n Il codice digitato non corrisponde a quello visualizzato. \n Verifica il codice, inseriscilo correttamente e premi il tasto \"Invia\" oppure regenera nuovo codice e riprovare\n";
       
        
        case "error2":
         return "MESSAGGIO NON INVIATO \n codice annuncio non trovato ";
        case "error3":
         return "MESSAGGIO NON INVIATO \n Errore durante invio di email, \n Riprovare ancora ";
        
        case "error5":
	          return "MESSAGGIO NON INVIATO \n Errore durante invio di email, \n Email in Blacklist Internazionale";
        
        
        case "error12":
         return "MESSAGGIO NON INVIATO \n Il campo \"email\" non è stato compilato corettamente, correggi email e riprova ancora ";
        case "error13":
         return "MESSAGGIO NON INVIATO \n Il campo \"nome\" non è stato compilato ";
        case "error14":
         return "MESSAGGIO NON INVIATO \n Il campo \"codice di verifica\" non è stato compilato";
        case "error15":
         return "MESSAGGIO NON INVIATO \n Il campo \"messaggio\" non è stato compilato ";
        case "error25":
         return "MESSAGGIO NON INVIATO \n Il campo \"id annuncio adid\"  è vuoto ";
         
        case "error144":
         return "MESSAGGIO NON INVIATO \n Il campo \"Importo della Proposta\" è vuoto ";
       case "error145":
         return " Non è possibile formulare \"proposte d'acquisto\" ribassando di oltre il  " + totpercent +" % il prezzo del venditore!";   // return "  \"Importo della Proposta\" " + val+"  è troppo basso ";
       case "error146":
         return " Non è possibile formulare \"proposte d'acquisto\" al rialzo !";  
         
        case "no response text":  
         return "MESSAGGIO NON INVIATO \n Server non risponde, riprovare ancora ";
    }        
}

function docontact (email, name, msg, imgtxt, ref)
{
 
    
     if (  !tr  )  {
       var tr="";
     }
      if (  !ref  )  {
         var ref="";
     }
     
    
    if (!name.strip() )  {
       alert ('Inserire Nome');
       $("contactname").focus();
       return false;
    }  
     
    if (!email.strip() ) {
        alert ('Inserire Email');
        $('contactemail').focus();  
        return false;
    }
    
    var uphone="";
    if ( $("uphone") ) {
       uphone=$("uphone").value;
    }
   
    
    email=email.replace(/\s+$|^\s+/g,""); //trim string
    arr = email.match("^[0-9a-zA-Z]([0-9a-zA-Z\._\-]*)@(([0-9a-zA-Z\-]+\.)+)([0-9a-zA-Z\-]+)$");
    if (!arr) {         
        alert ('Indirizzio Email non corretto' ); 
        $('contactemail').focus();   
        return;
    }
   
    
    if (!msg.strip() ) {
       alert ('Inserire il tuo messaggio');
       $('message').focus();  
       return false;
    }  
    
   if (  !imgtxt )
    {
       alert ('Inserire il codice di controllo');
       $('img').focus();  
       return false;
    }
 
 if ( $('_tbtr').value  ) {
    var tr=$('_tbtr').value; 
 }
    
    var url = '/action/mailtouser.php';
   
     
    var pars = 'email='+email+'&phone='+ encodeURI(escape(uphone))+'&name='+ encodeURI(escape(name))+'&msg='+ encodeURI(escape(msg)) +'&imgtxt='+ encodeURI(escape(imgtxt)+'&tr=' + tr +'&ref=' + ref); 
  
  new Ajax.Request(url,
  {
     method:'post',
     parameters: pars, 
    
    onCreate: function(){
		// alert('a request has been initialized!');
        Element.hide ('contactbutton');
        Element.show ('contactloading');	 
        $('contactemail').disabled = true;
        $('contactemail').readonly = true;
        $('contactname').disabled = true;
        $('contactname').readonly = true;
         $('message').disabled = true;
          $('img').disabled = true; 
	      
	    
				  

		}
  
    , 
 /* 		onFailure: function ( transport ) {
      var response = transport.responseText || "no response text";
     //  alert('a request onFailure:  ' + response );
          
         Element.hide ('contactloading');
         $('_errorcontactdiv').innerHTML= "Failure: Errore durante INVIO EMAIL riprovare (404) ";          
         alert ("\n Failure: (404) Server momentaneamente non disponible , riprovare ancora, grazie ");
    }
		,
    onException: function ( transport ) {
      var response = transport.responseText || "no response text"; 
      // alert('a request onException:  ' + response );
      	if ( response == "no response text") {
      	    Element.hide ('contactloading');
            $('_errorcontactdiv').innerHTML= "Exception: Errore durante invio, riprovare , grazie";
            alert ("\n Exception: Errore durante invio, riprovare , grazie");
      	
        }
    } ,
                
*/	 
   onComplete: function( transport ) {
      var response = transport.responseText || "no response text"; 
		//alert('a request completed');
		// $('contactloading').innerHTML= "";
        Element.hide ('contactloading' ); 
        Element.show ('contactbutton');        
        $('contactemail').disabled = false;
        $('contactemail').readonly = false;
        $('contactname').disabled = false;
        $('contactname').readonly = false;
        $('message').disabled = false;
        $('img').disabled = false;  
		 
	                    
  } ,
             
  onSuccess: function( transport ) {
            
           var response = transport.responseText || "no response text";
           
           
           if ( response=='error11' || response=='error25' || response=='error' || response=='error1' || response=='error2' || response=='error3' || response=='error12' ||response=='error13' ||response=='error14' ||response=='error15' ) 
           { 
             var err = getLabel(response);
             alert( "\n " + err + " \n (" + response + ")" ); // Error! \n
             
             
           } 
           else if ( response == "no response text") {
      	   
                $('_errorcontactdiv').innerHTML= "No response: Errore durante invio, 404 - Server momentaneamente non disponible ci scusiamo per inconviniente. Riprova ancora, grazie";
                alert ("\n No response: 404 - Server momentaneamente non disponible ci scusiamo per inconviniente. Riprova ancora, grazie");
      	
          }
           else {             
             
             Modalbox.show("<div id=resposnce> "+response+"</div>", {title: ' Invio E-mail ' + document.title, width: 400});
            // alert ("\n" + response );  // Success!  
             //  $('contactform').innerHTML = response; //  decodeURI(response); 
              $('_errorcontactdiv').innerHTML = response;
             // Effect.Fade('contactform', {duration: 22}); 
             // Element.hide('contactform');
            
             $("contactname").value="";
             $("contactemail").value="";
             $('message').value="";
             $('img').value="";
              Effect.SlideUp('contactform');
          }
    }
  });
     
}

function reportonSuccess () {
 
  newImage = "url(http://www.trovobarche.it/images/bulletArrowRight.gif)";
  document.getElementById('form_meta_id').style.backgroundImage = newImage;
  
  
}
 
  
function form () {
   
 if(document.getElementById('contactform').style.display=="none") {
   Element.show('contactform');
   newImage = "url(http://www.trovobarche.it/images/bulletArrowDown.gif)";
   
 }else{
    
    newImage = "url(http://www.trovobarche.it/images/bulletArrowRight.gif)"; 
     
     //  new Effect.BlindUp('contactform', {duration: 3});
      Effect.Fade('contactform', {duration: 2}); 
      // Element.hide('contactform'); // .style.display='none';
 }
    
  
   document.getElementById('form_meta_id').style.backgroundImage = newImage;
     
}


function win(fileName) {

     myFloater = window.open('','myWindow','scrollbars=yes,status=no,width=300,height=500')

     myFloater.location.href = fileName;

}

function winimage(fileName,width,height) {

     myFloater = window.open('','myWindow','scrollbars=yes,status=no,width=' + width + ',height=' + height)

     myFloater.location.href = fileName;

}



function regencaptcha () {
 
   var rid=Math.random()*11;
   // alert (' rid: ' +rid);
   var answer = confirm('Regenerare il  codice di verifica?')
   if ( answer  ) {
          
        //   $('captcha').innerHTML = "<img border=2 src='/action/imgtext.php?rid=" + rid + "'>";
      $('captcha').src = "http://www.trovobarche.it/action/imgtext.php?rid=" + rid;
      $('img').value = "";
       return true;
   
   } else 
     return false;

}

// ]]>

