var officeLocal = new Array();
var officeRegional = new Array();
var officeNational = new Array();
var RSS_NationalOffices = new Array();
var RSS_RegionalOffices = new Array();
var RSS_LocalOffices = new Array();
var RSS_errTag			= new Array();

function _runGL(lastViewedTab)
{
	// put a slight delay in in case the data is missing from the
	// select boxes when doing the refresh load
	if (_getGLSubmit().disabled) {
		setTimeout("_runGL('" + lastViewedTab + "')", 500);
	} else {
		_fetchGLIntialResults(lastViewedTab);
	}
}

function _fetchGLIntialResults(lastViewedTab)
{
	// Init surrounding div
	ajaxDiv = document.getElementById('ajaxDiv');

	// Init results div
	ajaxResDiv = document.getElementById('ajaxResDiv');

	// Init contact div
	ajaxContactsDiv = document.getElementById('ajaxContactsDiv');

	// Create the master form
	ajaxForm = document.createElement('form');

	_addKey();

	// Firstly we clear all children from the results div
	var len = ajaxResDiv.childNodes.length;
	while (ajaxResDiv.hasChildNodes()){
		ajaxResDiv.removeChild(ajaxResDiv.firstChild);
	}

	// Clear all children from the contacts div
	var len = ajaxContactsDiv.childNodes.length;
	while (ajaxContactsDiv.hasChildNodes()){
		ajaxContactsDiv.removeChild(ajaxContactsDiv.firstChild);
	}

	// Clear all children from the tabs div
	while (ajaxTabDiv.hasChildNodes()){
		ajaxTabDiv.removeChild(ajaxTabDiv.firstChild);
	}

	// Hide the contacts div
	hideDiv('ajaxContactsDiv');
	ajaxContactsDiv.style.height = '1px';
	ajaxContactsDiv.style.margin = '0';
	ajaxContactsDiv.style.padding = '0';
	ajaxContactsDiv.style.lineHeight = '0';

	// Show the results div
	showDiv('ajaxResDiv');

	_hideTabDiv();
	
	if (document.getElementById('columnTwoLarge')){
		document.getElementById('columnTwoLarge').className = 'col4withBg';
	}
	
	var loadingHolder = document.createElement('div');
	loadingHolder.className = loadingText;
	ajaxResDiv.appendChild(loadingHolder);
	ajaxResDiv.style.height = 'auto';
	ajaxResDiv.style.margin = 'auto';
	ajaxResDiv.style.lineHeight = '18px';

	// Show the results

	var countryValue 	= _getGLCountry().value;
	var regionValue 	= _getGLRegion().value;
	var subRegionValue 	= _getGLSubRegion().value;
	var industryValue 	= _getGLIndustry().value;
	var divisionValue 	= _getGLDivision().value;

	var xmlHttp = getXmlHttpRequest();

	var url = ctrlUrl + 'ajax/results' 	+
						'/countrycode/' + countryValue + // Country
						'/statecode/' 	+ regionValue + // Region
						'/countycode/' 	+ subRegionValue + // Sub Region
						'/industryKey/' 	+ industryValue + // Industry
						'/divisionKey/' 	+ divisionValue // Division
						;

	xmlHttp.open("GET", url, true);

	xmlHttp.send(null);

	xmlHttp.onreadystatechange = function()
	{

		/*switch(xmlHttp.readyState){
			case 3:
				alert(xmlHttp.responseXML);
				break;
			case 4:
				//
				break;

		}*/

		if (xmlHttp.readyState == 4){
			
				if (xmlHttp.status == 200){
					
					if (document.getElementById('columnTwoLarge')){
					document.getElementById('columnTwoLarge').className = '';
				}

				// Init surrounding div
				ajaxDiv = document.getElementById('ajaxDiv');

				// Init results div
				ajaxResDiv = document.getElementById('ajaxResDiv');

				// Init contact div
				ajaxContactsDiv = document.getElementById('ajaxContactsDiv');

				// Create the master form
				ajaxForm = document.createElement('form');

				ajaxResDiv.innerHTML = "";

				// Fetch the response tag
				var rspXML = xmlHttp.responseXML;

				// Fetch the error tag
				var errTag = rspXML.getElementsByTagName('error');

				// Was there an error?
				if (errTag.length != 0){

					// ContentWrapper
					var contentWrapper = document.createElement('div');
					contentWrapper.className = 'contentWrapper';
					
					// Heading
					var conH1 = document.createElement('h2');
					conH1.innerHTML = noResultsFoundText;
					contentWrapper.appendChild(conH1);

					// *No* results found
					var pEle = document.createElement('p');
					pEle.innerHTML = generalSearchSuggestionsText;
					contentWrapper.appendChild(pEle);
					
					ajaxResDiv.appendChild(contentWrapper);

					// Init tabs
					_showTabDiv();
					_initTabs();

					_switchTabDsp(1);
					document.getElementById('tab_pos_3').innerHTML = '<span>' + agentsText + ' (0)</span>';
					document.getElementById('tab_pos_3').className = 'inactive';
					
					document.getElementById('tab_pos_2').innerHTML = '<span>' + distributorsText + ' (0)</span>';
					document.getElementById('tab_pos_2').className = 'inactive';
					
					document.getElementById('tab_pos_1').innerHTML = '<span>' + officesText + ' (0)</span>';
					document.getElementById('tab_pos_1').className = 'inactive';

				} else {

					// Results found

					// Init tabs
					_showTabDiv();
					_initTabs();

					// Rotork sales structure link
					// We create the link here and set it's display property
					// to 'none'. We then check (after the results have been
					// fetched) that whether the div should be displayed or
					// not
					
					var conBackLinkWrap 	= document.createElement('div');
					conBackLinkWrap.className = 'lrgBtn';
					conBackLinkWrap.id = 'RSSTop';
					conBackLinkWrap.style.cssFloat = 'right';
					conBackLinkWrap.style.display = 'none';
					
					var conBackLink 		= document.createElement('a');
					conBackLink.innerHTML = viewSalesStructureText;
					conBackLink.title = viewSalesStructureText;
					conBackLink.href = 'javascript:_runGLSalesStructure();';
					
					conBackLinkWrap.appendChild(conBackLink);
					ajaxResDiv.appendChild(conBackLinkWrap);


					// Heading
					//conH1 = document.createElement('h1');
					//conH1.innerHTML = searchResultsText;
					//ajaxResDiv.appendChild(conH1);

					//var ins = document.createElement('p');
					//ins.innerHTML = clickOnTheCompanyText + '.<br />';
					//ajaxResDiv.appendChild(ins);

					
					// Agents/Reps
					
					var agentDiv = document.createElement('div');
					agentDiv.id = 'agentsRepsTB';
					ajaxResDiv.appendChild(agentDiv);

					var agentRep = rspXML.getElementsByTagName('agentRep');
					var lastAccountNumber = 0;
					
					for(var i = 0, j = agentRep.length; i < j; i++){

						if (0 == i){
							
							// Add the heading
							_addBlockHeading(rotorkAgentsRepsText + ' (' + agentRep.length + ')', agentDiv);
						}

						// Add details
						_addAccounts(agentRep, i, agentDiv, 'general', true, true);

					}

					
					// Distributors
					
					var distribDiv = document.createElement('div');
					distribDiv.id = 'distributorsTB';
					ajaxResDiv.appendChild(distribDiv);

					var distributor = rspXML.getElementsByTagName('distributor');
					
					for(var i = 0, j = distributor.length; i < j; i++){

						if (0 == i){
							
							// Add the heading
							_addBlockHeading(distributorsText + ' (' + distributor.length + ')', distribDiv);
						}

						// Add details
						_addAccounts(distributor, i, distribDiv, 'general', true, true);

					}

					
					// Offices
					
					var officesDiv = document.createElement('div');
					officesDiv.id = 'officesTB';
					ajaxResDiv.appendChild(officesDiv);
					
					// Local Offices
					
					officeLocal = rspXML.getElementsByTagName('officeLocal');

					for(var i = 0, j = officeLocal.length; i < j; i++){

						if (0 == i){

							// Add the heading
							_addBlockHeading(localOfficesText + ' (' + officeLocal.length + ')', officesDiv);
						}

						// Add details
						_addAccounts(officeLocal, i, officesDiv, 'general', true, true);

					}

					
					// Regional Offices
					
					officeRegional = rspXML.getElementsByTagName('officeRegional');

					for(var i = 0, j = officeRegional.length; i < j; i++){

						if (0 == i){
							
							// Add the heading
							_addBlockHeading(regionalOfficesText + ' (' + officeRegional.length + ')', officesDiv);
						}

						// Add details
						_addAccounts(officeRegional, i, officesDiv, 'general', true, true);

					}

					
					// National Offices
					
					officeNational = rspXML.getElementsByTagName('officeNational');
					
					for(var i = 0, j = officeNational.length; i < j; i++){

						if (0 == i){
							
							// Add the heading
							_addBlockHeading(nationalOfficesText + ' (' + officeNational.length + ')', officesDiv);
						}

						// Add details
						_addAccounts(officeNational, i, officesDiv, 'general', true, true);

					}

					// Total Offices calculation
					var totalOffices = (officeLocal.length + officeRegional.length + officeNational.length);

					// Which tab should be "on" be default?
					if (totalOffices > 0){
						_switchTabDsp(1);
					}else if(distributor.length > 0){
						_switchTabDsp(2);
					}else if(agentRep.length > 0){
						_switchTabDsp(3);
					}else{
						_switchTabDsp(1);
					}

					
					// Hrefs or inactive spans?
					
					if (0 == agentRep.length){
						document.getElementById('tab_pos_3').innerHTML = '<span>' + agentsText + ' (0)</span>';
						document.getElementById('tab_pos_3').className = 'inactive';
					}else{
						var resultTab3UlLi = document.getElementById('tab_pos_3');
						var resultTab3UlLiA = document.createElement('a');
						resultTab3UlLiA.innerHTML = agentsText + ' (' + agentRep.length + ')';
						resultTab3UlLiA.title =  agentsText + ' (' + agentRep.length + ')';
						resultTab3UlLiA.href = '#';
						resultTab3UlLiA.onclick = _displayAgents;
						resultTab3UlLi.appendChild(resultTab3UlLiA);
					}
					
					if (0 == distributor.length){
						document.getElementById('tab_pos_2').innerHTML = '<span>' + distributorsText + ' (0)</span>';
						document.getElementById('tab_pos_2').className = 'inactive';
					}else{
						var resultTab3UlLi = document.getElementById('tab_pos_2');
						var resultTab3UlLiA = document.createElement('a');
						resultTab3UlLiA.innerHTML = distributorsText + ' (' + distributor.length + ')';
						resultTab3UlLiA.title =  distributorsText + ' (' + distributor.length + ')';
						resultTab3UlLiA.href = '#';
						resultTab3UlLiA.onclick = _displayDistributors;
						resultTab3UlLi.appendChild(resultTab3UlLiA);
					}
					
					if (0 == totalOffices){
						document.getElementById('tab_pos_1').innerHTML = '<span>' + officesText + ' (0)</span>';
						document.getElementById('tab_pos_1').className = 'inactive';
					}else{
						var resultTab3UlLi = document.getElementById('tab_pos_1');
						var resultTab3UlLiA = document.createElement('a');
						resultTab3UlLiA.innerHTML = officesText + ' (' + totalOffices + ')';
						resultTab3UlLiA.title = officesText + ' (' + totalOffices + ')';
						resultTab3UlLiA.href = '#';
						resultTab3UlLiA.onclick = _displayOffices;
						resultTab3UlLi.appendChild(resultTab3UlLiA);
					}

					// Has the user selected a tab, viewed a record and then
					// returned back to the list page?
					if (is_int(lastViewedTab)){
						_switchTabDsp(lastViewedTab);
					}

					// Clear all
					var clearMe = document.createElement('div');
					clearMe.className = 'clearMe';
					ajaxResDiv.appendChild(clearMe);

					showDiv('ajaxResDiv');

				}

			} else {
				
				// Non 200 HTTP status
			}
		} 
	}
}

