var SubmitStep = new Array();
var currentWin = "";
var uniqueEl = new Array();

function setValidatorColor() {
	$("#Form_"+currentWin).find("[@color='vRed']").css({ border : "1px solid #CCC", borderBottom : "3px solid #FF0000" });
	$("#Form_"+currentWin).find("[@color='vGreen']").css({ border : "1px solid #CCC", borderBottom : "3px solid #00FF00" });
}

var childNodeFunc = function(node) {
	if (node.nodeName && (node.nodeName !== '#text') && (node.nodeName !== '#comment'))
		if (null!=node.getAttribute("color")){
			if (node.getAttribute("color") == "vRed") SubmitStep[currentWin]++;
		}
}
for (var i=0; i<document.forms.length;i++){
    currentWin = $(document.forms[i]).attr("name");
    if (currentWin && currentWin != "searchForm") {
    	if (document.getElementById(currentWin+"_prefix")) formPrefix[currentWin] = document.getElementById(currentWin+"_prefix").innerHTML;
    	else formPrefix[currentWin] = "f"; 
    	setValidatorColor();
		var rootElement = document.getElementById(currentWin);
		setElmAttr(rootElement, "nodeType", "root");
	 	SubmitStep[currentWin] = 0;
		walkTree(rootElement, childNodeFunc);
		if (SubmitStep[currentWin] == 0){if (Browser != "IE6" && Browser != "IE7") setElmAttr(document.getElementById("saveButton_"+currentWin), "class", "save"); else document.getElementById("saveButton_"+currentWin).className="save";}
		remElmAttr(rootElement, "nodeType");
	}
}

function checkActiveElement(){
	if (ElementToSelect == true){}
	else {if (checkElements == 1) closeImgDiv();}
}

function createRealIds(){
	var elem = document.createElement("input");
	elem.setAttribute("id","realpid_level2");
	elem.setAttribute("type", "hidden");
	elem.setAttribute("value", "1");
	document.getElementById("errorElement_"+currentWin).appendChild(elem);

	var elem = document.createElement("input");
	elem.setAttribute("id","realpid_level3");
	elem.setAttribute("type", "hidden");
	elem.setAttribute("value", "1");
	document.getElementById("errorElement_"+currentWin).appendChild(elem);
}

