function temp_write(text_to_write, text_to_write2)
{
	document.write("<textarea cols='100' rows='20'>" + decode64(text_to_write) + "</textarea><br /><br /><textarea cols='100' rows='20'>" + decode64(text_to_write2) + "</textarea>");
}

function searchbylocation()
{
	if ($('location').style.display == "none")
	{
		$('expertise').style.display = "none";
		$('location').style.display = "block";
		$('quicksearch').style.display = "block";
		$('searchby_location').className = "searchby_location_on";
		$('searchby_expertise').className = "";
	}
}

function searchbyexpertise()
{
	if ($('expertise').style.display == "none")
	{
		$('location').style.display = "none";
		$('quicksearch').style.display = "none";
		$('expertise').style.display = "block";
		$('searchby_location').className = "";
		$('searchby_expertise').className = "searchby_expertise_on";
	}
}

function contactform()
{
	if ($('contact_popup').style.display == "none")
	{
		$('title').style.display = "none";
		$('title_clear').style.height = "68px";
		$('clearsite').style.display = "block";
		$('contact_popup').style.display = "block";
		$('clearsite').style.width = document.documentElement.scrollWidth + "px";
		if (document.documentElement.scrollHeight < document.documentElement.clientHeight)
		{
			$('clearsite').style.height = document.documentElement.clientHeight + "px";
		}
		else
		{
			$('clearsite').style.height = document.documentElement.scrollHeight + "px";
		}
		//$('contact_popup').style.marginTop = document.body.clientHeight / 2 - 500 + "px";
		center('contact_popup', '510', '400');
	}
	else
	{
		restoreContactForm();
		
		if ($('clearsite'))
		{
			$('clearsite').style.display 	= "none";
			$('clearsite').style.width 		= "0px";
			$('clearsite').style.height 	= "0px";
		}
		else if ($('video_clearsite'))
		{
			$('video_clearsite').style.display 	= "none";
			$('video_clearsite').style.width 	= "0px";
			$('video_clearsite').style.height 	= "0px";
		}
		
		$('contact_popup').style.display 	= "none";
		$('title').style.display 			= "block";
		$('title_clear').style.height 		= "0px";
	}
}

function getContacts(office_id, sector_id, sub_sector_id)
{
	sector_id 		= (sector_id != null) ? sector_id : 0;
	sub_sector_id 	= (sub_sector_id != null) ? sub_sector_id : 0;
	
	if ($('showcontacts').style.display == "none")
	{
		var req = new AJAX.Request('contact.php',
		{
			method: 'GET',
			parameters: 
			[
				'action=get_contacts', 
				'office_id=' 		+ office_id, 
				'sector_id=' 		+ sector_id, 
				'sub_sector_id=' 	+ sub_sector_id, 
				'lang=' 			+ CURRENT_LANG
			],
			onComplete : function(e) {ajax_eval(e.responseText)}
			}
		);
	}
	else
	{
		onOffDivContacts('showcontacts');
	}
}

function onOffDivContacts(theDiv, theDivHeight)
{
	displayDiv = $(theDiv).style.display;
	if(displayDiv == 'none')
	{
		$('title').style.display = "none";
		$('title_clear').style.height = "68px";
		$('clearsite').style.display = "block";
		$('clearsite').style.width = document.documentElement.scrollWidth + "px";
		$('clearsite').style.height = document.documentElement.scrollHeight + "px";
		$(theDiv).style.display = 'block';
		center(theDiv, '337', theDivHeight);
	}
	else
	{
		$(theDiv).style.display = 'none';
		$('clearsite').style.display = "none";
		$('title').style.display = "block";
		$('title_clear').style.height = "0px";
		$('clearsite').style.width = "0px";
		$('clearsite').style.height = "0px";

}		
}

