/*
Page:           rating.js
Created:        Aug 2006
Last Mod:       Mar 11 2007
Handles actions and requests for rating bars.	
--------------------------------------------------------- 
ryan masuga, masugadesign.com
ryan@masugadesign.com 
Licensed under a Creative Commons Attribution 3.0 License.
http://creativecommons.org/licenses/by/3.0/
See readme.txt for full credit details.
--------------------------------------------------------- */

var xmlhttp
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	  try {
	  xmlhttp=new ActiveXObject("Msxml2.XMLHTTP")
	 } catch (e) {
	  try {
	    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
	  } catch (E) {
	   xmlhttp=false
	  }
	 }
	@else
	 xmlhttp=false
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	 try {
	  xmlhttp = new XMLHttpRequest();
	 } catch (e) {
	  xmlhttp=false
	 }
	}
	function myXMLHttpRequest() {
	  var xmlhttplocal;
	  try {
	    xmlhttplocal= new ActiveXObject("Msxml2.XMLHTTP")
	 } catch (e) {
	  try {
	    xmlhttplocal= new ActiveXObject("Microsoft.XMLHTTP")
	  } catch (E) {
	    xmlhttplocal=false;
	  }
	 }

	if (!xmlhttplocal && typeof XMLHttpRequest!='undefined') {
	 try {
	  var xmlhttplocal = new XMLHttpRequest();
	 } catch (e) {
	  var xmlhttplocal=false;
	  alert('couldn\'t create xmlhttp object');
	 }
	}
	return(xmlhttplocal);
}

function sndReq(vote,id_num,ip_num,units) {
	var theUL = document.getElementById('unit_ul'+id_num); // the UL
	
	// switch UL with a loading div
	theUL.innerHTML = '<div class="loading"></div>';
	
    xmlhttp.open('get', '/community/midifiles/rpc.php?j='+vote+'&q='+id_num+'&t='+ip_num+'&c='+units);
    xmlhttp.onreadystatechange = handleResponse;
    xmlhttp.send(null);	
}


function handleResponse() {
  if(xmlhttp.readyState == 4){
		if (xmlhttp.status == 200){
       	
			var response = xmlhttp.responseText;
			var update = new Array();
	
			if(response.indexOf('|') != -1) {
				update = response.split('|');
				changeText(update[0], update[1]);
			}
			else
			{
				window.location.reload(true);
			}
		}
    }
}

function changeText( div2show, text ) {
    // Detect Browser
    var IE = (document.all) ? 1 : 0;
    var DOM = 0; 
    if (parseInt(navigator.appVersion) >=5) {DOM=1};

    // Grab the content from the requested "div" and show it in the "container"
    if (DOM) {
        var viewer = document.getElementById(div2show);
        viewer.innerHTML = text;
    }  else if(IE) {
        document.all[div2show].innerHTML = text;
    }
}

function sendComment(f,comment)
{
	var div = document.getElementById('reCom');
	div.style.visibility='visible';
	div.innerHTML = '<div class="loading"></div>';
	
    xmlhttp.open('get', 'comment.php?c='+comment+'&f='+f);
    xmlhttp.onreadystatechange = changComment;
    xmlhttp.send(null);	
}

function updateCom(id, action,f,p)
{
	var div = document.getElementById('reCom');
	div.style.visibility='visible';
	div.innerHTML = '<div class="loading"></div>';
	xmlhttp.open('get', 'comment.php?a='+action+'&id='+id+'&f='+f+'&p='+p);
    xmlhttp.onreadystatechange = changComment;
    xmlhttp.send(null);
}

function saveEdit(f, action,id,comment,p)
{
	if(action=='s')
	{
		url='comment.php?a='+action+'&id='+id+'&c='+comment+'&f='+f+'&p='+p;
	}else if(action=='c')
	{
		url='comment.php?f='+f+'&p='+p;
	}
	var div = document.getElementById('reCom');
	div.style.visibility='visible';
	div.innerHTML = '<div class="loading"></div>';
	xmlhttp.open('get', url);
	xmlhttp.onreadystatechange = changComment;
	xmlhttp.send(null);
	
}

function changPage(f,action, p, u)
{
	var div = document.getElementById('reCom');
	div.style.visibility='visible';
	div.innerHTML = '<div class="loading"></div>';
	
	url=u+'?a='+action+'&p='+p+'&f='+f;
	xmlhttp.open('get', url);
	xmlhttp.onreadystatechange = changComment;
	xmlhttp.send(null);
}

function changComment()
{
	 if(xmlhttp.readyState == 4){
		if (xmlhttp.status == 200){
			var div = document.getElementById('reCom');
			div.style.visibility='hidden';
			div.innerHTML = '';
			var response = xmlhttp.responseText;
			var divText = document.getElementById('allcomment');
			divText.innerHTML = response;
		}
    }
}

