// JavaScript Document
function ajaxLoad(method, URL, data, displayId) {
	var ajax = null;
	if(window.ActiveXObject) {		
		ajax = new ActiveXObject("Microsoft.XMLHTTP");	
	}
	else if(window.XMLHttpRequest) {		
		ajax = new XMLHttpRequest();	
	}
	else {
		alert("Your browser doesn't support Ajax");
		return;
	}

	method = method.toLowerCase();

	URL += "?dummy=" + (new Date()).getTime();
	if(method.toLowerCase()=="get") {
		URL += "&" + data;
		data = null;
	}

	ajax.open(method, URL);

	if(method.toLowerCase()=="post") {
		ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	}
	
	ajax.onreadystatechange = function() {
		if(ajax.readyState==4 && ajax.status==200) {
			var ctype = ajax.getResponseHeader("Content-Type");
 			ctype = ctype.toLowerCase();

			ajaxCallback(ctype, displayId, ajax.responseText);

			delete ajax;
			ajax = null;
		}
	}

	ajax.send(data);
}

function ajaxCallback(contentType, displayId, responseText) {
	if(contentType.match("text/javascript")) {
	
		eval(responseText);
	}
	else {
		var el = document.getElementById(displayId);
		el.innerHTML = responseText;
	}
}

function getFormData(form_name_or_id) {
	
	var frm = document.forms[form_name_or_id];
	if(frm==null) {
		alert("form not found!");
		return;
	}

	var data = "";
	var num_el = frm.elements.length;
	for(i=0; i<num_el; i++) {
		var el = frm.elements[i];
		if(el.name=="" && el.id=="") {
			continue;
		}
		var param_name = "";
		if(el.name!="") {
			param_name = el.name;
		}
		else if(el.id!="") {
			param_name = el.id;
		}

		var t = frm.elements[i].type;
		var value = "";
		if(t=="text"||t=="password"||t=="hidden"||t=="textarea") {
			value = encodeURI(el.value);
		}
		else if(t=="radio"||t=="checkbox") {
			if(el.checked) {
				value = encodeURI(el.value);
			}
			else {
				continue;
			}
		}
		else if(t=="select-one") {
			value = encodeURI(el.options[el.selectedIndex].value);
		}
		else if(t=="select-multiple") {
			for(j=0; j<el.length; j++) {
				if(el.options[j].selected) {
					if(data!="") {
						data += "&";
					}
					data += param_name + "=";
					data += encodeURI(select.options[j].value);
				}
			}
			
			continue;
		}
		
		if(data!="") {
			data += "&";
		}

		data += param_name + "=" + value;
	}

	return data;

}



/*--------------------------------------------- ajax ข่าว -------------------------------------------*/

//ดึงข้อมูล News
 function content_Load(div,id,subid,limit,page,template){
 	var URL = "./content/content_show.php";
	var data = "div="+div;
	
	if (id != undefined && id !=0){ 
		data +="&CategoryID="+id;
		if (subid != undefined && subid !=0){ data +="&SubCategoryID="+subid; }
	}
	if (limit != undefined && limit !=0){ 
		data +="&limit="+limit; 
	}
	
	if (page != undefined && page !=0){ 
		data +="&page="+page; 
	}
	
	if (template != undefined && template !=0){	
		data +="&template="+template;
	}
	
	
	ajaxLoad('get', URL, data, div);
	document.getElementById(div).innerHTML='loading....';
	
}

//ดึงข้อมูล testimonial
 function testimonial_Load(div,id,subid,limit,page,template){
 	var URL = "./content/testimonial_show.php";
	var data = "div="+div;
	
	if (id != undefined && id !=0){ 
		data +="&CategoryID="+id;
		if (subid != undefined && subid !=0){ data +="&SubCategoryID="+subid; }
	}
	if (limit != undefined && limit !=0){ 
		data +="&limit="+limit; 
	}
	
	if (page != undefined && page !=0){ 
		data +="&page="+page; 
	}
	
	if (template != undefined && template !=0){	
		data +="&template="+template;
	}
	
	
	ajaxLoad('get', URL, data, div);
	document.getElementById(div).innerHTML='loading....';
	
}

//ดึงข้อมูล Job
 function Job_Load(div,id,subid,limit,page,template,order){
 	var URL = "./content/job_index.php";
	var data = "div="+div;
	
	if (id != undefined && id !=0){ 
		data +="&CategoryID="+id;
		if (subid != undefined && subid !=0){ data +="&SubCategoryID="+subid; }
	}
	if (limit != undefined && limit !=0){ 
		data +="&limit="+limit; 
	}
	
	if (page != undefined && page !=0){ 
		data +="&page="+page; 
	}
	
	if (template != undefined && template !=0){	
		data +="&template="+template;
	}

	if (order != undefined){	
		data +="&Order="+order;
	}
	
	ajaxLoad('get', URL, data, div);
	document.getElementById(div).innerHTML='loading....';
	
}

//ดึงข้อมูล scholarship
 function scholar_Load(div,id,subid,limit,page,template){
 	var URL = "./content/scholarship_show.php";
	var data = "div="+div;
	
	if (id != undefined && id !=0){ 
		data +="&CategoryID="+id;
		if (subid != undefined && subid !=0){ data +="&SubCategoryID="+subid; }
	}
	if (limit != undefined && limit !=0){ 
		data +="&limit="+limit; 
	}
	
	if (page != undefined && page !=0){ 
		data +="&page="+page; 
	}
	
	if (template != undefined && template !=0){	
		data +="&template="+template;
	}
	
	
	ajaxLoad('get', URL, data, div);
	document.getElementById(div).innerHTML='loading....';
	
}


//ดึงข้อมูล promotion
 function promotion_Load(div,id,subid,limit,page,template){
 	var URL = "./content/content_show.php";
	var data = "div="+div;
	
	if (id != undefined && id !=0){ 
		data +="&CategoryID="+id;
		if (subid != undefined && subid !=0){ data +="&SubCategoryID="+subid; }
	}
	if (limit != undefined && limit !=0){ 
		data +="&limit="+limit; 
	}
	
	if (page != undefined && page !=0){ 
		data +="&page="+page; 
	}
	
	if (template != undefined && template !=0){	
		data +="&template="+template;
	}
	
	
	ajaxLoad('get', URL, data, div);
	document.getElementById(div).innerHTML='loading....';
	
}

function calendar_load(month,year)
{
	var URL = "calendar.php";
	var data = "";
	
	if (month != undefined)
	{ 
		data +="month="+month; 
		if (year !=undefined) { data+= "&" }
	}
	if (year != undefined)
	{
		data +="year="+year;	
	}

	ajaxLoad('get', URL, data, 'calendar');

}

function schedule_load(date)
{
	var URL = "schedule.php";
	var data = "";
	
	if (date != undefined)
	{ 
		data +="Date="+date; 
	}

	ajaxLoad('get', URL, data, 'schedule');
	document.getElementById('schedule').innerHTML='loading....';

}

function schedule_show(num,date)
{
	var URL = "schedule.php";
	var data;
		data ="num="+num;

	if (date != undefined)
	{ 
		data +="&Date="+date;
	}

	ajaxLoad('get', URL, data, 'schedule');
	document.getElementById('schedule').innerHTML='loading....';

}

