//profile extra photos modificaiton
function fxRemoveExtraPhoto(idToReplace,UPID){
	cp.call(baseurl+'/ajax/myajaxphp.php','remove_extra_photo',"",UPID);
	document.getElementById(idToReplace).innerHTML = "Photo supprimer";

}

// !!!!!!! RATING PROCESS 
function fxRate(vkey,rate,idToHide,idToShow,vid){
	cp.call('/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 >Vidéo déjà voté</FONT>";
			}
				
				return false;
			}
			
//THUMBS DOWN / THUMBS DOWN
function fxLiked(idToHide,idToShow,vid, uid, vote){
	//alert(idToHide + " \n" + idToShow + " \n" + vid + " \n" + uid + " \n" + vote);
	cp.call('/ajax/myajaxphp.php','process_thumbs',return_data_like,vote,vid, uid);
	hideMe(idToHide);
	showMe(idToShow);
}

function return_data_like(restul){
				
				// Collect the number of BLUE star
			var exists=restul.getElementsByTagName('exists').item(0).firstChild.data;
	
			if (exists == "no"){
				var votes=restul.getElementsByTagName('likedIt').item(0).firstChild.data;
				var total=restul.getElementsByTagName('total').item(0).firstChild.data;
				var percent = (votes * 100) / total;
				
				var x=document.getElementById('likeProcessThanks').innerHTML="" + percent.toFixed(0) + "%";
			}else
				var x=document.getElementById('likeProcessThanks').innerHTML="<div style'font-size:12px; margin-bottom:10px; font-weight:bolder;'>Vidéo déjà voté</div>";
		/*			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,uid,vid){
	comment_type=document.getElementById('commentType').value;
	error = 0;
	pts = 1;
	if (comment_type == "text"){
		comment_value = addslashes(htmlentities( document.getElementById('txtComments').value, 'ENT_NOQUOTES', 'ISO8859-15'));
		if(comment_value=='')		error = 1;
	} else if (comment_type == "image"){
		img_url=document.getElementById('imageUrl').value;
		img_comment=document.getElementById('imageDesc').value
		img_width = getImgSize(document.getElementById('imageUrl').value);
		if (img_width > 340)	img_width = '340';

		if (!checkUrl(document.getElementById('imageUrl')))		error = 3;
		else if (img_url=='' || img_comment =='')		error = 1;
		else	comment_value = '<img src="' +img_url + '" width="' + img_width +'" /><br>' + addslashes(htmlentities( img_comment, 'ENT_NOQUOTES', 'ISO8859-15'));
		
	} else if (comment_type == "link"){
		link_url=document.getElementById('lienUrl').value;
		link_comment=document.getElementById('lienDesc').value
		if (!checkUrl(document.getElementById('lienUrl')))		error = 3;
		else if (link_url=='' || link_comment =='')		error = 1;
		else	comment_value = '<a href="' +link_url + '" target="_blank" />'+link_url+'</a><br>' + addslashes(htmlentities( link_comment, 'ENT_NOQUOTES', 'ISO8859-15'));
		
	} else if (comment_type == "video"){
		pts = 2;
		video_url=str_replace('youtube.com/watch?v=' , 'youtube.com/v/' , document.getElementById('videoUrl').value)
		video_comment=document.getElementById('videoDesc').value
		if (video_url=='' || video_comment =='')		error = 1;
		else if (video_url == document.getElementById('videoUrl').value)	error = 2;	
		else		comment_value = '<object width="320" height="265"><param name="movie" value="' + video_url + '&hl=en&fs=1&rel=0&color1=0xcc2550&color2=0xe87a9f"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="' + video_url + '&hl=en&fs=1&rel=0&color1=0xcc2550&color2=0xe87a9f" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="320" height="265"></embed></object><br>' + addslashes(htmlentities( video_comment, 'ENT_NOQUOTES', 'ISO8859-15'));
	}
		
	if (error == 1)		alert('Comment box is empty !!');
	else if (error == 2)		alert("Video YouTube Invalide");
	else if (error == 3)		alert("Lien invalide");
	else{
		hideMe(idToHide);	
		document.getElementById('newCommentMsg').innerHTML = comment_value;
		cp.call(baseurl+'/ajax/myajaxphp.php','process_comments_new',return_comment_response2,comment_type, comment_value,'',uid,vid, pts);
	}
}

function return_comment_response2(restul){

				if(restul.getElementsByTagName('a').item(0).firstChild.data==0){
					showMe('divComResult2');
				}
				else{
					showMe('divComResult1');
					fxLastComment();
				}
			}

function return_comment_response(restul){

				if(restul.getElementsByTagName('a').item(0).firstChild.data==0){
					showMe('divComResult2');
				}
				else{
					showMe('divComResult1');
				}
			}

function checkUrl(theUrl){
  if(theUrl.value.match(/^(http|ftp)\:\/\/\w+([\.\-]\w+)*\.\w{2,4}(\:\d+)*([\/\.\-\?\&\%\#]\w+)*\/?$/i) ||
     theUrl.value.match(/^mailto\:\w+([\.\-]\w+)*\@\w+([\.\-]\w+)*\.\w{2,4}$/i)){
    return true;
  } else {
    alert("Wrong address.");
    theUrl.select();
    theUrl.focus();
    return false;
  }
}

function getImgSize(imgSrc){
	var newImg = new Image();
	newImg.src = imgSrc;
	var width = newImg.width;
	return width;
}

function fxSendCommentsNew(idToHide,uid,vid){
	comment_type=document.getElementById('commentType').value;
	error = 0;
	pts = 1;
	if (comment_type == "text"){
		comment_value = addslashes(htmlentities( document.getElementById('txtComments').value, 'ENT_NOQUOTES', 'ISO8859-15'));
		if(comment_value=='')		error = 1;
	} else if (comment_type == "image"){
		img_url=document.getElementById('imageUrl').value;
		img_comment=document.getElementById('imageDesc').value
		img_width = getImgSize(document.getElementById('imageUrl').value);
		if (img_width > 340)	img_width = '340';

		if (!checkUrl(document.getElementById('imageUrl')))		error = 3;
		else if (img_url=='' || img_comment =='')		error = 1;
		else	comment_value = '<img src="' +img_url + '" width="' + img_width +'" /><br>' + addslashes(htmlentities( img_comment, 'ENT_NOQUOTES', 'ISO8859-15'));
		
	} else if (comment_type == "link"){
		link_url=document.getElementById('lienUrl').value;
		link_comment=document.getElementById('lienDesc').value
		if (!checkUrl(document.getElementById('lienUrl')))		error = 3;
		else if (link_url=='' || link_comment =='')		error = 1;
		else	comment_value = '<a href="' +link_url + '" target="_blank" />'+link_url+'</a><br>' + addslashes(htmlentities( link_comment, 'ENT_NOQUOTES', 'ISO8859-15'));
		
	} else if (comment_type == "video"){
		pts = 2;
		video_url=str_replace('youtube.com/watch?v=' , 'youtube.com/v/' , document.getElementById('videoUrl').value)
		video_comment=document.getElementById('videoDesc').value
		if (video_url=='' || video_comment =='')		error = 1;
		else if (video_url == document.getElementById('videoUrl').value)	error = 2;	
		else		comment_value = '<object width="320" height="265"><param name="movie" value="' + video_url + '&hl=en&fs=1&rel=0&color1=0xcc2550&color2=0xe87a9f"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="' + video_url + '&hl=en&fs=1&rel=0&color1=0xcc2550&color2=0xe87a9f" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="320" height="265"></embed></object><br>' + addslashes(htmlentities( video_comment, 'ENT_NOQUOTES', 'ISO8859-15'));
	}
		
	if (error == 1)		alert('Comment box is empty !!');
	else if (error == 2)		alert("Video YouTube Invalide");
	else if (error == 3)		alert("Lien invalide");
	else{
		hideMe(idToHide);	
		cp.call(baseurl+'/ajax/myajaxphp.php','process_comments_new',return_comment_response,comment_type, comment_value,'',uid,vid, pts);
	}
}

function fxSendCommentsPhoto(idToHide,uid,gid,photo){
	comment_type=document.getElementById('commentType').value;
	error = 0;
	pts = 1;
	if (comment_type == "text"){
		comment_value = addslashes(htmlentities( document.getElementById('txtComments').value, 'ENT_NOQUOTES', 'ISO8859-15'));
		if(comment_value=='')		error = 1;
	} else if (comment_type == "image"){
		img_url=document.getElementById('imageUrl').value;
		img_comment=document.getElementById('imageDesc').value
		img_width = getImgSize(document.getElementById('imageUrl').value);
		if (img_width > 340)	img_width = '340';

		if (!checkUrl(document.getElementById('imageUrl')))		error = 3;
		else if (img_url=='' || img_comment =='')		error = 1;
		else	comment_value = '<img src="' +img_url + '" width="' + img_width +'" /><br>' + addslashes(htmlentities( img_comment, 'ENT_NOQUOTES', 'ISO8859-15'));
		
	} else if (comment_type == "link"){
		link_url=document.getElementById('lienUrl').value;
		link_comment=document.getElementById('lienDesc').value
		if (!checkUrl(document.getElementById('lienUrl')))		error = 3;
		else if (link_url=='' || link_comment =='')		error = 1;
		else	comment_value = '<a href="' +link_url + '" target="_blank" />'+link_url+'</a><br>' + addslashes(htmlentities( link_comment, 'ENT_NOQUOTES', 'ISO8859-15'));
		
	} else if (comment_type == "video"){
		pts = 2;
		video_url=str_replace('youtube.com/watch?v=' , 'youtube.com/v/' , document.getElementById('videoUrl').value)
		video_comment=document.getElementById('videoDesc').value
		if (video_url=='' || video_comment =='')		error = 1;
		else if (video_url == document.getElementById('videoUrl').value)	error = 2;	
		else		comment_value = '<object width="320" height="265"><param name="movie" value="' + video_url + '&hl=en&fs=1&rel=0&color1=0xcc2550&color2=0xe87a9f"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="' + video_url + '&hl=en&fs=1&rel=0&color1=0xcc2550&color2=0xe87a9f" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="320" height="265"></embed></object><br>' + addslashes(htmlentities( video_comment, 'ENT_NOQUOTES', 'ISO8859-15'));
	}
		
	if (error == 1)		alert('Comment box is empty !!');
	else if (error == 2)		alert("Video YouTube Invalide");
	else if (error == 3)		alert("Lien invalide");
	else{
		hideMe(idToHide);	
		cp.call(baseurl+'/ajax/myajaxphp.php','process_comments_photo',return_comment_response,comment_type, comment_value,'',uid,gid,photo, pts);
	}
}

function return_comment_response(restul){

	if(restul.getElementsByTagName('a').item(0).firstChild.data==0){
		showMe('divComResult2');
	}
	else{
		showMe('divComResult1');
	}
}
function fxLastComment(){
	cp.call(baseurl+'/ajax/myajaxphp.php','get_last_comment',return_last_comment, document.getElementById('hiddenVID').value);
}

function return_last_comment(restul){
	document.getElementById('newCommentUserUrlPic').href = "http://www.bombe.tv/user/" + restul.getElementsByTagName('username').item(0).firstChild.data;
	document.getElementById('newCommentUserUrlName').href = "http://www.bombe.tv/user/" + restul.getElementsByTagName('username').item(0).firstChild.data;
	document.getElementById('newCommentUserUrlName').innerHTML = restul.getElementsByTagName('username').item(0).firstChild.data;
	document.getElementById('newCommentUserImg').src = "http://www.bombe.tv/photo/" + restul.getElementsByTagName('userImg').item(0).firstChild.data;
	
	document.getElementById('commentCounter').innerHTML = parseInt(document.getElementById('commentCounter').innerHTML) + 1;
	

	showTabData('featured');
	showMe('NewComment');
}

// END

// LOGIN IN VIEW VIDEO
function fxLogin(){
	document.getElementById('divBadLogin').innerHTML = "<p>Vérification en cours...</p>";
	
	loginname = document.getElementById('loginname').value;
	password = document.getElementById('password').value;
	
	if (loginname == "") alert("Nom d'utilisateur/ Mot de passe est vide");
	else if ( password == "") alert("Nom d'utilisateur/ Mot de passe est vide");
	else{
		cp.call(baseurl+'/ajax/myajaxphp.php','process_video_login',return_login_response,loginname, password);
	}
}

function return_login_response(restul){
	if(restul.getElementsByTagName('a').item(0).firstChild.data=="bad"){
		document.getElementById('divBadLogin').innerHTML = "<p><strong>Connexion : Nom d'utilisateur et mot de passe invalide</strong></p>";
	}	else{ 
		document.getElementById('hiddenUID').value = restul.getElementsByTagName('a').item(0).firstChild.data;
		
		hideMe('signup');	
		showMe('divAllowToAddComment');
		hideMe('divVoteGuest');
		showMe('divVoteUser');
		
		document.getElementById("divUserProfil").innerHTML = "Salut, " + restul.getElementsByTagName('u').item(0).firstChild.data;
		document.getElementById("divUserProfil").href = "http://www.bombe.tv/mon_profil";
		
		document.getElementById("divUserLogin").innerHTML = "Déconnexion";
		document.getElementById("divUserLogin").href = "http://www.bombe.tv/logout.php";

	}
}
//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 validateConcours(){
	error = "";
	if (document.getElementById('fname').value == "")		error += "Prénom est vide\n";
	if (document.getElementById('lname').value == "")		error += "Nom est vide\n";
	if (document.getElementById('email').value == "")		error += "Email est vide\n";
	else if(isValidEmail(document.getElementById('email').value)==false)		error += "Email n'est pas valide\n";

	if (error == "") return true;
	else{
		alert("SVP corrigé les erreurs suivant \n" + error);
		return false;
	}
}
	
function isValidEmail(str) {
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true					
	}