
// !!!!!!! RATING PROCESS 

	function fxRate(vkey,rate,idToHide,idToShow,vid){
		cp.call(baseurl+'ajax/myajaxphp.php','process_data',return_data,rate,vid);
		hideMe(idToHide);
		showMe(idToShow);
	}

			function return_data(restul){
				return false;
			}
// RATING PROCESS END  


// !!!!!!! My voting process 

		function fxVote(voteId)
		{
				voteAnswer=document.getElementById('opAns').value;
		
				if(voteAnswer==''){
					alert('กรุณาเลือกคำตอบ');
				}else{
						cp.call(baseurl+'/ajax/myajaxphp.php','process_Vote',return_vote_result,voteId,voteAnswer);
				}
		}
					function return_vote_result(result)
					{ var index_tr = 1;
					var xx=result.getElementsByTagName('result').item(0).firstChild.data;
					if (xx=='1'){
							count=result.getElementsByTagName('count').item(0).firstChild.data;
							for (var  ii=0; ii<count  ; ii++ ){

								 var vv='A1'+ii;
								 var pp='P1'+ii;	
								
								vv=result.getElementsByTagName(vv).item(0).firstChild.data;
								pp=result.getElementsByTagName(pp).item(0).firstChild.data;
								
								// # Generate Voring table					
								var tt=document.getElementById('tblVoteResult').insertRow(0);
									if(index_tr==1) tt.bgColor="#f4f4f4";else tt.bgColor="#dcdcdc";
								tt.align="left";
								var y=tt.insertCell(0);
								var z=tt.insertCell(1);
								var z2=tt.insertCell(2);
								y.innerHTML=vv;
								z.innerHTML='<img src="images/poll03.gif" width="'+ pp +'" height="15" />';
								z2.innerHTML=pp +'%';
								index_tr++; if(index_tr==3) index_tr=1;
								if(vv==""){
									break;
								}
							}
							//hideMe('divviewvresult');
							hideMe('tblVote');

					}
					else if(xx=='2')
					{
						//document.getElementById('err_poll').innerHTML='<font color=#B1C724 ><B>คุณได้ใช้สิทธิ์โหวตไปแล้ว</B></FONT>';
						//insertInToTable('tblVoteResult', 0,0,);
							viewVote(result.getElementsByTagName('vote_id').item(0).firstChild.data);
							//showMe('divviewvresult');
							hideMe('tblVote');
					}
				}


 // END

 // VIEW VOTE
function viewVote(pollId)
{
		cp.call(baseurl+'/ajax/myajaxphp.php','view_vote',return_view_vote,pollId);
}
		function return_view_vote(result){
			var xx;
			if (1){var index_tr = 1;
					count=result.getElementsByTagName('count').item(0).firstChild.data;
					for (var  ii=0; ii<count ; ii++ ){

						 var vv='A1'+ii;
						 var pp='P1'+ii;
						
						vv=result.getElementsByTagName(vv).item(0).firstChild.data;
						pp=result.getElementsByTagName(pp).item(0).firstChild.data;
						
						var tt=document.getElementById('tblVoteResult').insertRow(0);
							  if(index_tr==1) tt.bgColor="#f4f4f4";else tt.bgColor="#dcdcdc";
						  tt.align="left";
						  var y=tt.insertCell(0);
						  var z=tt.insertCell(1);
						  var z2=tt.insertCell(2);
						  y.innerHTML='&nbsp;&nbsp;' + vv;
						  z.innerHTML='<img src="images/poll03.gif" width="'+ pp +'" height="15" />';
						  z2.innerHTML=pp +'%';
						  index_tr++; if(index_tr==3) index_tr=1;

					
				if(vv==""){
					break;
				}
			}
			insertInToTable('tblViewVote', 0,0,'Current vote status');
		}
		
	}



