function makeHttpRequest(url, callback_function, return_xml)
{
	var http_request = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari,...
	   http_request = new XMLHttpRequest();
	   if (http_request.overrideMimeType) {
	       http_request.overrideMimeType('text/xml');
	   }
	} 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('Unfortunately your browser does not support this feature.');
		return false;
	}
	http_request.onreadystatechange = function() {
	   if (http_request.readyState == 4) {
	       if (http_request.status == 200 || http_request.status == 0) {
	           if (return_xml) {
	               eval(callback_function + '(http_request.responseXML)');
	           } else {
	               eval(callback_function + '(http_request.responseText)');
	           }
	       } else {
	           alert('There was a problem with the request. (Code: ' + http_request.status + ')');
	       }
	   }
	}
	http_request.open('GET', url, true);
	http_request.send(null);
}

function add_to_trolley(product_id, product_type) {
	if (product_type == 'used') {
		product_type = 'Used';
	}
	makeHttpRequest('/Trolley/add.dyn?id=' + product_id + '&type=' + product_type + '&xml=true', 'confirm_trolley', true);
}

function confirm_trolley(xmldoc) {
	var product_id = xmldoc.getElementsByTagName('product_id').item(0).firstChild.nodeValue;
	var status = xmldoc.getElementsByTagName('status').item(0).firstChild.nodeValue;
	var trolley_button = document.getElementById('trolleyButton[' + product_id + ']');
	var view_trolley_link = document.getElementById('viewTrolleyLink[' + product_id + ']');
	if (status == 'added') {
		trolley_button.src = '/images/buttons/add-to-trolley.on.gif';
		if (view_trolley_link.innerHTML == '') {
			view_trolley_link.innerHTML = '<br><small><a href="/Trolley/">View your Trolley</a></small>';
		}
	}
	else if (status == 'removed') {
		trolley_button.src = '/images/buttons/add-to-trolley.gif';
		if (view_trolley_link.innerHTML == '') {
			view_trolley_link.innerHTML = '<br><small><a href="/Trolley/">View your Trolley</a></small>';
		}
	}
	else if (status == 'redirect') {
		var relocate_url = xmldoc.getElementsByTagName('relocate_url').item(0).firstChild.nodeValue;
		window.location = relocate_url;
	}
}

function rate_product(product_id, rating, referrer, votepage) {
	makeHttpRequest('/ajax/rate-product.dyn?id=' + product_id + '&r=' + rating + '&referrer=' + referrer + '&votepage=' + votepage, 'confirm_rating', true);
}

function confirm_rating(xmldoc) {
	var status = xmldoc.getElementsByTagName('status').item(0).firstChild.nodeValue;
	var rating = xmldoc.getElementsByTagName('rating').item(0).firstChild.nodeValue;
	var votepage = xmldoc.getElementsByTagName('votepage').item(0).firstChild.nodeValue;
	var current_rating = xmldoc.getElementsByTagName('current_rating').item(0).firstChild.nodeValue;
	var ratings = xmldoc.getElementsByTagName('ratings').item(0).firstChild.nodeValue;
	if (ratings > 1) {
		vote_or_votes = 'ratings';
	}
	else {
		vote_or_votes = 'rating';
	}
	if (status == true) {
		document.getElementById('customer_rating').src = '/images/prod/stars-' + rating + '.gif';
		document.getElementById('vote_placed').style.display = '';
		document.getElementById('average_rating').innerHTML = '<img src="/images/prod/stars.' + current_rating + '.gif" width=53 height=10 alt="' + current_rating + ' stars" border=0> (' + ratings + ' ' + vote_or_votes + '.)';
		document.getElementById('map_1_star').onmouseout = function() { document.getElementById('customer_rating').src='/images/prod/stars-' + rating + '.gif'; }
		document.getElementById('map_2_star').onmouseout = function() { document.getElementById('customer_rating').src='/images/prod/stars-' + rating + '.gif'; }
		document.getElementById('map_3_star').onmouseout = function() { document.getElementById('customer_rating').src='/images/prod/stars-' + rating + '.gif'; }
		document.getElementById('map_4_star').onmouseout = function() { document.getElementById('customer_rating').src='/images/prod/stars-' + rating + '.gif'; }
		document.getElementById('map_5_star').onmouseout = function() { document.getElementById('customer_rating').src='/images/prod/stars-' + rating + '.gif'; }
	}
	else {
		window.location = votepage;
	}
}

function toggle_wishlist(product_id, referrer, nextpage) {
	makeHttpRequest('/ajax/wishlist.dyn?id=' + product_id + '&referrer=' + referrer + '&n=' + nextpage, 'confirm_wishlist', true);
}

function confirm_wishlist(xmldoc) {
	var status = xmldoc.getElementsByTagName('status').item(0).firstChild.nodeValue;
	var toggle = xmldoc.getElementsByTagName('toggle').item(0).firstChild.nodeValue;
	var nextpage = xmldoc.getElementsByTagName('nextpage').item(0).firstChild.nodeValue;
	
	if (status == true) {
		document.getElementById('wishlist_button').src = '/images/buttons/add-to-wishlist.' + toggle + '.gif';
	}
	else {
		window.location = nextpage;
	}
}

