function popUp(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=350,height=450');");
}

function popUpDistricts(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=800,height=500');");
}

function popUpMap(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=800,height=500');");
}

function getElementsByName_iefix(tag, name) {
     var elem = document.getElementsByTagName(tag);
     var arr = new Array();
     for(i = 0,iarr = 0; i < elem.length; i++) {
          att = elem[i].getAttribute("name");
          if(att == name) {
               arr[iarr] = elem[i];
               iarr++;
          }
     }

     return arr;
}

function escapeHTML(str) {
	var div = document.createElement('div');
	var text = document.createTextNode(str);
	div.appendChild(text);
	return div.innerHTML;
}

function collapseListings() {
	var els = document.getElementsByName("listingItem");
	if(els.length == 0) {
		els = getElementsByName_iefix("tr", "listingItem");
	}

	if(!(els) || els.length == 0) {
		return;
	}

	for(var i = 0; i < els.length - 1; ++i) {
		els[i].style.display = "none";
	}
}

function expandListings() {
	var els = document.getElementsByName("listingItem");
	if(els.length == 0) {
		els = getElementsByName_iefix("tr", "listingItem");
	}

	if(!(els) || els.length == 0) {
		return;
	}

	for(var i = 0; i < els.length - 1; ++i) {
		els[i].style.display = "";
	}
}

function updateSubCategories(e, e2) {
	if(!(e)) {
		if(window.event) {
			e = window.event;
		} else {
			return;
		}
	}

	var el;
	if(e.srcElement) {
		el = e.srcElement;
	} else if(e.targetElement) {
		el = e.targetElement;
	} else if(e.currentTarget) {
		el = e.currentTarget;
	} else if(typeof(e) == "object") {
		el = e;
	} else {
		return;
	}

	var sType = e2;
	if(!(sType)) {
		return;
	}

	while(sType.firstChild) {
		sType.removeChild(sType.firstChild);
	}

	if(sType.getAttribute("updateRelated")) {
		var eltmp = document.getElementById(sType.getAttribute("updateRelated"));
		if(eltmp) {
			while(eltmp.firstChild) {
				eltmp.removeChild(eltmp.firstChild);
			}

			var ctmp2 = document.createElement("option");
			ctmp2.innerHTML = "Select";
			ctmp2.value = "";
			ctmp2 = eltmp.appendChild(ctmp2);
		}
	}

	var ctmp = document.createElement("option");
	ctmp.innerHTML = "Select";
	ctmp.value = "";
	ctmp = sType.appendChild(ctmp);

	el = el.options[el.selectedIndex];
	if(!(el)) {
		return;
	}

	var tmp = el.getAttribute("subcategories");
	if(!(tmp) || tmp.length == 0) {
		return;
	}

	ctmp.disabled = true;

	var subCategories = tmp.split("|");
	for(var i = 0; i < subCategories.length; ++i) {
		var tmp2 = subCategories[i];

		var subCategoryId   = 0;
		var subCategoryName = "";
		var citiesStr       = "";

		subCategoryId = parseInt(tmp2.substring(0, tmp2.indexOf(':')));
		subCategoryName = tmp2.substring(tmp2.indexOf(':') + 1, tmp2.length);

		if(subCategoryName.indexOf('<') != -1) {
			var tmpStr = subCategoryName.substring(subCategoryName.indexOf('<') + 1, subCategoryName.indexOf('>'));
			subCategoryName = subCategoryName.substring(0, subCategoryName.indexOf('<'));

			var cities = tmpStr.split(" ");
			for(var j = 0; j < cities.length; ++j) {
				var tmp3 = cities[j];

				var cityId   = 0;
				var cityName = "";

				cityId = parseInt(tmp3.substring(0, tmp3.indexOf('~')));
				cityName = tmp3.substring(tmp3.indexOf('~') + 1, tmp3.length);

				citiesStr += (cityId + ":" + cityName);

				if(j < cities.length - 1) {
					citiesStr += "|";
				}
			}
		}

		ctmp = document.createElement("option");
		ctmp.innerHTML = subCategoryName;
		ctmp.alt = subCategoryName;
		ctmp.title = subCategoryName;
		ctmp.value = subCategoryId;
		ctmp.setAttribute("category", el.value);

		if(citiesStr && citiesStr.length > 0) {
			ctmp.setAttribute("subcategories", citiesStr);
		}

		sType.appendChild(ctmp);
	}

	/*if(sType.options.length > 1) {
		sType.selectedIndex = 1;
	}*/

	sType.focus();
}

