// JavaScript Document
/*
 * runOnLoad.js: portable registration for onload event handlers.
 * 
 * This module defines a single runOnLoad() function for portably registering
 * functions that can be safely invoked only when the document is fully loaded
 * and the DOM is available.
 *
 * Functions registered with runOnLoad() will not be passed any arguments when
 * invoked. They will not be invoked as a method of any meaningful object, and
 * the this keyword should not be used.  Functions registered with runOnLoad()
 * will be invoked in the order in which they were registered.  There is no
 * way to deregister a function once it has been passed to runOnLoad().
 *
 * In old browsers that do not support addEventListener() or attachEvent(),
 * this function relies on the DOM Level 0 window.onload property and will not
 * work correctly when used in documents that set the onload attribute
 * of their <body> or <frameset> tags.
 */
function runOnLoad(f) {
    if (runOnLoad.loaded) f();    // If already loaded, just invoke f() now.
    else runOnLoad.funcs.push(f); // Otherwise, store it for later
}

runOnLoad.funcs = []; // The array of functions to call when the document loads
runOnLoad.loaded = false; // The functions have not been run yet.

// Run all registered functions in the order in which they were registered.
// It is safe to call runOnLoad.run() more than once: invocations after the
// first do nothing. It is safe for an initialization function to call
// runOnLoad() to register another function.
runOnLoad.run = function() {
    if (runOnLoad.loaded) return;  // If we've already run, do nothing

    for(var i = 0; i < runOnLoad.funcs.length; i++) {
        try { runOnLoad.funcs[i](); }
        catch(e) { /* An exception in one function shouldn't stop the rest */ }
    }
    
    runOnLoad.loaded = true; // Remember that we've already run once.
    delete runOnLoad.funcs;  // But don't remember the functions themselves.
    delete runOnLoad.run;    // And forget about this function too!
};

// Register runOnLoad.run() as the onload event handler for the window
if (window.addEventListener)
    window.addEventListener("load", runOnLoad.run, false);
else if (window.attachEvent) window.attachEvent("onload", runOnLoad.run);
else window.onload = runOnLoad.run;


/**/

$(function() {  
$('.error').hide();
  $('input.text-input').css({backgroundColor:"#FFFFFF"});
  $('input.text-input').focus(function(){
    $(this).css({backgroundColor:"#FFDDAA"});
  });
  $('input.text-input').blur(function(){
    $(this).css({backgroundColor:"#FFFFFF"});
  });
  $(".button").click(function() {  
  // validate and process form here  
  });  
});  

  $(function() {  
  $('.error').hide();  
	  $(".button").click(function() {  
	  // validate and process form here  
	  
		   $('.error').hide();  
		   
		  var ditta = $("input#ditta").val();  
		  if (ditta == "" || ditta == "Società") {  
			 $("label#ditta_error").show();  
			 $("input#ditta").focus();  
			 return false;  
		   } 
		   
		  var telefono = $("input#telefono").val();  
		  if (telefono == "" || telefono == "Telefono") {  
		   $("label#tel_error").show();  
		   $("input#telefono").focus();  
		   return false;  
		  }   
		  
		  var email = $("input#email").val();  
		  if (email == "" || email == "Indirizzo e-mail" ) {  
			$("label#email_error").show();  
			$("input#email").focus();  
			return false;  
		  }  
		  
		  var cmail = $("input#cmail").val();  
		  if (cmail == "" || cmail == "Conferma e-mail") {  
			$("label#cmail_error").show();  
			$("input#cmail").focus();  
			return false;  
		  }  
		  
		  if (cmail != email) {  
			$("label#match_error").show();  
			$("input#cmail").focus();  
			return false;  
		  }  
		  
		  var nome = $("input#nome").val();  
		  if (nome == "" || nome == "Persona di riferimento") {  
		   $("label#nome_error").show();  
		   $("input#nome").focus();  
		   return false;  
		  }   
		  
		  var merce = $("input#merce").val();  
		  if (merce == "" || merce == "Descrizione merce") {  
		   $("label#merce_error").show();  
		   $("input#merce").focus();  
		   return false;  
		  } 
		  
		  var partenza = $("input#partenza").val();  
		  if (partenza == "" || partenza == "Partenza") {  
		   $("label#partenza_error").show();  
		   $("input#partenza").focus();  
		   return false;  
		  } 
		  
		  var destinazione = $("input#destinazione").val();  
		  if (destinazione == "" || destinazione == "Destinazione") {  
		   $("label#destinazione_error").show();  
		   $("input#destinazione").focus();  
		   return false;  
		  } 
		  
		  var colli = $("input#colli").val();  
		  if (colli == "" || colli=="Numero colli") {  
		   $("label#colli_error").show();  
		   $("input#colli").focus();  
		   return false;  
		  } 
		  
		  var peso = $("input#peso").val();  
		  if (peso == "" || peso == "Peso") {  
		   $("label#peso_error").show();  
		   $("input#peso").focus();  
		   return false;  
		  }
		  
		  var misure = $("input#misure").val();  
		  if (misure == "" || misure == "Misure") {  
		   $("label#misure_error").show();  
		   $("input#misure").focus();  
		   return false;  
		  }  
		   
		  var resa = $("input#resa").val();  
		  if (resa == "" || resa == "Resa") {  
		   $("label#resa_error").show();  
		   $("input#resa").focus();  
		   return false;  
		  }  
		  
		  var cod2 = $("input#cod2").val();  
		  if (cod2 == "") {  
		   $("label#cod2_error").show();  
		   $("input#cod2").focus();  
		   return false;  
		  }  
		  
		  var cod1 = $("input#cod1").val(); 
		  if (cod2 != cod1 ) {  
			
			$("label#cod2_error").show();  
			$("input#cod2").focus();  
			return false;  
		  } 
		  
		  		  
		  var assicurazione = $("select#assicurazione").val(); 
		  var speciali = $("select#speciali").val(); 
		  var note = $("textarea#note").val();
		  var cod1 = $("input#cod1").val(); 
		  var cod2 = $("input#cod2").val(); 	  
		  
		  var dataString = 'ditta='+ ditta + '&telefono=' + telefono + '&email=' + email + '&cmail=' + cmail + '&nome=' + nome + '&partenza=' + partenza + '&destinazione=' + destinazione + '&merce=' + merce + '&colli=' + colli + '&peso=' + peso + '&misure=' + misure + '&peso=' + peso + '&resa=' + resa + '&assicurazione=' + assicurazione + '&speciali=' + speciali + '&cod1=' + cod1 + '&cod2=' + cod2 + '&note=' + note;
		  //alert (dataString);return false;
		  $.ajax({
			type: "POST",
			url: "process.php",
			data: dataString,
			success: function() {
			alert ('Messaggio inviato, sarai ricontattato al più presto');		
			
			$("input#ditta").val('Società')
			$("input#telefono").val('Telefono') 
			$("input#email").val('Indirizzo e-mail') 
			$("input#cmail").val('Conferma e-mail')
			$("input#nome").val('Persona di riferimento')
			$("input#merce").val('Descrizione merce')
			$("input#partenza").val('Partenza')
			$("input#destinazione").val('Destinazione')
			$("input#colli").val('Numero colli')
			$("input#peso").val('Peso')
			$("input#misure").val('Misure') 
			$("input#resa").val('Resa')
			 
			$("input#cod2").val('') 
			$("select#assicurazione").val('') 
			$("select#speciali").val('')
			$("textarea#note").val('')
			location.reload();
			}
		  });
		  return false; 
		
	 });  
});  
runOnLoad(function(){
  /*$("input#nome").select().focus();*/
});
		 
 
