var xmlhttp

function addfriend(username)
{
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return;
	  }
	var url="/user/addfriend/";
		
	var sendparam="username=";
	
	
	sendparam=sendparam+username+"&sid="+Math.random();
	
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
	xmlhttp.send(sendparam);
	xmlhttp.onreadystatechange=function() {stateaddfriend();};
}
function stateaddfriend()
{
	
	
	if (xmlhttp.readyState==4)
	{
			if(xmlhttp.responseText=='0')
			{
			document.getElementById("addfrienddiv").innerHTML='User already in your friends list';
			
			}

		else 
			{
			document.getElementById("addfrienddiv").innerHTML='New friend added';
			}

	}
}


function comnotifyreply(com_id,replyset)
{
	if(replyset==0)
	{
		if (!confirm('Stop email reply notification for this topic?')) return false;
	}

	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return;
	  }
	var url="/user/setnotifyreply";
	var divnotifyreply="divnotifyreply"+com_id;
	var sendparam="com_id="+com_id+"&replyset="+replyset+"&sid="+Math.random();
	
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
	xmlhttp.send(sendparam);
	xmlhttp.onreadystatechange=function() {statereplynotify(replyset,divnotifyreply);};
}
function statereplynotify(replyset,divnotifyreply)
{
	
//	alert('Favorite submitted');
	if (xmlhttp.readyState==4)
	{
		if(replyset==0)
		{
			document.getElementById(divnotifyreply).innerHTML="Notify Stopped | ";
		}
		else
		{
			document.getElementById(divnotifyreply).innerHTML="Set to Notify | ";
		}
	}
	else
	{
		document.getElementById(divnotifyreply).innerHTML="<center><img border='0' src='/images/progress2.gif' width='16' height='16'></center>";
	}

}

function delete_this(whereurl,idcode)
{
	if (!confirm('Remove this movie?')) return false;

	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return;
	  }
	var url=whereurl+idcode;
		
	var sendparam="sid="+Math.random();
	var bdiv="bdiv"+idcode;
	
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
	xmlhttp.send(sendparam);
	xmlhttp.onreadystatechange=function() {statedelthis(bdiv);};
}
function statedelthis(bdiv)
{
	
//	alert('Favorite submitted');
	if (xmlhttp.readyState==4)
	{
		document.getElementById(bdiv).innerHTML='<br><br><br><br><br><br>Removed<br><br><br><br><br><br>';
	}
	else
	{
		document.getElementById(bdiv).innerHTML="<center><img border='0' src='/images/progress2.gif' width='16' height='16'></center>";
	}
}


function updateprofile(usr_id,usr_password,usr_email,usr_email_weekly,usr_email_comment)
{
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return;
	  }
	var url="/user/update_profile";
	var sendparam="usr_id=";
	
	sendparam=sendparam+usr_id+"&usr_password="+usr_password+"&usr_email="+usr_email+"&usr_email_weekly="+usr_email_weekly+"&usr_email_comment="+usr_email_comment+"&sid="+Math.random();
	

	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
	xmlhttp.send(sendparam);
	xmlhttp.onreadystatechange=function() {stateupdateprofile();};
}

function stateupdateprofile()
{
	
	//alert('You form is now submitted');
	if (xmlhttp.readyState==4)
	{
		if(xmlhttp.responseText=='0')
			{
			document.getElementById("updatemsg").innerHTML='<font color="#cc0000">Incomplete data. Please try again.</font> ';
			
			}

		else if(xmlhttp.responseText=='1')
			{
			document.getElementById("updatemsg").innerHTML='<font color="#cc0000">Email is invalid. Please try again.</font> ';
			}

		else if(xmlhttp.responseText=='2')
			{
			document.getElementById("updatemsg").innerHTML='<font color="#cc0000">Invalid logged user. Please logout and relogin again.</font> ';
			}

		else
			{
			document.getElementById("updatemsg").innerHTML='<br><b>Your account is now updated!</b><br>';
			}		
	}
	else
	{
		document.getElementById("updatemsg").innerHTML="<center><img border='0' src='/images/progress2.gif' width='16' height='16'></center>";
	}
}