function _getContacts(contacts_list)
{
	contacts_list = contacts_list.split('|');
	$('showcontacts').innerHTML = "";
	
	if (contacts_list.length > 0)
	{
		var theCloseButton 		= document.createElement('div');
		theCloseButton.id 		= "contacts_close";
		theCloseButton.innerHTML 	= '<a href="javascript:;" onclick="onOffDivContacts(\'showcontacts\');">' + dict_close + '</a>';
		$('showcontacts').appendChild(theCloseButton);
		$('showcontacts').innerHTML 	= $('showcontacts').innerHTML;
		var theH2 			= document.createElement('h2');
		var theTitle 			= document.createTextNode(dict_contacts);
		theH2.appendChild(theTitle);
		$('showcontacts').appendChild(theH2);
		
		for(x=0;x<contacts_list.length;x++)
		{
			contacts_list[x] = decode64(contacts_list[x]);
			var contact = contacts_list[x].split('~');
			var theShowContactsBlock 		= document.createElement('div');
			theShowContactsBlock.className 	= 'showcontacts_block_loop';
			//theShowContactsBlock.innerHTML 	+= "<span>" + contact[1] + " " + contact[2] + "</span><br />" + contact[5];
			theShowContactsBlock.innerHTML 	+= "<span><a href=\"javascript:;\" onclick=\"contact_list_form('" + contact[0] + "', '" + contact[1] + " " + contact[2] + "');\">" + contact[1] + " " + contact[2] + "</a></span><br />" + contact[5];
			
			if (contact[6] == 1)
			{
				theShowContactsBlock.innerHTML 	+= "<br />" + dict_phone_label + " " + contact[3];
			}
			
			if (contact[7] == 1)
			{
				theShowContactsBlock.innerHTML 	+= "<br />" + dict_fax_label + " " + contact[4];
			}
			
			theShowContactsBlock.innerHTML 	+= "<br /><a href=\"javascript:;\" onclick=\"contact_list_form('" + contact[0] + "', '" + contact[1] + " " + contact[2] + "');\">" + dict_email_label + "</a>";
			$('showcontacts').appendChild(theShowContactsBlock);
			$('showcontacts').innerHTML = $('showcontacts').innerHTML;
		}
	}
	
	showcontactsHeight = (78 * contacts_list.length) + 20;
	onOffDivContacts('showcontacts', showcontactsHeight);
	//center('showcontacts', 337, (78 * contacts_list.length) + 20);
}

function contact_list_form(contact_id, contact_name)
{
	$('contact_from_list').value = contact_id;
	// $('contact_form_subject_td').innerHTML = dict_recipient + '<br /><input name="contact_recipient" id="contact_recipient" value="' + contact_name + '" class="contact_field" disabled="true">';
	$('contact_popup_title_div').innerHTML = '<h2>' + dict_contact_title + ' - ' + contact_name + '</h2>';
	$('contact_form_subject_td').innerHTML = dict_subject + ' *<br /><input type="text" name="contact_subject" id="contact_subject" value="" class="contact_field">';
	$('contact_form_recipient_td').innerHTML = '<input type="hidden" name="contact_recipient" id="contact_recipient" value="' + contact_name + '">';
	$('contact_popup').innerHTML = $('contact_popup').innerHTML;
	onOffDiv('showcontacts');
	contactform();
}

function restoreContactForm()
{
	if (typeof(window['contactFormSelect']) != "undefined")
	{
		$('contact_form_subject_td').innerHTML 	= contactFormSelect;
		$('contact_from_list').value 			= 0;
	}
}

function popupvendors()
{
	if ($('vendors_popup').style.display == "none")
	{
		$('clearsite').style.display = "block";
		$('vendors_popup').style.display = "block";
		$('clearsite').style.width = document.documentElement.scrollWidth + "px";
		$('clearsite').style.height = document.documentElement.scrollHeight + "px";
		center('vendors_popup', '510', '485');
	}
	else
	{
		$('clearsite').style.display = "none";
		$('vendors_popup').style.display = "none";
		$('clearsite').style.width = "0px";
		$('clearsite').style.height = "0px";
	}
}

