
// !!!!!!! 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){
				
				// Collect the number of BLUE star
			var cnt=restul.getElementsByTagName('trate').item(0).firstChild.data;				
					hideMe('idViewVoteResult');
			if(cnt!='exist')
			{
				// # Show the vote 
				var x=document.getElementById('idVoteView').rows[0].cells;
					x[1].innerHTML=restul.getElementsByTagName('tvote').item(0).firstChild.data;

				
					
					if(cnt>6)
					{
						cnt=6;
					}	
					else if (cnt<0)
					{
						cnt=0;
					}
					
					blank_star=6-cnt;

					var x=document.getElementById('tblViewResult').rows[0].cells;
					
					for (i=0;i<cnt;i++ )
					{
							x[i].innerHTML='<img src='+imgurl+'/star.gif>';
					}
					
					for (j=cnt;j<5;j++ )
					{
							x[j].innerHTML='<img src=' + imgurl+'/blank_star.gif>';
					}
			}
			else
			{
				var x=document.getElementById('voteProcessthank').innerHTML="<FONT COLOR=#FF0000 >You already vote this video</FONT>";
			}
				
				return false;
			}
// RATING PROCESS END  


// !!!!!!! My voting process 

		function fxVote(voteId)
		{
				voteAnswer=document.getElementById('opAns').value;
		
				if(voteAnswer=='')
				{
					alert('Select any one');
				}
				else
				{
						cp.call(baseurl+'/ajax/myajaxphp.php','process_Vote',return_vote_result,voteId,voteAnswer);
				}
		}
					function return_vote_result(result)
					{
					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);
								var y=tt.insertCell(0);
								var z=tt.insertCell(1);
								y.innerHTML=vv;
								z.innerHTML=pp +'%';
							
								if(vv==""){
									break;
								}
							}
							insertInToTable('tblPResult', 0,0,'Vote result');
							// # Hide the previous tale
							hideMe('divviewvresult');
							hideMe('tblVote');

					}
					else if(xx>1)
					{
						insertInToTable('tblPResult', 0,0,'<font color=#FF0000><B>Sorry you already voted..</B></FONT>');
						viewVote(xx);
							// # Hide the previous table
							//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){
					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('tblViewVoteResult').insertRow(0);
						var y=tt.insertCell(0);
						var z=tt.insertCell(1);
						y.innerHTML=vv;
						z.innerHTML=pp +'%';
					
				if(vv==""){
					break;
				}
			}
			insertInToTable('tblViewVote', 0,0,'Current vote status');
		}
		
	}



// !!!!!!!! SEND COMMENT PROCESS

	function fxSendComments(idToHide,commentId,uid,vid){
		comment_value=document.getElementById(commentId).value;
		if(comment_value==''){
			alert(' Comment box is empty !!');
		}
		else{
			hideMe(idToHide);	
			cp.call(baseurl+'/ajax/myajaxphp.php','process_comments',return_comment_response,comment_value,uid,vid);
		}		
		
	}

			function return_comment_response(restul){

				if(restul.getElementsByTagName('a').item(0).firstChild.data==0){
					showMe('divComResult2');
				}
				else{
					showMe('divComResult1');
				}
			}
// 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 pollAnsBox($num){
	alert($num);
}

function blog_ext_login(){
    var blLogin=document.blForm.blLogin.value;
    var blPass=document.blForm.blPass.value;
    var blSite=document.blForm.blType.value;
    var blWPSite=document.blForm.blTypeWordpressLocURLField.value;
    var blRemember=document.blForm.blRemember.checked;
    if(blRemember){
        Set_Cookie( 'blremember', blLogin, 30, '/', '', '' );
    }
    beginLogin();
    cp.call(baseurl+'ajax/myajaxphp.php','blog_ext_login',blog_login_return_data,blLogin,blPass,blSite,blWPSite);
}

function blog_login_return_data(res){

    if(res.getElementsByTagName('loginresult').item(0)!=null && res.getElementsByTagName('loginresult').item(0).firstChild.data!=null && res.getElementsByTagName('loginresult').item(0).firstChild.data!=undefined){
        lr=res.getElementsByTagName('loginresult').item(0).firstChild.data;
    }else{
        lr='false';
    }
    endLogin(lr);

}

