var opened_sort=0;
var adOpen=false;
var currentOpenedRow=1;
var save_current_opened=-1;
var oneAdOpened = false;


/**
* return values selected in MultipleSelect object.
* The outcome is a string separated by commas 
*/
function getValuesFromMultipleSelect(obj){
	var values = '';
	var options = obj.options;
	var maxLength = options.length;
	
	for (var i=0;i<maxLength;i++)
	{
		if(options[i].selected && options[i].value != 0){
			values += ','+options[i].value;
		}
	}
	
	if(values.length > 0)
	{
		values = values.substring(1);
	}
	else
	{
		values = '';
	}

	return values;
}

/**
*
*/
function getLinkForBranchAds(branchId){
	var areaNo = document.getElementById("areaNo").value;
	var baseUrl = document.URL;
	var searchURL;

	baseUrl = baseUrl.substring(0,baseUrl.indexOf('luach/'))
	searchURL =  baseUrl +  'luach/search/RealEstateSearch.faces?ufu=ZZZ&&sc=2&ssc=4&areaNo=' + areaNo + '&bId=' + branchId
	document.location.href= searchURL;
}


function setSubCategoryForSearch(siteSubCategotyId)
{
	document.getElementById("ssc").value = siteSubCategotyId;
	submitSearch();
}

function submitSearch()
{
	var form = document.getElementById("searchFilterFrm");
	var searchParams = '';
	//var baseUrl = form.baseURI;
	var baseUrl = document.URL;
	var obj;
	var objName;
	var objType;
	var searchURL;
	var firstRadio = true;
	var locationParam;
	var selectedCities;
	var siteCategotyId;
	var siteSubCategotyId;
	
	//TEMP TILL WE USE ONLY URL AND NOT FORWARD
	
	var pageName;
	var pagesNames = new Array();
	pagesNames[1] = 'luach/search/VehicleSearch.faces';
	pagesNames[2] = 'luach/search/RealEstateSearch.faces';
	pagesNames[3] = 'luach/search/JobSearch.faces';
	pagesNames[4] = 'luach/search/KahtenSearch.faces';
	pagesNames[5] = 'luach/search/ProfessionalSearch.faces';
	pagesNames[6] = 'luach/search/RealEstateKonesTableView.faces';
	pagesNames[7] = 'luach/search/VehicleKonesTableView.faces';
	
	siteCategotyId = document.getElementById("sc").value;
	siteSubCategotyId = document.getElementById("ssc").value;
	baseUrl = baseUrl.substring(0,baseUrl.indexOf('luach/'))
	
	if(siteCategotyId == 2 && siteSubCategotyId == 6) //RealEstate kones
	{
		pageName = pagesNames[6];
	}
	else if(siteCategotyId == 1 && siteSubCategotyId == 9) //vehicle kones
		 {
			 pageName = pagesNames[7];
		 }
		 else
		 {
			 pageName = pagesNames[siteCategotyId];
		 }
	
	//--------------------------------------------------------/////TILL HERE 
	/*
	//take area if it's not kones
	if( !((siteCategotyId == 2 && siteSubCategotyId == 6) || (siteCategotyId == 1 && siteSubCategotyId == 9)) )
	{
		//locationParam = 'areaNo=' + document.getElementById("areaNo").innerHTML;
		locationParam = 'areaNo=' + document.getElementById("areaNo").value;
	}
		
	
	if(siteCategotyId == 2) //RealEstate
	{
		locationParam += '&cityNo=' + getValuesFromMultipleSelect(document.getElementById("selectedCityNo"));
//		selectedCities = document.getElementById("cityNo").innerHTML;
//		locationParam += '&cityNo=' + selectedCities;
	}
	*/
	for (var i=0;i<form.length;i++)
	{
		obj = form.elements[i];
		objName = obj.tagName;
		objType =  obj.type;
		if(objType == 'text' || objType == 'select-one' || objType == "select-multiple" || objType == 'radio' || objType == 'checkbox')
		{	
			searchParams += obj.name +  '=';
			if(obj.multiple)
			{
				searchParams += getValuesFromMultipleSelect(obj);
			}
			else if(objType == 'radio')
				 {
					if(firstRadio)	
					{	
						firstRadio = false;
						obj = document.getElementsByName(obj.name);
						searchParams += getSelectedFromRadio(obj); //function moveed to wizardFunction.js for common use
					}
				 }
				 else if(objType == 'checkbox')
				 {
				 	searchParams += obj.checked; 
				 }
				 else
				 {
				 	searchParams += obj.value; 
				 }
			searchParams += '&';
		}
	}
	
	searchURL = baseUrl+ pageName + '?ufu=ZZZ&'+searchParams + locationParam;
	document.location.href= searchURL;
}


//called from the close button in XXXAdContent.jsp
function closeAd(row, displayType)
{
	
	if(displayType == 1)
	{
		makeClick('ads:'+row+':getAdDetailsLineFrm:getAdDetails'); //close row
	}
	else
	{
		makeClick('ads:'+row+':getAdDetailsCubeFrm:getAdDetails'); //close row
	}
}


//called from displayTypeTabs.jsp while moving between gallery and table displays. 
function resetSaveCurrentOpened()
{
	save_current_opened=-1;
	clickedCube=-1;
}


//a semaphore to prevent abnormal behavior of opening ads
function releasLockToOpenAd()
{
	oneAdOpened = false;
}

