$(function() {
  $('.volunteer_error').hide();
  $('input.volunteer-text-input').css({backgroundColor:"#EAEAEA"});
  $('input.volunteer-text-input').focus(function(){
    $(this).css({backgroundColor:"#EAEAEA"});
  });
  $('input.volunteer-text-input').blur(function(){
    $(this).css({backgroundColor:"#EAEAEA"});
  });
  $('textarea.volunteer-text-input').focus(function(){
    $(this).css({backgroundColor:"#EAEAEA"});
  });
  $('textarea.volunteer-text-input').blur(function(){
    $(this).css({backgroundColor:"#EAEAEA"});
  });


  $(".volunteer_button_tc").click(function() {
		// validate and process form
		// first hide any error messages
    $('.volunteer_error').hide();
		 
	  var name = $("input#volunteer_name").val();
		if (name == "") {
      $("label#volunteer_name_error").show();
      $("input#volunteer_name").focus();
      return false;
    }

		var email = $("input#volunteer_email").val();
		if (email == "") {
      $("label#volunteer_email_error").show();
      $("input#volunteer_email").focus();
      return false;
    }
	
		var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i;		
		
		var returnval=emailfilter.test(email);
		if (returnval==false){
		  $("label#volunteer_email_error").show();
		  $("input#volunteer_email").focus();
   		  return false;	
		}

		var phone = $("input#volunteer_phone").val();
		if (phone == "") {
      $("label#volunteer_phone_error").show();
      $("input#volunteer_phone").focus();
      return false;
    }	

		var wsupdate = $("input[@name=volunteer_wsupdate][@checked]").val();
		if (wsupdate == null) {
			wsupdate = "0";
    }			
		var csupport = $("input[@name=volunteer_csupport][@checked]").val();
		if (csupport == null) {
			csupport = "0";
    }			
		var rdcollection = $("input[@name=volunteer_rdcollection][@checked]").val();
		if (rdcollection == null) {
			rdcollection = "0";
    }			
		var eosupport = $("input[@name=volunteer_eosupport][@checked]").val();
		if (eosupport == null) {
			eosupport = "0";
    }			
		var specialties = $("textarea#volunteer_specialties").val();		

		var dataString = 'name='+ name + '&email=' + email + '&phone=' + phone + '&wsupdate=' + wsupdate + '&csupport=' + csupport + '&rdcollection=' + rdcollection + '&eosupport=' + eosupport + '&specialties=' + specialties + '&KT_Insert1=1';
		//alert (dataString);return false;
		
		$.ajax({
      type: "POST",
      url: "php_bin/process_volunteer.php",
      data: dataString,
      success: function() {
        $('#volunteer_form').html("<div id='volunteer_message' class='volunteer_message'></div>");
        $('#volunteer_message').html("<div id='volunteer_check' class='right'><img id='volunteer_checkmark' src='images/check.png' width='30' height='30' /></div><div class='right'>招募義工團隊表格經已送出! </br>我們會盡早聯絡您, 謝謝!</div>")
        .append("</br>")
        .hide()
        .fadeIn(1500, function() {
          $('#volunteer_message').append("</br>");
        });
      }
     });
    return false;
	});
});
runOnLoad(function(){
  //$("input#volunteer_name").select().focus();
});


