// JavaScript Document
document.observe("dom:loaded", function(){		
										
	Event.observe("privacy", "change", function(){									   										   
		if ($('moduloContatto').getInputs('checkbox','privacy').pluck('checked').any()) {	
		$('inviabtn').show();
		} else {	
		 $('inviabtn').fade({ duration: 1 });
		}
	});	
	
	
	function greet(){
		
	/*	
		$("moduloContattoCercaVendi").innerHTML = 'valore = '+$('nomecogn').value ;
	*/
	
     var url = 'public/send-form.php';
     //var pars = 'nomecogn='+escape($('nomecogn').value);
     var target = 'moduloContattoCercaVendi';
     var myAjax = new Ajax.Updater(target, url, {method: 'get', parameters: $('moduloContatto').serialize(true)});
	 
	}



	var url = 'captcheck.php?code=';
	var captchaOK = 0;  // 1 - correct, 0 - failed


	
	function checkcode(thecode){
	
		//alert('checkcode = '+thecode);
	
		new  Ajax.Request('captcheck.php?code='+thecode, 
		{ 
			method:'get', 
			onSuccess: function(transport){ 
			var response = transport.responseText || "no response text"; 
	
		//	alert('response = '+response);
			
			if(response == '1'){
	//			return true;	
				
				$('captchastring').setStyle({ border: 'none'});
					
				greet();
				
			} else {
				$('captchastring').setStyle({ border: '1px dashed #900'});
				$('captchastring').focus();
				errConter++;	
			}
			
			}, 
			 
			onFailure: function(){ alert('Errore di controllo antispam')} 
		}); 
	
	 
	}
	


	


	Event.observe("inviabtn", "click", function(){	
				
		//alert($('nomecogn').getValue().length);
			var errConter = 0;
												
				if(($('nomecogn').getValue() == '') || ($('nomecogn').getValue().length < 5)){								
						$('nomecogn').setStyle({ border: '1px dashed #900'});
						$('nomecogn').focus();
						errConter++;
				} else {
					$('nomecogn').setStyle({ border: 'none'});
				
						if(($('messaggio').getValue() == '') || ($('messaggio').getValue().length < 5)){								
								$('messaggio').setStyle({ border: '1px dashed #900'});
								$('messaggio').focus();
								errConter++;
						} else {
							$('messaggio').setStyle({ border: 'none'});
							
								if(($('email').getValue() == '') || ($('email').getValue().length < 5)){								
										$('email').setStyle({ border: '1px dashed #900'});
										$('email').focus();
										errConter++;
								} else {
									$('email').setStyle({ border: 'none'});
									
									if(($('telefono').getValue() == '') || ($('telefono').getValue().length < 5)){								
											$('telefono').setStyle({ border: '1px dashed #900'});
											$('telefono').focus();
											errConter++;
									} else {
										$('telefono').setStyle({ border: 'none'});
										
										if(($('captchastring').getValue() == '') || ($('captchastring').getValue().length < 6)){								
											$('captchastring').setStyle({ border: '1px dashed #900'});
											$('captchastring').focus();
											errConter++;
									} else {
										$('captchastring').setStyle({ border: 'none'});
																			
										// Now the Ajax CAPTCHA validation
										
										
										
																		
								}//end if telefono	
										
																		
								}//end if telefono						
							
						}//end if messaggio					
					
				}//end if nomecogn
				
				
				}
				
				
				if(errConter == 0){
					
					checkcode($('captchastring').getValue())
										
					//alert('controlloCaptcha = '+controlloCaptcha);
					//greet();
				}
				
	});
		

	
});



