
var http = createRequestObject(); 

function createRequestObject(){
	var requestObject; 
	var browser = navigator.appName; 
	if(browser == "Microsoft Internet Explorer"){
		requestObject = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		requestObject = new XMLHttpRequest();
	}
	return requestObject; 
}

function check() {
	alertTest();
}

function alertTest() {
	checkDownloads();
	
}

function setReply(replyId) {
	document.getElementById('postCommentWrapper').style.display = 'block';
	document.getElementById('replyTarget').value = replyId;
	document.getElementById('commentDescription').innerHTML = "Post a Reply&nbsp;&nbsp;<a style=\"font-size:12px\" href=\"#postComment\" class=\"darkLink\" onClick=\"resetReply()\">Post a NEW Comment</a>\n";
}

function resetReply() {
	document.getElementById('replyTarget').value = 0;
	document.getElementById('commentDescription').innerHTML = "Post a Comment";
}

function checkCmt() {
	
	if (document.getElementById('commentValue').value == "Type a Comment") {
		document.getElementById('commentValue').innerHTML = '';
		document.getElementById('commentValue').value = '';
		document.getElementById('commentValue').style.color = 'black';
	}
}

	
function switchComments() {
	var currentState = document.getElementById('commentDiv').style.display;
	if (currentState == "block") {
		document.getElementById('commentDiv').style.display = 'none';
		document.getElementById('postCommentWrapper').style.display = 'none';
		document.getElementById('switchLink').innerHTML = 'Show';
	}
	else {
		document.getElementById('commentDiv').style.display = 'block';
		document.getElementById('postCommentWrapper').style.display = 'block';
		document.getElementById('switchLink').innerHTML = 'Hide';
	}
	return true;
}
function postComment(ntxId, userId) {

	var httpx = createRequestObject();
	var commentValue = document.getElementById('commentValue').value;
	var needsDisplay = document.getElementById('needsDisplay').value;
	var displayAdd = "";
	var localDisplayName;
	if (needsDisplay == 1) {
		localDisplayName = document.getElementById('displayName').value;
		displayAdd = "&nd=1&displayName=" + localDisplayName;
	}
	var replyTarget = document.getElementById('replyTarget').value;
	
	httpx.open('get', '/ajaxHandler.php?action=postComment&ntxId=' + ntxId + '&userId='+userId+'&repTarget='+replyTarget+'&comment='+encodeURIComponent(commentValue)+displayAdd);
	httpx.onreadystatechange = function(foo) {
		if (httpx.readyState == 4) {	

			//var rez = eval(httpx.responseText);

			var response = eval(httpx.responseText);
			if (response[0] == 0) {
			
				
				document.getElementById('commentDiv').innerHTML = response[1]['html'];
				document.getElementById('commentSuccess').style.display = 'block';
				document.getElementById('commentError').style.display = 'none';
				document.getElementById('showUserName').style.display = 'none';
				document.getElementById('hideUserName').style.display = 'block';
				document.getElementById('needsDisplay').value = 0;
				if (needsDisplay == 1) {
					document.getElementById('hideUserName').innerHTML = localDisplayName;
				}
				document.getElementById('commentValue').value = "Type a Comment";
				document.getElementById('commentValue').style.color = '#999999';
				document.getElementById('commentDiv').style.display = 'block';
				document.getElementById('postCommentWrapper').style.display = 'none';
				postCount = parseInt(response[1]['posts']);
				if (postCount > 1) {
					tag = " comments";
				}
				else {
					tag = "comment";
				}
				
				document.getElementById('commentNumber').innerHTML = postCount + " " + tag;
				location.href="#postHeader";
				
				//document.getGetElementById('contentDiv').innerHTML = "";
				
			}
			else if (response[0] == 1) {

				document.getElementById('commentSuccess').style.display = 'none';
				document.getElementById('commentError').style.display = 'block';
				document.getElementById('errorBody').innerHTML = response[1];
				location.href="#commentErrorPointer";
			}
			

			
		}
	}
	httpx.send(null);
	
}

function pollMessagesStart(inviteId, fbuId, lastId) {

	lastMessageId = lastId;
	//pollMessages(inviteId, fbuId);

	//setTimeout('pollMessages('+inviteId+','+fbuId+')', 15000);
}

function fadeTip() {
	if (tipOpacity > 0) {
		
		document.getElementById('searchTip').style.opacity = tipOpacity;
		document.getElementById('searchTip').style.filter = 'alpha(opacity=' + tipOpacity + ')';

		tipOpacity = tipOpacity - 5;
		setTimeout('fadeTip()', 1);
	}
}

function pauseDownload(downloadId, refId) {
	var httpx = createRequestObject();
	userHash = document.getElementById('hashValue').value;
	
	httpx.open('get', 'http://beta.gygan.com/socketHandler.php?action=pauseDownload&downloadId=' + downloadId + '&refId='+refId+'&hashContact='+userHash);
	httpx.onreadystatechange = function(foo) {
		if (httpx.readyState == 4) {	

		}
	}
	httpx.send(null);
}

function startDownload(downloadId, refId) {
	userHash = document.getElementById('hashValue').value;
var httpx = createRequestObject();
	httpx.open('get', 'http://beta.gygan.com/socketHandler.php?action=startDownload&downloadId=' + downloadId + '&refId='+refId+'&hashContact='+userHash);
	httpx.onreadystatechange = function(foo) {
		if (httpx.readyState == 4) {	
		}
	}
	httpx.send(null);
}


function addDownload() {
	userHash = document.getElementById('hashValue').value;
	downloadString = document.getElementById('downloadLink').value;
	
	var httpx = createRequestObject();
	httpx.open('get', 'http://beta.gygan.com/socketHandler.php?action=requestDownload&link='+encodeURIComponent(downloadString)+'&hashContact='+userHash);
	httpx.onreadystatechange = function(foo) {
		if (httpx.readyState == 4) {	
				var rez = httpx.responseText;				
		}
	}
	httpx.send(null);	
}

function checkDownloads() {
	userHash = document.getElementById('hashValue').value;
	var httpx = createRequestObject();
	httpx.open('get', 'http://beta.gygan.com/socketHandler.php?action=checkDownloads&hashContact='+userHash);
	httpx.onreadystatechange = function(foo) {
		if (httpx.readyState == 4) {	

				var rez = httpx.responseText;
				
				if (rez.length > 2) {

					setTimeout('alertTest()', 5000);
					//var xrez = eval("(" + rez + ")");	
					document.getElementById('loadingBar').style.display = 'none';
					document.getElementById('remoteError').style.display = 'none';
					document.getElementById('transferContainerDownload').style.display = 'block';
					document.getElementById('transferContainerDownload').innerHTML = rez;
				}
				else {
					document.getElementById('transferContainerDownload').style.display = 'none';
					document.getElementById('remoteError').style.display = 'block';
					document.getElementById('loadingBar').style.display = 'none';
				}
		}
	}
	httpx.send(null);
}