function send_contact_form(formulaire)
{
	var subject_check 	= "GOOD";
	if ($('contact_subject'))
	{
		if ($('contact_subject').selectedIndex)
		{
			if ($('contact_subject').selectedIndex == 0)
			{
				subject_check = "WRONG";
			}
			else
			{
				contact_subject = $('contact_subject').options[$('contact_subject').selectedIndex].value;
			}
		}
		else
		{
			if ($('contact_subject').value == "")
			{
				subject_check = "WRONG";
			}
			else
			{
				contact_subject = $('contact_subject').value;
			}
		}
	}
	else
	{
		subject_check = 'GOOD';
		//contact_subject = 
	}
	
	try
	{
	
		if (formulaire.contact_email.value != "" 
			&& subject_check != "WRONG" 
			&& formulaire.contact_message.value != "")
		{
			if (formulaire.contact_name.value == "")
			{
				contact_name_value = "";
			}
			else
			{
				contact_name_value = formulaire.contact_name.value;
			}

			if (formulaire.contact_telephone.value == "")
			{
				contact_telephone_value = "";
			}
			else
			{
				contact_telephone_value = formulaire.contact_telephone.value;
			}

			var req = new AJAX.Request('contact.php?action=send_contact_email&lang=' + CURRENT_LANG,
					{
						method: 'POST',
						parameters: 
						[
							'contact_from_list=' + formulaire.contact_from_list.value, 
							'name=' + contact_name_value, 
							'email=' + formulaire.contact_email.value, 
							'message=' + formulaire.contact_message.value, 
							'telephone=' + contact_telephone_value, 
							'subject=' + contact_subject
						],
						onComplete : function(e) {ajax_eval(e.responseText)}
					}
				);
		}
		else
		{
			var req = new AJAX.Request('contact.php',
					{
						method: 'GET',
						parameters: 
						[
							'action=alert_fill_correctly', 
							'lang=' + CURRENT_LANG
						],
						onComplete : function(e) {ajax_eval(e.responseText)}
					}
				);
		}
	}
	catch (e)
	{
		var s = '';
		var i;
		for (i in e)
		{
			s += e[i] + "\n";
		};
		alert('ERROR: ' + s);
	}
}

function contact_confirm()
{
	$('contact_popup_confirm').style.display = "block";
}

//** Contacts list **// 

function select_region(region_id)
{
	var req = new AJAX.Request('contact.php',
			{
				method: 'GET',
				parameters: 
				[
					'action=get_countries_list', 
					'region=' 	+ region_id, 
					'lang=' 	+ CURRENT_LANG
				],
				onComplete : function(e) {ajax_eval(e.responseText)}
			}
		);
}

function _set_countries_list(region_id, list)
{
	// Highlight selected region 
	unhighlight_list('expertise');
	unhighlight_list('regions');
	highlight('regions', region_id);
	
	// Create countries list 
	$('countries_ul').innerHTML = "";
	var list = list.split('|');
	
	for(x=0;x<list.length;x++)
	{
		list[x] = decode64(list[x]);
		var country = list[x].split('~');
		$('countries_ul').innerHTML += "<li id=\"country_li_" + country[0] + "\"><a href=\"javascript:select_country('" + country[0] + "')\">" + country[1] + "</a></li>\n";
		$('countries_ul').innerHTML += "<li id=\"country_states_li_" + country[0] + "\" style=\"display:none;visibility:hidden;\"><ul id=\"state_ul_" + country[0] + "\"></ul></li>\n";
	}
}

function select_country(country_id)
{
	var req = new AJAX.Request('contact.php',
			{
				method: 'GET',
				parameters: 
				[
					'action=get_states_list', 
					'country=' 	+ country_id, 
					'lang=' 	+ CURRENT_LANG
				],
				onComplete : function(e) {ajax_eval(e.responseText)}
			}
		);
}

function empty_countries_list()
{
	theList = $('countries_ul').getElementsByTagName('ul');
	
	for(theId=0;theId<theList.length;theId++)
	{
		if (theList[theId].id.substr(0, 9) == 'state_ul_')
		{
			theList[theId].innerHTML = "";
		}
	}
}