function blog_ext_post(){
    var blLogin=document.blForm.blLogin.value;
    var blPass=document.blForm.blPass.value;
    var blVID=document.blForm.blVID.value;
    var blTitle=document.blForm.blTitle.value;
    var blDescription=document.blForm.blDescription.value;
    var blAutostart=document.blForm.blAutostart.checked;
    var blMyspacePersonalSection=document.blForm.blMyspacePersonalSection.value;
    if(blAutostart){
        blAutostart=1;
    }else{
        blAutostart=0;
    }
    var blPostto=0;
    if(document.blForm.blTypeMyspace1.checked){
        blPostto+=document.blForm.blTypeMyspace1.value;
    }
    if(document.blForm.blTypeMyspace2.checked){
        blPostto+=document.blForm.blTypeMyspace2.value;
    }
    
    var blSite=document.blForm.blType.value;
    var blWPSite=document.blForm.blTypeWordpressLocURLField.value;
    var blWPBlogid=document.blForm.blWordpressPostDetailsBlogidH.value;
    var blWPCatid=document.blForm.blWordpressPostDetailsCatidH.value;
    if(document.blForm.blWordpressPublish.checked){
        blWPPublish=true;
    }else{
        blWPPublish=false;
    }
    beginPost();
    cp.call(baseurl+'ajax/myajaxphp.php','blog_ext_post',blog_post_return_data,blLogin,blPass,blTitle,blDescription,blVID,blAutostart,blPostto,blSite,blMyspacePersonalSection, blWPSite, blWPBlogid, blWPCatid, blWPPublish);
}

function blog_post_return_data(res){

    if(res!=null && res.getElementsByTagName('postresult')!=null && res.getElementsByTagName('postresult').item(0)!=null && res.getElementsByTagName('postresult').item(0).firstChild.data!=null && res.getElementsByTagName('postresult').item(0).firstChild.data!=undefined){
        lr=res.getElementsByTagName('postresult').item(0).firstChild.data;
    }else{
        lr='false';
    }
    endPost(lr);

}

function blog_ext_get_categories(){
    var blLogin=document.blForm.blLogin.value;
    var blPass=document.blForm.blPass.value;
    var blSite=document.blForm.blType.value;
    var blWPSite=document.blForm.blTypeWordpressLocURLField.value;
    beginLogin();
    cp.call(baseurl+'ajax/myajaxphp.php','blog_ext_get_categories',blog_get_categories_return_data,blLogin,blPass,blSite,blWPSite);
}

function blog_get_categories_return_data(res){

    if(res.getElementsByTagName('recordcount').item(0)!=null && res.getElementsByTagName('recordcount').item(0).firstChild.data!=null && res.getElementsByTagName('recordcount').item(0).firstChild.data!=undefined){
        lr=res.getElementsByTagName('recordcount').item(0).firstChild.data;
    }else{
        lr='false';
    }
    if(lr!='false'){
        for(i=0;i<lr;i++){
            
        }
    }
    
}


function blog_ext_get_blogs(){
    var blLogin=document.blForm.blLogin.value;
    var blPass=document.blForm.blPass.value;
    var blSite=document.blForm.blType.value;
    var blWPSite=document.blForm.blTypeWordpressLocURLField.value;
    beginGettingBlogs();
    cp.call(baseurl+'ajax/myajaxphp.php','blog_ext_get_blogs',blog_get_blogs_return_data,blLogin,blPass,blSite,blWPSite);
}

