function getRequestObject() {
	var requestObject = null;
	if (window.XMLHttpRequest) {
		requestObject = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
		requestObject = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return requestObject;
}

function processDetailBox(newInnerHTML) {
var requestWindow = document.getElementById("detailbox");
requestWindow.innerHTML = newInnerHTML;
}

function updateDetailBox(newInnerHTML) {
var requestWindow = document.getElementById("detailbox");
requestWindow.innerHTML = newInnerHTML;
}

function populateDetails(listingid) {
	subscriptionId = listingid;
	var xmlhttp = null;
	var listingurl = null;
	var categoryId = document.getElementById("currentcategoryid").value;
	if (listingid != undefined) {
		listingurl = "bgtrackview.cfm?tracktype=listing&subscriptionid=" + listingid + "&categoryid=" + categoryId;
	}
	else {
	 	return true;
	 }
	var details = document.getElementById("details");
	details.innerHTML = "<span class=\"loading\"><img src=\"bgimages/spinner.gif\" />loading...</span>";
	xmlhttp = getRequestObject();
	xmlhttp.open("GET", listingurl, true);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState==4) {
			details.innerHTML = xmlhttp.responseText;
			prepareDetailTabs();
			prepareEmailToFriend();
			preparePhoneLink(listingid);
			setCurrentListing(listingid);
			var hoverlisting = document.getElementsByClassName("hoverlisting");
			var fullClass = hoverlisting[0].className;
			var newClass = fullClass.replace(" hoverlisting","");
			hoverlisting[0].className = newClass;
		}
	}
	xmlhttp.send(null);
}

function changeDetailBox(boxToShow) {
	if (!document.getElementById("detailbox")) return false;
	var xmlhttp = null;
	var categoryId = document.getElementById("currentcategoryid").value;
	var listingId = document.getElementById("currentlistingid").value;
	url = "bgtrackview.cfm?tracktype=" + boxToShow + "&subscriptionid=" + listingId + "&categoryid="+ categoryId;
	var detailBox = document.getElementById("detailbox");
	detailBox.innerHTML = "<span class=\"loading\"><img src=\"bgimages/spinner.gif\" />loading...</span>";
	xmlhttp = getRequestObject();
	xmlhttp.open("GET", url, true);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState==4) {
			var detailBox = document.getElementById("detailbox");
   			detailBox.innerHTML = xmlhttp.responseText;
   			var infoTabBox = document.getElementById("infotabs");
   			var infoTabLinks = infoTabBox.getElementsByTagName("li");
   			for (var i = 0; i < infoTabLinks.length; i++) {
   				if (infoTabLinks[i].className.indexOf("currentinfotab") > -1) {
   					var fullClass = infoTabLinks[i].className;
					var newClass = fullClass.replace(" currentinfotab","");
					var newClass = newClass.replace("currentinfotab ","");
					var newClass = fullClass.replace("currentinfotab","");
					infoTabLinks[i].className = newClass;
   				}
   			}
   			var newCurrentTab = document.getElementById(boxToShow + "show");
   			newCurrentTab.className = newCurrentTab.className + "currentinfotab";
   			if (boxToShow == "emailvendor") {
   				prepareEmailFormValidation();
   			}
   			if (boxToShow == "requestfromvendor") {
   				prepareRFISelectAll();
   				prepareRequestFormValidation();
   			}
   		}
   	}
   	xmlhttp.send(null);
}

function getEmailToFriend() {
	var xmlhttp = null;
	var url = "bgtrackview.cfm?tracktype=emailfriend";
	var emailDiv = document.getElementById("emailtofriendcontainer");
	emailDiv.innerHTML = "<span class=\"loading\"><img src=\"bgimages/spinner.gif\" />processing...</span>";
	xmlhttp = getRequestObject();
	xmlhttp.open("GET", url, true);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState==4) {
			var response = xmlhttp.responseText;
			emailDiv.innerHTML = response;
			var detailBox = document.getElementById("detailbox");
			var emailFriendContainer = document.getElementById("emailtofriendcontainer");
			emailFriendContainer.style.display = "inline";
			//detailBox.style.height = "120px";
			if (document.getElementById("sendtofriendcancel")) {
				var cancelEmail = document.getElementById("sendtofriendcancel");
				cancelEmail.onclick = function() {
					if (!document.getElementById) return false;
					var emailFriendContainer = document.getElementById("emailtofriendcontainer");
					emailFriendContainer.style.display = "none";
					var detailBox = document.getElementById("detailbox");
					//detailbox.style.height = "260px";
					prepareEmailToFriend();
					return false;
				}
				EmailFriendValidation();
			}
		}
	}
	xmlhttp.send(null);
}

