$(document).ready(function() {
    $("#email").remove();
    var formoptions = { 
        target: '#form1wrapper',   // target element(s) to be updated with server response 
        clearForm: true
    };
    $('#form1').ajaxForm(formoptions); 

    var spt = $('span.mailme');
    var at = / at /;
    var dot = / dot /g;
    var addr = $(spt).text().replace(at,"@").replace(dot,".");
    $(spt).after('<a href="mailto:'+addr+'" title="Send an email">'+ addr +'</a>')
    .hover(function(){window.status="Send a letter!";}, function(){window.status="";});
    $(spt).remove(); 

    $.getJSON("/db.php?function=yearlist", 
       function(data){
		  $.each(data, function(val, text) {
            $('#selectyear').append(
              $('<option></option>').val(text).html(text)
            );
           });
        });

    $('#selectyear').change(function () {
      $("#selectyear option:selected").each(function () {
			  $.getJSON("/db.php?function=modellist&val="+$(this).val(), 
	       		function(data){
                $('#model').find('option').remove().end().append('<option value="">Select model</option>').val('')
	   	  	      $.each(data, function(val, text) {
	           		 $('#model').append(
              		 $('<option></option>').val(val).html(text)
            	    );
           		 }); 
       		 });			 
			 
	 
           });
      $("#passenger1").val('');
      $("#passenger2").val('');
      $('#intmargin').html('&nbsp;');
     });
     $('#model').change(function () {
       $("#model option:selected").each(function () {
			 $.getJSON("/db.php?function=model&id="+$(this).val(), 
	       		function(data){
	   	  	        $.each(data, function(key, text) {
	   	  	        $("#"+key+"weight").val(text);
	   	  	        $("#int"+key+"weight").html(text);
           		}); 
       		 });			 
			 
	 
           });

     });
    $("#passenger1").keypress(function (e) { 
	  	  if( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)) {
	  	     e.preventDefault();
		      $("#errmsg1").html("Digits Only").show().fadeOut(3000); 
      }	
	  });

    $("#passenger2").keypress(function (e) { 
	  	  if( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)) {
	  	     e.preventDefault();
		      $("#errmsg2").html("Digits Only").show().fadeOut(3000); 
      }	
	  });

    $("#passenger1").focus(function (e) { 
	  	 $("#passenger1").val('');
	  });
	  
    $("#passenger2").focus(function (e) { 
	  	 $("#passenger2").val('');
	  });
	  	  
	  $("#calculate").click(function () {
	      if ($("#wetweight").length > 0) {
	        totalweight = parseInt($("#wetweight").val()) + parseInt($("#passenger1").val()) + parseInt($("#passenger2").val());
		      margin = parseInt($("#grossweight").val()) - totalweight;
		      if (isNaN(margin)) {
		        if ($("#passenger1").val() < 1) {
		           $("#errmsg1").html("Please enter weight").show().fadeOut(4000);
		        }
		        if ($("#passenger2").val() < 1) {
			        $("#errmsg2").html("Please enter weight").show().fadeOut(4000);
			      }
			      margin = 0;
		      }
		      $("#intmargin").removeClass();
          $('#intmargin').html(margin); 
          if (margin < 1) {
             $('#intmargin').addClass('warning');
          } else if (margin <= 15) { 
             $('#intmargin').addClass('caution');         
             $('#intmargin').append('*');  
          } else {
             $('#intmargin').addClass('safe');             
             $('#intmargin').append('*');  

          }
          pageTracker._trackPageview('/weight_and_riders_form/calculation');
        }
	  
	  });

});