function initNextSelect(sid, type){
// Выбор значений на основе Realty.php -> getObjectTypes
	if (document.getElementById("level2")) {
		var level2 = document.getElementById("level2").value;
		var selectVal = document.getElementById("robject");
		var selectName = document.getElementById("_robject");
		var selectOps = document.getElementById("ops_robject");
		var selectButton = document.getElementById("button_robject");
	
		if (level2 == 20) {
	    		selectName.value = "Гаражи"; selectVal.value = 9;
	    		selectButton.setAttribute("disabled", "true");
				selectName.setAttribute("disabled", "true");
		}
		else{
			var svalue = document.getElementById(sid).value;
			var tvalue = document.getElementById(type).value;
	
			if ((tvalue > 15) && (tvalue < 20)){
				switch (svalue){
					case "Однокомнатные" : selectButton.setAttribute("disabled", "true"); selectName.setAttribute("disabled", "true"); selectName.value = "1-комнатные квартиры"; selectVal.value = 1;break;
					case "Двухкомнатные" : selectButton.setAttribute("disabled", "true"); selectName.setAttribute("disabled", "true"); selectName.value = "2-комнатные квартиры"; selectVal.value = 2;break;
					case "Трехкомнатные" : selectButton.setAttribute("disabled", "true"); selectName.setAttribute("disabled", "true"); selectName.value = "3-комнатные квартиры"; selectVal.value = 3;break;
					case "Многокомнатные" : selectButton.setAttribute("disabled", "true"); selectName.setAttribute("disabled", "true"); selectName.value = "Многокомнатные квартиры"; selectVal.value = 4;break;
					case "Малосемейки, комнаты" : selectButton.removeAttribute("disabled");selectName.removeAttribute("disabled");selectOps.value = "5=Малосемейки|6=Комнаты|";
												  selectName.value = "Малосемейки"; selectVal.value = 5;break;
	//				selectButton.setAttribute("disabled", "true"); selectName.setAttribute("disabled", "true"); selectName.value = "Малосемейки, комнаты"; selectVal.value = 5;break;
					case "Дома, земельные участки" : selectButton.removeAttribute("disabled");selectName.removeAttribute("disabled");selectOps.value = "7=Дома|10=Земельные участки|";
			    									 selectName.value = "Дома"; selectVal.value = 7;break;
	//				selectButton.setAttribute("disabled", "true"); selectName.setAttribute("disabled", "true"); selectName.value = "Дома, земельные участки"; selectVal.value = 6;break;
					default :
						selectButton.removeAttribute("disabled"); selectName.removeAttribute("disabled");
						selectName.value = "-не указано-"; selectVal.value = 0;
	   					selectOps.value = "1=1-комнатные квартиры|2=2-комнатные квартиры|3=3-комнатные квартиры|4=Многокомнатные квартиры|5=Малосемейки|6=Комнаты|7=Дома|10=Земельные участки|";
				}
			}
			else if ((tvalue > 20) && (tvalue < 24)){
				switch (svalue){
					case "Коттеджи, дома" : selectButton.removeAttribute("disabled");selectName.removeAttribute("disabled");selectOps.value = "8=Коттеджи|7=Дома|";
											selectName.value = "Коттеджи"; selectVal.value = 8;break;
					//selectButton.setAttribute("disabled", "true"); selectName.setAttribute("disabled", "true"); selectName.value = "Коттеджи, дома"; selectVal.value = 1;break;
					case "Земельные участки" : selectButton.setAttribute("disabled", "true"); selectName.setAttribute("disabled", "true"); selectName.value = "Земельные участки"; selectVal.value = 10;break;
					default :
						selectButton.removeAttribute("disabled"); selectName.removeAttribute("disabled");
						selectName.value = "-не указано-"; selectVal.value = 0;
						selectOps.value = "8=Коттеджи|7=Дома|10=Земельные участки|";
				}
			}
			else if ((tvalue > 23) && (tvalue < 27)){
				switch (svalue){
					case "Здания" : selectButton.setAttribute("disabled", "true"); selectName.setAttribute("disabled", "true"); selectName.value = "Здания"; selectVal.value = 1;break;
					case "Офисы" : selectButton.setAttribute("disabled", "true"); selectName.setAttribute("disabled", "true"); selectName.value = "Офисы"; selectVal.value = 2;break;
					case "Магазины, торговые площади" : selectButton.removeAttribute("disabled");selectName.removeAttribute("disabled");selectOps.value = "3=Магазины|4=Торговые площади|";
														selectName.value = "Магазины"; selectVal.value = 3;break;
					//selectButton.setAttribute("disabled", "true"); selectName.setAttribute("disabled", "true"); selectName.value = "Магазины, торговые площади"; selectVal.value = 3;break;
					case "Производства и склады" : selectButton.removeAttribute("disabled");selectName.removeAttribute("disabled");selectOps.value = "5=Производства|6=Склады|";
												   selectName.value = "Производства"; selectVal.value = 5;break;
					//selectButton.setAttribute("disabled", "true"); selectName.setAttribute("disabled", "true"); selectName.value = "Производства и склады"; selectVal.value = 5;break;
					case "Земельные участки" : selectButton.setAttribute("disabled", "true"); selectName.setAttribute("disabled", "true"); selectName.value = "Земельные участки"; selectVal.value = 7;break;
					case "Продажа бизнеса" : selectButton.setAttribute("disabled", "true"); selectName.setAttribute("disabled", "true"); selectName.value = "Продажа бизнеса"; selectVal.value = 8;break;
					default :
						selectButton.removeAttribute("disabled");
						selectName.removeAttribute("disabled");
						selectName.value = "-не указано-"; selectVal.value = 0;
					    selectOps.value = "1=Здания|2=Офисы|3=Магазины|4=Торговые площади|5=Производства|6=Склады|7=Земельные участки|8=Продажа бизнеса|";
				}
			}
		}
	}
}

function hideFields(type){
	if (!type) type = 0;
    switch (type) {
		case "1" :
			$("#_aobject").parent().parent().css("display", "none");
			$("#_amodel").parent().parent().css("display", "none");
			break;
		case "2" :
			//$("#_level3").parent().parent().css("display", "none");
			$("#robject").parent().parent().css("display", "none");
            break;
		default :
			$("#_aobject").parent().parent().css("display", "none");
			$("#_amodel").parent().parent().css("display", "none");

			//$("#_level3").parent().parent().css("display", "none");
			$("#robject").parent().parent().css("display", "none");
    }
}

