$(function() {
  $('.error').hide();
  $('input.text-input').css({backgroundColor:"#FFFFFF"});
  $('input.text-input').focus(function(){
    $(this).css({backgroundColor:"#F0C9FF"});
  });
  $('input.text-input').blur(function(){
    $(this).css({backgroundColor:"#FFFFFF"});
  });
  $('textarea.text-input').focus(function(){
    $(this).css({backgroundColor:"#F0C9FF"});
  });
  $('textarea.text-input').blur(function(){
    $(this).css({backgroundColor:"#FFFFFF"});
  });


  $(".button_tc").click(function() {
		// validate and process form
		// first hide any error messages
    $('.error').hide();
		
	  var newsid = 	$("input#newsid").val(); var newstitle = 	$("input#newstitle").val();

	  var title = $('input[@name=title][@checked]').val(); 
  
	  var name_f = $("input#name_f").val();
	 if (name_f == "") {
      $("label#name_f_error").show();
      $("input#name_f").focus();
      return false;
    }
	
	   var name_m = $("input#name_m").val();
	   
	   var name_l = $("input#name_l").val();
	 if (name_l == "") {
      $("label#name_l_error").show();
      $("input#name_l").focus();
      return false;
    }
	
	   var coname = $("input#coname").val();
	   var position = $("input#position").val();	   
	  
		var membershipname = $("input#membershipname").val();
		if (membershipname == "") {
      $("label#membershipname_error").show();
      $("input#membershipname").focus();
      return false;
    }	
	
		var membershipno = $("input#membershipno").val();
		if (membershipno == "") {
      $("label#membershipno_error").show();
      $("input#membershipno").focus();
      return false;
    }		

	    var iChars = /^\w+$/i;
		var returniChars=iChars.test(membershipno);
		if (returniChars==false){
		  $("label#membershipno_error").show();
		  $("input#membershipno").focus();
   		  return false;	
		}		

        var email = $("input#email").val();
		if (email == "") {
      $("label#email_error").show();
      $("input#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#email_error").show();
		  $("input#email").focus();
   		  return false;	
		}
	
	   var phone = $("input#phone").val();
		if (phone == "") {
      $("label#phone_error").show();
      $("input#phone").focus();
      return false;
    }	   
	   var fax = $("input#fax").val();
	   var address = $("textarea#address").val();
	   
		var dataString = 'newsid=' + newsid + '&newstitle=' + newstitle + '&title=' + title + '&name_f='+ name_f + '&name_m='+ name_m + '&name_l='+ name_l + '&coname=' + coname + '&position=' + position + '&membershipname=' + membershipname + '&membershipno=' + membershipno + '&email=' + email + '&phone=' + phone + '&fax=' + fax + '&address=' + address + '&KT_Insert1=1';
		//alert (dataString);return false;
		
		$.ajax({
      type: "POST",
      url: "php_bin/process.php",
      data: dataString,
      success: function() {
        $('#enrolment_form').html("<div id='message'></div>");
        $('#message').html("<div id='check' class='left'><img id='checkmark' src='images/check.png' width='30' height='30' /></div><div class='left'>多謝網上登記!  回條將於15分鐘內發送給您，這並非講座核實通知。</br>但如未有收到此回條，可能閣下的網上申請並不成功，請再嘗試。謝謝!</div>")
        .append("</br>")
        .hide()
        .fadeIn(1500, function() {
          $('#message').append("</br>");
        });
      }
     });
    return false;
	});
});
runOnLoad(function(){
  $("input#name_f").select().focus();
});