function processEmailToFriend(params) {
	var xmlhttp = null;
	var listingId = document.getElementById("currentlistingid").value;
	var url = "bgtrackview.cfm?subscriptionid=" + listingId;
	var emailDiv = document.getElementById("emailtofriendcontainer");
	emailDiv.className = "sucessmessage";
	emailDiv.innerHTML = "<span class=\"loading\"><img src=\"bgimages/spinner.gif\" />processing...</span>";
	xmlhttp = getRequestObject();
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState==4) {
			var detailBox = document.getElementById("detailbox");
			var response = xmlhttp.responseText;
			emailDiv.innerHTML = response;
			//detailbox.style.height = "260px";
			prepareEmailToFriend();
			emailDiv.className = "sucessmessage";
			Fat.fade_element("emailfriendsuccess");
			setTimeout("document.getElementById(\"emailtofriendcontainer\").style.display = \"none\";", 5000)
		}
	}
	xmlhttp.open("POST", url, true);
	xmlhttp.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded");
	xmlhttp.send(params);
}

function processEmailForm(params, verify) {
	var xmlhttp = null;
	var url = "bgtrackview.cfm?tracktype=emailvendor&subscriptionid=" + subscriptionId + "&vendorid=" + vendorId;
	var emailWindow = document.getElementById("detailbox");
	emailWindow.innerHTML = "<span class=\"loading\"><img src=\"bgimages/spinner.gif\" />processing...</span>";
	xmlhttp = getRequestObject();
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState==4) {
			var response = xmlhttp.responseText;
			emailWindow.innerHTML = response;
		}
	}
	xmlhttp.open("POST", url, true);
	xmlhttp.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded");
	xmlhttp.send(params);
}

function processRequestForm() {
	var requestWindow = document.getElementById("requestwindow");
	requestWindow.innerHTML = "<span class=\"loading\"><img src=\"bgimages/spinner.gif\" />processing...</span>";
	if (!document.getElementById) return false;
	if (!document.getElementById("requestform")) return false;
	var requestForm = document.getElementById("requestform");
	requestForm.action = "requestform.html";
}

function handleRequestFeedback(message) {
	var requestWindow = document.getElementById("requestwindow");
	requestWindow.innerHTML = "<p>Your request has been sent.  Thank you.</p>" + message;
}

var phoneIcon;

function getPhoneForDisplay(listingid) {
	var xmlhttp = null;
	var url = "bgtrackview.cfm?subscriptionid=" + listingid + "&tracktype=phone";
	var phoneLink = document.getElementById("viewphonelink");
	phoneIcon = document.getElementById("phoneicon");
	var phoneContainer = phoneLink.parentNode;
	phoneContainer.innerHTML = "<span class=\"loading\"><img src=\"bgimages/spinner.gif\" />loading...</span>";
	xmlhttp = getRequestObject();
	xmlhttp.open("GET", url, true);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState==4) {
			var response = xmlhttp.responseText;
			var phoneContainer = document.getElementById("phonecontainer");
			if (response != "not available") {
				phoneContainer.innerHTML = "";
				phoneContainer.appendChild(phoneIcon);
				phoneContainer.innerHTML = phoneContainer.innerHTML + response;
			}
			else {
				phoneContainer.innerHTML = "<p>We encountered a problem trying to send your request.  We apologize for any inconvenience and ask you to try again later.  Thank you.</p>";
			}
		}
	}
	xmlhttp.send(null);
}

function getURLForDisplay(listingid, vendorid) {
	var xmlhttp = null;
	var url = "bgtrackview.cfm?subscriptionid=" + listingid + "&tracktype=website&vendorid=" + vendorid;
	var urlLink = document.getElementById("viewsitelink");
	var urlContainer = urlLink.parentNode;
	urlContainer.innerHTML = "<span class=\"loading\"><img src=\"bgimages/spinner.gif\" />loading...</span>";
	xmlhttp = getRequestObject();
	xmlhttp.open("GET", url, true);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState==4) {
			var response = xmlhttp.responseText;
			var urlContainer = document.getElementById("urlcontainer");
			if (response != "not available") {
				urlContainer.innerHTML = response;
			}
			else {
				urlContainer.innerHTML = "<p>We encountered a problem trying to send your request.  We apologize for any inconvenience and ask you to try again later.  Thank you.</p>";
			}
		}
	}
	xmlhttp.send(null);
}