
//Quick thanks to Apple Developer Network for parts of this code - http://developer.apple.com/internet/webcontent/xmlhttpreq.html

var XMLRequest = Array ();
var RequestStr = Array ();
var ResponseStr = new String;
var cacheRequest = new getXMLRequest ();

function icVote  (user, id, coupon){

	url = base_url + "func/func_vote.php?id=" + id + "&user=" + user + "&coupon=" + coupon;
	if(cacheRequest) {
		XMLRequest[id] = new getXMLRequest ();
		XMLRequest[id].open("GET", url, true);
		XMLRequest[id].send("");
		XMLRequest[id].onreadystatechange = function () {
			// only if XMLRequest shows "loaded"
			if (XMLRequest[id].readyState == 4) {
				RequestStr[id] = XMLRequest[id].responseText;
				error_topic = new RegExp ("^ERROR:");
				if (RequestStr[id].match (error_topic)) {
					RequestStr[id] = RequestStr[id].substring (6, RequestStr[id].length);
					alert (RequestStr[id]);
					displayVote (id, true, 'votes');
					displayVoters(id);
				} else {
					warn_topic = new RegExp ("^WARN:");
					if (RequestStr[id].match (warn_topic)) {
					alert(RequestStr[id]);
					} else {
						displayVote (id, false, 'votes');
						displayVoters(id);
					}
				}
			}
		}
	} else {alert ('Couldn\'t process xml request.');}
}

function icBury  (user, id, coupon){

	url = base_url + "func/func_bury.php?id=" + id + "&user=" + user + "&coupon=" + coupon;
	if(cacheRequest) {
		XMLRequest[id] = new getXMLRequest ();
		XMLRequest[id].open("GET", url, true);
		XMLRequest[id].send("");
		XMLRequest[id].onreadystatechange = function () {
			// only if XMLRequest shows "loaded"
			if (XMLRequest[id].readyState == 4) {
				RequestStr[id] = XMLRequest[id].responseText;
				error_topic = new RegExp ("^ERROR:");
				if (RequestStr[id].match (error_topic)) {
					RequestStr[id] = RequestStr[id].substring (6, RequestStr[id].length);
					alert (RequestStr[id]);
					displayVote (id, true, 'burys');
					displayVoters(id);
				} else {
					warn_topic = new RegExp ("^WARN:");
					if (RequestStr[id].match (warn_topic)) {
					alert(RequestStr[id]);
					} else {
						displayVote (id, false, 'burys');
						displayVoters(id);
					}
				}
			}
		}
	} else {alert ('Couldn\'t process xml request.');}
}

function displayVote (id, error, type)
{
	pattern = new RegExp ("~--~");
	vote = RequestStr[id].split (pattern);
	total = vote[0].split(',');
	if (error) {
		disableVote(id, "Try again later.");
		return false;
	}
	if (vote.length <= 3) {
		html_element = document.getElementById (type+'-' + id);
		html_element.innerHTML = total[0];
		html_element = document.getElementById ('votetotal-' + id);
		html_element.innerHTML = total[1];
		disableVote(id, "Vote cast!");
	}
	return false;
}

function disableVote(id, msg) {
	html_element = document.getElementById ('icContent-' + id);
	if (html_element) {
		html_element.innerHTML = '<center>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:green;"><img src="img/thoughtfarm/alertGreenVote.gif">'+msg+'</span></center>';
	}
}

function displayVoters(id) {
		get_votes('func_voters.php', 'voters', 'voters-container',1, id);
}

function getXMLRequest ()
{
	var req;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest) {
    	try {
			req = new XMLHttpRequest();
        } catch(e) {
			req = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		req = false;
        	}
		}
    }
	
	if(!req) {alert ('Couldn\'t process xml request.');}
	return req;
}

function get_content(program,container,u,page) {
	var url = base_url + 'func/'+program+'?u='+u+'&p='+page;
	contentRequest = new getXMLRequest ();
	contentRequest.open('get', url, true);
	contentRequest.send("");
	contentRequest.onreadystatechange = function () {
		if(contentRequest.readyState == 4){
			response = contentRequest.responseText;
			if (response.length > 10) {
				document.getElementById(container).innerHTML = response;
			}
		}
	}
}

function get_votes(program,type,container,page,id) {
	var url = base_url + 'func/'+program+'?id='+id+'&p='+page+'&type='+type;
	votesRequest = new getXMLRequest ();
	votesRequest.open('get', url, true);
	votesRequest.send("");
	votesRequest.onreadystatechange = function () {
		if(votesRequest.readyState == 4){
			response = votesRequest.responseText;
			if (response.length > 10) {
				document.getElementById(container).innerHTML = response;
			}
		}
	}
}
