var xmlhttp
if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
{
try {
xmlhttp = new XMLHttpRequest ();
}
catch (e) {
xmlhttp = false}
}
function myXMLHttpRequest ()
{
var xmlhttplocal;
if (!xmlhttplocal && typeof XMLHttpRequest != 'undefined') {
try {
var xmlhttplocal = new XMLHttpRequest ();
}
catch (e) {
var xmlhttplocal = false;
}
}
return (xmlhttplocal);
}
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); }
var mnmxmlhttp = Array ();
var xvotesString = Array ();
var mnmPrevColor = Array ();
var responsestring = Array ();
var myxmlhttp = Array ();
var responseString = new String;


function voteup (id, md5, value)
{
  	if (xmlhttp) {
		url = "/movie/voteup.php";
		var mycontent = "id=" + id + "&md5=" + md5 + "&value=" + value;
    		mnmxmlhttp[id] = new myXMLHttpRequest ();
    		if (mnmxmlhttp) {
    			mnmxmlhttp[id].open ("POST", url, true);
    			mnmxmlhttp[id].setRequestHeader ('Content-Type',
    					   'application/x-www-form-urlencoded');
    //alert(mnmxmlhttp[id]);
    			mnmxmlhttp[id].send (mycontent);
    			errormatch = new RegExp ("^ERROR:");    
    			//target1 = document.getElementById ('xvotes-' + id);    
    		//	mnmPrevColor[id] = target1.style.backgroundColor;
    			mnmxmlhttp[id].onreadystatechange = function () {
    				if (mnmxmlhttp[id].readyState == 4) {
    					xvotesString[id] = mnmxmlhttp[id].responseText;
    					//alert(mnmxmlhttp[id].responseText);
    					if (xvotesString[id].match (errormatch)) {
    						xvotesString[id] = xvotesString[id].substring (6, xvotesString[id].length);
							changemnmvalues (id, value, true);    					} else {
														if (xvotesString[id].substring (1, 6) == "ERROR") { 
                            
                } else {
                changemnmvalues ('up',id, value, false); 
                }     					}
    				}
    			}
    		}
    
	}
}

function flag (id, md5, value)
{
  	if (xmlhttp) {
		url = "/movie/flag.php";
		var mycontent = "id=" + id + "&md5=" + md5 + "&value=" + value;
    		mnmxmlhttp[id] = new myXMLHttpRequest ();
    		if (mnmxmlhttp) {
    			mnmxmlhttp[id].open ("POST", url, true);
    			mnmxmlhttp[id].setRequestHeader ('Content-Type',
    					   'application/x-www-form-urlencoded');
    //alert(mnmxmlhttp[id]);
    			mnmxmlhttp[id].send (mycontent);
    			errormatch = new RegExp ("^ERROR:");    
    			//target1 = document.getElementById ('xvotes-' + id);    
    		//	mnmPrevColor[id] = target1.style.backgroundColor;
    			mnmxmlhttp[id].onreadystatechange = function () {
    				if (mnmxmlhttp[id].readyState == 4) {
    					xvotesString[id] = mnmxmlhttp[id].responseText;
    					//alert(mnmxmlhttp[id].responseText);
    					if (xvotesString[id].match (errormatch)) {
    						xvotesString[id] = xvotesString[id].substring (6, xvotesString[id].length);
							changemnmvalues (id, value, true);    					} else {
														if (xvotesString[id].substring (1, 6) == "ERROR") { 
                            
                } else {
                changemnmvalues ('flag',id, value, false); 
                }     					}
    				}
    			}
    		}
    
	}
}

function changemnmvalues (vote_type, id, value, error)
{
	split = new RegExp ("~--~");
	b = xvotesString[id].split (split);
	//alert(id);
	//alert("--" + b[0]+"--");
	if( b[0]!=''){
  	if(vote_type=='up'){
  	 target1 = document.getElementById ('xapplaud-' + id);
	if(b[0].trim()=="1"){
	target1.innerHTML = "" + b[0].trim() + " person likes this";	
	  	target2 = document.getElementById ('xvotes-' + id);
  	 target2.innerHTML = b[0].trim();
	} else if(b[0].trim()=="0"){
	target1.innerHTML = "Your submission has been recorded";
	} else {
	target1.innerHTML = "" + b[0].trim() + " people likethis";
	  	target2 = document.getElementById ('xvotes-' + id);
  	 target2.innerHTML = b[0].trim();
	}

  	 target3 = document.getElementById ('xvote-' + id);
  	 target3.innerHTML = "Thanks"; 
  	 target4 = document.getElementById ('xbox-' + id);
  	 newImage = "url(/movie/images/vote-l.png)";
     target4.style.backgroundImage = newImage; 	 
    } else if (vote_type=='down'){
      target1 = document.getElementById ('xsmite-' + id);
      target1.innerHTML = "(-" + b[0].trim() + ")";
    }  else {
      target1 = document.getElementById ('xflag-' + id);
      target1.innerHTML = " (" + b[0].trim() + ")";
    } 
	}
	return false;
}