// !!!!!!!! SEND COMMENT PROCESS

	function fxSendComments(idToHide,commentId,nameID,vid){
		comment_value=document.getElementById(commentId).value;
		name_value=document.getElementById(nameID).value;
		if(name_value==''){
			alert(' กรุณากรอกชื่อของท่าน !!');
		}else if(comment_value==''){
			alert(' กรุณากรอกข้อความความ !!');
		}
		else{
			//hideMe(idToHide);	
			cp.set_transfer_mode('POST');
			cp.call(baseurl+'/ajax/myajaxphp.php','process_comments',return_comment_response,comment_value,name_value,vid);
		}		
		
	}

	function return_comment_response(restul){
		
		msg_number = restul.getElementsByTagName('a').item(0).firstChild.data;
		if(msg_number==0){//showMe('divComResult2');
			//alert('คุณได้เพิ่มความคิดเห็นเข้าในเนื้อหานี้แล้ว !');
		} else if ( msg_number==1) {//showMe('divComResult1');
			//alert('ความคิดเห็นของคุณ ได้ถูกเพิ่มเข้าไปแล้ว !');
		} else if ( msg_number==3) {
			alert('ไม่สามารถใช้ tag HTML !');
		} else{//showMe('divComResult3');
			alert('ระบบไม่สามารถบันทึกรายการได้ !');
		}
		onCommentSubmitComplete();
		document.getElementById('txtName').value=document.getElementById('setName').value;
		document.getElementById('addcomment').value='';
		//var url = document.frames['fram_comment'].location;
		//document.frames['fram_comment'].location = url;
		//invoke the container to refresh
		
	}
// END

// RECENT VIEW PROCESS
	var current_position=4;
	function recentview(amount,flag){
		
		gflag="viewrecent";
		if(flag=='next')
		{		
				var start=current_position
					current_position=current_position+amount;
				var end=current_position;
				if(dbreport!='1'){

				}

			sql="SELECT VID, title, viewtime, vkey from video where viewtime<>'0000-00-00 00:00:00' order by viewtime desc limit "+start + " , " +end;  
			executeDB(sql);	
			//alert(sql);
 			if(dbreport<0)
			{
					end=current_position;
					current_position=current_position-amount;
					start=current_position;
					alert("End");

			}
		}

		if(flag=='prev')
		{
			var end=current_position;
				current_position=current_position-amount;
			var start=current_position;

			if(start<0){
				start=amount;
				end=start+amount;
				alert("End");
			}

			sql="SELECT VID, title, viewtime, vkey from video where viewtime<>'0000-00-00 00:00:00' order by viewtime desc limit "+start + " , " +end;  
			executeDB(sql);
		}
	}
//END


function pollAnsBox(myID){
	Me=document.getElementById(myID);
	if(Me.value==""){
		  Me.style.background="#3366FF";
		
	}
	else{
		
		Me.style.background="#FFFFFF";
			xy=Me.value;
		for (i=0;i<Me.value;i++ ){		
			var x=document.getElementById('tblViweAnsBox').insertRow(0);
			var y=x.insertCell(0);
			var z=x.insertCell(1);
			y.innerHTML='Answer ' + (xy-i);			
			z.innerHTML='<INPUT TYPE=text SIZE=40 NAME=voteAnsBox'+i+' ID=voteAnsBox'+i+' onBlur=txtBoxValidation(voteAnsBox'+i+', #EAEAEA,#FF0033) >';
		}
	}
	
}

// ## Delete row of a Tabile
	function delteRow(){
		var x=document.getElementById('tblViweAnsBox').rows.length-1;

		for (var i=x;i>=0;i--){
					document.getElementById('tblViweAnsBox').deleteRow(i);
			}
	}


	function fxvalidation(){
		var flag=true;
		var x=document.getElementById('tblViweAnsBox').rows.length-1;

		// ## Question text
		flag=txtBoxValidation('txtQtn','#EAEAEA','#FF0033');

		// ## Questin qty
		flag=txtBoxValidation('txtPollAnsQty','#EAEAEA','#FF0033');
		
		
		for ( i=x; i>=0; i-- )
		{
			targetID='voteAnsBox'+i;
			if (document.getElementById(targetID).value==""){
				txtBoxValidation(targetID,'#EAEAEA','#FF0033');
				flag=false;
				break;
			}
				
							
		}

		return flag;
	}
  

function fxShowAccInfo(a,b){
	showMe(a);
	hideMe(b);	
}

function fxReportVideo(hidediv, uid, vid ) {
	if ( uid == '0' ) {
		hideMe(hidediv);
		showMe('reportVideoLogin');
	} else {
		showMe('reportVideoBox');
		
	}
}

function fxFeatureVideo( uid, vid ) {
	if ( uid == '0' ) {
		hideMe('featureVideoSuccess');
		hideMe('featureVideoFailed');
		showMe('featureVideoLogin');
	} else {
		cp.call(baseurl+'/ajax/myajaxphp.php','featureVideo', featureVideoResponse, uid, vid);
	}
}

