
var glRequestRedirect = false;
var glRequestParams = new Array();
var glAutoRunComplete = false;

var ajaxDiv = null;
var ajaxTabDiv = null;
var ajaxResDiv = null;
var ajaxContactsDiv = null;
var ajaxForm = null;
var ajaxFormDiv = null;
var tabOn = false;

var industries = new Array();

/**
 * On page load init function
 *
 */
function initGL(redirectOnSubmit, requestParams)
{
	// Request params stuff
	// Are we redirecting on suibmit?
	glRequestRedirect = redirectOnSubmit;
	// Make the request params global
	glRequestParams = requestParams;

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

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

	// Non submit data
	if ( (1 != glRequestParams['auto']) ){

		var nonResP = document.createElement('div');
		nonResP.className = 'contentWrapper';

		var nonResPSpan = document.createElement('p');
		nonResPSpan.innerHTML = pleaseUseText;

		nonResP.appendChild(nonResPSpan);

		ajaxResDiv.appendChild(nonResP);

	} 

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

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

	// Create the form div that each element is wrapped within
	ajaxFormDiv = document.createElement('div');
	ajaxFormDiv.className = 'inputBg';

	// Tab div
	ajaxTabDiv = document.getElementById('tab-container');

	// For now, hide the tab div
	_hideTabDiv();

	// Init Country
	_initGLCountry();

	// Init Region
	_initGLRegion();

	// Init Sub Region
	_initGLSubRegion();

	// Init Industry
	_initGLIndustry();

	// Init division
	_initGLDivision();

	// Init controls
	_initGLControls();

}

function makeDDNodeNA(node)
{
	// Remove all children from the region drop down
	var len = node.childNodes.length;
	while (node.hasChildNodes()){
		node.removeChild(node.firstChild);
	}

	// Now add a sole 'NA' option to region drop down with
	// a value of -2 so we can differentiate this from the
	// --SELECT-- option
	var nodeOpt = document.createElement('option');
	nodeOpt.value = -2;
	nodeOpt.innerHTML = 'N/A';
	nodeOpt.selected = true;
	node.appendChild(nodeOpt);
}

function _resetGLRegion()
{
	var regionDD = document.getElementById('statecode');

	// Firstly clear the list of drop down options
	var len = regionDD.childNodes.length;
	while (regionDD.hasChildNodes()){
		regionDD.removeChild(regionDD.firstChild);
	}

	// Add the --SELECT-- option back in
	var regionDDOpt = document.createElement('option');
	regionDDOpt.value = -1;
	regionDDOpt.innerHTML = pleaseSelectText;
	regionDD.appendChild(regionDDOpt);

}

function _resetGLSubRegion()
{
	var subRegionDD = document.getElementById('countycode');

	// Firstly clear the list of drop down options
	var len = subRegionDD.childNodes.length;
	while (subRegionDD.hasChildNodes()){
		subRegionDD.removeChild(subRegionDD.firstChild);
	}

	// Add the --SELECT-- option back in
	var subRegionDDOpt = document.createElement('option');
	subRegionDDOpt.value = -1;
	subRegionDDOpt.innerHTML = pleaseSelectText;
	subRegionDD.appendChild(subRegionDDOpt);

}

function _resetGLIndustry()
{
	_getGLIndustry().options[0].selected = true;
}
function _resetGLDivision()
{
	_getGLDivision().options[0].selected = true;
}

function _getGLCountry()
{
	return document.getElementById('countrycode');
}
function _getGLRegion()
{
	return document.getElementById('statecode');
}
function _getGLSubRegion()
{
	return document.getElementById('countycode');
}
function _getGLIndustry()
{
	return document.getElementById('industryKey');
}
function _getGLDivision()
{
	return document.getElementById('divisionKey');
}
function _getGLSubmit()
{
	return document.getElementById('gLSubmit');
}
function _getGLReset()
{
	return document.getElementById('gLReset');
}
function _setCtrlUrl()
{
	return ctrlUrl;
}

function _getGLRegionValue()
{
	return _getGLRegion().value.split('_');

}
function _initTabs()
{

	// The tab container
	var resultTab3 = document.createElement('div');
	resultTab3.id = 'result-tab-3';

	// The UL
	var resultTab3Ul = document.createElement('ul');
	resultTab3Ul.id = 'result-tab-ul';

	// The LI
	for(var i = 0, j = 3; i < j; i++){

		var resultTabName;

		switch(i){
			case 2:
				resultTabName = agentsText;
				break;
			case 1:
				resultTabName = distributorsText;
				break;
			case 0:
				resultTabName = officesText;
				break;
		}

		var resultTab3UlLi = document.createElement('li');
		resultTab3UlLi.className = 'off';
		resultTab3UlLi.id = 'tab_pos_' + (i+1);

		resultTab3Ul.appendChild(resultTab3UlLi);
	}

	resultTab3.appendChild(resultTab3Ul);
	ajaxTabDiv.appendChild(resultTab3);
}
function _hideTabDiv()
{
	if (document.getElementById('tab-container')){
		document.getElementById('tab-container').style.display = 'none';
		document.getElementById('columnTwoTabs').style.display = 'none';
	}
}
function _showTabDiv()
{
	if (document.getElementById('tab-container')){
		document.getElementById('tab-container').style.display = 'block';
		document.getElementById('columnTwoTabs').style.display = 'block';
	}
}