function toggle_collection(product_id, referrer, nextpage) {
	makeHttpRequest('/ajax/collection.dyn?id=' + product_id + '&referrer=' + referrer + '&n=' + nextpage, 'confirm_collection', true);
}

function confirm_collection(xmldoc) {
	var status = xmldoc.getElementsByTagName('status').item(0).firstChild.nodeValue;
	var toggle = xmldoc.getElementsByTagName('toggle').item(0).firstChild.nodeValue;
	var nextpage = xmldoc.getElementsByTagName('nextpage').item(0).firstChild.nodeValue;
	
	if (status == true) {
		document.getElementById('collection_button').src = '/images/buttons/add-to-collection.' + toggle + '.gif';
	}
	else {
		window.location = nextpage;
	}
}

function email_to_friend(email_address, product_id, referrer, nextpage) {
	makeHttpRequest('/ajax/email-to-friend.dyn?e=' + email_address + '&id=' + product_id + '&referrer=' + referrer + '&n=' + nextpage, 'confirm_email_to_friend', true);
}

function confirm_email_to_friend(xmldoc) {
	var status = xmldoc.getElementsByTagName('status').item(0).firstChild.nodeValue;
	var nextpage = xmldoc.getElementsByTagName('nextpage').item(0).firstChild.nodeValue;
	var email_address = xmldoc.getElementsByTagName('email_address').item(0).firstChild.nodeValue;
	var referrer = xmldoc.getElementsByTagName('relocate_url').item(0).firstChild.nodeValue;
	
	if (status == 'sent') {
		document.getElementById('friends_address').innerHTML = 'Your e-mail message has been sent to ' + email_address + '.';
		document.getElementById('sent_to_friend').style.display = '';
	}
	else if (status == 'invalid') {
		document.getElementById('friends_address').innerHTML = 'That e-mail address appears to be invalid.';
		document.getElementById('sent_to_friend').style.display = '';
	}
	else {
		nextpage = nextpage + '&referrer=' + referrer;
		window.location = nextpage;
	}
}

function tag_product(tag, product_id, referrer, nextpage) {
	makeHttpRequest('/ajax/tag-product.dyn?t=' + tag + '&id=' + product_id + '&referrer=' + referrer + '&n=' + nextpage, 'confirm_tag_product', true);
}

function confirm_tag_product(xmldoc) {
	var status = xmldoc.getElementsByTagName('status').item(0).firstChild.nodeValue;
	var nextpage = xmldoc.getElementsByTagName('nextpage').item(0).firstChild.nodeValue;
	var tag = xmldoc.getElementsByTagName('tag').item(0).firstChild.nodeValue;
	var referrer = xmldoc.getElementsByTagName('relocate_url').item(0).firstChild.nodeValue;
	
	if (status == '1') {
		var result = xmldoc.getElementsByTagName('result').item(0).firstChild.nodeValue;
		var tag_html = xmldoc.getElementsByTagName('tags').item(0).firstChild.nodeValue;
		var your_tags_count = xmldoc.getElementsByTagName('your_tags_count').item(0).firstChild.nodeValue;
		if (result == 'added') {
			document.getElementById('tag_status_text').innerHTML = 'Your tag, "' + tag + '", has been added.';
			if (document.getElementById('your_tags').innerHTML.match(/^No tags.+/)) {
				document.getElementById('your_tags').innerHTML = '<span id="tags[' + tag + ']">' + tag_html + '</span>';
			}
			else {
				document.getElementById('your_tags').innerHTML += ' <span id="tags[' + tag + ']">' + tag_html + '</span>';
			}
		}
		else if (result == 'removed') {
			document.getElementById('tag_status_text').innerHTML = 'Your tag, "' + tag + '", has been removed.';
			//tag_html = tag_html.replace(/\?/g, '\\\?');
			//tag_html = tag_html.replace(/</g, '&lt;');
			//tag_html = tag_html.replace(/>/g, '&gt;');
			//tag_html = tag_html.replace(/\"/g, '\\\"');
			//tag_html = tag_html.replace(/\'/g, '\\\'');
			//var tag_html_regex = new RegExp(tag_html, 'g');
			//alert(tag_html_regex);
			//newstring = document.getElementById('your_tags').innerHTML.replace(tag_html_regex, '');
			tag_id = 'tags[' + tag + ']';
			document.getElementById(tag_id).style.display = 'none';
			if (your_tags_count == 0) {
				document.getElementById('your_tags').innerHTML = 'No tags yet.';
			}
		}
		if (result == 'added' || result == 'removed') {
			document.getElementById('tag_status').style.display = '';
		}
	}
	else if (status == 'invalid') {
		// do nothing
	}
	else {
		nextpage = nextpage + '&referrer=' + referrer;
		window.location = nextpage;
	}
}