﻿var RESERVED = "abc";
var	returnedFromNoResultsFound = false;
var	firstRestore = true;
var	initSearchType = 0;

var	xddlPracticeAreas =	null;
var	listPracticeAreas =	new	Array();

var	xddlIndustries = null;
var	listIndustries = new Array();

var	xddlLanguages =	null;
var	listLanguages =	new	Array();

var	xddlCountries =	null;
var	listCountries =	new	Array();

var	xddlCountriesOfBirth = null;
var	listCountriesOfBirth = new Array();

var	xddlAreaCodes =	null;
var	listAreaCodes =	new	Array();

var	xddlCities = null;
var	listCities = new Array();

var	xddlNumOfAccountants = null;
var	xtxtName = null;
var	xtxtFirstName =	null;
var	xtxtKeywords = null;
var	xchkAnd	= null;

var	hidSearchType =	null;
var	finishedPreLoad	= false;

// cell	counters
var	max_oa = 10; //	office/accountant cell count
var	max_kw = 2 ; //	keywords cell count

// busy	flag. useful for onclick commands
var	busy = false;

// how long	(in	miliseconds) should	we wait	when busy.
// note	that this is the max value (a random value is used)
var	wait_for = 500;

var	no_results = null;

function search()
{
	if (hidSearchType.value == RESERVED)
		return;
	
	var	searchUrl =	new	URL(document.location.href);
	var	href, encodedHref;

	if (hidSearchType.value	== search_keywords)
		searchUrl.setParam(paramNameKeywords, xtxtKeywords.value);
	else
		searchUrl.setParam(paramNameKeywords, xtxtName.value);
	searchUrl.setParam(paramNameCountryID, getItemValue(xddlCountries));
	searchUrl.setParam(paramNameCountryOfBirthID, getItemValue(xddlCountriesOfBirth));
	searchUrl.setParam(paramNameAreaCodeID,	getItemValue(xddlAreaCodes));
	searchUrl.setParam(paramNameCityID,	getItemValue(xddlCities));
	if (hidSearchType.value	== search_accountant)
		searchUrl.setParam(paramNameFirstName, xtxtFirstName.value);
	else
		searchUrl.removeParam(paramNameFirstName);
	searchUrl.setParam(paramNameIndustryID,	getItemValue(xddlIndustries));
	searchUrl.setParam(paramNamePracticeAreaID,	getItemValue(xddlPracticeAreas));
	searchUrl.setParam(paramNameNumOfAccountantsID,	getItemValue(xddlNumOfAccountants));
	searchUrl.setParam(paramNameLanguageID,	getItemValue(xddlLanguages));

	searchUrl.setParam(paramNameSearchTypeID, hidSearchType.value);
	searchUrl.setParam(paramNameAllKeywords, xchkAnd.checked ? "true" :	"false");
	
	searchUrl.setParam(QS_PARAM_NAME_LANG, inHebrew	? QS_PARAM_VALUE_LANG_HE : QS_PARAM_VALUE_LANG_EN);
	
	href = searchUrl.getHref();
	encodedHref	= encodeURI(href);
	if (_get("txtURL") != null)
		_get("txtURL").value	= encodedHref;
	
	if ((no_results	!= null) &&	(_get("txtNoResultsStatus") != null))
		_get("txtNoResultsStatus").value = no_results.style.visibility == "hidden" ? "hidden" : "shown";
	
	// for debug only
	// setTimeout("document.location.href =	'" + encodedHref + "';", 100);
	document.location.href = encodedHref;
}

function hideNoResultsCaption()
{
	if (null ==	no_results)
		return;
	hideElem(no_results);
}

// last	user selection.	keeps state	of selection
var	lastSelection =	new	UserSelection(null,	null, null,	null, null,	null, null);

// UserSelection - CTOR
function UserSelection(practiceAreaID, industryID, languageID, countryID, areaCodeID, cityID, numOfAccountantsID)
{
	this.PracticeAreaID	 = practiceAreaID;
	this.IndustryID		 = industryID;
	this.LanguageID		 = languageID;
	this.CountryID		  =	countryID;
	this.AreaCodeID		 = areaCodeID;
	this.CityID			 = cityID;
	this.NumOfAccountantsID	= numOfAccountantsID
	
	this.ToString =	function()
	{
		var	s;
		
		s =	new	String();
		
		s += "CountryID	= "	+ nullToString(this.CountryID) + ";	"
		s += "AreaCodeID = " + nullToString(this.AreaCodeID) + "; "
		s += "CityID = " + nullToString(this.CityID) + "; "
		s += "PracticeAreaID = " + nullToString(this.PracticeAreaID) + "; "
		s += "IndustryID = " + nullToString(this.IndustryID) + "; "
		s += "LanguageID = " + nullToString(this.LanguageID) + "."
		
		return s;
	}
}

