// Removes leading whitespaces
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/g;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/g;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value ) {
	
	return LTrim(RTrim(value));
	
}

	function sendEmails(){
		var x=document.getElementById('send_result').rows[0].cells;
		var url = document.getElementsByName("video_link");
		var video_url;
		if (url[0] != null) 
			video_url=encodeURI(url[0].defaultValue);
		else
			video_url="SHARE_THE_REWARD";//will use this as a flag in myajaxphp.php
		var me = document.getElementsByName("myemail");
		var myname = document.getElementsByName("myname");
		var friends=document.getElementsByName("friends");
		var friendAddresses="";
		var message= document.getElementsByName("my_msg");
		for (var i = 0; i < friends.length ; i++)
		{
			if(friends[i]!=null)
				if(friends[i].value!=null)
				{
					afriend=friends[i].value;
					afriend=trim(afriend);
					if(afriend!="")
					{
						var lastIndex=friendAddresses.lastIndexOf(';');
						if(lastIndex>=(friendAddresses.length-2))
							friendAddresses=friendAddresses+encodeURI(afriend);
						else
							friendAddresses=friendAddresses+";"+encodeURI(afriend);
					}
				}
		}
		var myemail = "";
		if(me[0]!=null)
			myemail = encodeURI(me[0].value);
		if(myemail==null||myemail=="")
		{
			x[0].innerHTML="<font color=red><b>Please enter your OWN Email first.</b></font>";
			return;
		}
		if(myname[0]!=null)
			myemail = "\""+encodeURI(myname[0].value)+"\" <"+myemail+">";
		if(friendAddresses==null||friendAddresses=="")
		{
			x[0].innerHTML="<font color=red><b>Please enter at least ONE friend email address.</b></font>";
			return;
		}
		var msg=""
		if(message[0]!=null)
		{
			msg=message[0].value;
			msg = encodeURI(msg);
		}
//		alert(myemail);
//		alert(friendAddresses);
		x[0].innerHTML='<b>Sending, please wait ... </b><br><img src="http://us.i1.yimg.com/us.yimg.com/i/us/per/gr/gp/rel_interstitial_loading.gif">';
	//	alert(video_url);
		cp.call(baseurl+'/ajax/myajaxphp.php','sendEmails', sendResult, video_url, myemail, friendAddresses, msg);
//		hideMe("email_addresses");
	}
/*	function sendResult(result, textresult){
		var x=document.getElementById('send_result').rows[0].cells;
		var send_result=result.getElementsByTagName('send_result').item(0).firstChild.data;
		if(send_result!="Email Sent Successfully")
			send_result="<font color=red size='+1'><b>"+send_result+"</b></font>";
		else
			send_result="<font color=green size='+1'><b>"+send_result+"</b></font>";
		x[0].innerHTML=send_result;

	}*/
	function sendResult(result, textresult){
		var x=document.getElementById('send_result').rows[0].cells;
		var send_result=result.getElementsByTagName('send_result').item(0).firstChild.data;
		if(send_result!="Email Sent Successfully")
			send_result="<tr><td><font color=red size='+1'><b>"+send_result+"</b></font></td></tr>";
		else
		{
			send_result="";
			switchTwoDiv('emailafriend','send_success');
		}
		x[0].innerHTML=send_result;

	}
// !!!!!!! 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;

                              var rate=restul.getElementsByTagName('rate').item(0).firstChild.data;
			
					if(cnt>5)
					{
						cnt=5;
					}	
					else if (cnt<0)
					{
						cnt=0;
					}
					
					blank_star=5-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++ )
					{
                                                if((rate-cnt*2)==1&&j==cnt)
                                                        x[j].innerHTML='<img src=' + imgurl+'/half_star.gif>';
                                                else
							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');
                                                        window.location.reload();
					}
					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);
}
