var autoSearch = true, autoSearchPhrase = '';

function toggleCompany()
{
	if ($("company_required").value == 1) {
		$("addLink").innerHTML = "Add my Company";
		$("company").style.display = "none";
		//$("company_id").style.display = "";
	}
	else if ($("company_required").value == 0) {
		$("addLink").innerHTML = "Select from List";
		$("company").style.display = "";
		//$("company_id").style.display = "none";
		$("company_id").selectedIndex = 0;
	}
		
	if ($("employment_status").value == "Employed") {
		$("company_required").value = ($("company_required").value == 1) ? 0 : 1;
	}
	else {
		$("company_required").value = 2;
	}
	if (typeof($("formRegister").submitted)) {
		form.runChecks($("formRegister"));
	}
}

function searchCompanies(e)
{
	var v = $('company').value;
	if (v.replace(/\\S/, '').length) {
		
		if (e && ! autoSearch) {
			if (v == autoSearchPhrase) {
				return;
			}
			autoSearchPhrase = null;
			autoSearch = true;
		}
		
		xml.queue_add('/AU/company-search?name=' + encodeURIComponent(v), showCompanies);
	}
	else if (!e) {
		alert("Please enter some text to search on");
	}
}

function showCompanies(r)
{
	eval('companyData = ' + r.responseText + ';');
	var i = $('companySelectorPanel');
			
	if (! i) {
		i = $('companyWrap').appendChild(create('div'));
		i.id = 'companySelectorPanel';		
	}
	
	i.innerHTML = '';
	
	if (companyData.length) {
		
		$('companySelectorPanel').style.display = 'block';
		
		var b = i.appendChild(create('div'));
		b.className = 'warn2';
		b.innerHTML = 'Matches found. Click to view in a new window and confirm';
		
		for (var x = 0, d = companyData, l = d.length; x < l; x ++) {
			var a = i.appendChild(create('a'));
			a.href = '#';
			a.id = 'company_sel_link_' + d[x].id;
			a.windowLink = '/AU/directory/' + d[x].id + '/' + d[x].name.replace(/[^a-z]/i, '') + "?confirmation=1";						
			a.onclick = function() { 				
				window.open(this.windowLink, '', 'width=670,scrollbars=yes'); 				
				return false;
			};
			a.innerHTML = d[x].name;
		}
		
		a = i.appendChild(create('a'));
		a.href = '#';
		a.style.color = 'red';
		a.style.paddingTop = '10px';
		a.innerHTML = '<strong>None of these, create new listing</strong>';
		a.onclick = function() { 
			$('companySelectorPanel').style.display = 'none'; 
			autoSearch = false;
			autoSearchPhrase = $('company').value;
			return false; 
		}
	}
	else {
		$('companySelectorPanel').style.display = 'none';
	}
}

function removeCompanyOption(id)
{
	$('company_sel_link_' + id).parentNode.removeChild($('company_sel_link_' + id));
	if (! byTag('a', $('companySelectorPanel')).length) {
		$('companySelectorPanel').style.display = 'none';
	}
}

function selectCompanyOption(id)
{
	$('company_id').value = id;
	for (var x = 0, d = companyData, l = d.length; x < l; x ++) {
		if (d[x].id == id) {
			$('companyWrapX').innerHTML = d[x].name + ' &nbsp; <a href="#" class="inline" onclick="companyReset();return false">Change</a>';
			$('companyWrapX').style.display = 'block';
		}
	}	
	$('companyWrap').style.display = 'none';
	$('companySelectorPanel').style.display = 'none';	
}

function companyReset()
{
	$('companyWrapX').style.display = 'none';
	$('companyWrap').style.display= 'block';
	$('company_id').value = 0;
	$('company').value = '';
}