function getXMLHTTPRequest() {
    try {
	req = new XMLHttpRequest();
    } catch(err1) {
	try {
	    req = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (err2) {
	    try {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	    } catch (err3) {
		req = false;
	    }
	}
    }
    return req;
}

function vote(id, direction) {
    var url = 'vote.php?did=' + id + '&direction=' + direction + '&rand=' + parseInt(Math.random() * 99999999);
    var http = getXMLHTTPRequest();
    http.open("GET", url, true);
    http.onreadystatechange = function() {
	if (http.readyState == 4) {
	    var response = http.responseXML;
	    var errors = response.getElementsByTagName("error");
	    if (errors.length == 0) {
		document.getElementById("add_rating_" + id).style.display = "none";
		document.getElementById("rated_" + id).style.display = "block";
		count_node = document.getElementById("rating_count_" + id);
		new_count = parseInt(count_node.innerHTML);
		if (direction == 1) {
		    document.getElementById("rated_down_" + id).style.display = "none";
		    document.getElementById("rated_up_" + id).style.display = "inline";
		    new_count++;
		}
		else {
		    document.getElementById("rated_up_" + id).style.display = "none";
		    document.getElementById("rated_down_" + id).style.display = "inline";
		    new_count--;
		}
		count_node.innerHTML = new_count;
	    }
	    else if (errors[0].firstChild.nodeValue == '0') {
		document.location = 'login.php?rf=' + escape(url + "&rf=" + escape(document.location));
	    }
	    else {
		alert(errors[0].firstChild.nodeValue);
	    }
	}
    };
    http.send(null);
}

function favorite(id) {
    var url = 'favorite.php?did=' + id + '&rand=' + parseInt(Math.random() * 99999999);
    var http = getXMLHTTPRequest();
    http.open("GET", url, true);
    http.onreadystatechange = function() {
	if (http.readyState == 4) {
	    var response = http.responseXML;
	    var errors = response.getElementsByTagName("error");
	    if (errors.length == 0) {
		// mark it favorited
		document.getElementById("add_favorite_" + id).style.display = "none";
		document.getElementById("favorited_" + id).style.display = "block";
	    }
	    else if (errors[0].firstChild.nodeValue == '0') {
		document.location = 'login.php?rf=' + escape(url + "&rf=favorites.php");
	    }
	    else {
		alert(errors[0].firstChild.nodeValue);
	    }
	}
    };
    http.send(null);
}

function dead(id) {
    var url = 'dead.php?did=' + id;
    var http = getXMLHTTPRequest();
    http.open("GET", url, true);
    http.send(null);
    document.getElementById('dead_link').style.display = 'block';
    document.getElementById('dead_image').style.display = 'block';
    document.getElementById('live_link').style.display = 'none';
    document.getElementById('live_image').style.display = 'none';
}

function dead_pp(id) {
    var url = 'dead.php?did=' + id;
    var http = getXMLHTTPRequest();
    http.open("GET", url, true);
    http.send(null);
    document.getElementById('dead_link_' + id).style.display = 'block';
    document.getElementById('dead_image_' + id).style.display = 'block';
    document.getElementById('live_link_' + id).style.display = 'none';
    document.getElementById('live_image_' + id).style.display = 'none';
}

function submitDeal(args) {
    var url = 'submit_deal.php';
    var sd_url = document.getElementById('submit_deal_url').value;
    var sd_price = document.getElementById('submit_deal_price').value;
    var sd_instr = document.getElementById('submit_deal_instructions').value;
    var sd_cid = document.getElementById('submit_deal_cid').value;
    var sd_email = document.getElementById('submit_deal_email');
    url += "?cid=" + encodeURIComponent(sd_cid) + "&buy_url=" + encodeURIComponent(sd_url) + "&price=" + encodeURIComponent(sd_price) + "&special_instructions=" + encodeURIComponent(sd_instr)
    if (sd_email) { url += "&email=" + encodeURIComponent(sd_email.value); }
    var http = getXMLHTTPRequest();
    http.open("GET", url, true);
    http.onreadystatechange = function() {
	if (http.readyState == 4) {
	    var response = http.responseXML;
	    var errors = response.getElementsByTagName("error");
	    if (errors.length == 0) {
		document.getElementById('submit_deal_errors').style.display = 'none';
		document.getElementById('submit_deal_confirm').style.display = 'block';
		document.getElementById('submit_deal_url').value = "";
		document.getElementById('submit_deal_price').value = "";
		document.getElementById('submit_deal_instructions').value = "";
		tmp = document.getElementById('submit_deal_email');
		if (tmp) { tmp.value = ""; }


	    }
	    else if (errors[0].firstChild.nodeValue == '0') {
		document.location = 'login.php?rf=' + escape(url + "&rf=" + escape(document.location));
	    }
	    else {
		document.getElementById('submit_deal_errors').innerHTML = errors[0].firstChild.nodeValue;
		document.getElementById('submit_deal_errors').style.display = 'block';
		document.getElementById('submit_deal_confirm').style.display = 'none';
	    }
	}
    };
    http.send(null);
}

function createPriceHunt(args) {
    //document.getElementById('create_price_hunt_start').style.display = 'none';
    //document.getElementById('create_price_hunt_pending').style.display = 'block';
    var url = 'price_hunt.php?action=create';
    var pid = document.getElementById('create_price_hunt_pid').value;
    url += "&pid=" + encodeURIComponent(pid)
    var http = getXMLHTTPRequest();
    http.open("GET", url, true);
    http.onreadystatechange = function() {
	if (http.readyState == 4) {
	    var response = http.responseXML;
	    var errors = response.getElementsByTagName("error");
	    if (errors.length == 0) {
		contestId = response.getElementsByTagName("ContestID")[0].firstChild.nodeValue;
		//document.getElementById('create_price_hunt_pending').style.display = 'none';
		document.getElementById('create_price_hunt_errors').style.display = 'none';
		//document.getElementById('create_price_hunt_area').style.display = 'none';
		//document.getElementById('create_price_hunt_confirm').style.display = 'block';
		document.getElementById('submit_deal_cid').value = contestId;
		document.getElementById('price_hunt_entry_area').style.display = 'block';
	    }
	    else if (errors[0].firstChild.nodeValue == '0') {
		document.location = 'login.php?rf=' + escape(url + "&rf=" + escape(document.location));
	    }
	    else {
		document.getElementById('create_price_hunt_errors').innerHTML = errors[0].firstChild.nodeValue;
		//document.getElementById('create_price_hunt_pending').style.display = 'none';
		document.getElementById('create_price_hunt_errors').style.display = 'block';
		//document.getElementById('create_price_hunt_confirm').style.display = 'none';
		//document.getElementById('create_price_hunt_start').style.display = 'block';
	    }
	}
    };
    http.send(null);
}


function subscribe(args) {
    var url = 'subscribe.php';
    document.getElementById('deal_signup').style.display = 'none';
    document.getElementById('subscribe_pending').style.display = 'block';
    var email = document.getElementById('subscribe_email').value;
    url += "?action=subscribe&email=" + encodeURIComponent(email);
    var http = getXMLHTTPRequest();
    http.open("GET", url, true);
    http.onreadystatechange = function() {
	if (http.readyState == 4) {
	    var response = http.responseXML;
	    var errors = response.getElementsByTagName("error");
	    document.getElementById('subscribe_pending').style.display = 'none';
	    if (errors.length == 0) {
		document.getElementById('subscribe_errors').style.display = 'none';
		document.getElementById('subscribe_confirm').style.display = 'block';
		document.getElementById('subscribe_email').value = "Enter email...";
	    }
	    else {
		document.getElementById('subscribe_errors').style.display = 'block';
		document.getElementById('subscribe_confirm').style.display = 'none';
		document.getElementById('deal_signup').style.display = 'block';
	    }
	}
    };
    http.send(null);
}

function add_alert(id) {
    document.getElementById('alert_pending').style.display = 'block';
    var url = 'alert.php?did=' + id;
    var http = getXMLHTTPRequest();
    address = document.getElementById("email_drops_address");
    if (address) {
	url += "&email=" + encodeURIComponent(address.value);
    }
    http.open("GET", url, true);
    http.onreadystatechange = function() {
	if (http.readyState == 4) {
	    var response = http.responseXML;
	    var errors = response.getElementsByTagName("error");
	    if (errors.length == 0) {
		// mark it watched
		document.getElementById('alert_pending').style.display = 'none';
		form = document.getElementById('email_drops_confirm');
		if (form) form.style.display = 'block';
		form = document.getElementById('email_drops_errors');
		if (form) form.style.display = 'none';
	    }
	    else if (errors[0].firstChild.nodeValue == '0') {
		document.location = 'login.php?rf=' + escape(url + "&rf=deal-" + id + ".html");
	    }
	    else {
		document.getElementById('alert_pending').style.display = 'none';
		form = document.getElementById('email_drops_errors');
		if (form) form.style.display = 'block';
		form = document.getElementById('email_drops_errors_text');
		if (form) form.innerHTML = "Errors: " + errors[0].firstChild.nodeValue;
		form = document.getElementById('email_drops_confirm');
		if (form) form.style.display = 'none';
	    }
	}
    };
    http.send(null);
}