function a(id) {
	element = document.getElementById(id);
	element.style.background = "#222";
}
function b(id) {
	element = document.getElementById(id);
	element.style.background = "#111";
}
function toggle(divname) {
	if (document.getElementById(divname).style.display == 'none')document.getElementById(divname).style.display = 'block';
	else document.getElementById(divname).style.display = 'none';
}
function toggle(divname,obj) {
	if (document.getElementById(divname).style.display == 'none')document.getElementById(divname).style.display = 'block';
	else document.getElementById(divname).style.display = 'none';
	if (obj.innerHTML.indexOf("more")!= -1)obj.innerHTML = obj.innerHTML.replace("more","less");
	else obj.innerHTML = obj.innerHTML.replace("less","more");
}
function gotourl(url) {
	location.href=url;
}
var http_request = false;
function makePOSTRequest(url, parameters) {
   http_request = false;
   if (window.XMLHttpRequest) { // Mozilla, Safari,...
      http_request = new XMLHttpRequest();
      if (http_request.overrideMimeType) {
      	// set type accordingly to anticipated content type
         //http_request.overrideMimeType('text/xml');
         http_request.overrideMimeType('text/html');
      }
   } else if (window.ActiveXObject) { // IE
      try {
         http_request = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
         try {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e) {}
      }
   }
   if (!http_request) {
      alert('Cannot create XMLHTTP instance');
      return false;
   }
   
   http_request.onreadystatechange = alertContents;
   http_request.open('POST', url, true);
   http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   http_request.setRequestHeader("Content-length", parameters.length);
   http_request.setRequestHeader("Connection", "close");
   http_request.send(parameters);
}
function vote(videoid){
	   var poststr = "vote=" + videoid;
	   makePOSTRequest('/postvote.php', poststr );
}
function subscribe(user){
	   var poststr = "user=" + user;
	   makePOSTRequest('/actions/subscribe.php', poststr );
}
function favorite(videoid){
	var poststr = "videoid=" + videoid;
	makePOSTRequest('/actions/favorite.php', poststr );
}
function removefavorite(videoid){
	var poststr = "videoid=" + videoid;
	makePOSTRequest('/actions/removefavorite.php', poststr );
}
function removeinbox(videoid){
	var poststr = "videoid=" + videoid;
	makePOSTRequest('/actions/removeinbox.php', poststr );
}
function unsubscribe(subscription){
	var poststr = "subscription=" + subscription;
	makePOSTRequest('/actions/unsubscribe.php', poststr );
}
function like(videoid){
	var poststr = "videoid=" + videoid;
	makePOSTRequest('/actions/like.php', poststr );
}
function dislike(videoid){
	var poststr = "videoid=" + videoid;
	makePOSTRequest('/actions/dislike.php', poststr );
}
function postcomment(videoid,post){
	var poststr = "videoid=" + videoid + "&post=" + post ;
	makePOSTRequest('/actions/postcomment.php', poststr );
}
function replycomment(videoid,post,comment){
	var poststr = "videoid=" + videoid + "&post=" + post + "&comment=" + comment;
	makePOSTRequest('/actions/replycomment.php', poststr );
}
function share(user,videoid){
	var poststr = "videoid=" + videoid + "&user=" + user ;
	makePOSTRequest('/actions/share.php', poststr );
}
function changeQuality(){
	   makePOSTRequest('/actions/changeQuality.php','0' );
}


function alertContents() {
   if (http_request.readyState == 4) {
      if (http_request.status == 200) {
         if (http_request.responseText == 1)
         alert('You have voted successfully for this video!');
         else if (http_request.responseText === 0)
          alert('You have already voted for this video this week!');
         else if (http_request.responseText == 2)
         	alert('Error: Could not perform this action. Try again later');
         else if (http_request.responseText == 832){
        	 alert('You have posted your comment!');
        	 window.location.reload( false );
         }
         else if (http_request.responseText == "reload")
        	 window.location.reload( false );
       	 else if (http_request.responseText != "")
         	alert(http_request.responseText);
      } else if (http_request.status == 202) {
     	 alert('You are now subscribed to this channel');
      } else {
     	 alert(http_request.status);
         alert('Error: Could not perform this action.');
      }
   }
}

var bustcachevar=1 //bust potential caching of external pages after initial request? (1=yes, 0=no)
var loadedobjects=""
var rootdomain="http://"+window.location.hostname
var bustcacheparameter=""

function ajaxpage(url, containerid){
	var page_request = false
	if (window.XMLHttpRequest) // if Mozilla, Safari etc
		page_request = new XMLHttpRequest()
	else if (window.ActiveXObject){ // if IE
			try {
			page_request = new ActiveXObject("Msxml2.XMLHTTP")
			} 
			catch (e){
				try{
					page_request = new ActiveXObject("Microsoft.XMLHTTP")
				}
				catch (e){}
			}
		}
		else
			return false
	page_request.onreadystatechange=function(){
			loadpage(page_request, containerid)
		}
	if (bustcachevar) // if bust caching of external page
	bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
	page_request.open('GET', url+bustcacheparameter, true)
	page_request.send(null)
}

function loadpage(page_request, containerid){
	if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
		document.getElementById(containerid).innerHTML=page_request.responseText
}

function loadobjs(){
	if (!document.getElementById)
		return
		for (i=0; i<arguments.length; i++){
			var file=arguments[i]
			var fileref=""
			if (loadedobjects.indexOf(file)==-1){ //Check to see if this object has not already been added to page before proceeding
				if (file.indexOf(".js")!=-1){ //If object is a js file
				fileref=document.createElement('script')
				fileref.setAttribute("type","text/javascript");
				fileref.setAttribute("src", file);
				}
				else if (file.indexOf(".css")!=-1){ //If object is a css file
					fileref=document.createElement("link")
					fileref.setAttribute("rel", "stylesheet");
					fileref.setAttribute("type", "text/css");
					fileref.setAttribute("href", file);
				}
			}
			if (fileref!=""){
				document.getElementsByTagName("head").item(0).appendChild(fileref)
				loadedobjects+=file+" " //Remember this object as being already added to page
			}
		}
}

