var xmlHttp;
var xmlHttp2
var url = document.location.host;
var URL = "http://"+url+"/";

//-------------generating xmlHttp object
function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	 {
		 // Firefox, Opera 8.0+, Safari
		 xmlHttp=new XMLHttpRequest();
	 }
	catch (e)
	 {
	 //Internet Explorer
		 try
		  {
			  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		  }
		 catch (e)
		  {
			  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		  }
	 }
	return xmlHttp;
}
//-----------------getting left side news for main page
function Do_UserLogin(userEmail,userPass,remember,Option){ 
 	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	 {
		 alert ("Browser does not support HTTP Request")
		 return
	 }	 
	var url=web_url+"users.php";
	url=url+"?userEmail="+userEmail;	
	url=url+"&userPass="+userPass;	
	url=url+"&remember="+remember;	
	url=url+"&action="+Option;	
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange = handleResultsLogin; 
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	}
	function handleResultsLogin(){
		if(xmlHttp.readyState == 4){
		if( xmlHttp.responseText=='1')
		{
			window.location.href=web_url+'login';
			//document.getElementById('errorloc').innerHTML = "";			
		}
		else if( xmlHttp.responseText=='2')
		{
			url=web_url+'home';
			window.location.href=url;
			//document.getElementById('errorloc').innerHTML = "";			
		}	
		}
}//function
//-----------------getting left side news for main page
function Do_UserRegister(email,dob,gender,l_gender,country,zip_code,Option){ 
 	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	 {
		 alert ("Browser does not support HTTP Request")
		 return
	 }	 
	var url=web_url+"users.php";
	url=url+"?register_mail="+email;	
	url=url+"&udob="+dob;	
	url=url+"&gender="+gender;	
	url=url+"&udob="+dob;	
	url=url+"&l_gender="+l_gender;	
	url=url+"&countries="+country;	
	url=url+"&zip_code="+zip_code;	
	url=url+"&action="+Option;	
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange = handleResultsRegister; 
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	}
	function handleResultsRegister(){
		if(xmlHttp.readyState == 4){ 
		var response = xmlHttp.responseText;
		if(response==1)
		{
			window.location.href=web_url+'step1';
			//document.getElementById('errorloc').innerHTML = "";			
		}
		else if(response==2)
		{
			window.location.href=web_url+'step2';
			//document.getElementById('errorloc').innerHTML = "";			
		}	
		}
}//function
//=================
function Del_Pictures(IDs)
{	
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	 {
		 alert ("Browser does not support HTTP Request")
		 return
	 }	 	
	var url=web_url+"pictures/del_pics.php";
	url=url+"?IDs="+IDs;
	url=url+"&option=Del_Friend";
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=stateChanged_Del_Pictures;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
function stateChanged_Del_Pictures()
{
 	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	 {
		window.location.href=web_url+"disabled-pictures/my_album"; 		
	 }
}
//=================
function Remove_friend(UserID,FriendID)
{	
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	 {
		 alert ("Browser does not support HTTP Request")
		 return
	 }	 	
	var url=web_url+"common.php";
	url=url+"?UserID="+UserID;
	url=url+"&FriendID="+FriendID;
	url=url+"&option=Del_Friend";
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=stateChanged_Remove_friend;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
function stateChanged_Remove_friend()
{
 	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	 {
		window.location.href=web_url+"home"; 		
	 }
}
//=================
function Pick_States(CountryID)
{	
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	 {
		 alert ("Browser does not support HTTP Request")
		 return
	 }	 	
	var url=web_url+"Pick_States.php";
	url=url+"?CountryID="+CountryID;
	url=url+"&option=Pick_States";
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=stateChanged_Remove_friend;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
function stateChanged_Remove_friend()
{
 	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	 {
		 document.getElementById("DIVStates").innerHTML=xmlHttp.responseText;
	 }
}
//============
function FillStates(CountryID)
{	
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	 {
		 alert ("Browser does not support HTTP Request")
		 return
	 }	 	
	var url=web_url+"Fill_States.php";
	url=url+"?CountryID="+CountryID;
	url=url+"&option=Pick_States";
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=stateChanged_FillStates;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
function stateChanged_FillStates()
{
 	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	 {
		 document.getElementById("DIVStates").innerHTML=xmlHttp.responseText;
	 }
}
//============accept request
function Accept(ID)
{	
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	 {
		 alert ("Browser does not support HTTP Request")
		 return
	 }	 	
	var url=web_url+"requests.php";
	url=url+"?ID="+ID;
	url=url+"&option=Accept";	
	url=url+"&sid="+Math.random();
	//alert(url);
	xmlHttp.onreadystatechange=stateChanged_Accept;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
function stateChanged_Accept()
{
 	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	 {
		if(document.getElementById("page").value!=''){
			window.location.href=URL+"friends_incoming/"+document.getElementById("page").value;
		}else{
		window.location.href=URL+"friends_incoming";}
	 }
}
//=============reject request
function Deny(ID)
{	
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	 {
		 alert ("Browser does not support HTTP Request")
		 return
	 }	 	
	var url=web_url+"requests.php";
	url=url+"?ID="+ID;
	url=url+"&option=Deny";	
	url=url+"&sid="+Math.random();
	//alert(url);
	xmlHttp.onreadystatechange=stateChanged_Deny;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
function stateChanged_Deny()
{
 	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	 {
		window.location.href="friends_incoming";
	 }
}
//=========delete ougoing request
function Delete(ID)
{	
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	 {
		 alert ("Browser does not support HTTP Request")
		 return
	 }	 	
	var url=web_url+"requests.php";
	url=url+"?ID="+ID;
	url=url+"&option=Delete";	
	url=url+"&sid="+Math.random();
	//alert(url);
	xmlHttp.onreadystatechange=stateChanged_Delete;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
function stateChanged_Delete()
{
 	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	 {
		if(document.getElementById("page").value!=''){
			window.location.href=URL+"friends_out/"+document.getElementById("page").value;
		}else{
		window.location.href=URL+"friends_out";}
	 }
}
//=================
var Id;
function FavNews(ID)
{	
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	 {
		 alert ("Browser does not support HTTP Request")
		 return
	 }
	Id=ID;
	var url=web_url+"requests.php";
	url=url+"?ID="+ID;
	url=url+"&option=FavNews";
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=stateChanged_FavNews;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
function stateChanged_FavNews()
{
 	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	 {
		window.location.href=web_url+"disabled-news/"+document.getElementById("cat").value+"/"+document.getElementById("desc").value+"-"+Id; 		
	 }
}
//============
var VidID;
function AddCommets(VideoComments,VIDEO_ID)
{
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	 {
		 alert ("Browser does not support HTTP Request")
		 return
	 }
	 VidID=VIDEO_ID;
	var url=web_url+"videos/video_commets.php";
	url=url+"?VideoComments="+VideoComments;
	url=url+"&VideoID="+VIDEO_ID;	
	url=url+"&option=VideoCommets";
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=stateChanged_AddCommets;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
function stateChanged_AddCommets()
{
 	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	 {
		 if(xmlHttp.responseText=='1')
		{
			alert("Please login to post your comments");
		}
		else if(xmlHttp.responseText=='true')
		{
			document.getElementById("cam").style.display='none';	
			document.getElementById("vid_comments").value='';
			document.getElementById("other").style.display='inline';	
			//TotVideoComments();
			showPage("1",VidID);
		}
	 }
}
//====================
function TotVideoComments()
{
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	 {
		 alert ("Browser does not support HTTP Request")
		 return
	 }
	 var videoid=document.getElementById("VideoID").value;
	var url=web_url+"videos/video_commets.php";
	url=url+"?VideoID="+videoid;	
	url=url+"&option=TotVideoComments";
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=stateChanged_TotVideoComments;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);

}
function stateChanged_TotVideoComments()
{
 	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	 {
		if(xmlHttp.responseText=='true')
		{
			document.getElementById("TotCount").innerHTML=xmlHttp.responseText;	
			showPage("1");
		}
	 }
}
//=============
function showPage(str,VIDEO_ID) { 
	//Function that gets called
	//Currently we only call one other sub, but this could change
	showStates(str,VIDEO_ID)
}

function showStates(str,VIDEO_ID) { 
	//This sub will populate a table with all the states and get the 
	//pagination built
	
	//Make the AJAX connection for both the navigation and content
	xmlHttp=GetXmlHttpObject()
	xmlHttp2=GetXmlHttpObject()
	
	//If we cant do the request error out
	if (xmlHttp==null || xmlHttp2==null ) {
	 	alert ("Browser does not support HTTP Request")
	 	return
	}
	//var videoid=document.getElementById("VideoID").value;
	//First build the navigation panel
	var url=web_url+"videos/getcomments.php"
	url=url+"?p="+str
	url=url+"&video_id="+VIDEO_ID;	
	url=url+"&t=nav"
	url=url+"&sid="+Math.random()

	//Once the page finished loading put it into the div
	xmlHttp2.onreadystatechange=navDone 

	//Get the php page
	xmlHttp2.open("GET",url,true)
	xmlHttp2.send(null)
	
	//Build the url to call
	//Pass variables through the url
	var url=web_url+"videos/getcomments.php"
	url=url+"?p="+str
	url=url+"&video_id="+VIDEO_ID;	
	url=url+"&t=con"
	url=url+"&sid="+Math.random()
	//Once the page finished loading put it into the div
	xmlHttp.onreadystatechange=stateChanged 
	
	//Get the php page
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

function navDone() { 
	//IF this is getting called when the page is done loading then fill the pagination div
	if (xmlHttp2.readyState==4 || xmlHttp2.readyState=="complete") { 
	 	//Update the Div tag with the outputted text
	 	document.getElementById("pgNavigation").innerHTML=xmlHttp2.responseText 
	} 
}

function stateChanged() { 
	//IF this is getting called when the page is done loading the states then output the div
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
	 	//Update the Div tag with the outputted text
	 	document.getElementById("pgContent").innerHTML=xmlHttp.responseText 
	} 
}
function plusOne() {
	//This is just a second counter to prove that we arent refreshing 
	// If using the content ('childNode') of an id element...
	//var spanEl = document.getElementById('spanEl');
	//spanEl.childNodes[0].nodeValue = ( parseInt(spanEl.childNodes[0].nodeValue) + 1 );
}
//====================
function Share_Video(Frndmail,RecName,Frndmsg,VIDEO_ID,CatID)
{
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	 {
		 alert ("Browser does not support HTTP Request")
		 return
	 }
	// var videoid=document.getElementById("VideoID").value;
	var url=web_url+"videos/video_commets.php";
	url=url+"?VideoID="+VIDEO_ID;	
	url=url+"&RecName="+RecName;		
	url=url+"&Frndmail="+Frndmail;	
	url=url+"&Frndmsg="+Frndmsg;
	url=url+"&CatID="+CatID;	
	url=url+"&option=SendtoFrnd";
	url=url+"&sid="+Math.random();
	alert(url);
	xmlHttp.onreadystatechange=stateChanged_Share_DISABLEDVideo;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);

}
function stateChanged_Share_DISABLEDVideo()
{
 	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	 {		alert(xmlHttp.responseText);
	 	if(xmlHttp.responseText ==1){
			alert("Please Login to share this video");	
		}else if(xmlHttp.responseText ==2){
			document.getElementById("user").style.display='none';	
			document.getElementById("frnd_email").value='';
			document.getElementById("rec_name").value='';
			document.getElementById("msg_txt").value='';
			document.getElementById("otherMSG").style.display='inline';		
		}else{
			alert("Message sending failed");	
		}
		
	 }
}
//Creates a timer 
var stop = setInterval("plusOne()",1000);

//Starts the counter
window.onload = plusOne;

//Onload start the user off on page one
//window.onload = showPage("1");

function DelComments(CommentID){ 
 	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	 {
		 alert ("Browser does not support HTTP Request")
		 return
	 }	 
	if(!confirm("Are you sure to delete?")){
		return ;
	}
	var url=web_url+"user_profile.php";
	url=url+"?ID="+CommentID;
	url=url+"&option=DelComments";	
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange = handleDelComments; 
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	}
	function handleDelComments(){
		if(xmlHttp.readyState == 4){	
			window.location.href=web_url+'view_comments/';		
		}
}//function