function updateFormFields(e, e2) {
	if(!(e)) {
		if(window.event) {
			e = window.event;
		} else {
			return;
		}
	}

	var el;
	if(e.srcElement) {
		el = e.srcElement;
	} else if(e.targetElement) {
		el = e.targetElement;
	} else if(e.currentTarget) {
		el = e.currentTarget;
	} else if(typeof(e) == "object") {
		el = e;
	} else {
		return;
	}

	var tmp, 
	    tmpA  = document.getElementsByName("hAutomobileFields"), 
	    tmpRa = document.getElementsByName("hRealEstateFieldsA"), 
	    tmpRb = document.getElementsByName("hRealEstateFieldsB");

	if(tmpA.length == 0) {
		tmpA = getElementsByName_iefix("tr", "hAutomobileFields");
	}

	if(tmpRa.length == 0) {
		tmpRa = getElementsByName_iefix("tr", "hRealEstateFieldsA");
	}

	if(tmpRb.length == 0) {
		tmpRb = getElementsByName_iefix("tr", "hRealEstateFieldsB");
	}

	for(var i = 0; i < tmpA.length; ++i) {
		tmpA[i].style.position = "absolute";
		tmpA[i].style.display  = "none";
	}

	for(i = 0; i < tmpRa.length; ++i) {
		tmpRa[i].style.position = "absolute";
		tmpRa[i].style.display  = "none";
	}

	for(i = 0; i < tmpRb.length; ++i) {
		tmpRb[i].style.position = "absolute";
		tmpRb[i].style.display  = "none";
	}

	el = el.options[el.selectedIndex];
	if(!(el)) {
		return;
	}

	if(e2) {
		// Category changed.
		switch(parseInt(el.value)) {
			default:
				break;
		}
	} else {
		// Subcategory changed.
		switch(parseInt(el.value)) {
			// Automobiles
			case 1: case 20: case 6: case 22:
				for(i = 0; i < tmpA.length; ++i) {
					tmpA[i].style.position = "";
					tmpA[i].style.display  = "";
				}
				break;

			// Real Estate (Commercial & Residential Properties)
			case 23:
				for(i = 0; i < tmpRa.length; ++i) {
					tmpRa[i].style.position = "";
					tmpRa[i].style.display  = "";
				}
				break;

			// Real Estate (Land)
			 case 26:
				for(i = 0; i < tmpRb.length; ++i) {
					tmpRb[i].style.position = "";
					tmpRb[i].style.display  = "";
				}
				break;

			default:
				break;
		}
	}
}

var sCatOrig, sTypeOrig, sCityOrig;