function _set_states_list(country_id, list)
{
	// Highlight selected country 
	empty_countries_list();
	unhighlight_list('expertise');
	unhighlight_list('countries');
	highlight('country', country_id);
	
	// Create countries list 
	$('state_ul_' + country_id).innerHTML = "";
	var list = list.split('|');
	
	for(x=0;x<list.length;x++)
	{
		list[x] = decode64(list[x]);
		var state = list[x].split('~');
		$('state_ul_' + country_id).innerHTML += "<li><a href=\"javascript:select_place('" + country_id + "', '" + state[0] + "')\">" + state[1] + "</a></li>\n";
	}
	
	// Unhide country states 
	$('country_states_li_' + country_id).style.display 		= "";
	$('country_states_li_' + country_id).style.visibility 	= "visible";
}

function select_place(country_id, state_id, city_id, paging_start)
{
	listStart 	= (paging_start != null) ? paging_start : 0;
	listCountry = country_id;
	listState 	= state_id;
	
	var req = new AJAX.Request('contact.php',
			{
				method: 'GET',
				parameters: 
				[
					'action=select_place', 
					'start=' 	+ listStart, 
					'country=' 	+ listCountry, 
					'state=' 	+ listState, 
					'city=' 	+ city_id, 
					'lang=' 	+ CURRENT_LANG
				],
				onComplete : function(e) {ajax_eval(e.responseText)}
			}
		);
}


function select_expertise(expertise_id)
{
	$('sub_expertise_ul').innerHTML = "";
	empty_countries_list();
	unhighlight_list('expertise');
	unhighlight_list('regions');
	
	var req = new AJAX.Request('contact.php',
			{
				method: 'GET',
				parameters: 
				[
					'action=get_sub_expertises', 
					'sector=' 	+ expertise_id, 
					'lang=' 	+ CURRENT_LANG
				],
				onComplete : function(e) {ajax_eval(e.responseText)}
			}
		);
}

function select_expertise_final(expertise_id, sub_exp, paging_start)
{
	listStart 		= (paging_start != null) ? paging_start : 1;
	sub_exp 		= (sub_exp != null) ? sub_exp : 0;
	listExpertise 	= expertise_id;
	
	if (sub_exp == 1)
	{
		unhighlight_list('sub_expertise');
		highlight('sub_expertise', expertise_id);
	}
	else
	{
		highlight('expertise', expertise_id);
	}
	
	var req = new AJAX.Request('contact.php',
		{
			method: 'GET',
			parameters: 
			[
				'action=select_expertise', 
				'sub=' 			+ sub_exp, 
				'start=' 		+ listStart, 
				'expertise=' 	+ listExpertise, 
				'lang=' 		+ CURRENT_LANG
			],
			onComplete : function(e) {ajax_eval(e.responseText)}
		}
	);
}

function _set_sub_expertises_list(expertise_id, list)
{
	// Highlight selected region 
	highlight('expertise', expertise_id);
	
	// Create countries list 
	$('sub_expertise_ul').innerHTML = "";
	var list = list.split('|');
	
	for(x=0;x<list.length;x++)
	{
		list[x] = decode64(list[x]);
		var expertise = list[x].split('~');
		$('sub_expertise_ul').innerHTML += "<li id=\"sub_expertise_li_" + expertise[0] + "\"><a href=\"javascript:select_expertise_final('" + expertise[0] + "', 1)\">" + expertise[1] + "</a></li>\n";
	}
}