function _displayStructure()
{
	/*
	alert('Local:'+officeLocal.length);
	alert('Regional:'+officeRegional.length);
	alert('National:'+officeNational.length);
	alert('Local RSS:'+RSS_LocalOffices.length);
	alert('Regional RSS:'+RSS_RegionalOffices.length);
	alert('National RSS:'+RSS_NationalOffices.length);
	*/

	if(officeLocal.length == 0 && officeRegional.length == 0 && officeNational.length == 0){
		if(RSS_LocalOffices.length || RSS_RegionalOffices.length || RSS_NationalOffices.length){
			return 1;
		} else {
			return 0;
		}
	} else {
		return 0;
	}
}

function _addTableHeading(headingName)
{
	// Add Rotork Agent / Reps header
	var headerRow = document.createElement('tr');
	var headerTh = document.createElement('th');
	headerTh.colSpan = 2;
	headerTh.innerHTML = headingName;
	headerRow.appendChild(headerTh);

	// append the header row to the table
	tableBody.appendChild(headerRow);
}

function _addBlockHeading(headingName, parentDiv)
{
	var blockHeading = document.createElement('h2');
	blockHeading.className = 'blockHeader';
	blockHeading.innerHTML = headingName;
	
	parentDiv.appendChild(blockHeading);
}

