// prepare the form when the DOM is ready
$(document).ready(function() {
	$("div.submit").html("");
	$("div.add").html("");
	if($("input[@name=reg_no]").val()!="" && $("input[@name=reg_no]").val()!=null ){
		//alert($("input[@name=reg_no]").val());
		$.post("sprofile.php", {display: $("input[@name=reg_no]").val()},function(hihi){
			//$("div.info").html($("input[@name=regno]").val());
			$("div.student").html(hihi);
			pq();
		});
	}
	$('#ssubmit').ajaxForm( {url: 'sprofile.php', type: 'post', success: display} ); 
	function display(student, jqForm, options){
		if(student=="FAIL" || student==""){
			$("div.student").html("<font size='16'><center>登入資料錯誤</center></font><font size='16'><a href='index.php' ><center>請重新登入</center></a></font>");
		}else{
			//alert(student);
			$("div.name").html(student);
			$.post("sprofile.php", {display: $("input[@name=reg_no]").val()},function(hihi){
				//$("div.info").html($("input[@name=regno]").val());
				$("div.student").html(hihi);
				pq();
			});
			
		}
	}
	function pq(){//個人資料事件
		$.post("sprofile.php",
			{pq: $("input[@name=reg_no]").val(), form: $("input[@name=form]").val()},
			function(pq){
				$("div.pq").html(pq);
				$("input[@name=add]").click(
					function(){
						$('div.submit').html("");
						add();//click左add button之後
					}
				);
				$("input[@name=edit]").click(
					function(){
						$('div.submit').html("");
						edit();//click左edit button之後
					}
				);
				$("input[@name=del]").click(
					function(){
						$('div.submit').html("");
						del();//click左del button之後
					}
				);
				$("input[@name=cancel]").click(
					function(){
						$('div.submit').html("");
						cancel();//click左cancel button之後
					}
				);
			}
		 );
	}
	function cancel(){
		$("div.add").html("");//清除個人資料事件
		$('div.submit').html("");
		scroll(0,10000);
	}
	function del(){
		$.post("sprofile.php",
			{del: $("input[@name=reg_no]").val() },
			function(del){//顯示del table
				$("div.add").html(del);
				scroll(0,10000);
				$("a[@name=submit]").click(
					function(){
						//submit form
						var form = "#"+$(this).parents("form").attr("id");
						//alert(form);
						$(form).ajaxSubmit( {url: 'sprofile.php', type: 'post', success: reset} ); 
						//scroll(0,10000);
					}
				);
			}
		);
	}
	function edit(){
		$.post("sprofile.php",
			{edit: $("input[@name=reg_no]").val() },
			function(edit){//顯示edit table
				$("div.add").html(edit);
				scroll(0,10000);
				$("select[@name=sprole]").click(
					function(){
						//alert($(this).val());
						if($(this).val()=="7"){
							$(this).next().html("<input type='text' name='sproleother' size='10' />");
						}else{
							$(this).next().html("");
						}
					}
				);
				$("select[@name=spyear]").change(
					function(){
						//alert($(this).prev().val());
						if($(this).val()=="20"+$("input[@name=reg_no]").val().substr(0,2)){
							$(this).prev().remove();
							$(this).before(genMonth(0));
						}else if($(this).val()==new Date().getFullYear()){
							$(this).prev().remove();
							$(this).before(genMonth(1));
						}else{
							$(this).prev().remove();
							$(this).before(genMonth(2));
						}
					}
				);
				$("a[@name=submit]").click(
					function(){
						//submit form
						var form = "#"+$(this).parents("form").attr("id");
						//alert(form);
						$(form).ajaxSubmit( {url: 'sprofile.php', beforeSubmit: validate, type: 'post', success: reset} ); 
						scroll(0,10000);
						//alert(form);
					}
				);
			}
		);
	}
	function add(){
		$.post("sprofile.php",
			{add: $("input[@name=reg_no]").val() },
			function(add){//顯示add table
				$("div.add").html(add);
				showMonth();
				scroll(0,10000);
				$("select[@name=sprole]").click(
					function(){
						if($("select[@name=sprole]").val()=="7"){
							$("div.divrole").html("<input type='text' name='sproleother' size='10' />");
						}else{
							$("div.divrole").html("");
						}
					}
				);		
				$("select[@name=spyear]").change(
					function(){
						showMonth();
					}
				);
				$("input[@name=submit]").click(
					function(){
						//submit form
						
						$('#insertsp').ajaxForm( {url: 'sprofile.php', beforeSubmit: validateadd, type: 'post', success: reset} ); 
						//scroll(0,10000);
					}
				);
			}
		);
	}
	function showMonth(){
	/*
		if($("select[@name=spyear]").val()=="20"+$("input[@name=reg_no]").val().substr(0,2)){
			$("select[@name=spmonth]").remove();
			$("select[@name=spyear]").before(genMonth(0));
		}else if($("select[@name=spyear]").val()==new Date().getFullYear()){
			$("select[@name=spmonth]").remove();
			$("select[@name=spyear]").before(genMonth(1));
		}else{
			$("select[@name=spmonth]").remove();
			$("select[@name=spyear]").before(genMonth(2));
		}
		*/
		//for year 1 students only
		$("select[@name=spmonth]").remove();
		$("select[@name=spyear]").before(genMonth(2));
	}
	function genMonth(type){
		var stMonth = 1;
		var edMonth = 12;
		if(type==0){
			stMonth=9;
		}else if(type==1){
			edMonth = new Date().getMonth()+1;
		}
		var htmlMonth = "<select name='spmonth' size='1'>";
		for(var i=stMonth; i<=edMonth; i++){
			htmlMonth += "<option value="+ i + ">"+ i +"</option>";
		}
		htmlMonth += "</select>";
		return htmlMonth;
	}
	
	function validate(formData, jqForm, options) {//submit之前check下有無填晒d input
		var form = jqForm[0];
	    if (!form.spsubject.value || !form.splevel.value || !form.spyear.value || !form.sprole.value || !form.sporg.value) {
			//alert("<font color='red'>請確認填寫好全部的項目</font>");
		   $('div.submit').html("<font color='red'>請確認填寫好全部的項目</font>");
	        return false;
	    }else if(form.sprole.value=="7"){
			if(!form.sproleother.value){
				$('div.submit').html("<font color='red'>請確認填寫好全部的項目</font>");
				return false;
			}
		}
	    //alert('Both fields contain values.'); 
		//alert("SUCCESS");
	}
	function validateadd(formData, jqForm, options) {//submit之前check下有無填晒d input
		var form = jqForm[0];
	    if (!form.spsubject.value || !form.splevel.value || !form.spyear.value || !form.sprole.value || !form.sporg.value || !form.sptype.value) {
			//alert("<font color='red'>請確認填寫好全部的項目</font>");
		   $('div.submit').html("<font color='red'>請確認填寫好全部的項目</font>");
	        return false;
	    }else if(form.sprole.value=="7"){
			if(!form.sproleother.value){
				$('div.submit').html("<font color='red'>請確認填寫好全部的項目</font>");
				return false;
			}
		}
	    //alert('Both fields contain values.'); 
		//alert("SUCCESS");
	}
	function reset(formData, jqForm, options) {//submit後reset 個人資料table
		//alert(formData);
	    //$('#csinsert').resetForm(); 
		if(formData=="SUCCESS"){
			$("div.add").html("");
			$('div.submit').html("");
			pq();
		}else{
			$('div.submit').html("加入失敗");
		}
		scroll(0,10000);
	}
});