function _set_offices_list(list, sector_id, sub_sector_id)
{
	var results 		= $('contacts_results');
	results.innerHTML 	= '';
	var current_city 	= '';
	list 			= list.split('|');
	var results_list 	= document.createElement('div');
	results_list.className 	= 'results_box';
	
	/** office data **/
	// 0 => City Name  
	// 1 => Office ID 
	// 2 => Office Name 
	// 3 => Address 
	// 4 => State 
	// 5 => Country 
	// 6 => PostalCode 
	// 7 => Phone 
	// 8 => Fax 
	// 9 => Has contacts (0 or 1) 
	/** **/
	
	for(x=0;x<list.length;x++)
	{
		list[x] 	= decode64(list[x]);
		var office 	= list[x].split('~');
		
		if (office[0] != current_city)
		{
			if (current_city != '')
			{
				results_list.innerHTML += "<br style=\"clear:both\">";
				results.appendChild(results_list);
				results_list 			= document.createElement('div');
				results_list.className 	= 'results_box';
			}
			
			current_city 		= office[0];
			var theCity 		= document.createElement('div');
			theCity.className 	= 'results_box_city';
			var theCityText 	= document.createTextNode(office[0]);
			theCity.appendChild(theCityText);
			results_list.appendChild(theCity);
		}
		
		var theAddressBlock 		= document.createElement('div');
		theAddressBlock.className 	= 'results_box_address';
		
		if (sector_id > 0)
		{
			theAddressBlock.innerHTML 	= "<div class=\"results_showcontacts\"><a href=\"javascript:getContacts('" + office[1] + "', '" + sector_id + "', '" + sub_sector_id + "')\"><img src=\"images/"+CURRENT_LANG+"/btn_showcontacts.gif\" border=\"0\"/></a></div>";
		}
		
		var theH3 = document.createElement('h3');
		var theOfficeName 			= document.createTextNode(office[2]);
		theH3.appendChild(theOfficeName);
		theAddressBlock.appendChild(theH3);
		
		if (office[3] != '')
		{
			theAddressBlock.innerHTML += office[3] + "<br />";
		}
		
		var theCityNameText = document.createTextNode(office[0]);
		theAddressBlock.appendChild(theCityNameText);
		
		if (office[4] != '')
		{
			theAddressBlock.innerHTML += ", " + office[4];
		}
		
		theAddressBlock.innerHTML += "<br />";
		
		if (office[5] != '')
		{
			theAddressBlock.innerHTML += office[5] + "<br />";
		}
		
		if (office[6] != '')
		{
			theAddressBlock.innerHTML += office[6] + "<br />";
		}
		
		if (office[7] != '')
		{
			theAddressBlock.innerHTML += dict_phone_label + " " + office[7] + "<br />";
		}
		
		if (office[8] != '')
		{
			theAddressBlock.innerHTML += dict_fax_label + " " + office[8];
		}
		
		results_list.appendChild(theAddressBlock);
	}
	
	results_list.innerHTML += "<br style=\"clear:both\">";
	results.appendChild(results_list);
}

function list_no_result(search_statement)
{
	var results 			= $('contacts_results');
	results.innerHTML 		= '';
	change_header('', 0, '');
	var no_results 			= document.createElement('div');
	
	no_results.className 	= 'no_results';
	no_results.innerHTML 	= search_statement;
	results.appendChild(no_results);
}

function change_header(levels, num_results, pagination)
{
	$('results_paging_top').innerHTML = pagination;
	$('results_paging_bottom').innerHTML = pagination;
	$('results_levels').innerHTML = levels;
	/*
	if (pagination != '')
	{
		$('results_paging').innerHTML = pagination;
	}
	
	if (levels != '')
	{
		$('results_levels').innerHTML = levels;
	}
	*/
	
	if (num_results != "| 0" && num_results != "| 1" && num_results != "0" && num_results != "1")
	{
		$('results_num_results').innerHTML = "<span>" + num_results + " " + dict_results_dict + "</span>";
	}
	else
	{
		$('results_num_results').innerHTML = "";
	}
}

function unhighlight_list(listType)
{
	var list_ul 		= $(listType + '_ul');
	var ul_a_href 		= list_ul.getElementsByTagName('a');
	
	for(x=0;x<ul_a_href.length;x++)
	{
		ul_a_href[x].className = "";
	}
}

function highlight(listType, listId)
{
	var the_li 				= $(listType + '_li_' + listId);
	var li_a_href 			= the_li.getElementsByTagName('a');
	li_a_href[0].className 	= "select_" + listType + "_on";
}

