var subscriptionId = 0;
var vendorId = 0;


function enablePreloadedListing() {
	if (document.getElementById("currentlistingid")) {
		var listingId = document.getElementById("currentlistingid").value;
		prepareDetailTabs();
		prepareEmailToFriend();
		preparePhoneLink(listingId);
	}
}

function prepareListingRollovers() {
	if (!document.getElementsByClassName) return false;
	var listings = document.getElementsByClassName("listing");
	for (var i = 0; i < listings.length; i++) {
		listings[i].onmouseover = function () {
			if (this.getAttribute("id") && this.className.indexOf("premiercurrentlisting") == -1) {
				if (this.className.indexOf("hoverlisting") == -1) {
					this.className = this.className + " hoverlisting";
				}
				this.style.cursor = "pointer";
				this.setAttribute("title", "Click to view details.");
			}
			if (!this.getAttribute("id")) {
				if (this.className.indexOf("hoverlisting") == -1) {
					this.className = this.className + " hoverlisting";
				}
				this.style.cursor = "pointer";
				this.setAttribute("title", "Click to view details.");
			}
		}
		listings[i].onmouseout = function () {
			if (this.getAttribute("id") && this.className.indexOf("premiercurrentlisting") == -1) {
				var fullClass = this.className;
				var newClass = fullClass.replace(" hoverlisting","");
				this.className = newClass;
				this.style.cursor = "default";
				this.setAttribute("title", "");
			}
			if (!this.getAttribute("id")) {
				var fullClass = this.className;
				var newClass = fullClass.replace(" hoverlisting","");
				this.className = newClass;
				this.style.cursor = "default";
				this.setAttribute("title", "");
			}
		}
		listings[i].onclick = function () {
			var listingid = 0;
			if (this.getAttribute("id") != "currentlisting") {
				if (this.getAttribute("id")) {
					listingFullId = this.getAttribute("id");
					var idParams = listingFullId.split("_");
					listingId = idParams[0].replace("listing", "");
				}
				populateDetails(listingId);
				return false;
			}
		}
	}
	var nameLinks = document.getElementsByClassName("listingname");
	for (var i = 0; i < nameLinks.length; i++) {
		nameLinks[i].onclick = function() {
			return false;
		}
	}
	return true;
}

function setCurrentListing(listingId) {
	if (!document.getElementById) return false;
	if (!document.getElementsByClassName) return false;
	var oldCurrent = document.getElementsByClassName("premiercurrentlisting");
	if (oldCurrent.length > 0) {
		for (var i = 0; i < oldCurrent.length; i++) {
			var oldCurrentClass = oldCurrent[i].className;
			var newClass = oldCurrentClass.replace(" premiercurrentlisting", "");
			var newClass = oldCurrentClass.replace("premiercurrentlisting ", "");
			var newClass = oldCurrentClass.replace("premiercurrentlisting", "");
			oldCurrent[i].className = newClass;
		}
	}
	var oldCurrent = document.getElementsByClassName("enhancedcurrentlisting");
	if (oldCurrent.length > 0) {
		for (var i = 0; i < oldCurrent.length; i++) {
			var oldCurrentClass = oldCurrent[i].className;
			var newClass = oldCurrentClass.replace(" enhancedcurrentlisting", "");
			var newClass = oldCurrentClass.replace("enhancedcurrentlisting ", "");
			var newClass = oldCurrentClass.replace("enhancedcurrentlisting", "");
			oldCurrent[i].className = newClass;
		}
	}
	var oldCurrent = document.getElementsByClassName("basiccurrentlisting");
	if (oldCurrent.length > 0) {
		for (var i = 0; i < oldCurrent.length; i++) {
			var oldCurrentClass = oldCurrent[i].className;
			var newClass = oldCurrentClass.replace(" basiccurrentlisting", "");
			var newClass = oldCurrentClass.replace("basiccurrentlisting ", "");
			var newClass = oldCurrentClass.replace("basiccurrentlisting", "");
			oldCurrent[i].className = newClass;
		}
	}
	if (!document.getElementById("listing" + listingId)) return false;
	var currentListing = document.getElementById("listing" + listingId);
	if (currentListing.className.indexOf("premier") > 0) {
		currentListing.className = currentListing.className + " premiercurrentlisting";
	}
	else if (currentListing.className.indexOf("enhanced") > 0) {
			currentListing.className = currentListing.className + " enhancedcurrentlisting";
	}
	else if (currentListing.className.indexOf("basic") > 0) {
				currentListing.className = currentListing.className + " basiccurrentlisting";
	}
}

