var curuinp = 1;

function chDefVal(cid, defaultvalue, newvalue)
{
	if(document.getElementById(cid).value == defaultvalue)
	{
		document.getElementById(cid).value = newvalue;
	}
}

function add_smilie(id, smilie)
{
	var taText = document.getElementById(id).value;
	taText += " " + smilie;
	document.getElementById(id).value = taText;
}

function getNewMovies()
{
	if(window.XMLHttpRequest)
	{
		xmlhttp = new XMLHttpRequest();
		xmlhttp.open("GET", "/newmovies.php", false);
		xmlhttp.send(null);
	}
	else
	{
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		xmlhttp.open("GET", "/newmovies.php", false);
		xmlhttp.send();
	}
	document.getElementById("newmovies").className = "active";
	document.getElementById("newseries").className = "";
	document.getElementById("newestpanel").innerHTML = xmlhttp.responseText;
}

function getNewSeries()
{
	if(window.XMLHttpRequest)
	{
		xmlhttp = new XMLHttpRequest();
		xmlhttp.open("GET", "/newseries.php", false);
		xmlhttp.send(null);
	}
	else
	{
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		xmlhttp.open("GET", "/newseries.php", false);
		xmlhttp.send();
	}
	document.getElementById("newseries").className = "active";
	document.getElementById("newmovies").className = "";
	document.getElementById("newestpanel").innerHTML = xmlhttp.responseText;
}

function viewAll()
{
	if(document.getElementById("newseries").className == "active")
	{
		window.location = "/seriali.html";
	}
	else
	{
		window.location = "/filmi.html";
	}
}

function getMVMovies()
{
	if(window.XMLHttpRequest)
	{
		xmlhttp = new XMLHttpRequest();
		xmlhttp.open("GET", "/mvmovies.php", false);
		xmlhttp.send(null);
	}
	else
	{
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		xmlhttp.open("GET", "/mvmovies.php", false);
		xmlhttp.send();
	}
	document.getElementById("mvmovies").className = "active";
	curuinp = 1;
	document.getElementById("mvseries").className = "";
	document.getElementById("mostViewed").innerHTML = xmlhttp.responseText;
}

function getMVSeries()
{
	if(window.XMLHttpRequest)
	{
		xmlhttp = new XMLHttpRequest();
		xmlhttp.open("GET", "/mvseries.php", false);
		xmlhttp.send(null);
	}
	else
	{
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		xmlhttp.open("GET", "/mvseries.php", false);
		xmlhttp.send();
	}
	document.getElementById("mvseries").className = "active";
	curuinp = 2;
	document.getElementById("mvmovies").className = "";
	document.getElementById("mostViewed").innerHTML = xmlhttp.responseText;
}

function rate(rating, movieid)
{
	var sendstr = "/rate.php?rating=" + rating + "&movieid=" + movieid;
	
	if(window.XMLHttpRequest)
	{
		xmlhttp = new XMLHttpRequest();
		xmlhttp.open("GET", sendstr, false);
		xmlhttp.send(null);
		var rt = xmlhttp.responseText;
	}
	else
	{
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		xmlhttp.open("GET", sendstr, false);
		xmlhttp.send();
		var rt = xmlhttp.responseText;
	}
	
	if(rt.length == 0)
	{
		if(curuinp == 2)
		{
			getMVSeries();
		}
		
		if(curuinp == 1)
		{
			getMVMovies();
		}
	}
	else
	{
		alert(rt);
	}
}

function ratemovie(rating, movieid)
{
	var sendstr = "/rateMovie.php?rating=" + rating + "&movieid=" + movieid;
	var resp;
	
	if(window.XMLHttpRequest)
	{
		xmlhttp = new XMLHttpRequest();
		xmlhttp.open("GET", sendstr, false);
		xmlhttp.send(null);
		resp = xmlhttp.responseText;
	}
	else
	{
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		xmlhttp.open("GET", sendstr, false);
		xmlhttp.send();
		resp = xmlhttp.responseText;
	}
	
	if(resp.length > 3)
	{
		alert(resp);
	}
	else
	{
		document.getElementById("cur-rate").style.width = resp + "px";
	}
}

function favorite(movieid)
{
	var sendstr = "/favorites.php?movieid=" + movieid;
	
	if(window.XMLHttpRequest)
	{
		xmlhttp = new XMLHttpRequest();
		xmlhttp.open("GET", sendstr, false);
		xmlhttp.send(null);
		var rt = xmlhttp.responseText;
	}
	else
	{
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		xmlhttp.open("GET", sendstr, false);
		xmlhttp.send();
		var rt = xmlhttp.responseText;
	}
	
	if(rt.length > 2)
	{
		alert(rt);
	}
	else
	{
		if(rt == "0")
		{
			document.getElementById("fav-info").innerHTML = "добави в любими";
		}
		else
		{
			document.getElementById("fav-info").innerHTML = "премахни от любими";
		}
	}
}

function loadcomments(movieid)
{
	if(window.XMLHttpRequest)
	{
		xmlhttp = new XMLHttpRequest();
		xmlhttp.open("GET", "/comments.php?id=" + movieid, false);
		xmlhttp.send(null);
		document.getElementById("commentzone").innerHTML = xmlhttp.responseText;
	}
	else
	{
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		xmlhttp.open("GET", "/comments.php?id=" + movieid, false);
		xmlhttp.send();
		document.getElementById("commentzone").innerHTML = xmlhttp.responseText;
	}
}

function addsmilie(text)
{
	document.getElementById("comment").value = document.getElementById("comment").value + ' ' + text + ' ';
}

function fetchAscii(obj)
{
      var convertedObj = '';
	  
      for(i = 0; i < obj.length; i++)
      {
            var asciiChar = obj.charCodeAt(i);
            convertedObj += '&#' + asciiChar + ';';
      }
      return convertedObj;
}

function addcomment(movieid)
{
	var commenttext = document.getElementById("comment").value;
	var params = "id=" + movieid + "&comment=" + encodeURIComponent(fetchAscii(commenttext));
	
	document.getElementById("sendcomment").disabled = "disabled";
	document.getElementById("comment").value = "";
	setTimeout(function() { document.getElementById("sendcomment").disabled = false; }, 30000);
	
	if(window.XMLHttpRequest)
	{
		xmlhttp = new XMLHttpRequest();
		xmlhttp.open("POST", "/addcomment.php", true);
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp.setRequestHeader("Content-length", params.length);
		xmlhttp.setRequestHeader("Connection", "close");
		xmlhttp.send(params);
		xmlhttp.onreadystatechange = function() {
			if(xmlhttp.readyState == 4) {
				var response = xmlhttp.responseText;
				
				if(response != 1)
				{
					alert(response);
				}
				else
				{
					loadcomments(movieid);
					window.location = "#latest-comment";
				}
			}
		}
	}
	else
	{
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		xmlhttp.open("POST", "/addcomment.php", true);
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp.setRequestHeader("Content-length", params.length);
		xmlhttp.setRequestHeader("Connection", "close");
		xmlhttp.send(params);
		xmlhttp.onreadystatechange = function() {
			if(xmlhttp.readyState == 4) {
				var response = xmlhttp.responseText;
				
				if(response != 1)
				{
					alert(response);
				}
				else
				{
					loadcomments(movieid);
					window.location = "#latest-comment";
				}
			}
		}
	}
}