function blog_get_blogs_return_data(res){

    if(res.getElementsByTagName('recordcount').item(0)!=null && res.getElementsByTagName('recordcount').item(0).firstChild.data!=null && res.getElementsByTagName('recordcount').item(0).firstChild.data!=undefined){
        lr=res.getElementsByTagName('recordcount').item(0).firstChild.data;
    }else{
        lr='false';
    }
    if(lr!='false'){
            document.getElementById('blWordpressPostDetailsBlogs').innerHTML="";
            var select1 = document.createElement("select");
            select1.setAttribute("name", "blWordpressPostDetailsBlogsSel");
            select1.setAttribute("width", "500");
            select1.setAttribute("size", "5");
            select1.setAttribute("onclick", "blog_ext_get_cats(this.value);");
            select1.onclick=function(){blog_ext_get_cats(select1.value);};
            select1.style.width=300;
        for(i=0;i<lr;i++){
            if(i==0){
                document.blForm.blWordpressPostDetailsBlogidH.value=res.getElementsByTagName('blogid'+i).item(0).firstChild.data;
            }
        	var srow = document.createElement("option");
            srow.setAttribute("value", res.getElementsByTagName('blogid'+i).item(0).firstChild.data);
            srow.appendChild(document.createTextNode(res.getElementsByTagName('blogName'+i).item(0).firstChild.data));
        	select1.appendChild(srow);	
        }
        document.getElementById('blWordpressPostDetailsBlogs').appendChild(select1);
    }
    endGettingBlogs(lr);
    blog_ext_get_cats(select1.value);
}



function blog_ext_get_cats(blogid){
    document.blForm.blWordpressPostDetailsBlogidH.value=blogid;
    var blLogin=document.blForm.blLogin.value;
    var blPass=document.blForm.blPass.value;
    var blSite=document.blForm.blType.value;
    var blWPSite=document.blForm.blTypeWordpressLocURLField.value;
    beginGettingCats();
    cp.call(baseurl+'ajax/myajaxphp.php','blog_ext_get_cats',blog_get_cats_return_data,blLogin,blPass,blSite,blWPSite,blogid);
}

function blog_get_cats_return_data(res){

    if(res.getElementsByTagName('recordcount').item(0)!=null && res.getElementsByTagName('recordcount').item(0).firstChild.data!=null && res.getElementsByTagName('recordcount').item(0).firstChild.data!=undefined){
        lr=res.getElementsByTagName('recordcount').item(0).firstChild.data;
    }else{
        lr='false';
    }
    if(lr!='false'){
            document.getElementById('blWordpressPostDetailsCats').innerHTML="";
            var select1 = document.createElement("select");
            select1.setAttribute("name", "blWordpressPostDetailsCatsSel");
            select1.setAttribute("width", "500");
            select1.setAttribute("size", "5");
            select1.setAttribute("onclick", "blog_ext_set_cat(this.value);");
            select1.onclick=function(){blog_ext_set_cat(select1.value);};
            select1.style.width=300;
        for(i=0;i<lr;i++){
            if(i==0){
                document.blForm.blWordpressPostDetailsCatidH.value=res.getElementsByTagName('categoryId'+i).item(0).firstChild.data;
            }
        	var srow = document.createElement("option");
            srow.setAttribute("value", res.getElementsByTagName('categoryId'+i).item(0).firstChild.data);
            srow.appendChild(document.createTextNode(res.getElementsByTagName('categoryName'+i).item(0).firstChild.data));
        	select1.appendChild(srow);	
        }
        document.getElementById('blWordpressPostDetailsCats').appendChild(select1);
    }
    endGettingCats(lr);
}

function blog_ext_set_cat(catgid){
    document.blForm.blWordpressPostDetailsCatidH.value=catgid;
}



function box_get_videos(typeid)
{
    
    var blUid=document.getElementById('UID').value;
	//blUid=1;
    
    var blT1Res=document.getElementById('boxVideosRelatedRes').value;
    var blT2Res=document.getElementById('boxVideosUsersRes').value;
    var blT3Res=document.getElementById('boxVideosTopRes').value;
    var blT4Res=document.getElementById('boxVideosMDRes').value;

    if(typeid==1){
        endGetVideos(1);
    }else if(typeid==2){
        if(blT2Res==0){
            beginGetVideos();
            cp.call(baseurl+'ajax/myajaxphp.php','box_get_videos',box_get_videos_return_data,typeid,20,blUid);
        }else{
            endGetVideos(2);
        }
    }else if(typeid==3){ 
        if(blT3Res==0){
            beginGetVideos();
            cp.call(baseurl+'ajax/myajaxphp.php','box_get_videos',box_get_videos_return_data,typeid,20,blUid);
        }else{
            endGetVideos(3);
        }
    }else if(typeid==4){ 
        if(blT4Res==0){
            beginGetVideos();
            cp.call(baseurl+'ajax/myajaxphp.php','box_get_videos',box_get_videos_return_data,typeid,20,blUid);
        }else{
            endGetVideos(4);
        }
    } 
         
}