function featureVideoResponse( feature_result ) {
	var feature_response_id=feature_result.getElementsByTagName('featureVideoMessage').item(0).firstChild.data;
	if ( feature_response_id == '0' ) {
		hideMe('featureVideoFailed');
		hideMe('featureVideoLogin');
		showMe('featureVideoSuccess');
	} else {
		hideMe('featureVideoSuccess');
		hideMe('featureVideoLogin');
		showMe('featureVideoFailed');
	}
}

function fxReportVideo( uid, vid ) {
	if ( uid == '0' ) {
		hideMe('reportVideoSuccess');
		hideMe('reportVideoFailed');
		showMe('reportVideoLogin');
	} else {
		cp.call(baseurl+'/ajax/myajaxphp.php','reportVideo', reportVideoResponse, uid, vid);
	}
}

function reportVideoResponse( report_result ) {
	var report_response_id=report_result.getElementsByTagName('reportVideoMessage').item(0).firstChild.data;
	if ( report_response_id == '0' ) {
		hideMe('reportVideoFailed');
		hideMe('reportVideoLogin');
		showMe('reportVideoSuccess');
	} else {
		hideMe('reportVideoSuccess');
		hideMe('reportVideoLogin');
		showMe('reportVideoFailed');
	}
}

function fxAddFavorite( hidediv, uid, vid, vuid ) {	
	hideMe(hidediv);
	if ( uid == '0' ) {
		hideMe('addToFavLink');
		hideMe('addToFavSuccess');
		hideMe('addToFavFailed');
		hideMe('addToFavAlready');
		hideMe('addToFavOwner');
		showMe('addToFavLogin');	
	} else if ( uid == vuid ) {	
		hideMe('addToFavLink');
		hideMe('addToFavSuccess');
		hideMe('addToFavFailed');
		hideMe('addToFavAlready');
		hideMe('addToFavLogin');
		showMe('addToFavOwner');
	} else {
		cp.call(baseurl+'/ajax/myajaxphp.php','addToFavorites', addToFavoritesResponse, uid, vid, vuid);
	}
}

function addToFavoritesResponse( fav_result ) {
	var fav_response_id=fav_result.getElementsByTagName('addFavMessage').item(0).firstChild.data;
	if( fav_response_id == '2' ) {
		hideMe('addToFavLink');
		hideMe('addToFavSuccess');
		hideMe('addToFavFailed');
		hideMe('addToFavLogin');
		hideMe('addToFavOwner');
		showMe('addToFavAlready');
	} else if( fav_response_id == '0' ) {
		hideMe('addToFavLink');
		hideMe('addToFavFailed');
		hideMe('addToFavAlready');
		hideMe('addToFavLogin');
		hideMe('addToFavOwner');
		showMe('addToFavSuccess');
	} else {
		hideMe('addToFavLink');
		hideMe('addToFavSuccess');
		hideMe('addToFavAlready');
		hideMe('addToFavLogin');
		hideMe('addToFavOwner');
		showMe('addToFavFailed');
	}
}

function pollAnsBox($num){
	alert($num);
}

function showRelatedVideos()
{
    var tabRelatedVideos = document.getElementById('tabRelatedVideos');
    var tabUservideos = document.getElementById('tabUserVideos');
    tabUservideos.className = "";
    tabRelatedVideos.className = "tabactive";
    hideMe('userVideos');
    showMe('relatedVideos');
}

function showUserVideos()
{
    var tabRelatedVideos = document.getElementById('tabRelatedVideos');
    var tabUservideos = document.getElementById('tabUserVideos');
    tabRelatedVideos.className = "";
    tabUservideos.className = "tabactive";
    hideMe('relatedVideos');
    showMe('userVideos');
}

function hidesubmenu() {
	document.getElementById("menu_news").style.display='none';
	document.getElementById("menu_variety").style.display='none';
}

function gettab(id){
	for (i=1;i<=5;i++)
	{
		if (id == i){
			showMe("box_content"+i);
		}else{
			hideMe("box_content"+i);
		}
	}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function shownews(id,a)  {
	for(i=1;i<=a;i++) {	
		if (i==id) {document.getElementById('bnews_title_'+i).style.display = "none";
		document.getElementById('bnews_desc_'+i).style.display = "block";
		}
		else { document.getElementById('bnews_title_'+i).style.display = "block";
		document.getElementById('bnews_desc_'+i).style.display = "none"; }
	}
}