// JavaScript Document

var ran;
//===================Function สำหรับ เมื่อ add ข้อมูลแบบ Post สำเร็จแล้ว============================
function afterQuery(theframe,linkurl,text,displayid,displayid2){
	
	theframe.afterQuery2(linkurl,text,displayid,displayid2);

}

function afterQuery2(linkurl,text,displayid,displayid2){
	$(displayid).innerHTML="<center><br><br><br><font size=3px>"+text+"</font><br></center>";
	setTimeout("afterQuery3('"+linkurl+"','"+displayid+"','"+displayid2+"')",1500);
}
	
function afterQuery3(linkurl,displayid,displayid2){
	ran=Math.random();
		 if (linkurl.indexOf('?',0)==-1){
			var url=linkurl+"?ran="+ran;
		 }else{
			var url=linkurl+"&ran="+ran;
		 }
	new Ajax.Request(url,
					 {method : "GET",
					 onComplete:function afterQuery4(sq){
						 if (displayid2 ==''){
						 $(displayid).innerHTML=sq.responseText;
						 }else{
						 $(displayid2).innerHTML=sq.responseText; 
						 }
					 }
					 }
					 );
}


//===============สร้าง List Menu============================
	var nCat;
	var nCat2;
		function selectCat(linkurl,catcase,nextcat,value1){
			nCat=nextcat;
			ran=Math.random();
			var url=linkurl+"?id="+value1+"&cat="+catcase+"&ran="+ran;
			new Ajax.Request(url,
							 {method:"GET",
							 onComplete:selectCat2
							 }
							 );
		} 
		function clearCatList(){
			while ($(nCat).childNodes.length>0){
				$(nCat).removeChild($(nCat).childNodes[0]);
			}
		}
		
		function selectCat2(sMain){
		clearCatList();
		var addOption=sMain.responseText;
			var option=null;
			p=addOption.split(",");
				option=document.createElement("option");
				option.appendChild(document.createTextNode("กรุณาเลือก"));
				$(nCat).appendChild(option);
			for (var i=0;i<(p.length-1);i++){
				option=document.createElement("option");
				option.appendChild(document.createTextNode(p[i]));
				$(nCat).appendChild(option);
			}
		}
		
		function check(linkurl,catcase,nextcat,value1){
			nCat2=nextcat;
			ran=Math.random();
			var url=linkurl+"?id="+value1+"&cat="+catcase+"&ran="+ran;
		new Ajax.Request(url,
							 {method:"GET",
							 onComplete:check2
							 }
							 );
		} 
		
		function check2(cm){
			var row=cm.responseText;
				if (row!=0){
									$(nCat2).style.display='';
								 }else{
									 $(nCat2).style.display='none';
				
				}
		}
		

		//=================function เปลี่ยนหน้า=============================
		function changeRunPage(linkurl,npage,displayid){
				ran=Math.random();
				 if (linkurl.indexOf('?',0)==-1){
				 var url=linkurl+"?page="+npage+"&ran="+ran;
			}else{
				var url=linkurl+"&page="+npage+"&ran="+ran;
			}

				new Ajax.Request(url,
								 {method: "GET",
								 onComplete:function change1(ch){
									 $(displayid).innerHTML=ch.responseText;
								 }
								 }
								 );
		}
		
		//=================function โชว์รูป เมื่อ Click หรือ Mouse Over 
		// อธิบายการใช้งาน     showPicTime(element,ลิงค์ที่จะส่งข้อมูลไป,id ของพื้นที่ๆจะใส่ข้อมูล,ความกว้าง,กำหนดว่า จะใช้งาน scrolls หรือไม่ ถ้าค่าเป็น 1 คือไม่แสดง scrolls,กำหนดตำแหน่งของข้อมูลหรือไม่ ถ้าค่าเป็น 1 ก็จะกำหนดตาม ค่า sx,sy แต่ถ้าค่าไม่เท่ากับ 1 จะแสดงค่าตาม ตำแหน่ง Mouse,ค่าที่จะกำหนดในแกน X,ค่าที่จะกำหนดใน แกนY)========================
		
		function showPic(element,linkurl,displayid,width,scrolls,position,sx,sy){ //ถ้า ค่า sx และ sy ไม่ได้ใส่มา ค่า position ก็ต้องไม่ใส่ ไม่งั้น Error
			var idt=element.id;
			var y=Event.pointerY(event);
			var x=Event.pointerX(event);
			Event.observe(idt,"click",showPic1(linkurl,displayid,width,x,y,scrolls,position,sx,sy));
		}
		
		function showPic1(linkurl,displayid,width,x,y,scrolls,position,sx,sy){						
			ran=Math.random();
			 if (linkurl.indexOf('?',0)==-1){
				 var url=linkurl+"?ran="+ran;
			}else{
				var url=linkurl+"&ran="+ran;
			}
			new Ajax.Request(url,
							 {method :"GET",
							 onComplete : function showPic2(sp){
								 $(displayid).innerHTML='';
								 $(displayid).style.display='';
								 if (scrolls!=1){
								 $(displayid).style.overflow='scroll';
								 }
								 $(displayid).style.position='absolute';
								 $(displayid).style.width=width+"px";
								 if (position!=1){
								 $(displayid).style.top=y+"px";
								 $(displayid).style.left=x+"px";
								 }else{
								 $(displayid).style.top=(y+sy)+"px";
								 $(displayid).style.left=(x+sx)+"px";
								 }
								 $(displayid).style.background="#ffffff";
								 $(displayid).style.border="2px solid #009900";
							 	 $(displayid).style.fontSize="small";
								 $(displayid).style.padding=5+"px";
								 $(displayid).style.filter="alpha(opacity=95)";
								 $(displayid).innerHTML=sp.responseText;
							 }
							 }
							 );
		}

	//=================function โชว์รูป เมื่อ Click หรือ Mouse Over แบบกำหนดเวลาปิด อัตโนมัติ 
	// อธิบายการใช้งาน     showPicTime(element,ลิงค์ที่จะส่งข้อมูลไป,id ของพื้นที่ๆจะใส่ข้อมูล,ระยะเวลาในการDelay,ความกว้าง,กำหนดว่า จะใช้งาน scrolls หรือไม่ ถ้าค่าเป็น 1 คือไม่แสดง scrolls,กำหนดตำแหน่งของข้อมูลหรือไม่ ถ้าค่าเป็น 1 ก็จะกำหนดตาม ค่า sx,sy แต่ถ้าค่าไม่เท่ากับ 1 จะแสดงค่าตาม ตำแหน่ง Mouse,ค่าที่จะกำหนดในแกน X,ค่าที่จะกำหนดใน แกนY)========================
		
		function showPicTime(element,linkurl,displayid,sec,width,scrolls,position,sx,sy){ //ถ้า ค่า sx และ sy ไม่ได้ใส่มา ค่า position ก็ต้องไม่ใส่ ไม่งั้น Error
			var idt=element.id;
			var y=Event.pointerY(event);
			var x=Event.pointerX(event);
			Event.observe(idt,"click",showPicTime1(linkurl,displayid,sec,width,x,y,scrolls,position,sx,sy));
		}
		
		function showPicTime1(linkurl,displayid,sec,width,x,y,scrolls,position,sx,sy){		
			ran=Math.random();
			 if (linkurl.indexOf('?',0)==-1){
				 var url=linkurl+"?ran="+ran;
			}else{
				var url=linkurl+"&ran="+ran;
			}
			new Ajax.Request(url,
							 {method :"GET",
							 onComplete : function showPicTime2(sp){
								 $(displayid).innerHTML='';
								 $(displayid).style.display='';
								 if (scrolls!=1){
								 $(displayid).style.overflow='scroll';
								 }
								 $(displayid).style.position='absolute';
								 $(displayid).style.width=width+"px";
								 if (position!=1){
								 $(displayid).style.top=y+"px";
								 $(displayid).style.left=x+"px";
								 }else{
								 $(displayid).style.top=(y+sy)+"px";
								 $(displayid).style.left=(x+sx)+"px";
								 }
								 $(displayid).style.background="#ffffff";
								 $(displayid).style.border="2px solid #009900";
							 	 $(displayid).style.fontSize="small";
								 $(displayid).style.padding=5+"px";
								 $(displayid).style.filter="alpha(opacity=95)";
								 $(displayid).innerHTML=sp.responseText;
								 setTimeout("closePic('"+displayid+"')",sec);
							 }
							 }
							 );
		}
		
		
		//===================function ปิด การโชว์รูป=========================
		
		function closePic(displayid) {
			$(displayid).innerHTML='';
			$(displayid).style.display='none';
		}
			
		
		//====================function ใช้งาน Ajax ทั่วๆไป============================
		
		function general(linkurl,displayid){
			ran=Math.random();
			if (linkurl.indexOf('?',0)==-1){
				 var url=linkurl+"?ran="+ran;
			}else{
				var url=linkurl+"&ran="+ran;
			}
			new Ajax.Request(url,
							 {method : "GET",
							 onComplete:function general2(sp){
								 $(displayid).innerHTML=sp.responseText;
							 }
							 }
							 );
			
		}
		
		//====================function ใช้งาน Ajax ทั่วๆไปแบบกำหนด Timeout============================
		
		function generalTime(linkurl,displayid,sec,finishlink){
			ran=Math.random();
			if (linkurl.indexOf('?',0)==-1){
				 var url=linkurl+"?ran="+ran;
			}else{
				var url=linkurl+"&ran="+ran;
			}
			new Ajax.Request(url,
							 {method : "GET",
							 onComplete:function generalTime2(sp){
								 $(displayid).innerHTML=sp.responseText;
								 setTimeout("general('"+finishlink+"','"+displayid+"')",sec);
							 }
							 }
							 );
			
		}
		
		//=======================เรียก News มาแสดง แล้วเปลี่ยนข้อมูลทุกๆ 10 วินาที===================================
		
		function callNews(linkurl,displayid,sec,page,total){
			ran=Math.random();
			if (linkurl.indexOf('?',0)==-1){
				 var url=linkurl+"?page="+page+"&ran="+ran;
			}else{
				var url=linkurl+"&page="+page+"&ran="+ran;
			}
			new Ajax.Request(url,
							 {method:"GET",
							 onComplete:function callNews1(cn){
								 $(displayid).innerHTML=cn.responseText;
								  if (page<total){
									 page=page+1;
								 }else{
									 page=1;
								 }
								 setTimeout("callNews('"+linkurl+"','"+displayid+"','"+sec+"',"+page+",'"+total+"')",sec);
							 }
							 }
							 );
		}
								 
