
//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, area, v_display, visual){

	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', area, v_display, visual);
					if (area != 'short' && area != 'small'){
					displayVoters(id, 'votes');}
				} else {
					warn_topic = new RegExp ("^WARN:");
					if (RequestStr[id].match (warn_topic)) {
					alert(RequestStr[id]);
					} else {
						displayVote (id, false, 'votes', area, v_display, visual);
						if (area != 'short' && area != 'small'){
						displayVoters(id, 'votes');}					
					}
				}
			}
		}
	} else {alert ('Couldn\'t process xml request.');}
}

function icBury  (user, id, coupon, area, v_display, visual){

	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', area, v_display, visual);
					if (area != 'short' && area != 'small'){
					displayVoters(id, 'burys');}
				} else {
					warn_topic = new RegExp ("^WARN:");
					if (RequestStr[id].match (warn_topic)) {
					alert(RequestStr[id]);
					} else {
						displayVote (id, false, 'burys', area, v_display, visual);
						if (area != 'short' && area != 'small'){
						displayVoters(id, 'burys');}
					}
				}
			}
		}
	} else {alert ('Couldn\'t process xml request.');}
}

function displayVote (id, error, type, area, v_display, visual)
{
	pattern = new RegExp ("~--~");
	vote = RequestStr[id].split (pattern);
	total = vote[0].split(',');
	if (error) {
		disableVote(id, "- try again.", "none", area, visual);
		return false;
	}
	if (vote.length <= 3) {
		if (v_display == type) {
		html_element = document.getElementById (type+'-' + id);
		html_element.innerHTML = total[0];
		}
		//html_element = document.getElementById ('votetotal-' + id);
		//html_element.innerHTML = total[1];
		disableVote(id, "cast!", type, area, visual);
	}
	return false;
}

function disableVote(id, msg, type, area, visual) {
	html_element = document.getElementById ('v-area-' + id);
	if (type == 'votes') {vote_color = '#0263AA'; v_icon = 'votes/alertGreenVote.gif';}else{vote_color = 'red'; v_icon = 'votes/alertRedVote.gif';}
	if (area == 'full') {v_class = 'v_voted_media';v_message = '';v_style='style="padding-bottom:2px;"';}else{v_class = 'v_voted';v_message = '<span class="v_vote_title">Voted</span>';v_style='';}
	if (html_element) {
		if (area == 'full') {html_element.innerHTML = '<span class="'+v_class+'"><img '+v_style+' src="/img/visuals/'+visual+'/'+v_icon+'"></span>'+v_message;}else{html_element.innerHTML = '<span style="float:left; width:0px;"><img class="'+v_class+'" src="/img/visuals/'+visual+'/'+v_icon+'"></span>'+v_message;}	
	}
}

function displayVoters(id, type) {
	display_votes('func_voters.php', type+'-container', 'voters', id, 1);
}

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 display_content(func,container,u,page) {
	var url = base_url + 'func/'+func+'?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 display_votes(func,container,type,id,page) {
	
	var url = base_url + 'func/'+func+'?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;
			}
		}
	}
}
