var ajaxcomm = 0;
var ajaxArray = new Array();
var cSearch = "";

function fSearch(obj){
	if(obj.value != cSearch){	
		obj2 = fGetObject("content");
		if(obj2)
			obj2.style.display = "none";
			
		obj2 = fGetObject("searchresult");
		if(obj2)
			obj2.style.display = "block";
			
		cSearch = obj.value;
		if(obj.value.length > 2){
			fGetObject("searchresult").innerHTML = '<table width="511" height="250" border="0" cellspacing="0" cellpadding="0" style="padding-left:15px;"><tr><td align="left" valign="top" style="padding:20px 20px 140px 25px;"><h1>Search results</h1>freshening...</td></tr></table>';
			xmlhttpPostSearch("../doajax.php?type=search", "str_search="+String(obj.value), "commGateSearch");
		}else if(obj.value.length > 0){
			tmp = '<table width="511" height="250" border="0" cellspacing="0" cellpadding="0" style="padding-left:15px;"><tr><td align="left" valign="top" style="padding:20px 20px 140px 25px;"><h1>Search results</h1>search starts at the third pushed character</td></tr></table>';
			obj2 = fGetObject("searchresult");
			if(obj)
				obj2.innerHTML = tmp;
		}else{
			obj2 = fGetObject("searchresult");
			if(obj2)
				obj2.style.display = "none";
				
			obj2 = fGetObject("content");
			if(obj2)
				obj2.style.display = "block";
		}
	}
}


function commGateSearch(str){
	var aTmp = fGetToken(str, "<optikmed>");
	var aSearch = new Array;
	var vszam = new Number(0);
	
	tmp = '<table width="511" height="250" border="0" cellspacing="0" cellpadding="0" style="padding-left:15px;"><tr><td align="left" valign="top" style="padding:20px 20px 140px 25px;"><h1>Search results</h1>';
	
	if(aTmp[0] == -1 || aTmp[0] == "")
		tmp += 'there are no properties that match your search criteria';
	else{
		for(q=1; q<aTmp.length; q++){
			tmp += '<div style="border-top:1.5px dotted #53533C; padding-bottom:10px; padding-top:5px;"><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td align="left" valign="middle">'+aTmp[q]+'<div style="padding-top:5px;" align="right"><a class="link1" href="'+aTmp[++q]+'">more ></a></div></td></tr></table></div>';
		}
		
		tmp += '<div style="border-top:1.5px dotted #53533C; padding-bottom:10px; padding-top:5px;"> </div>';
	}
	
	tmp += '</td></tr></table>';
	
	obj2 = fGetObject("searchresult");
	if(obj2)
		obj2.innerHTML = tmp;
}

function fSelectDay(obj){
	ajaxArray.push(Array("doajax.php?type=selectday","pid=" + String(obj.id) + "&ptmp=" + String(obj.value),"commGatefSelectDay"));
	fCallxmlhttpPost();
}

function commGatefSelectDay(str){
	ajaxcomm = 0;
	var aTmp = fGetToken(str, "<optikmed>");
	
	if(aTmp[0] == "date_from_day")
		sstyle = "styled_k";
	else
		sstyle = "styled_k2";
	
	tmp = "<select id='"+ aTmp[0]+"' class='"+sstyle+"' name='"+ aTmp[0]+"'>";		
	
	for(q=1; q<aTmp.length; q++){
		if(fGetObject(aTmp[0]).value == aTmp[q])
			sselected = "selected";
		else
			sselected = "";
			
		tmp += '<OPTION style="background-color:#b28025; color:#dedede;" value="'+aTmp[q]+'" '+sselected+'>'+aTmp[++q]+'</OPTION>';
	}
		
	tmp += "</select>";
	
	obj = fGetObject("div_" + aTmp[0]);
	if(obj)
		obj.innerHTML = tmp;		
	
	if(aTmp[0] == "date_from_day"){
		Custom_k.init();
		fCheckDatum(0);		
	}else{
		Custom_k2.init();
		fCheckDatum(1);
	}
}

function commGateGeneral(str){
	ajaxcomm = 0;
	var aTmp = fGetToken(str, "<optikmed>");
	if(aTmp[0] != -1)
		eval(aTmp[1]);
}

function fSearchAll(obj, stype){
	if(stype == "undefined")
		stype = 1;
		
	if(obj.value == ""){
		cSearch = "";
		obj2 = fGetObject("searchResultList");
		if(obj2)
			obj2.innerHTML = '<span class="refresh">the search is running automatically during typing (joker character:*)</span>';		
	}else if(obj.value != cSearch || stype == 2){
		cSearch = obj.value;
		fGetObject("content").style.display = "none";
		fGetObject("searchresult").style.display = "block";
		
		fGetObject("searchResultList").innerHTML = "searching...";
		
		var strtmp = new String("");
		var strsend = new String("");
		for(q=1;q<5;q++){
			strtmp = new String(fGetObject("filter_"+String(q)).src);
			strtmp = strtmp.substr(strtmp.lastIndexOf("/")+1);
	
			if(strtmp == "cb.gif")
				strsend += "&filter_"+String(q)+"=0";
			else
				strsend += "&filter_"+String(q)+"=1";
		}
		xmlhttpPostSearch("../doajax.php?type=searchweb", "str_search="+String(obj.value)+strsend+"&searchall="+String(searchall), "commGateResultTable");
	}
}