function prepareAdRollovers() {
	var oldCaption = "";
	if (!document.getElementsByClassName) return false;
	var sponsorAds = document.getElementsByClassName("adlink");
	for (var i = 0; i < sponsorAds.length; i++) {
		sponsorAds[i].onmouseover = function() {
			this.style.cursor = "pointer";
			var caption = this.parentNode.getElementsByTagName("p");
			for (var j = 0; j < caption.length; j++) {
				if (caption[j].className && caption[j].className == "adcaption") {
					oldCaption = this.parentNode.getAttribute("title");
					caption[j].innerHTML = "Link Opens In New Window";
				}
			}
		}
		sponsorAds[i].onmouseout = function() {
			this.style.cursor = "default";
			var caption = this.parentNode.getElementsByTagName("p");
			for (var j = 0; j < caption.length; j++) {
				if (caption[j].className && caption[j].className == "adcaption") {
					caption[j].innerHTML = oldCaption;
				}
			}
		}
	}
	return false;
}

function preparePhoneLink(listingId, vendorid) {
	if (!document.getElementById) return false;
	if (!document.getElementById("viewphonelink")) return false;
	var phoneLink = document.getElementById("viewphonelink");
	phoneLink.onclick = function() {
		getPhoneForDisplay(listingId, vendorid);
		return false;
	}
	return false;
}

function prepareURLLink(listingId, vendorid) {
	if (!document.getElementById) return false;
	if (!document.getElementById("viewsitelink")) return false;
	var siteLink = document.getElementById("viewsitelink");
	siteLink.onclick = function() {
		popUp("bgtrackview.cfm?subscriptionid=" + listingId + "&tracktype=website&vendorid=" + vendorid, "elastic", 300, 300);
	}
}

function prepareEmailToFriend() {
	if (!document.getElementById) return false;
	if (!document.getElementById("emailfriendlink")) return false;
	var emailFriendLink = document.getElementById("emailfriendlink");
	emailFriendLink.onclick = function() {
		if (!document.getElementById) return false;
		if (!document.getElementById("emailtofriendcontainer")) {
			var emailFriendContainer = document.createElement("div");
			emailFriendContainer.setAttribute("id", "emailtofriendcontainer");
			emailFriendContainer.className = "emailtofriendcontainer";
			var contactLinks = document.getElementById("contactlinks");
			insertAfter(emailFriendContainer, contactLinks);
		}
		else {
			var emailFriendContainer = document.getElementById("emailtofriendcontainer");
			emailFriendContainer.className = "emailtofriendcontainer";
		}
		changeEmailFriendLinkAction();
		getEmailToFriend();
		return false;
	}
	return true;
}

function changeEmailFriendLinkAction() {
	if (!document.getElementById("emailfriendlink")) return false;
	var emailFriendLink = document.getElementById("emailfriendlink");
	emailFriendLink.onclick = function() {
		var emailFriendContainer = document.getElementById("emailtofriendcontainer");
		emailFriendContainer.style.display = "none";
		var detailBox = document.getElementById("detailbox");
		detailbox.style.height = "260px";
		prepareEmailToFriend();
		return false;
	}
}