function delcomment(com_id)
{
	if (!confirm('You are removing this comment, continue?')) return false;

	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return;
	  }
	var url="/movies/delcomment";
		
	var sendparam="com_id=";
	var divcom="divcom";
	divcom=divcom+com_id;
	
	
	sendparam=sendparam+com_id+"&sid="+Math.random();
	
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
	xmlhttp.send(sendparam);
	xmlhttp.onreadystatechange=function() {statedelcom(divcom);};
}
function statedelcom(divcom)
{
	
//	alert('Favorite submitted');
	if (xmlhttp.readyState==4)
	{
		document.getElementById(divcom).innerHTML='Comment removed.';
	}
	else
	{
		document.getElementById(divcom).innerHTML="<center><img border='0' src='/images/progress2.gif' width='16' height='16'></center>";
	}
}


function flagspamno(com_id)
{
	alert('Login is required. Please login or register');
}

function flagspam(com_id)
{
	if (!confirm('You are flagging this comment as spam, continue?')) return false;

	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return;
	  }
	var url="/movies/flagcomment";
		
	var sendparam="com_id=";
	var divcom="divcom";
	divcom=divcom+com_id;
	
	
	sendparam=sendparam+com_id+"&sid="+Math.random();
	
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
	xmlhttp.send(sendparam);
	xmlhttp.onreadystatechange=function() {stateflagcom(divcom);};
}
function stateflagcom(divcom)
{
	
//	alert('Favorite submitted');
	if (xmlhttp.readyState==4)
	{
		document.getElementById(divcom).innerHTML='You flagged this comment as spam. Thank you';
	}
	else
	{
		document.getElementById(divcom).innerHTML="<center><img border='0' src='/images/progress2.gif' width='16' height='16'></center>";
	}
}




function add_comments(commentpost,movie_id)
{
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return;
	  }
	var url="/movies/add_comment";
		
	var sendparam="commentpost=";
	
	var notifyviaemail="&notifyemail=";
	
	var movietitle="&movietitle="+document.commentform.movietitle.value;
	var movie_fname="&movie_fname="+document.commentform.movie_fname.value;
	
	if(document.commentform.notifyemail.checked)
	{
		notifyviaemail=notifyviaemail+"1";
	}
	else
	{
		notifyviaemail=notifyviaemail+"0";
	}
	
	var mrating=0;
	for (var i = 0; i < 5; i++) 
	{   
		if(document.commentform.mrating[i].checked)
		{
			mrating = i+1;
			break;
		}
	}
	
	sendparam=sendparam+commentpost+"&mrating="+mrating+"&movie_id="+movie_id+notifyviaemail+movie_fname+movietitle+"&sid="+Math.random();
	
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
	xmlhttp.send(sendparam);
	xmlhttp.onreadystatechange=function() {statepostcom();};
}

function statepostcom()
{
	
//	alert('Favorite submitted');
	if (xmlhttp.readyState==4)
	{
		document.getElementById("commentdiv").innerHTML=xmlhttp.responseText+'<center>Your comment is now posted. Thank you</center>';
	}
	else
	{
		document.getElementById("commentdiv").innerHTML="<center><img border='0' src='/images/progress2.gif' width='16' height='16'></center>";
	}
}


function addfavorite(movie_id,loggedid,loggeduser)
{
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return;
	  }
	var url="/user/add_favorite";
	var sendparam="movie_id=";
	
	sendparam=sendparam+movie_id+"&loggedid="+loggedid+"&loggeduser="+loggeduser+"&sid="+Math.random();
	
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
	xmlhttp.send(sendparam);
	xmlhttp.onreadystatechange=function() {statefavorite(loggeduser);};
}