/*
 * Adds account details to 'parentDiv'
 *
 * account object The xml account object
 * i integer The iteration
 * parentDiv object The div to append the results to
 * accountClassName string The name of the class (For odd rows)
 * area string The area were in. 'general' or 'sales structure';
 * displayAsLink boolean Whether the account should be linked
 */
function _addAccounts(account, i, parentDiv, area, displayAsLink, displayViewDetails)
{
	
	// Global table child
	tableChild = document.createElement('table');
	tableChild.cellSpacing = 0;
	tableChild.className = 'globalLocatorItem';

	// Create tbody as this is required for correct display in IE
	// Must be global.
	tableBody = document.createElement('tbody');
	
	var accountNumber = nodeValue(account[i].getElementsByTagName('accountnumber')[0]);

	// Company name

	var detailsDef = document.createElement('h2');
	detailsDef.className = 'blockTitle';

	if(displayViewDetails){
				
		var viewDetailsHref = document.createElement('a');
		viewDetailsHref.href = 'javascript:displayAccount(\'' + accountNumber + '\', \'' + area + '\')';
		viewDetailsHref.className = 'button';
		viewDetailsHref.style.cssFloat = 'right';
		viewDetailsHref.style.styleFloat = 'right'; // For IE
		viewDetailsHref.style.marginTop = '4px'; // For IE
		viewDetailsHref.innerHTML = viewDetailsText;
		viewDetailsHref.title = viewDetailsText;

		detailsDef.appendChild(viewDetailsHref);
		
	} 
		
	if (true == displayAsLink){
	
		var companyLink = document.createElement('a');
		companyLink.href = 'javascript:displayAccount(\'' + accountNumber + '\', \'' + area + '\')';
		companyLink.className = 'glAccountLink';
		companyLink.innerHTML = nodeValue(account[i].getElementsByTagName('companyname')[0]);
		detailsDef.appendChild(companyLink);
		
	} else {
		
		detailsDef.innerHTML = nodeValue(account[i].getElementsByTagName('companyname')[0]);

	}
	
	parentDiv.appendChild(detailsDef);

	// Industries Box

	var detailsDef = document.createElement('div');
	detailsDef.className = 'industries';
	var industryTags = account[i].getElementsByTagName('industries')[0].childNodes;
	detailsDef.innerHTML = '<h3>Industries</h3>';
	var jInd = industryTags.length
	
	for(var iInd = 0; iInd < jInd; iInd++) {
		
		var indKey = nodeValue(industryTags[iInd].getElementsByTagName('key')[0]);

		var industry = false;
		var jIcon = industries.length;
		
		for(var iIcon=0; iIcon < jIcon; iIcon++){
			
			if(industries[iIcon].key == indKey){
				
				industry = industries[iIcon];
			}
		}
		
		if(industry){

			var industryIcon = document.createElement('img');
			industryIcon.title = industry.name;
			industryIcon.alt = industry.name;
			//imageUrl = ctrlUrl + '../public/frontend/i/icons/' + industry.icon;
			imageUrl = '/public/frontend/i/icons/' + industry.icon;
			industryIcon.src = imageUrl;
			detailsDef.appendChild(industryIcon);
			detailsDef.innerHTML += '&nbsp;';
			delete industryIcon;
		
		}
	}
	
	parentDiv.appendChild(detailsDef);
	
	
	// City Field
	
	var subHeaderRow = document.createElement('tr');

	var subHeaderDef = document.createElement('td');
	subHeaderDef.className = 'header';
	subHeaderDef.innerHTML = cityText;
	subHeaderRow.appendChild(subHeaderDef);
	var detailsDef = document.createElement('td');
	detailsDef.className = 'details';

	var town 		= nodeValue(account[i].getElementsByTagName('town')[0]);
	var address3 	= nodeValue(account[i].getElementsByTagName('address3')[0]);

	if (town.length > 0){
		//detailsDef.innerHTML = toTitleCase(town);
		detailsDef.innerHTML = town;
	}else if (address3.length > 0){
		detailsDef.innerHTML = toTitleCase(address3);
	}else{
		detailsDef.innerHTML = '-';
	}
	subHeaderRow.appendChild(detailsDef);
	tableBody.appendChild(subHeaderRow);

	
	// Postal Region Field
	
	var subHeaderRow = document.createElement('tr');

	var subHeaderDef = document.createElement('td');
	subHeaderDef.className = 'header';
	subHeaderDef.innerHTML = regionText;
	subHeaderRow.appendChild(subHeaderDef);

	var detailsDef = document.createElement('td');
	detailsDef.className = 'details';

	var statedesc = nodeValue(account[i].getElementsByTagName('statedesc')[0]);

	if (statedesc.length > 0){
		detailsDef.innerHTML = toTitleCase(statedesc);
	}else{
		detailsDef.innerHTML = '-';
	}
	subHeaderRow.appendChild(detailsDef);
	tableBody.appendChild(subHeaderRow);

	
	// Description Field
	
	var subHeaderRow = document.createElement('tr');

	var subHeaderDef = document.createElement('td');
	subHeaderDef.className = 'header';
	subHeaderDef.innerHTML = descriptionText;
	subHeaderRow.appendChild(subHeaderDef);

	var detailsDef = document.createElement('td');
	detailsDef.className = 'details';

	var description = nodeValue(account[i].getElementsByTagName('description')[0]);

	if (description.length > 0){
		detailsDef.innerHTML = description;
	}else{
		detailsDef.innerHTML = '-';
	}
	subHeaderRow.appendChild(detailsDef);
	tableBody.appendChild(subHeaderRow);


	// Divisions Field
	
	var subHeaderRow = document.createElement('tr');

	var subHeaderDef = document.createElement('td');
	subHeaderDef.className = 'header';
	subHeaderDef.innerHTML = divisionsText;
	subHeaderRow.appendChild(subHeaderDef);

	var detailsDef = document.createElement('td');
	detailsDef.className = 'details';
	var divisions = account[i].getElementsByTagName('divisions')[0].childNodes;
	detailsDef.innerHTML = ''; // null
	for(var iDiv = 0, jDiv = divisions.length; iDiv < jDiv; iDiv++){
		detailsDef.innerHTML += nodeValue(divisions[iDiv].getElementsByTagName('name')[0]);
		if ((iDiv + 1) < jDiv){
			detailsDef.innerHTML += ', ';
		}
	}
	subHeaderRow.appendChild(detailsDef);
	tableBody.appendChild(subHeaderRow);
	
	
	
	if (!displayViewDetails) {

		// Phone Number Field
		
		var subHeaderRow = document.createElement('tr');

		var subHeaderDef = document.createElement('td');
		subHeaderDef.className = 'header';
		subHeaderDef.innerHTML = phoneNumberText;
		subHeaderRow.appendChild(subHeaderDef);

		var detailsDef = document.createElement('td');
		detailsDef.className = 'details';

		var phoneNo = nodeValue(account[i].getElementsByTagName('phoneno')[0]);
		
		if (phoneNo.length > 0){
			
			detailsDef.innerHTML = '';

			var mpcountry 	= nodeValue(account[i].getElementsByTagName('mpcountry')[0]);
			var mparea 		= nodeValue(account[i].getElementsByTagName('mparea')[0]);

			detailsDef.innerHTML += '+' + mpcountry + ' ';
			detailsDef.innerHTML += mparea + ' ';
			detailsDef.innerHTML += phoneNo;
		
		} else {
			
			detailsDef.innerHTML = '-';
		}

		subHeaderRow.appendChild(detailsDef);

		tableBody.appendChild(subHeaderRow);

		
		// Fax Number Field
		
		var subHeaderRow = document.createElement('tr');

		var subHeaderDef = document.createElement('td');
		subHeaderDef.className = 'header';
		subHeaderDef.innerHTML = faxNumberText;
		subHeaderRow.appendChild(subHeaderDef);

		var detailsDef = document.createElement('td');
		detailsDef.className = 'details';

		var faxNo = nodeValue(account[i].getElementsByTagName('faxno')[0]);
		
		if (faxNo.length > 0){

			var fxcountry 	= nodeValue(account[i].getElementsByTagName('fxcountry')[0]);
			var fxarea 		= nodeValue(account[i].getElementsByTagName('fxarea')[0]);

			detailsDef.innerHTML += '+' + fxcountry + ' ';
			detailsDef.innerHTML += fxarea + ' ';
			detailsDef.innerHTML += faxNo;

		} else {
			
			detailsDef.innerHTML = '-';
		}

		subHeaderRow.appendChild(detailsDef);
		tableBody.appendChild(subHeaderRow);

		
		// Description Field
		
		var subHeaderRow = document.createElement('tr');

		var subHeaderDef = document.createElement('td');
		subHeaderDef.className = 'header';
		subHeaderDef.innerHTML = generalEmailText;
		subHeaderRow.appendChild(subHeaderDef);

		var detailsDef = document.createElement('td');
		detailsDef.className = 'details';

		var generalemail = nodeValue(account[i].getElementsByTagName('generalemail')[0]);

		if (generalemail.length > 0){

			emailLink = document.createElement('a');
			emailLink.href = 'mailto:' + generalemail
			emailLink.innerHTML = generalemail;
			detailsDef.appendChild(emailLink);
			delete emailLink;
		
		} else {
			
			detailsDef.innerHTML = '-';
		}
		subHeaderRow.appendChild(detailsDef);
		tableBody.appendChild(subHeaderRow);

		// Website Field
		
		var subHeaderRow = document.createElement('tr');

		var subHeaderDef = document.createElement('td');
		subHeaderDef.className = 'header';
		subHeaderDef.innerHTML = websiteText;
		subHeaderRow.appendChild(subHeaderDef);

		var detailsDef = document.createElement('td');
		detailsDef.className = 'details';
		var websiteLink = nodeValue(account[i].getElementsByTagName('websitelink')[0]);
		
		if (websiteLink.length > 0){
			
			// Format the website link
			var siteLink = websiteLink.replace(/http:\/\//,"");
			siteLink.replace(/http\/\//,"");
						
			var websitelinkHref = document.createElement('a');
			websitelinkHref.href = siteLink;
			websitelinkHref.target = '_blank';
			websitelinkHref.innerHTML = websiteLink.replace(/http:\/\/http:\/\//,"http://");
			detailsDef.appendChild(websitelinkHref);
		
		} else {
			
			detailsDef.innerHTML = '-';
		}
	}


	subHeaderRow.appendChild(detailsDef);
	tableBody.appendChild(subHeaderRow);
	
	
	// Append all
	
	tableChild.appendChild(tableBody);
	parentDiv.appendChild(tableChild);
}

function _addSpacerRow(parentDiv)
{
	var subSpacerRow = document.createElement('tr');
	subSpacerRow.className = 'gLSpacer';
	var subSpacerDef = document.createElement('td');
	subSpacerDef.innerHTML = '<img src="' + ctrlUrl.replace('/globallocator', '/public/frontend/i') + 'spacer.gif" width="496" height="1" />';
	subSpacerDef.colSpan = 2;
	subSpacerRow.appendChild(subSpacerDef);
	tableBody.appendChild(subSpacerRow);
	tableChild.appendChild(tableBody);
	parentDiv.appendChild(tableChild);
}
function _addSpacerRow2(parentDiv)
{
	_addSpacerRow(parentDiv);
	var subSpacerRow = document.createElement('tr');
	subSpacerRow.className = 'gLSpacer2';
	var subSpacerDef = document.createElement('td');
	subSpacerDef.innerHTML = '<img src="' + ctrlUrl.replace('/globallocator', '/public/frontend/i') + 'spacer.gif" width="496" height="1" />';
	subSpacerDef.colSpan = 2;
	subSpacerRow.appendChild(subSpacerDef);
	tableBody.appendChild(subSpacerRow);
	tableChild.appendChild(tableBody);
	parentDiv.appendChild(tableChild);
	_addSpacerRow(parentDiv)
}
function _displayAgents()
{
	// Set the tab to on
	_switchTabDsp(3);
}
function _displayDistributors()
{
	// Set the tab to on
	_switchTabDsp(2);
}
function _displayOffices()
{
	// Set the tab to on
	_switchTabDsp(1);
}
function _checkTabInactive(elementID)
{
	if (document.getElementById(elementID).innerHTML.match('(0)')) {
		return 'inactive';
	} else {
		return 'off';
	}
}

function _switchTabDsp(pos)
{
	// Affect HTML class/ids
	if(document.getElementById('result-tab-ul')){
		document.getElementById('result-tab-ul').id = 'on-' + pos;
	}else if(document.getElementById('none-on')){
		document.getElementById('none-on').id = 'on-' + pos;
	}else if(document.getElementById('on-1')){
		document.getElementById('on-1').id = 'on-' + pos;
	}else if(document.getElementById('on-2')){
		document.getElementById('on-2').id = 'on-' + pos;
	}else if(document.getElementById('on-3')){
		document.getElementById('on-3').id = 'on-' + pos;
	}

	document.getElementById('tab_pos_' + pos).className = 'on';
	switch(pos){
		case 1:
			document.getElementById('tab_pos_2').className = _checkTabInactive('tab_pos_2');
			document.getElementById('tab_pos_3').className = _checkTabInactive('tab_pos_3');
			break;
		case 2:
			document.getElementById('tab_pos_1').className = _checkTabInactive('tab_pos_1');
			document.getElementById('tab_pos_3').className = _checkTabInactive('tab_pos_3');
			break;
		case 3:
			document.getElementById('tab_pos_1').className = _checkTabInactive('tab_pos_1');
			document.getElementById('tab_pos_2').className = _checkTabInactive('tab_pos_2');
			break;
	}

	// Set the global tab on value
	tabOn = pos;

	// Now show/hide the relevant data
	switch(pos){
		case 3:
			if(document.getElementById('agentsRepsTB')){
				document.getElementById('agentsRepsTB').style.display = 'block';
			}
			if(document.getElementById('distributorsTB')){
				document.getElementById('distributorsTB').style.display = 'none';
			}
			if(document.getElementById('officesTB')){
				document.getElementById('officesTB').style.display = 'none';
			}
			break;
		case 2:
			if(document.getElementById('agentsRepsTB')){
				document.getElementById('agentsRepsTB').style.display = 'none';
			}
			if(document.getElementById('distributorsTB')){
				document.getElementById('distributorsTB').style.display = 'block';
			}
			if(document.getElementById('officesTB')){
				document.getElementById('officesTB').style.display = 'none';
			}
			break;
		case 1:
			if(document.getElementById('agentsRepsTB')){
				document.getElementById('agentsRepsTB').style.display = 'none';
			}
			if(document.getElementById('distributorsTB')){
				document.getElementById('distributorsTB').style.display = 'none';
			}
			if(document.getElementById('officesTB')){
				document.getElementById('officesTB').style.display = 'block';
			}
			break;
	}
}
function _clearChildren(elementID)
{
	var theElement = document.getElementById(elementID);
	while (theElement.hasChildNodes()){
		theElement.removeChild(theElement.firstChild);
	}
}

function _redirectToGL()
{
	var countryValue 	= _getGLCountry().value;
	var regionValue 	= _getGLRegion().value;
	var subRegionValue 	= _getGLSubRegion().value;
	var industryValue 	= _getGLIndustry().value;
	var divisionValue 	= _getGLDivision().value;

	var redirectURL = ctrlUrl.replace('/globallocator', '/contact/index/globallocator');
	redirectURL += 'auto/1/';
	redirectURL += 'countrycode/' + countryValue  + '/';
	redirectURL += 'statecode/' + regionValue + '/';
	redirectURL += 'countycode/' + subRegionValue + '/';
	redirectURL += 'industryKey/' + industryValue + '/';
	redirectURL += 'divisionKey/' + divisionValue + '/';

	window.location = redirectURL;
}



function _addKey()
{
	if(!(document.getElementById('keydiv'))){
		keyDiv = document.createElement('div');
		keyDiv.className = 'quickLinks';
		keyDiv.id = 'keydiv';
		
		keySeparatorDiv = document.createElement('div');
		keySeparatorDiv.className = 'columnOneFooter';
		
		keyHeadingDiv = document.createElement('div');
		keyHeadingDiv.className = 'columnOneTitle';
		keyHeadingDiv.innerHTML = '<h2>' + keyText + '</h2>';
		
		keyDl = document.createElement('dl');
		keyDl.id = 'iconKey';
		for(i=0,j=industries.length; i < j; i++){
			if(industries[i].icon){
				var industryTerm = document.createElement('dt');
				var industryIcon = document.createElement('img');

				industryIcon.title = industries[i].name;
				industryIcon.alt = industries[i].name;
				imageUrl = '/public/frontend/i/icons/' + industries[i].icon;
				industryIcon.src = imageUrl;
				industryTerm.appendChild(industryIcon);
				var industryDescription = document.createElement('dd');
				industryDescription.innerHTML = industries[i]['name'];
				keyDl.appendChild(industryTerm);
				keyDl.appendChild(industryDescription);
				delete industryTerm;
				delete industryDescription;
			}
		}
		
		keyDiv.appendChild(keyDl);
		delete keyDl;
		
		var columnOneDiv = document.getElementById('columnOne');
		
		columnOneDiv.appendChild(keySeparatorDiv);
		columnOneDiv.appendChild(keyHeadingDiv);
		columnOneDiv.appendChild(keyDiv);
		
		delete keySeparatorDiv;
		delete keyHeadingDiv;
		delete keyDiv;
		
		
	}
}

function _removeKey()
{
	if(document.getElementById('keydiv')){
		keyDiv = document.getElementById('keydiv');
		document.removeChild(keyDiv);
	}

}
