﻿//ממלאה את תתי הקטגוריות ברשימה לפי הקטגוריה שנבחרה
function listSubcategories(category, subcategory) {
	//ניקוי הרשימה והוספת פריט כללי
	document.form.subcategory.length = 0;
	var opt = document.createElement("option");
	opt.style.color = "Gray";
	opt.text = "-- כל תתי הקטגוריות --";
	opt.value = "";
	try {
		document.form.subcategory.add(opt, null);	//standards compliant
	} catch(ex) {
		document.form.subcategory.add(opt);	//IE only
	}

	if (category != "") {
		var xmlHttp = getXmlHttpObject();

		xmlHttp.onreadystatechange = function() {
			if (xmlHttp.readyState == 4) {
				var subCatsArr = xmlHttp.responseText.split("<br />");
				var i, subCatArr, lastMidCat=null;

				//הוספת תתי הקטגוריות לרשימה
				for (i=0; i<subCatsArr.length; i++) {
					if (subCatsArr[i] != "") {
						subCatArr = subCatsArr[i].split("|");

						//הוסף קטגוריה אמצעית
						if (subCatArr[0] != lastMidCat) {
							opt = document.createElement("option");
							opt.value = subCatArr[0];
							opt.text = subCatArr[1];
							try {
								document.form.subcategory.add(opt, null);	//standards compliant
							} catch(ex) {
								document.form.subcategory.add(opt);	//IE only
							}

							lastMidCat = subCatArr[0];
						}

						opt = document.createElement("option");
						opt.style.fontWeight = "normal";
						opt.value = subCatArr[2];
						opt.text = "-- " + subCatArr[3];
						try {
							document.form.subcategory.add(opt, null);	//standards compliant
						} catch(ex) {
							document.form.subcategory.add(opt);	//IE only
						}
					}
				}

				//מצא את תת הקטגוריה אם צריך
				if (subcategory != "") {
					var list = document.getElementById("subcategory");

					for (i=0; i<list.options.length; i++) {
						if (list.options[i].value == subcategory) {
							list.selectedIndex = i;
							break;
						}
					}
				}
			}
		};
		xmlHttp.open("GET", "/addAgent.asp?submitValue=listSubcategories&category=" + category, true);
		xmlHttp.setRequestHeader("Content-Type", "text/html; charset=UTF-8");
		xmlHttp.setRequestHeader("Content-Language", "he");
		xmlHttp.send(null);
	}
}

//מסמנת את מילות החיפוש בתוך התוצאות
var globalSearchString = "";
function highlightSearchTerms(elementId, extraStyle) {
	if (globalSearchString != "") {
		var searchTerms = globalSearchString.split(" ");
		var results = document.getElementById(elementId).innerHTML;
		var searchTerm, replacementText;
		var patterns = ["(כ|ך)","(מ|ם)","(נ|ן)","(צ|ץ)"];
		var regex, lastGroupNum;

		for (var i in searchTerms) {
			searchTerm = searchTerms[i];
			replacementText = ">$1<span class=\"searchTerm\" style=\""+extraStyle+"\">$2"
			lastGroupNum = 3;

			for (var j in patterns) {
				regex = new RegExp(patterns[j], "ig");

				if (regex.test(searchTerm)) {
					searchTerm = searchTerm.replace(regex, patterns[j]);
					lastGroupNum++;
				}
			}

			results = results.replace(new RegExp(">([^<]*)?("+searchTerm+")([^>]*)?<","ig"), replacementText+"</span>$"+lastGroupNum+"<");
		}

		document.getElementById(elementId).innerHTML = results;
	}
}

//מפעילה/מבטלת את סימון מילות החיפוש
var highlight = true;
function toggleHighlight() {
	var spans = document.getElementsByTagName("span");
	highlight = !highlight;

	for (var i=0; i<spans.length; i++)
		 if (spans[i].className=="searchTerm") spans[i].style.backgroundColor = (highlight ? "#FFFF99" : "transparent");

	document.getElementById("highlightSwitch").innerHTML = (highlight ? "בטל הדגשה" : "הפעל הדגשה");
	document.getElementById("highlightSwitch").style.backgroundColor = (highlight ? "#FFFF99" : "transparent");
}

//פונקציה לסימון כל המודעות
var checked = true;
function selectAll() {
	var elements = document.getElementsByTagName("input");

	for (var i = 0; i < elements.length; i++) {
		if (elements[i].name.substring(0,1) == "B")
			elements[i].checked = checked;
	}

	checked = !checked;
}