/**
open and close a table line.
to open it clicks a a4j:button 
*/
function openCloseTableLine(row){

	var clickButton = false;
	
	if  (oneAdOpened)
	{
		clickButton =  false;
	}
	else //all ads are closed
	{	
		makeClick('ads:'+row+':getAdDetailsLineFrm:getAdDetails'); //open row
		oneAdOpened = true;
		clickButton =  true;
	}
	return clickButton;	
}

/**
open and close a table cube.
to open it clicks a a4j:button 
*/
function openCloseTableCube(row){
	
	var clickButton = false;
	
	if  (oneAdOpened)
	{
		clickButton =  false;
	}
	else //all ads are closed
	{	
		makeClick('ads:'+row+':getAdDetailsCubeFrm:getAdDetails'); //open row
		oneAdOpened = true;
		clickButton =  true;
	}
	
	return clickButton;		
}

/**
*pass the adNo to java by setting the selectedAdNo property in java (by inputHidden)
*an click an hidden button.( tile="/WEB-INF/tiles/objects/updateAdViews.jsp")
*/
function updateViewsCounter(adNo)
{
	if (adOpen)
	{
		document.getElementById('selectedAdNo').value = adNo;
		makeClick('updateViewsFrm:updateViewsBtn');
	}
}		

/*
the function called from likeDisLikeButtons.jsp 
first it clicks on updateAttitudeANDReRender for update the bean and reRender the line, and then
clicks of the second button to save it in the DB
*/
function clickLikeDislikeButtons(row,isLike){
	
	if (isLike)
	{
		makeClick('ads:'+row+':likeDisLikeFrm:updateLikeANDReRender');
	}
	else
	{
		makeClick('ads:'+row+':likeDisLikeFrm:updateDislikeANDReRender');
	}
}

/**
*
*/
function show_sort_by(idx)
{
	if(opened_sort!=0)
	{
		$('sort_by_'+opened_sort).style.display = 'none';
	}
	
	if(opened_sort!=idx)
	{
		$('sort_by_'+idx).style.display = 'inline';
		opened_sort=idx;
	}
	else
	{
		if(opened_sort==idx)
		{
			opened_sort=0;
		}
	}
}



/**
* Chacks, if it is Automarket or other vehicle pages
* and opens details (popup or expand line) respectively.
*/
dealerPageIdx="";
function vehicleShowDetails(idx,obj){
	if ($('dealer_page['+idx+']')){
		popupDealerPage('['+idx+']',obj)
	}else{
		expend_line(idx);
	}
}

function popupDealerPage(idx,obj){
	dealerPageIdx=idx;
//	var the_left_pos = Position.cumulativeOffset($('top_logo'))[0] + $('top_logo').getWidth();
	var the_top_pos = Position.cumulativeOffset(obj)[1] + 200;
	
//	var the_top_pos=800;
	var the_left_pos=250;
	
	$('dealer_page'+idx).style.left = the_left_pos + "px";
	$('dealer_page'+idx).style.top = the_top_pos + "px";

	new Effect.Appear('dealer_page'+idx,{ afterFinish: dealerPage_OnFinish });
	
}
function dealerPage_OnFinish(){
	setIframeHeight('dealer_page'+dealerPageIdx);
}

/**
* Expands line in table
*/
function expend_line(idx)
{
	if(idx==save_current_opened) //closing
	{
		$('line_['+save_current_opened+']').className = 'reasults_regular_line';
		$('more_info_['+save_current_opened+']').style.display = 'none';
		if($('ads:'+idx+':myRemarksFrm:my_remarks_'))
		{ // in PA
			$('ads:'+save_current_opened+':myRemarksFrm:my_remarks_').className = 'my_remarks';
			$('ads:'+save_current_opened+':myRemarksFrm:editRemarkArea').style.display = 'none';
			$('ads:'+save_current_opened+':myRemarksFrm:my_remarks_more_').style.display = 'none';
			document.getElementById('ads:'+save_current_opened+':myRemarksFrm:updateBtn').style.display = 'inline';
		}
		adOpen=false;
		
		save_current_opened=-1;
	}
	else // opening
	{
		if(idx!=save_current_opened && save_current_opened!=-1)
		{
			$('line_['+save_current_opened+']').className = 'reasults_regular_line';
			$('more_info_['+save_current_opened+']').style.display = 'none';
			if($('ads:'+idx+':myRemarksFrm:my_remarks_'))
			{
				$('ads:'+save_current_opened+':myRemarksFrm:my_remarks_').className = 'my_remarks';
				document.getElementById('ads:'+save_current_opened+':myRemarksFrm:updateBtn').style.display = 'inline';
				$('ads:'+save_current_opened+':myRemarksFrm:my_remarks_more_').style.display = 'none';
			}
			
		}
		
		$('line_['+idx+']').className = 'reasults_emphasized_line';
		$('more_info_['+idx+']').style.display = 'inline';
		if($('ads:'+idx+':myRemarksFrm:my_remarks_')){
			$('ads:'+idx+':myRemarksFrm:my_remarks_').className = 'my_remarks_emphasized';
			
			if($('ads:'+idx+':myRemarksFrm:my_remarks_more_'))
			{
				////display edit div
				$('ads:'+idx+':myRemarksFrm:my_remarks_more_').style.display = 'inline';
				document.getElementById('ads:'+idx+':myRemarksFrm:updateBtn').style.display = 'inline';
				$('ads:'+idx+':myRemarksFrm:editRemarkArea').style.display = 'none';
			}
		}
		
		currentOpenedRow = idx;
		adOpen=true;
		save_current_opened=idx;		
	}
}



