
var http_request = false;
var http_request2 = false;
   
function get(obj) {
	  
      var poststr = 'text=' + encodeURI( document.getElementById('commentstext').value ) + '&id=' + encodeURI( document.getElementById('commentid').value ) + '&quoteid=' + encodeURI( document.getElementById('quoteid').value );
      var gourl = 'http://www.wanknation.com/php/comments.php?action=add';
      makeSyncRequest(gourl, poststr);
}

function quoteComment(commentid) {
	  var gourl = 'http://www.wanknation.com/php/comments.php?action=quote&commentid=' + encodeURI(commentid);
	  makeSyncRequest(gourl, null);
}

function voteup(fileid) {
	  var poststr = 'id=' + fileid + '&action=up';
	  var gourl = 'http://www.wanknation.com/php/vote.php';
	  makeSyncRequest_vote(gourl, poststr, fileid);
}

function votedown(fileid) {
	  var poststr = 'id=' + fileid + '&action=down';
	  var gourl = 'http://www.wanknation.com/php/vote.php';
	  makeSyncRequest_vote(gourl, poststr, fileid);
}

function reportComment(commentid) {
	  var gourl = 'http://www.wanknation.com/php/comments.php?action=report&commentid=' + encodeURI(commentid);
	  makeSyncRequest(gourl, null);
}

function clearComment() {
	document.getElementById('quote').innerHTML = '<input type="hidden" id="quoteid" name="quoteid" value="null">'; 
	document.getElementById('quote').style.display = "none";
}

function searchOut(){
	if (document.getElementById('searchbox').value == ''){
		document.getElementById('searchbox').value = 'search';
	}
}
   
function makeSyncRequest_vote(doiturl, poststr, content_id){
	  
      http_request2 = false;
	   

      if (window.XMLHttpRequest) { // Mozilla, Safari,...
            http_request2 = new XMLHttpRequest();
            if (http_request2.overrideMimeType) {
                  http_request2.overrideMimeType('text/xml');
                  // See note below about this line
            }
      } else if (window.ActiveXObject) { // IE
            try {
                  http_request2 = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                  try {
                        http_request2 = new ActiveXObject("Microsoft.XMLHTTP");
                  } catch (e) {}
            }
      }
      if (!http_request2) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
      }
	  
	  if (poststr == null){
		   
		    http_request2.onreadystatechange = function() { alertContentsVote(http_request2, content_id); };
			http_request2.open('GET', doiturl, false);
      		http_request2.send(null);
	  }else{
		    http_request2.onreadystatechange = function() { alertContentsVote(http_request2, content_id); };
		  	http_request2.open('POST', doiturl, true);
			http_request2.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			http_request2.setRequestHeader("Content-length", poststr.length);
			http_request2.setRequestHeader("Connection", "close");
			http_request2.send(poststr);	
	  }
}

function makeSyncRequest(doiturl, poststr){
	  
      http_request = false;

      if (window.XMLHttpRequest) { // Mozilla, Safari,...
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                  http_request.overrideMimeType('text/xml');
                  // See note below about this line
            }
      } 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('Giving up :( Cannot create an XMLHTTP instance');
            return false;
      }
	  
	  if (poststr == null){
			http_request.open('GET', doiturl, false);
      		http_request.send(null);
			
			if(http_request.status == 200) {
				var emsreturn = http_request.responseText;
				if (emsreturn.indexOf(":emsnotify:") != -1){
					var emsnotify = emsreturn.split(":emsnotify:");
					emsPop(emsnotify[1]);
				}else{
					var responsearr = emsreturn.split(":ems:");
					document.getElementById('quote').innerHTML = 'Re: "' + responsearr[0] + '" - <strong>' + responsearr[2] + '</strong><br><span style="position: relative; float: left; margin: 5px 0px 5px 8px;"><a href="javascript:clearComment();" style="text-decoration: none; color: red; font-size: 80%; font-style: normal;">( - ) remove quote</a></span><input type="hidden" id="quoteid" name="quoteid" value="' + responsearr[1] + '">';    
					document.getElementById('quote').style.display = "block";
					document.getElementById('commentstext').focus();
				}
		  	}
	  }else{
		    http_request.onreadystatechange = function() { alertContents(http_request); };
		  	http_request.open('POST', doiturl, true);
			http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			http_request.setRequestHeader("Content-length", poststr.length);
			http_request.setRequestHeader("Connection", "close");
			http_request.send(poststr);	
	  }
}

function alertContents(hrequest){
	if (hrequest.readyState == 4) {
		if (hrequest.status == 200) {
			if (hrequest.responseText.indexOf(":emsnotify:") != -1){
				var emsnotify = hrequest.responseText.split(":emsnotify:");
				emsPop(emsnotify[1]);
			}else{
				document.getElementById('commentdiv').innerHTML = hrequest.responseText;
				document.getElementById('commentstext').value = '';
				document.getElementById('quote').style.display= 'none';
				document.getElementById('errorDiv').style.display= 'none';
			}
		} else {
			emsPop('There was a problem with the request.');
		}
	}
}

function alertContentsVote(hrequest, content_id){
	// alert('voting up via makeSyncRequest (in-request)!! ');
	if (hrequest.readyState == 4) {
		if (hrequest.status == 200) {
			if (hrequest.responseText.indexOf(":emsnotify:") != -1){
				var emsnotify = hrequest.responseText.split(":emsnotify:");
				alert(emsnotify[1]);
			}else{
				if (hrequest.responseText.indexOf(":emserror:") != -1){
					var emsnotify = hrequest.responseText.split(":emserror:");
					alert(emsnotify[1]);
				}else{
					document.getElementById('vote_box_' + content_id).innerHTML = '<p>' + hrequest.responseText + '</p>';
				}
			}
		} else {
			emsPop('There was a problem with the request.');
		}
	}
}

function tellFriend(){
	document.getElementById('telldiv').style.display='block';
	document.getElementById('tellclick').style.display='none';
}

function emsPop(msg){
	divStyle = document.getElementById('errorDiv').style;
	document.getElementById('errorDiv').innerHTML ='<img src="/img/error.gif" style="float: left;"><span>' + msg + '<br><br><a href="javascript: hideError();">Ok</a></span>';
	emsFade('errorDiv', 0, 100, 250);
}


function hideError(){
	emsFade('errorDiv', 100, 0, 250);
}

function showRollTip(msg, e) {
  if ( typeof RollTip == "undefined" || !RollTip.ready ) return;
  RollTip.reveal(msg, e);
}

function hideRollTip() {
  if ( typeof RollTip == "undefined" || !RollTip.ready ) return;
  RollTip.conceal();
}


function emsSet(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
    if (opacity == 0){
	object.display='none';
	object.visibility='hidden';
    }else{
	object.display='block';
	object.visibility='visible';
    }
}

function emsFade(id, opacStart, opacEnd, millisec) {
    var speed = Math.round(millisec / 100);
    var timer = 0;

    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("emsSet(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("emsSet(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
} 