function nullToString(v)
{
	if (null ==	v)
		return "[null]"
	else
		return v;
}

// load	the	last settings and change the display
function loadSettings(objSelections)
{
	if (null ==	objSelections)
		return;
	
	if (objSelections.PracticeAreaID !=	null)
		setValue(xddlPracticeAreas,	objSelections.PracticeAreaID);
	if (objSelections.IndustryID !=	null)
		setValue(xddlIndustries, objSelections.IndustryID);
	if (objSelections.LanguageID !=	null)
		setValue(xddlLanguages,	objSelections.LanguageID);
	if (objSelections.CountryID	!= null)
	{
		setValue(xddlCountries,	objSelections.CountryID);
		// countryChanged();
	}
	if (objSelections.AreaCodeID !=	null)
	{
		setValue(xddlAreaCodes,	objSelections.AreaCodeID);
		areaCodeChanged();
	}
	if (objSelections.CityID !=	null)
		setValue(xddlCities, objSelections.CityID);
		
	if (objSelections.NumOfAccountantsID !=	null)
		setValue(xddlNumOfAccountants, objSelections.NumOfAccountantsID);
		
}

function countryChanged()
{
	var	idxSelected;
	
	prm("countryChanged");
	if (finishedPreLoad)
		hideNoResultsCaption();
	idxSelected	= xddlCountries.selectedIndex;
	if (null ==	idxSelected)
		return;
	if (idxSelected	< 0)
		return;
	if (xddlCountries.options.item(idxSelected).value == "1")
		showIsraelRelated();
	else
		hideIsraelRelated();
}

// show	rows that are relevant 
// only	when "israel" was selected (as a country)
function showIsraelRelated()
{
	prm("showIsraelRelated");
	showElem(getElemByName("oa3"));
	showElem(getElemByName("oa4"));
}

// hide	rows that are relevant 
// only	when "israel" was selected (as a country)
function hideIsraelRelated()
{
	prm("hideIsraelRelated");
	hideElem(getElemByName("oa3"));
	hideElem(getElemByName("oa4"));
}

function areaCodeChanged()
{
	var	v;
	
	if (finishedPreLoad)
		hideNoResultsCaption();

	v =	getItemValue(xddlCities);
	processComplexCities(listCities, xddlCities, (hidSearchType.value == search_office));
	if (v != null)
		setValue(xddlCities, v);
}

// load	all	shared office/accountant data into drop	down lists
function setAllData(asOffice)
{
	processComplexObjects(listPracticeAreas, xddlPracticeAreas,	asOffice);
	processComplexObjects(listIndustries, xddlIndustries, asOffice);
	processComplexObjects(listLanguages, xddlLanguages,	asOffice);
	processComplexObjects(listCountries, xddlCountries,	asOffice);
	processComplexObjects(listAreaCodes, xddlAreaCodes,	asOffice);
	processComplexCities(listCities, xddlCities, asOffice);
}

function getWaitFor()
{
	var	min_value =	100;
	var	temp;
	
	temp = Math.floor(Math.random()	* wait_for);
	temp = Math.min(temp, min_value);
	
	return temp;
}

function setLoader(v)
{
	var	x;
	
	v =	eval(v);
	
	x =	eval(search_accountant);
	if (v == x)
	{
		setAccountant();
		return;
	}
	
	x =	eval(search_office);
	if (v == x)
	{
		setOffice();
		return;
	}
	
	x =	eval(search_keywords);
	if (v == x)
	{
		setSearchKeywords();
		return;
	}
}				

// save	user selections	to the state object
function saveSettings()
{
	lastSelection =	new	UserSelection(
		getItemValue(xddlPracticeAreas), 
		getItemValue(xddlIndustries), 
		getItemValue(xddlLanguages), 
		getItemValue(xddlCountries), 
		getItemValue(xddlAreaCodes), 
		getItemValue(xddlCities),
		getItemValue(xddlNumOfAccountants)
		);
	prm("saveSettings --> lastSelection	= "	+ lastSelection.ToString());
}