function box_get_videos_return_data(res){

    var baseurl="http://www.myvideo.co.za";
    var tmburl=baseurl+"/thumb";

    if(res.getElementsByTagName('recordcount').item(0)!=null && res.getElementsByTagName('recordcount').item(0).firstChild.data!=null && res.getElementsByTagName('recordcount').item(0).firstChild.data!=undefined){
        lr=res.getElementsByTagName('recordcount').item(0).firstChild.data;
    }else{
        lr='false';
    }
    if(lr!='false'){
        
        
        //tc=res.getElementsByTagName('totalcount').item(0).firstChild.data;
        type=res.getElementsByTagName('type').item(0).firstChild.data;
        if(type==1){
            endGetVideos(1);
        }else if(type==2 || type==3 || type==4){
            
            if(type==2){
                document.getElementById('boxVideosUsers').innerHTML="";
                //document.getElementById('boxVideosCurrent2').innerHTML=lr;
                //document.getElementById('boxVideosTotal2').innerHTML=tc;
            }
            
            if(type==3){
                document.getElementById('boxVideosTop').innerHTML="";
/*
                document.getElementById('boxVideosCurrent3').innerHTML=lr;
                document.getElementById('boxVideosTotal3').innerHTML=tc;
*/
            }
            if(type==4){
                document.getElementById('boxVideosMD').innerHTML="";
            }
            
            
            vkey=document.getElementById('inp_share_viewkey').value;
            
            for(i=0;i<lr;i++){
                cvkey=res.getElementsByTagName('vkey'+i).item(0).firstChild.data;
                cstitle=res.getElementsByTagName('stitle'+i).item(0).firstChild.data;
                cvid=res.getElementsByTagName('vid'+i).item(0).firstChild.data;
                ctitle=res.getElementsByTagName('title'+i).item(0).firstChild.data;
                csuser=res.getElementsByTagName('susername'+i).item(0).firstChild.data;
                cuser=res.getElementsByTagName('username'+i).item(0).firstChild.data;
                cduration=res.getElementsByTagName('duration'+i).item(0).firstChild.data;
                cviewnumber=res.getElementsByTagName('viewnumber'+i).item(0).firstChild.data;
                ccommentcount=res.getElementsByTagName('commentcount'+i).item(0).firstChild.data;
                cuid=res.getElementsByTagName('uid'+i).item(0).firstChild.data;
                
                var div1 = document.createElement("div");
                
                if(cvkey==vkey){
                    div1.setAttribute("class", "");
                    div1.setAttribute("className", "");
                }else{
                    div1.setAttribute("class", "");
                    div1.setAttribute("className", "");
                }
                
                
				
				
				//alex's block
				var ul = document.createElement("ul");
				ul.className = "movies";
				div1.appendChild(ul);
				
				var li = document.createElement("li");
				ul.appendChild(li);
				
				//*** left ***
				var spanLeft = document.createElement("span");
				spanLeft.className = "floatLeft";
				li.appendChild(spanLeft);
				
                var aImg = document.createElement("a");
                if(cstitle!=""){
                    aImg.setAttribute("href", ""+baseurl+"/video/"+cstitle);
                }else{
                    aImg.setAttribute("href", ""+baseurl+"/view_video.php?viewkey="+cvkey);
                }
                aImg.setAttribute("target", "_parent");
                spanLeft.appendChild(aImg);
                
                var img = document.createElement("img");
                img.setAttribute("src", tmburl+"/"+cvid+".jpg");
                aImg.appendChild(img);
				
				//*** right ***
				var spanRight = document.createElement("span");
				spanRight.className = "floatRight";
				li.appendChild(spanRight);
				
				var h4 = document.createElement("h4");
				h4.appendChild(document.createTextNode(ctitle));
				spanRight.appendChild(h4);
				
                var ah4 = document.createElement("a");
                if(cstitle!=""){
                    ah4.setAttribute("href", ""+baseurl+"/video/"+cstitle);
                }else{
                    ah4.setAttribute("href", ""+baseurl+"/view_video.php?viewkey="+cvkey);
                }
                ah4.setAttribute("target", "_parent");
                h4.appendChild(ah4);
				
				var spanFrom = document.createElement("span");
				spanFrom.className = "fromUser";
				spanFrom.appendChild(document.createTextNode("from "));
				spanRight.appendChild(spanFrom);
                
				var aFrom = document.createElement("a");
                aFrom.setAttribute("target", "_parent");
                aFrom.setAttribute("href", baseurl+"/channels/"+csuser);
                aFrom.appendChild(document.createTextNode(cuser));
                spanFrom.appendChild(aFrom);
				
				var separator5 = document.createElement("span");
				separator5.className = "separator5";
				spanRight.appendChild(separator5);
				
				var spanNums1 = document.createElement("span");
				spanNums1.className = "nums";
				spanNums1.appendChild(document.createTextNode("Video Runtime: "+cduration));
				spanRight.appendChild(spanNums1);
				
				var spanNums2 = document.createElement("span");
				spanNums2.className = "nums";
				var strongViews = document.createElement("strong");
				strongViews.appendChild(document.createTextNode(cviewnumber));
				spanNums2.appendChild(strongViews);
				spanNums2.appendChild(document.createTextNode(" views"));
				spanRight.appendChild(spanNums2);
				
				var spanNums3 = document.createElement("span");
				spanNums3.className = "nums";
				var strongComments = document.createElement("strong");
				strongComments.appendChild(document.createTextNode(ccommentcount));
				spanNums3.appendChild(strongComments);
				spanNums3.appendChild(document.createTextNode(" comments"));
				spanRight.appendChild(spanNums3);
				
                if(cvkey==vkey){
                    var spanNow = document.createElement("span");
                    spanNow.className = "nowPlaying";
                    spanNow.appendChild(document.createTextNode("<<<NOW PLAYING!"));
                    spanRight.appendChild(spanNow);
                }

				var spanResults = document.createElement("div");
                spanResults.className = "seeAllResults";
				spanRight.appendChild(spanResults);
				
				
				var spanClear = document.createElement("span");
				spanClear.className = "clear";
				li.appendChild(spanClear);
				
				
				// end of alex's block
				
				
                
            if (type==2){
                var aResults = document.createElement("a");
                aResults.setAttribute("target", "_parent");
                aResults.setAttribute("href", baseurl+"/uvideos.php?UID="+cuid+"&type=public");
                aResults.appendChild(document.createTextNode("See All Results"));
                spanResults.appendChild(aResults);
                
                document.getElementById('boxVideosUsers').appendChild(div1);
            }
            
            if (type==3){
                
                var aResults = document.createElement("a");
                aResults.setAttribute("target", "_parent");
                aResults.setAttribute("href", baseurl+"/video.php?category=mv");
                aResults.appendChild(document.createTextNode("See All Results"));
                spanResults.appendChild(aResults);

				document.getElementById('boxVideosTop').appendChild(div1);
            }
                

            if (type==4){
                var aResults = document.createElement("a");
                aResults.setAttribute("target", "_parent");
                aResults.setAttribute("href", baseurl+"/video.php?category=md");
                aResults.appendChild(document.createTextNode("See All Results"));
                spanResults.appendChild(aResults);

				document.getElementById('boxVideosMD').appendChild(div1);
            }
                

                
            }
            


				if (type==2){
            
                document.getElementById('boxVideosUsersRes').value=1;
                endGetVideos(2);
            }
            
            if (type==3){                
            
                document.getElementById('boxVideosTopRes').value=1;
                endGetVideos(3);
                
            }
            if (type==4){                
            
                document.getElementById('boxVideosMDRes').value=1;
                endGetVideos(4);
                
            }
           
            
        }
    }
}