function showObjectDetails(dir,objId,takeCity,category,subcategory,iseek) {
	if (document.getElementById("A"+objId) == null) {
		var xmlHttp = getXmlHttpObject();

		showPleaseWaitMsg = true;
		setTimeout("showPleaseWait("+objId+")", 1000);

		xmlHttp.onreadystatechange = function() {
			if (xmlHttp.readyState == 4) {
				showPleaseWaitMsg = false;
				document.getElementById("objectDetails"+objId).innerHTML = xmlHttp.responseText;
				highlightSearchTerms("objectDetails"+objId, "");

				//הפעלת הפרסומת הפנימית
				setInterval("animateAd("+objId+")", 2000);
			}
		};
		xmlHttp.open("GET", "cache/"+dir+"/"+objId+".asp?takeCity="+encodeURIComponent(takeCity.replace(/##QUOTATION_MARK##/g,"\""))+"&category="+category+"&subcategory="+subcategory+"&iseek="+encodeURIComponent(iseek.replace(/##QUOTATION_MARK##/g,"\""))+"&return="+encodeURIComponent(location.pathname+location.search)+"&r="+Math.random(), true);
		xmlHttp.setRequestHeader("Content-Type", "text/html; charset=UTF-8");
		xmlHttp.setRequestHeader("Content-Language", "he");
		xmlHttp.send(null);
	}
	else {
		show_details("A"+objId);
	}
}

//מציגה הודעת "אנא המתן" בעת טעינת פרטי החפץ
var showPleaseWaitMsg = true;
function showPleaseWait(objId) {
	if (showPleaseWaitMsg)
		document.getElementById("objectDetails" + objId).innerHTML = "<div id=\"pleaseWait\">פרטי המודעה נטענים.&nbsp;&nbsp;נא להמתין...</div>";
}

//מחליפה בין כותרת הפרסומת לתוכן - פרסומות פנימיות
function animateAd(id) {
	if (document.getElementById("linkTitle"+id).style.display=="none") {
		document.getElementById("linkTitle"+id).style.display="block";
		document.getElementById("linkText"+id).style.display="none";
	}
	else {
		document.getElementById("linkTitle"+id).style.display="none";
		document.getElementById("linkText"+id).style.display="block";
	}
}

//מתאימה את השדות השונים לפני החיפוש
function prepareSearch() {
	if (trim(document.form.tags.value) != "" && trim(document.form.tags.value) != "[כל החפצים באזור]") {
		document.form.iseek.value = document.form.tags.value;
		document.form.searchType.value = "textSearch";

	}
	else if (document.form.subcategory.value != "") {
		document.form.iseek.value = document.form.subcategory.value;
		document.form.searchType.value = (document.form.subcategory.value.length == 3 ? "middleCategory" : "subCategory");

	}
	else if (document.form.category.value != "") {
		document.form.iseek.value = document.form.category.value;
		document.form.searchType.value = "categorySearch";
	}
	else if (document.form.searchType.value != "userSearch") {
		document.form.iseek.value = "";
		document.form.searchType.value = "searchAll";
	}
}

//עידכון תת קטגוריה עבור הילה
function updateSubcategory(dealId,optionValue) {
	var newCategory = optionValue.substring(0,1);
	var newSubcategory = optionValue.substring(2);
	var xmlHttp = getXmlHttpObject();

	eval("document.form.subcategories"+dealId).style.backgroundColor = "White";
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4) eval("document.form.subcategories"+dealId).style.backgroundColor = "#D6EB9C";
	};
	xmlHttp.open("GET", "manager/subcategoriesManager.asp?action=updateSubcategory&dealId="+dealId+"&newCategory="+newCategory+"&newSubcategory="+newSubcategory, true);
	xmlHttp.send(null);
}

//ממקמת את הבאנר האנכי תוך התחשבות בגודל החלון
function positionVerticalBanner() {
	var myWidth = 0, myHeight = 0;

	if (screen.availWidth >= 1024) {
		//הצג את הבאנר
		repositionVerticalBanner();
		window.onresize = repositionVerticalBanner;
	}

	function repositionVerticalBanner() {
		//חישוב מימדי חלון הדפדפן
		if (typeof(window.innerWidth) == "number") {	//Non-IE
			myWidth = window.innerWidth;
			myHeight = window.innerHeight;
		}
		else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {	//IE 6+ in 'standards compliant mode'
			myWidth = document.documentElement.clientWidth;
			myHeight = document.documentElement.clientHeight;
		}
		else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {	//IE 4 compatible
			myWidth = document.body.clientWidth;
			myHeight = document.body.clientHeight;
		}

		document.getElementById("verticalBanner").style.top = Math.round((myHeight-600)/2)+"px";
		document.body.style.paddingLeft = (myWidth>=950 ? "140px" : "0px");
		document.getElementById("verticalBanner").style.display = (myWidth>=950 ? "block" : "none");
	}
}