function _close_sub_panel()
{
	/*
	$('select_sub_expertise').style.display = "none";
	$('expertise').style.width = "302px";
	$('selection_expertise').style.width = "302px";
	*/
	$('sub_expertise_ul').innerHTML = '<li>' + dict_no_sub_expertise + '</li>';
}

function _expand_sub_panel()
{
	/*
	$('select_sub_expertise').style.display = "";
	$('expertise').style.width = "620px";
	$('selection_expertise').style.width = "685px";
	*/
}

//** Search autocomplete **// 
var KEY_ASCII_LEFT = 37;
var KEY_ASCII_RIGHT = 39;
var KEY_ASCII_UP = 38;
var KEY_ASCII_DOWN = 40;
var KEY_ASCII_ENTER = 13;
var KEY_ASCII_ESC = 27;	

function autocomplete_field(event, acField)
{
	value = acField.value;	

	if(window.event)
	{
		keyAscii = event.keyCode;
	}
	else if(event.which)
	{
		keyAscii = event.which;
	}
	
	if (keyAscii == KEY_ASCII_LEFT || keyAscii == KEY_ASCII_RIGHT)
	{
		return false;
	}
	
	if (keyAscii == KEY_ASCII_DOWN)
	{
		AutoCompleteMoveDown(acField.name);
		return true;
	}
	
	if (keyAscii == KEY_ASCII_UP)
	{
		AutoCompleteMoveUp(acField.name);
		return true;
	}
	
	if (keyAscii == KEY_ASCII_ENTER)
	{
		AutoCompleteSelect(acField);
		remove_autocomplete(acField);
		return true;
	}
	
	if (keyAscii == KEY_ASCII_ESC)
	{
		AutoCompleteHide(acField.name);
		return true;
	}
	
	if (value.length < 3)
	{
		remove_autocomplete(acField);
		return false;
	}
	
	if(typeof(value) == 'string' && required(value))
	{
		var req = new AJAX.Request('contact.php',
		{
			method: 'GET',
			parameters: 
			[
				'action=autocomplete_field', 
				'value=' 	+ encode64(value), 
				'lang=' 	+ CURRENT_LANG
			],
			onComplete : function(e) {ajax_eval(e.responseText)}
			}
		);
	}
	else
	{
		AutoCompleteEmptyData();
		AutoCompleteShow('form_display_results', acField.id);
	}
}
	
function AutoCompleteSelectClick(acFormName, acFieldName, acKey)
{
	if($(acFieldName))
	{
		var acField = $(acFieldName)
	}
	else
	{
		var acField = document[acFormName][acFieldName];
	}
	
	acField.value = AUTOCOMPLETE_DATA[acKey];
	AutoCompleteHide(acFieldName);
	autocomplete_field(acField, 'true');
}

function fill_fields(quicksearch)
{
	$('quicksearch').value = quicksearch;
}

function quicksearch()
{
	unhighlight_list('regions');
	unhighlight_list('countries');
	unhighlight_list('expertise');
	var field_value = $('quicksearch_field').value;
	
	if (field_value != '')
	{
		var req = new AJAX.Request('contact.php',
		{
			method: 'GET',
			parameters: 
			[
				'action=quicksearch', 
				'field_value=' + encode64(field_value), 
				'lang=' + CURRENT_LANG 
			],
			onComplete : function(e) {ajax_eval(e.responseText)}
			}
		);
	}
}

INPUT = '';

function set_cookie_wait()
{
	set_cookie(INPUT.id,INPUT.value);
}

function set_cookie_autocomplete(input)
{
	INPUT = input;
	setTimeout('set_cookie_wait()',1000);
}

var quicksearch_timeout = '';

function hide_autocomplete_timeout(field)
{
	quicksearch_timeout = window.setTimeout("remove_autocompleteby_id('" + field.id + "');", 3500);
}

function clear_timeout_autocomplete(timeout_object)
{
	if (timeout_object != null && timeout_object != '')
	{
		window.clearTimeout(timeout_object);
	}
}