function commGateResultTable(str){
	var aTmp = fGetToken(str, "<optikmed>");
	var first = true;
	var strclass = "tf";
	tmp = '<table width="100%" debug border="0" cellspacing="0" cellpadding="0">';
	
	tmpcount = 0;
	for(q=0; q<aTmp.length; q++){
		tmpcount++;
		musttopline = false;
		if(aTmp[q] == "<title>"){
			tmp += '<tr><td><img src="../images/spacer.gif" width="1" height="10" /></td></tr>';
			tmp += '<tr><td valign="middle" bgcolor="#857040" class="search_list_section_title" style="padding-left:5px; padding-top:3px; padding-bottom:3px;">'+aTmp[++q]+'</td></tr>';
			tmp += '<tr><td><img src="../images/spacer.gif" width="1" height="5" /></td></tr>';
			musttopline = true;
		}else if(aTmp[q] == "<more>"){
			tmp += '<tr><td><img src="../images/spacer.gif" width="1" height="10" /></td></tr>';
			tmp += '<tr><td align="right" style="text-align:right;"><a class="link_10" href="javascript:fSearchAll(\''+aTmp[++q]+'\')">show all</a></td></tr>';
			tmp += '<tr><td><img src="../images/spacer.gif" width="1" height="5" /></td></tr>';
		}else if(aTmp[q] == "<toomuch>"){
			q++;
			tmp += '<tr><td><img src="../images/spacer.gif" width="1" height="10" /></td></tr>';
			tmp += '<tr><td align="right" style="text-align:right;" class="form_text">too many search results, narrow your search...</td></tr>';
			tmp += '<tr><td><img src="../images/spacer.gif" width="1" height="5" /></td></tr>';
		}else if(aTmp[q] == "<item>"){	
			tmp += '<tr><td valign="top" onclick="document.location=\''+aTmp[++q]+'\';" onmouseover="fOverSearchItem(\'result_'+String(tmpcount)+'\', this)" onmouseout="fOutSearchItem(\'result_'+String(tmpcount)+'\', this)" style="cursor:pointer; background-image: url(../images/search_list_bg.gif); background-repeat:no-repeat; background-position:bottom;"><table width="100%" border="0" cellspacing="0" cellpadding="0">';
			
			if(musttopline)
				tmp += '<tr><td valign="top" bgcolor="#dbdbdc" id="result_'+String(tmpcount-1)+'_v"><img src="../images/spacer.gif" width="1" height="1" /></td></tr>';
			
            tmp += '<tr><td><img src="../images/spacer.gif" width="1" height="5" /></td></tr><tr><td height="40" valign="top">';
			
			tmp += aTmp[++q].replaceAll("<rid>", String(tmpcount));
			
			tmp += '</td></tr><tr><td><img src="../images/spacer.gif" width="1" height="5" /></td></tr><tr><td bgcolor="#dbdbdc" id="result_'+String(tmpcount)+'_v"><img src="../images/spacer.gif" width="1" height="1" /></td></tr></table></td></tr>'
			musttopline = false;
		}
	}
		
	tmp += '</table>';
	
	obj = fGetObject("searchResultList");
	if(obj)
		obj.innerHTML = tmp;
}

function fCallxmlhttpPost(){
	if(ajaxcomm == 0 && ajaxArray.length>0){
		ajaxcomm = 1;
		param = ajaxArray.shift()
		xmlhttpPost(param[0], param[1], param[2]);
	}else if(ajaxcomm == 1 && ajaxArray.length>0){
		setTimeout("fCallxmlhttpPost()", 500);
	}	
}

function fGetToken(str, token){
	var aReturn = new Array();
	var str = new String(str);
	var token = new String(token);
	pos = str.indexOf(token);
	while(pos != -1){
		aReturn.push(str.substring(0,pos));
		str = str.substring(pos + token.length)
		pos = str.indexOf(token);
	}
	aReturn.push(str);
	return aReturn;
}

function xmlhttpPost(strUrl, strQuery, strCallBack) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strUrl, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4){
			eval(strCallBack + "(self.xmlHttpReq.responseText)");
        }
    }
    self.xmlHttpReq.send(strQuery);
}

function xmlhttpPostSearch(strUrl, strQuery, strCallBack) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strUrl, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4){
			eval(strCallBack + "(self.xmlHttpReq.responseText)");
        }
    }
    self.xmlHttpReq.send(strQuery);
}

function commGateChangeDropDown(str){
	ajaxcomm = 0;
	var aTmp = fGetToken(str, "<optikmed>");
	
	tmp = "<select id='"+ aTmp[0] +"' name='"+ aTmp[0] +"' class='input' onchange='" + aTmp[1] + "'>";		
	
	for(q=2; q<aTmp.length; q++)
		tmp += "<option value='"+aTmp[q]+"' id='"+aTmp[++q]+"'>"+aTmp[q]+"</option>";
		
	tmp += "</select>";
	
	obj = fGetObject("div_" + aTmp[0]);
	if(obj)
		obj.innerHTML = tmp;
}

function commGateChangeDropDownSearch(str){
	var aTmp = fGetToken(str, "<optikmed>");
	
	tmp = "<select id='"+ aTmp[0] +"' name='"+ aTmp[0] +"' class='input' onchange='" + aTmp[1] + "'>";		
	
	for(q=2; q<aTmp.length; q++)
		tmp += "<option value='"+aTmp[q]+"' id='"+aTmp[++q]+"'>"+aTmp[q]+"</option>";
		
	tmp += "</select>";
	
	obj = fGetObject("div_" + aTmp[0]);
	if(obj)
		obj.innerHTML = tmp;
}