function addfav(file_id,file_name,action,u, p)
{
	var div = document.getElementById('fav');
	div.innerHTML = '<div class="loading" style="padding-left:15px;"></div>';
	
    xmlhttp.open('get', '/community/midifiles/addfav.php?f='+file_id+'&n='+file_name+'&a='+action+'&u='+u+'&p='+p);
    xmlhttp.onreadystatechange = changFav;
    xmlhttp.send(null);	
}

function changFav()
{
	if(xmlhttp.readyState == 4){
		if (xmlhttp.status == 200){
			var response = xmlhttp.responseText;
			var divText = document.getElementById('fav');
			divText.innerHTML = response;
		}
    }
}

function actionTags(id,action,tag,i)
{
	if(action=='d')
	{
		var r=confirm("Are you sure?");
		if (r==false)
		{
			return false;
		}else
		{
			sendRqTag(id,action,tag,i);
		}	
	}
	else if(action=='e')
	{
		var div='tag_'+i;
		var href="sendRqTag("+id+",'"+action+"','"+tag+"',"+i+")";
		document.getElementById(div).innerHTML='<div id="tags"><input class="small_text" id="i_'+div+'" value="'+tag+'">&nbsp;<a href="javascript:;" onclick="'+href+'">Save</a>&nbsp;&nbsp;<a href="javascript:;" onclick="sendRqTag('+id+',\'c\',\''+tag+'\','+i+')">Cancel</a></div>';
	}
	
}

function sendRqTag(id,action,tag,i)
{	
	var div='tag_'+i;
	if(action=='c')
	{
		var h='<a style="color:#00F" href="archive.php?key='+tag+'">'+tag+'</a>';
		h+='&nbsp;<a href="javascript:;" onclick="actionTags('+id+',\'e\',\''+tag+'\','+i+')">(edit)</a>&nbsp;<a href="javascript:;" onclick="actionTags('+id+',\'d\',\''+tag+'\','+i+')">(remove)</a>'
		document.getElementById(div).innerHTML=h;
	}else
	{
		
		var n='';
		if(action=='e')
		{
			var na='i_tag_'+i;
			n=document.getElementById(na).value;
			if(n =='') 
			{
				alert('Tag no blank');
				return;
			}
		}
		
		document.getElementById(div).innerHTML = '<div class="loading"></div>';
		xmlhttp.open('get', '/community/midifiles/comment.php?a='+action+'&id='+id+'&t='+tag+'&n='+n+'&i='+i);
		xmlhttp.onreadystatechange = changTags;
		xmlhttp.send(null);
	}
}

function changTags()
{
	if(xmlhttp.readyState == 4){
		if (xmlhttp.status == 200){
			var response = xmlhttp.responseText;
			var update = new Array();
	
			if(response.indexOf('|') != -1) {
				update = response.split('|');
				var div='tag_'+update[1];
				document.getElementById(div).innerHTML = update[0];
			}
			else
			{
				window.location.reload(true);
			}
		}
    }
}

function sendRqMidi(request_id, action)
{
	var div='request_'+request_id;
	xmlhttp.open('get', '/community/midifiles/midirequest.php?do='+action+'&rid='+request_id);
	xmlhttp.onreadystatechange = changNbRq;
	xmlhttp.send(null);
}

function changNbRq()
{
	if(xmlhttp.readyState == 4){
		if (xmlhttp.status == 200){
			window.location.reload(true);
		}
    }
}




/* =============================================================== */
var ratingAction = {
		'a.rater' : function(element){
			element.onclick = function(){

			var parameterString = this.href.replace(/.*\?(.*)/, "$1"); // onclick="sndReq('j=1&q=2&t=127.0.0.1&c=5');
			var parameterTokens = parameterString.split("&"); // onclick="sndReq('j=1,q=2,t=127.0.0.1,c=5');
			var parameterList = new Array();

			for (j = 0; j < parameterTokens.length; j++) {
				var parameterName = parameterTokens[j].replace(/(.*)=.*/, "$1"); // j
				var parameterValue = parameterTokens[j].replace(/.*=(.*)/, "$1"); // 1
				parameterList[parameterName] = parameterValue;
			}
			var theratingID = parameterList['q'];
			var theVote = parameterList['j'];
			var theuserIP = parameterList['t'];
			var theunits = parameterList['c'];
			
			//for testing	alert('sndReq('+theVote+','+theratingID+','+theuserIP+','+theunits+')'); return false;
			sndReq(theVote,theratingID,theuserIP,theunits); return false;		
			}
		}
		
	};
Behaviour.register(ratingAction);