// to do either	when user selected "accountant"	search 
// or "office" search
function setOfficeAccountantFields()
{
	// display the office/accountant rows
	for	(var k = 1;	k <= max_oa; k++)
		setRowDisplay(getElemByName("oa" + k), true);
	
	// hide	the	keywords rows
	for	(var k = 1;	k <= max_kw; k++)
		setRowDisplay(getElemByName("kw" + k), false);
}

// set current search to "accountant"
function setAccountant()
{
	if (hidSearchType.value	== search_accountant)
		return;
		
	// explanations
	_get('Explanations').innerHTML = _get('accountantExplanation').innerHTML;
		
	hidSearchType.value	= search_accountant;
	
	if (busy)
	{
		setTimeout("setAccountant()", getWaitFor());
		return;
	}
	
	busy = true;
	
	setOfficeAccountantFields();
	
	// display the relevant	caption
	showElem(getElemByName("accountantName"));
	hideElem(getElemByName("officeName"));
	
	// save	the	current	settings into the state	object
	saveSettings()
	
	// reload lists
	setAllData(false);
	prm("from setAccountant");
	
	// set the user	selections from	the	state object
	loadSettings(lastSelection);
	
	// reset classes for captions
	clearCaptionsClassName();
	
	// set the "accountant"	caption	to be the selected one
	_get("captionOfAccountant").className = "SearchSelected";
	
	// reset the seperators
	setDefaultSeperators();
	
	// set the surrounding seperators of "accountant" to be	selected
	_get("td_acc_ofc").style.backgroundImage	= inHebrew ? "url(images/tab_left_on_right_off.gif)" : "url(images/tab_left_off_right_on.gif)";
	_get("td_acc_kwd").style.backgroundImage	= inHebrew ? "url(images/tab_left_off_right_on.gif)" : "url(images/tab_left_on_right_off.gif)";
	
	// hide	the	country
	setRowDisplay(getElemByName("oa2"),	false)
	// set as israel
	setValue(xddlCountries,	"1");
	showIsraelRelated();
	
	setRowDisplay(getElemByName("oa8"),	false)
	setRowDisplay(getElemByName("oa9"),	true)
	setRowDisplay(getElemByName("oa10"), true)
	// that's it!
	busy = false;
	
}

function setOffice()
{
	prm("begin setOffice");
	
	if (hidSearchType.value	== search_office)
		return;

	// explanations
	_get('Explanations').innerHTML = _get('officeExplanation').innerHTML;

	hidSearchType.value	= search_office;

	prm("busy =	" +	busy);
	if (busy)
	{
		setTimeout("setOffice()", getWaitFor());
		return;
	}
	
	busy = true;
	
	setOfficeAccountantFields();
	hideElem(getElemByName("accountantName"));
	showElem(getElemByName("officeName"));
	saveSettings();
	setAllData(true);
	prm("from setOffice");
	loadSettings(lastSelection);
	clearCaptionsClassName();
	_get("captionOfOffice").className = "SearchSelected";
	setDefaultSeperators();

	_get("td_off_rgt").style.backgroundImage	= inHebrew ? "url(images/tab_right_on.gif)"	: "url(images/tab_left_on.gif)";
	_get("td_acc_ofc").style.backgroundImage	= inHebrew ? "url(images/tab_left_off_right_on.gif)" : "url(images/tab_left_on_right_off.gif)" ;

	// show	the	country
	setRowDisplay(getElemByName("oa2"),	true)
	if (getItemValue(xddlCountries)	== "1")
		showIsraelRelated();
	else
		hideIsraelRelated();
	setRowDisplay(getElemByName("oa8"),	true)
	setRowDisplay(getElemByName("oa9"),	false)
	setRowDisplay(getElemByName("oa10"), false)
	busy = false;
}

function clearCaptionsClassName()
{
	_get("captionOfAccountant").className = "SearchRegular";
	_get("captionOfOffice").className = "SearchRegular";
	_get("captionOfKeywords").className = "SearchRegular";
}

function getSeperatorWidth()
{
	return "13px";
}