function initFields(){
	switch(document.getElementById("level1_f").value){
		case "1" :
				//$("#_level3").parent().parent().css("display", "");
				$("#robject").parent().parent().css("display", "");
		        hideFields("1");
				break;
		case "2" :
				$("#_aobject").parent().parent().css("display", "");
				$("#_amodel").parent().parent().css("display", "");
		        hideFields("2");
				break;
		default : hideFields("0");
	}
}

function tabChange(tabId, mode){
	if (!openTab[currentWin]) openTab[currentWin] = 0;
	if (openTab[currentWin] != tabId) {
		document.getElementById("errorElement_"+currentWin).style.display="none";
		if (document.getElementById("formContent"+tabId+"_"+currentWin)){
			if ((Browser == "IE6") || (Browser == "IE7")){
				document.getElementById("tab"+openTab[currentWin]+"_"+currentWin).setAttribute('className', '');
				if (mode=="realty") {
					document.getElementById("img"+tabId+"_"+currentWin).setAttribute('src', '/front/i/mark_left.gif');
					document.getElementById("img2"+tabId+"_"+currentWin).setAttribute('src', '/front/i/mark_right.gif');
				}
				else {
					document.getElementById("img"+tabId+"_"+currentWin).setAttribute('src', '/front/i/mark_left3.gif');
					document.getElementById("img2"+tabId+"_"+currentWin).setAttribute('src', '/front/i/mark_right3.gif');
				}			
				document.getElementById("tab"+tabId+"_"+currentWin).setAttribute('className', 'active');
	
			}
			else{
				document.getElementById("tab"+openTab[currentWin]+"_"+currentWin).setAttribute('class', '');
				if (mode=="realty") {
					document.getElementById("img"+tabId+"_"+currentWin).setAttribute('src', '/front/i/mark_left.gif');
					document.getElementById("img2"+tabId+"_"+currentWin).setAttribute('src', '/front/i/mark_right.gif');
				}
				else {
					document.getElementById("img"+tabId+"_"+currentWin).setAttribute('src', '/front/i/mark3_left.gif');
					document.getElementById("img2"+tabId+"_"+currentWin).setAttribute('src', '/front/i/mark3_right.gif');
				}
				document.getElementById("tab"+tabId+"_"+currentWin).setAttribute('class', 'active');
			}
	
			document.getElementById("formContent"+openTab[currentWin]+"_"+currentWin).style.display = 'none';
			document.getElementById("img"+openTab[currentWin]+"_"+currentWin).setAttribute('src', '/front/i/mark2_left.gif');
			document.getElementById("img2"+openTab[currentWin]+"_"+currentWin).setAttribute('src', '/front/i/mark2_right.gif');
			document.getElementById("formContent"+tabId+"_"+currentWin).style.display = '';
	
			openTab[currentWin] = tabId;
		}
	}
	return false;
}

function makePreview(){
	form = document.getElementById("Form_"+currentWin);

	if (document.getElementById("level1_f")) var level1 = document.getElementById("level1_f");
	else if (document.getElementById("level1")) var level1 = document.getElementById("level1");
    switch (level1.value) {
		case "1" : var url = "?act=realty"; break;
		case "2" : var url = "?act=auto"; break;
		default : var url = "?act=announcement";
    }

	if ((Browser == "IE6" || Browser == "IE7") && document.getElementById("street") && !document.getElementById("street").value) {
		alert("Укажите улицу!!!");
		return false;
	}

    var req = new JsHttpRequest();
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
			//document.getElementById("wcount_f").value = req.responseText.length;

			var res = req.responseText;
			if (url != "?act=announcement") {
				var patt = /([А-Яа-яA-Za-z0-9- ]+[\.|,])/;
	            result = res.replace(patt, '$1');
			}
			else result = res;

			patt=new RegExp("Цена \\d+ руб");
			var price = patt.exec(result);
			if (price){
				price+="";
				price = price.split(" ");
				result = result.replace(patt,'Цена '+price["1"]+' руб');
			}
 
 			if (Browser == "IE6" || Browser == "IE7") document.getElementById("preview_f").innerHTML = result;
 			else document.getElementById("preview_f").value = result;
        }
    }

    req.open(null, '/back/js/Asetting.php'+url, false);
    req.send( { q: form } );
}

if(document.getElementById("_tYardistrict")) $("#_tYardistrict").parent().parent().css("display", "none");
