$jq(function(){
	var search_type = 'choices';
	var options = { 
        //target:        '#output1',   // target element(s) to be updated with server response 
		//dataType:  "text",
        beforeSubmit:  showRequest,  // pre-submit callback 
        success:       showResponse  // post-submit callback 
		
        // other available options: 
        //url:       url         // override for form's 'action' attribute 
        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
        //clearForm: true        // clear all form fields after successful submit 
        //resetForm: true        // reset the form after successful submit 
 
        // $.ajax options can be used here too, for example: 
        //timeout:   3000 
    }; 
	$jq('#search_form').ajaxForm(options); 
	
	$jq('#search_school_button').click(function(){
		var type = $jq('#school_type').val();
		var city = $jq('#city').val();
		var year = $jq('#year').val();
		var keyword = $jq('#school_keyword').val();
		var msg = '';
		if(search_type == 'choices'){
			if(type == '0') msg += "請選擇學校類型\n";
			if(city == '0') msg += "請選擇縣市\n";
			if(msg != '') alert(msg);
			else{
				$jq('#search_form').submit();
			}
		}
		else if(search_type == 'keyword'){
			if(keyword == '') alert('請輸入學校關鍵字');
			else $jq('#search_form').submit();
		}
		return false;
	});	
	
	$jq('#search_keyword').toggle(
		function(event){
			search_type = 'keyword';
			$jq('#school_keyword_div').fadeIn("slow");
			$jq('#type_div').fadeOut("slow");
			$jq('#city_div').fadeOut("slow");
			$jq('#year_div').fadeOut("slow");
			
			$jq('#search_classes_block').fadeOut("slow");
			$jq('#search_classes_list').fadeOut("slow");
		},
		function(event){
			search_type = 'choices';
			$jq('#school_keyword_div').fadeOut("slow");
			$jq('#type_div').fadeIn("slow");
			$jq('#city_div').fadeIn("slow");
			$jq('#year_div').fadeIn("slow");
			$jq('#search_form').resetForm();
			
			$jq('#search_classes_block').fadeOut("slow");
			$jq('#search_classes_list').fadeOut("slow");
		}
	);

});

function showRequest(formData, jqForm, options) { 
    var queryString = $jq.param(formData); 
    //alert('About to submit: \n\n' + queryString); 
    return true; 
} 
function showResponse(responseText, statusText)  { 
    //alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + 
   //   '\n\nThe output div should have already been updated with the responseText.'); 
	var host = window.location.href.split(".");
	if(responseText != 'null'){
		var myObject = JSON.parse(responseText);
		var data_length = myObject.search_data.length;
		var data = '';
		for(i=0; i<data_length; i++){
			data += '<img src="../images/icons/icon_arrow.gif" align="absmiddle"><a href="'+host[0]+'.class.uschoolnet.com/class/?csid='+myObject.search_data[i]['class_active_siteid']+'" target="_blank">'+myObject.search_data[i]['class_title']+'</a><br/><br/>';
		}
		$jq('#search_classes_block').hide();
		$jq('#search_classes_list').hide();
		$jq('#search_classes_list').html(data);
		$jq('#search_classes_block').fadeIn("slow");
		$jq('#search_classes_list').fadeIn("slow");
	}
	else{
		alert('查無資料');
		$jq('#search_classes_block').fadeOut("slow");
		$jq('#search_classes_list').fadeOut("slow");
	}
	
	
	
	
} 