function setSearchKeywords()
{
	var	obj;
	var	k;

	prm("setSearchKeywords begin");
	
	if ((hidSearchType.value ==	search_keywords))
		return;

	// explanations
	_get('Explanations').innerHTML = _get('freetextExplanation').innerHTML;

	hidSearchType.value	= search_keywords;
	prm("busy =	" +	busy);
	if (busy)
	{
		prm("setSearchKeywords setting timeout and return");
		setTimeout("setSearchKeywords()", getWaitFor());
		return;
	}
	
	busy = true;
	
	for	(k = 1;	k <= max_oa; k++)
		setRowDisplay(getElemByName("oa" + k), false);
	
	for	(k = 1;	k <= max_kw; k++)
		setRowDisplay(getElemByName("kw" + k), true);
		
	clearCaptionsClassName();
	obj	= _get("captionOfKeywords");
	if (null ==	obj)
	{
		busy = false;
		return;
	}
	obj.className =	"SearchSelected";
	
	setDefaultSeperators();
	_get("td_acc_kwd").style.backgroundImage	= inHebrew ? "url(images/tab_left_on_right_off.gif)" : "url(images/tab_left_off_right_on.gif)";
	_get("td_kwd_lft").style.backgroundImage	= inHebrew ? "url(images/tab_left_on.gif)" : "url(images/tab_right_on.gif)";
	
	busy = false;
	
	prm("hidSearchType.value = " + hidSearchType.value);
	prm("setSearchKeywords end");
}

function showElem(elem)
{
	elem.style.visibility =	"visible";
	elem.style.display = "block";
}

function hideElem(elem)
{
	if (null ==	elem)
		return;
	elem.style.visibility =	"hidden";
	elem.style.display = "none";
}

function setRowDisplay(elem, v)
{
	elem.style.display = (v	? "block" :	"none");
}

function prm(msg)
{
	// document.getElementById("myprompt").innerHTML +=	"; " + msg
}

function cursorToHand(obj)
{
	var	s;
	
	s =	new	String(obj.className);
	if (s.indexOf("elected", 0)	< 0)
		obj.style.cursor='pointer';
	else
		obj.style.cursor='';
}
				
function starter()
{
	// finishedPreLoad = true;
	_get("td_off_rgt").style.backgroundPosition = inHebrew ?	"left" : "right" ;
	setTabsWidth();
}

function _get(obj)
{
	return document.getElementById(obj);
}

function restoreSelectionsByURL1()
{
	var	url;
	var	currentValue, currentValue1;
	var	s;
	var	t;
	
	if (typeof (setArrayVariables) != 'undefined')
		setArrayVariables();

	// try to get the state	of the "no results"	message	(hidden	or shown)
	//		t =	_get("txtNoResultsStatus");
	//		if (t != null)
	//		{
	//			s =	t.value;
	//			if ((s != null)	&& (s.length !=	0))
	//			{
	//				if (s == "shown")
	//					returnedFromNoResultsFound = true;
	//				else if	(s == "hidden")
	//					returnedFromNoResultsFound = false;
	//			}
	//		}

	s =	null;
	
	// try to get the user selection from the text box.
	// this	is done	if the user	clicked	on "back"
	t = $("#txtURL")[0];
	if (t != null)
	{
		s =	t.value;
		if ((s != null)	&& (s.length ==	0))
			s =	null;
	}
	
	
	
	// do we have a	"back" situation?
	if (null ==	s)
		// no, we don't
		s =	document.location.href;
	else
		// yes,	we do
		hideNoResultsCaption();
		
	// var s holds the relevant	url
	
	// create the url object
	url	= new URL(s);
	
	// now restore the info	from the url
	currentValue = url.getParam(paramNameAllKeywords);
	if (currentValue !=	null)
	{
		currentValue = currentValue.toLowerCase();
		if ("true" == currentValue)
			xchkAnd.checked	= true;
		else
			xchkAnd.checked	= false;
	}
	
	currentValue = url.getParam(paramNameCityID);
	if (currentValue !=	null)
	{
		setValue(xddlCities, currentValue);
	}
	
	currentValue = url.getParam(paramNameAreaCodeID);
	if (currentValue !=	null)
	{
		setValue(xddlAreaCodes,	currentValue);
		areaCodeChanged();
	}
	
	currentValue = url.getParam(paramNameCountryID);
	if (currentValue !=	null)
	{
		setValue(xddlCountries,	currentValue);
		countryChanged();
	}
	
	currentValue = url.getParam(paramNameIndustryID);
	if (currentValue !=	null)
	{
		setValue(xddlIndustries, currentValue);
	}

	currentValue = url.getParam(paramNamePracticeAreaID);
	if (currentValue !=	null)
	{
		setValue(xddlPracticeAreas,	currentValue);
	}
	
	currentValue = url.getParam(paramNameCountryOfBirthID);
	if (currentValue !=	null)
	{
		setValue(xddlCountriesOfBirth, currentValue);
	}
	
	currentValue = url.getParam(paramNameLanguageID);
	if (currentValue !=	null)
	{
		setValue(xddlLanguages,	currentValue);
	}
	
	currentValue = url.getParam(paramNameNumOfAccountantsID);
	if (currentValue !=	null)
	{
		setValue(xddlNumOfAccountants, currentValue);
	}

	currentValue = url.getParam(paramNameFirstName);
	if (currentValue !=	null)
	{
		xtxtFirstName.value	= decodeURI(currentValue);
	}

	currentValue = url.getParam(paramNameKeywords);
	
	currentValue1 = url.getParam(paramNameSearchTypeID);

	if (currentValue != null)
	{
		//if (hidSearchType.value == search_keywords)
		if (currentValue1 == search_keywords)
			xtxtKeywords.value = decodeURI(currentValue);
		else
			xtxtName.value = decodeURI(currentValue);
	}

	//currentValue = url.getParam(paramNameSearchTypeID);
	
	// this	is just	to make	sure that the display is done,
	// even	if we're in	a "back" situation
	hidSearchType.value = RESERVED;
	
	if (null ==	currentValue1)
		setLoader(search_office);
	else
		setLoader(currentValue1);
		
	firstRestore = false;
	returnedFromNoResultsFound = false;
	finishedPreLoad	= true;
}