function prepareDetailTabs() {
	if (!document.getElementById) return false;
	if (!document.getElementsByTagName) return false;
	if (!document.getElementsByClassName) return false;
	if (!document.getElementById("infotabs")) return false;
	var infoTabList = document.getElementById("infotabs");
	var infoTabs = infoTabList.getElementsByTagName("li");
	for (var i = 0; i < infoTabs.length; i++) {
		infoTabs[i].onclick = function() {
			if (!this.getAttribute("id")) return true;
			var linkId = this.getAttribute("id");
			var showWindowId = linkId.replace("show", "");
			changeDetailBox(showWindowId);
			//if (!document.getElementById(showWindowId)) return false;
			//var showWindow = document.getElementById(showWindowId);
			//var allScrollWindows = document.getElementsByClassName("detailscrollwindow");
			//for (var j = 0; j < allScrollWindows.length; j++) {
			//	allScrollWindows[j].style.display = "none";
			//}
			//showWindow.style.display = "block";
			return false;
		}
	}
	return true;
}

function prepareRFISelectAll() {
	if (!document.getElementById) return true;
	if (!document.getElementById("sendtoall")) return true;
	if (!document.getElementById("requestcheckboxes")) return true;
	if (!document.getElementsByTagName) return false;
	var selectAllBox = document.getElementById("sendtoall");
	var premiumList = document.getElementById("requestcheckboxes");
	selectAllBox.onclick = function() {
		if (selectAllBox.checked == true) {
			var premiumList = document.getElementById("requestcheckboxes");
			if (premiumList != null){
				var premiumSelects = premiumList.getElementsByTagName("input");
				for (var i = 0; i < premiumSelects.length; i++) {
					if (premiumSelects[i].getAttribute("type") && premiumSelects[i].getAttribute("type") == "checkbox") {
						premiumSelects[i].checked = true;
					}
				}
			}
		}
		if (selectAllBox.checked == false) {
			var premiumList = document.getElementById("requestcheckboxes");
			if (premiumList != null){
				var premiumSelects = premiumList.getElementsByTagName("input");
				for (var i = 0; i < premiumSelects.length; i++) {
					if (premiumSelects[i].getAttribute("type") && premiumSelects[i].getAttribute("type") == "checkbox") {
						premiumSelects[i].checked = false;
					}
				}
			}
		}
	}
	if (premiumList != null) {
		var premiumSelects = premiumList.getElementsByTagName("input");
		for (var i = 0; i < premiumSelects.length; i++) {
			if (premiumSelects[i].getAttribute("type") && premiumSelects[i].getAttribute("type") == "checkbox") {
				premiumSelects[i].onclick = function() {
					if (this.checked == false) {
						var selectAllBox = document.getElementById("sendtoall");
						if (selectAllBox != null) {
							selectAllBox.checked = false;
						}
					}
					if (this.checked == true) {
						var allChecked = true;
						var premiumList = document.getElementById("requestcheckboxes");
						var selectAllBox = document.getElementById("sendtoall");
						if (premiumList != null) {
							var premiumSelects = premiumList.getElementsByTagName("input");
							for (var i = 0; i < premiumSelects.length; i++) {
								if (premiumSelects[i].getAttribute("type") && premiumSelects[i].getAttribute("type") == "checkbox") {
									if (premiumSelects[i].checked == false) {
										allChecked = false;
									}
								}
							}
						}
						if (selectAllBox != null && allChecked == true) {
							selectAllBox.checked = true;
						}
					}
				}
			}
		}
	}
	return true;
}

function fixHeaderWidthIE() {
	if (window.ActiveXObject) {
		document.getElementById("header").style.width = document.body.clientWidth;
		window.onresize = function() {
			if (document.body.clientWidth < 800) {
				document.getElementById("header").style.width = 800;
			}
			else {
				document.getElementById("header").style.width = document.body.clientWidth;
			}
		}
	}
}

addOnLoadEvent(prepareListingRollovers);
addOnLoadEvent(prepareAdRollovers);
addOnLoadEvent(prepareDetailTabs);
addOnLoadEvent(fixHeaderWidthIE);
addOnLoadEvent(enablePreloadedListing);