function verifyForm() {
	var sCat, sType, sCity, tmp;
	sCat  = document.getElementById("cId");
	sType = document.getElementById("sId");
	sCity = document.getElementById("city");
	if(!(sCat) || !(sType) || !(sCity)) {
		return false;
	}

	if(!(sCatOrig)) {
		sCatOrig = sCat.parentNode.style.border;
	} else {
		sCat.parentNode.style.border = sCatOrig;
	}

	if(!(sTypeOrig)) {
		sTypeOrig = sType.parentNode.style.border;
	} else {
		sType.parentNode.style.border = sTypeOrig;
	}

	if(!(sCityOrig)) {
		sCityOrig = sCity.parentNode.style.border;
	} else {
		sCity.parentNode.style.border = sCityOrig;
	}

	tmp = sCat.options[sCat.selectedIndex];
	if(!(tmp) || !(tmp.value)) {
		sCat.parentNode.style.border = "2px dashed red";
		sCat.focus();
		return false;
	}

	tmp = sType.options[sType.selectedIndex];
	if(!(tmp) || !(tmp.value)) {
		sType.parentNode.style.border = "2px dashed red";
		sType.focus();
		return false;
	}

	tmp = sCity.options[sCity.selectedIndex];
	if(!(tmp) || !(tmp.value)) {
		sCity.parentNode.style.border = "2px dashed red";
		sCity.focus();
		return false;
	}

	return true;
}

function submitForm() {
	document.listingsForm.submit();
}

function addAttachment(id, name, description, type) {
	var anchor  = document.getElementById("attachmentsAnchor"),
	    anchorN = document.getElementById("attachmentsNotFound");
	if(!(anchor)) {
		return;
	}

	if(anchorN) {
		anchorN.parentNode.removeChild(anchorN);
	}

	var el = anchor.cloneNode(true);
	el.id = "listingAttachmentItem";
	el.name = "listingAttachmentItem";
	el.style.display = "";

	el.childNodes[0].innerHTML = '<a target="hiddenFrame" href="javascript:;" onclick="confirmRemoveAttachment(\'stallAttachments.jsp?deleteAttachment=true&aId=' + escapeHTML(id) + '\');" style="float: right;"><img alt="Delete attachment" title="Delete attachment" border="0" width="16" height="16" src="/images/icon_deletelisting.gif" /></a>' + escapeHTML(name);
	if(description && description.length > 0) {
		el.childNodes[0].innerHTML += ' <em>(' + escapeHTML(description) + ')</em>';
	}

	el.childNodes[1].innerHTML = escapeHTML(type);
	if(type == "Image") {
		el.childNodes[2].innerHTML = '<img alt="' + escapeHTML(description) + '" title="' + escapeHTML(description) + '" src="stallAttachments.jsp?thumbnail=true&aId=' + escapeHTML(id) + '" style="border: 1px solid black;" />';
	} else {
		el.childNodes[2].innerHTML = '<em>(No preview)</em>';
	}

	anchor.parentNode.insertBefore(el, anchor);
}

function addAttachmentError(str) {
	alert(str);
}

function confirmRemoveAttachment(url) {
	if(confirm("Are you sure you want to remove the selected attachment?")) {
		frames.hiddenFrame.location.href = url;
	}
}

function removeAttachment(id) {
	var anchor  = document.getElementById("attachmentsAnchor"),
	    anchorN = document.getElementById("attachmentsNotFound");
	if(!(anchor)) {
		return;
	} else if(anchorN) {
		return;
	}

	var el = anchor.parentNode.firstChild;
	var numAttachments = 0;

	while(el) {
		if(el.name == 'listingAttachmentItem' || el.id == 'listingAttachmentItem') {
			numAttachments++;

			if(el.innerHTML.indexOf('aId=' + escapeHTML(id)) != -1) {
				numAttachments--;

				var tmp = el.nextSibling;
				el.parentNode.removeChild(el);
				el = tmp;
				continue;
			}
		}

		el = el.nextSibling;
	}

	if(numAttachments == 0) {
		anchorN = document.createElement('tr');
		anchorN.id = 'attachmentsNotFound';

		var tmp = document.createElement('td');
		tmp.colSpan = 3;
		tmp.align = "center";
		tmp.style.border = "2px solid #efefef";
		tmp.style.padding = "6px";
		tmp.innerHTML = '<br /><em>(No attachments found)</em><br /><br />';
		anchorN.appendChild(tmp);

		anchor.parentNode.insertBefore(anchorN, anchor);
	}
}

function removeAttachmentError(str) {
	alert(str);
}