function statefavorite(loggeduser)
{
	
//	alert('Favorite submitted');
	if (xmlhttp.readyState==4)
	{
		if(xmlhttp.responseText=='0')
			{
			document.getElementById("favoritediv").innerHTML="Login is required, Please <a href='#loginreg' title='Member Login' alt='Login'>Login</a> or <a href='#loginreg' title='Register new user' alt='Register'>Register</a>";
			}
		else if(xmlhttp.responseText=='1')
			{
			document.getElementById("favoritediv").innerHTML='Movie is already in your <a href="/user/favorite/'+loggeduser+'">Favorites</a>';
			}
		else 
			{
			document.getElementById("favoritediv").innerHTML='Movie added to your <a href="/user/favorite/'+loggeduser+'">Favorites</a>';
			}
	}
	else
	{
		document.getElementById("favoritediv").innerHTML="<center><img border='0' src='/images/progress2.gif' width='16' height='16'></center>";
	}
}


function forgotpw(emailforgotpw)
{
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return;
	  }
	var url="/user/forgotpw";
	var sendparam="emailforgotpw=";
	
	sendparam=sendparam+emailforgotpw+"&sid="+Math.random();
	

	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
	xmlhttp.send(sendparam);
	xmlhttp.onreadystatechange=function() { stateforgot(); };
}

function stateforgot()
{
	
	//alert('Your email is now submitted');
	if (xmlhttp.readyState==4)
	{
		if(xmlhttp.responseText=='0')
			{
			document.getElementById("forgoterror").innerHTML='<font color="#cc0000">Incorrect email address. Please try again.</font> ';
			
			}
		else if(xmlhttp.responseText=='1')
			{
			document.getElementById("forgoterror").innerHTML='<font color="#cc0000">Email not found in our record. Please try again.</font> ';
			
			}

		else
			{
			
			document.getElementById("forgotdiv").innerHTML='<br><br><center>Your login info has been sent to your email.</center><br><br><br>';
			}		
	}
	else
	{
		document.getElementById("forgoterror").innerHTML="<center><img border='0' src='/images/progress2.gif' width='16' height='16'></center>";
	}	
}


function loginuser(loginusername,loginpassword)
{
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return;
	  }
	var url="/user/login_user";
	var sendparam="loginusername=";
	
	sendparam=sendparam+loginusername+"&loginpassword="+loginpassword+"&sid="+Math.random();
	

	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
	xmlhttp.send(sendparam);
	xmlhttp.onreadystatechange=function() { statelog(); };
}

function statelog()
{
	
	//alert('You login is now submitted');
	if (xmlhttp.readyState==4)
	{
		if(xmlhttp.responseText=='0')
			{
			document.getElementById("loginerror").innerHTML='<font color="#cc0000">Incorrect username or password. Please try again.</font> ';
			
			}

		else
			{
			window.location.reload();
			//document.getElementById("logindiv").innerHTML='<br><br><center>You are now logged.</center><br><br><br>';
			}		
	}
	else
	{
		document.getElementById("loginerror").innerHTML="<center><img border='0' src='/images/progress2.gif' width='16' height='16'></center>";
	}
}



function reguser(regusername,regpassword,regemail)
{
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return;
	  }
	var url="/user/reg_user";
	var sendparam="regusername=";
	
	sendparam=sendparam+regusername+"&regpassword="+regpassword+"&regemail="+regemail+"&sid="+Math.random();
	

	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
	xmlhttp.send(sendparam);
	xmlhttp.onreadystatechange=function() {statereg();};
}

function statereg()
{
	
	//alert('You form is now submitted');
	if (xmlhttp.readyState==4)
	{
		if(xmlhttp.responseText=='0')
			{
			document.getElementById("regerror").innerHTML='<font color="#cc0000"><br>Incomplete form. Please try again.</font> ';
			
			}

		else if(xmlhttp.responseText=='1')
			{
			document.getElementById("regerror").innerHTML='<font color="#cc0000"><br>User already exists. Please try again.</font> ';
			}

		else if(xmlhttp.responseText=='2')
			{
			document.getElementById("regerror").innerHTML='<font color="#cc0000"><br>Email is invalid. Please try again.</font> ';
			}
		else if(xmlhttp.responseText=='3')
			{
			document.getElementById("regerror").innerHTML='<font color="#cc0000"><br>Username must be alphanumeric only. Please try again.</font> ';
			}

		else
			{
			document.getElementById("divreg").innerHTML='<br>Thank you for registering.<br><br>Your account is now registered.<br>Login to your account on your left side.<br>';
			}		
	}
	else
	{
		document.getElementById("regerror").innerHTML="<center><img border='0' src='/images/progress2.gif' width='16' height='16'></center>";
	}
}