function setDefaultSeperators()
{
	var	current;
	var	src;
	
	current	= document.getElementById("td_off_rgt");
	if (null ==	current)
		return;
	src	= inHebrew ? "url(images/tab_right_off.gif)" : "url(images/tab_left_off.gif)";
	current.style.backgroundImage =	src;
	current.style.width	= getSeperatorWidth();
	
	current	= document.getElementById("td_acc_ofc");
	if (null ==	current)
		return;
	src	= "url(images/tab_left_off_right_off.gif)";
	current.style.backgroundImage =	src;
	current.style.width	= getSeperatorWidth();
	
	current	= document.getElementById("td_acc_kwd");
	if (null ==	current)
		return;
	src	= "url(images/tab_left_off_right_off.gif)";
	current.style.backgroundImage =	src;
	current.style.width	= getSeperatorWidth();
	
	current	= document.getElementById("td_kwd_lft");
	if (null ==	current)
		return;
	src	= inHebrew ? "url(images/tab_left_off.gif)"	: "url(images/tab_right_off.gif)";
	current.style.backgroundImage =	src;
	current.style.width	= getSeperatorWidth();
}

function setTabsWidth()
{
	document.getElementById("captionOfOffice").style.width = (inHebrew ? "70px"	: "65px");
	document.getElementById("captionOfAccountant").style.width = (inHebrew ? "81px"	: "32px");
	document.getElementById("captionOfKeywords").style.width = (inHebrew ? "79px" :	"73px");
}

$(function() {
	no_results = $("#tdNoResults")[0];
	xddlPracticeAreas = $("[name$=ddlPracticeAreas]")[0];
	xddlIndustries = $("[name$=ddlIndustries]")[0];
	xddlLanguages = $("[name$=ddlLanguages]")[0];
	xddlCountries = $("[name$=ddlCountries]")[0];
	xddlCountriesOfBirth = $("[name$=ddlAccountantCountryOfBirth]")[0];
	xddlAreaCodes = $("[name$=ddlAreaCodes]")[0];
	xddlCities = $("[name$=ddlCities]")[0];
	xddlNumOfAccountants = $("[name$=ddlNumOfAccountants]")[0];
	hidSearchType = $("[name$=hiddenSearchType]")[0];
	xtxtName = $("[name$=txtName]")[0];
	xtxtFirstName = $("[name$=txtFirstName]")[0];
	xtxtKeywords = $("[name$=txtKeywords]")[0];
	xchkAnd = $("[name$=chkAnd]")[0];
});