function submit_return()
{
	document.getElementById("divsubmit").innerHTML='<form>	<table border="0" width="100%">	  <tr>	<td width="50%">IMDB.com page</td>	<td width="50%"><input name="imdburl" size="30" value=""></td>	</tr><tr><td width="50%">Link to movie</td>	<td width="50%"><input name="suglink" size="30" value=""></td>  </tr></table><input type="submit" value="Submit" OnClick="submit_movie_lk(imdburl.value,suglink.value);return false;">	</form>	';
}

function request_return()
{
	document.getElementById("divrequest").innerHTML='<form>	<table border="0" width="100%">  <tr><td width="50%">IMDB.com page</td>					<td width="50%"><input name="imdburl" size="30" value=""></td>  </tr>	</table><input type="submit" value="Submit" OnClick="request_movie(imdburl.value);return false;"></form>';
}


function request_movie(imdburl)
{
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return;
	  }
	var url="/user/request_movie";
	var sendparam="imdburl=";
	
	sendparam=sendparam+imdburl+"&sid="+Math.random();

	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
	xmlhttp.send(sendparam);
	xmlhttp.onreadystatechange=function() {stateChangedrequest();};

}

function stateChangedrequest()
{
	//alert(xmlhttp.readyState);
	if (xmlhttp.readyState==4)
	{	
		if(xmlhttp.responseText=='0')
		{
			document.getElementById("requesterror").innerHTML='<font color="#cc0000"><br>Invalid IMDB url page, try again.</font>';
		}
		else if(xmlhttp.responseText=='1')
		{
			document.getElementById("requesterror").innerHTML='<font color="#cc0000"><br>Movie is already requested.</font>';
		}

		else if(xmlhttp.responseText=='999')
		{
		document.getElementById("divrequest").innerHTML='<br><br><center>Movie request submitted. We regularly add new movies and prioritize requested movies, visit your Profile to monitor your request. Thank you.<br><br>[<a href="#" OnClick="request_return();return false;">Request another? click here</a>]</center>';
		}
		else
		{
		document.getElementById("divrequest").innerHTML=xmlhttp.responseText+'<br>[<a href="#" OnClick="request_return();return false;">Request another? click here</a>]</center><br>';
		}
	}
	else
	{
		document.getElementById("requesterror").innerHTML="<center><img border='0' src='/images/progress2.gif' width='16' height='16'></center>";
	}
}

function submit_movie_lk(imdburl,suglink)
{
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return;
	  }
	var url="/user/request_movie";
	var sendparam="imdburl=";
	
	sendparam=sendparam+imdburl+"&suglink="+suglink+"&sid="+Math.random();;
	xmlhttp.onreadystatechange=function() {stateChanged2();};

	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
	xmlhttp.send(sendparam);
}

function stateChanged2()
{
	//alert(xmlhttp.readyState);
	if (xmlhttp.readyState==4)
	{
			if(xmlhttp.responseText=='0')
		{
			document.getElementById("submiterror").innerHTML='<font color="#cc0000"><br>Invalid IMDB url page, try again.</font>';
		}
		else if(xmlhttp.responseText=='1')
		{
			document.getElementById("submiterror").innerHTML='<font color="#cc0000"><br>Invalid movie link, try again.</font>';
		}

		else
		{
		document.getElementById("submiterror").innerHTML='';
		document.getElementById("divsubmit").innerHTML='<br><center>Thank you for sharing.<br><br>[<a href="#" OnClick="submit_return();return false;">Submit another? click here</a>]</center><br><br>';
		}
	}
	else
	{
		document.getElementById("submiterror").innerHTML="<center><img border='0' src='/images/progress2.gif' width='16' height='16'></center>";
	}

}


function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}
