// Should have urchinTracker enabled
// Should not have an .htm references or http://195.11....

var region = ''; 
var product = '';
var industry = '';

var regionBio;
var industryBio;
var productBio;

var saveSettings = false;

var DEFAULT_PAGE = 'Default.aspx';

var pageLayoutType = 'Master';  // Should be overwritten by each Page Layout

var _uacct = "UA-886546-1";

function pageInit(){
    urchinTracker();
		
	expireLegacyCookies(); 
	getCurrentCookies();
	if (!isValidQueryString()){
		return false;
	}
	getCurrentCookies();	
	document.getElementById("saveMySelections").checked = saveSettings;

	// Displays ServiceRollup Image Gradient and Header
	contentPageInit();
		
	doProductRender(product);
//	doRegionRender(region);
//	doIndustryRender(industry);
		
	displayContactInformation();
	setMenu();
};

function getCurrentCookies(){
	product = getCookie('product');
	region = getCookie('region');
	industry = getCookie('industry');
	
	saveSettings = (getCookie('saveToolSettings') != '')? getCookie('saveToolSettings'): false;
}

function isValidQueryString(){
	// Get QueryString Values
	qProduct = getQueryString('p');
	qRegion = getQueryString('r');
	qIndustry = getQueryString('i');
	
	var QS = new QueryString();
	var isValid = true;
	var querystring = '';

	// Empty QueryString and Empty Global Variables
	if (location.search.length == 0 && !valuesInGlobalVariables()){
		return true;  // do nothing
	}

	// Empty QueryString and Global Variables Values Exist
	if (location.search.length == 0 && valuesInGlobalVariables()){
		//Populate querystring from global variables
		if (isDefaultPageType() || isAboutUsPageType()){
			product = '';
			setCookie('product', '');
			location.href = location.pathname + generateQueryString('', region, industry);
		}
		else
			location.href = location.pathname + generateQueryString(product, region, industry);
			
		return false;
	}

	// Something in QueryString and Empty Global Variables
	if (location.search.length > 0 && !valuesInGlobalVariables()){
		if (isDefaultPageType() || isAboutUsPageType()){
			if (qProduct == ''){
				mergeQueryStringAndSetCookies('', qRegion, qIndustry);
				return true;
			}
			else{
				product = '';
				querystring = mergeQueryStringAndSetCookies(qProduct, qRegion, qIndustry);
				location.href = location.pathname + querystring;
				return false;						
			}
		}
		else{
			querystring = mergeQueryStringAndSetCookies(qProduct, qRegion, qIndustry);
			return true;
		}
	}
		
	// Something in QueryString and Global Variables Exist
	if (location.search.length > 0 && valuesInGlobalVariables()){
		if (isDefaultPageType() || isAboutUsPageType()){
			if (qProduct == ''){	
				if (qRegion == region && qIndustry == industry){
					return true;
				}
				else{
					//merge the differences
					location = location.pathname + mergeQueryStringAndSetCookies('',qRegion, qIndustry);			
					return false;				
				}
			}
			else {
				product = '';
				location = location.pathname + mergeQueryStringAndSetCookies('',qRegion, qIndustry);			
				return false;
			}
		}
		else
		{
			if (qProduct == product && qRegion == region && qIndustry == industry){
				return true;
			}
			else {
				//merge the differences
				location = location.pathname + mergeQueryStringAndSetCookies(qProduct,qRegion, qIndustry);			
				return false;
			}			
		}
	}
}

function mergeQueryStringAndSetCookies(qProduct, qRegion, qIndustry){
	var QS = new QueryString();
	QS.SetValue('p', (qProduct != '')? qProduct : (product == '')? null : product );
	setCookie('product', (qProduct != '')? qProduct : product );
	QS.SetValue('r', (qRegion != '')? qRegion : (region == '')? null : region );
	setCookie('region', (qRegion != '')? qRegion : region );
	QS.SetValue('i', (qIndustry != '')? qIndustry : (industry == '')? null : industry );
	setCookie('industry', (qIndustry != '')? qIndustry : industry );			
	return QS.ToString();
 }


function valuesInGlobalVariables(){
	if (product != '' || region != '' || industry != '')
		return true;
	else
		return false;
}

function generateQueryString(p, r, i){
	var QS = new QueryString();
	QS.SetValue('p', (p == '')? null: p);
	QS.SetValue('r', (r == '')? null: r );
	QS.SetValue('i', (i == '')? null: i);
	return QS.ToString();
}

function saveToolSettings(value){
	if(value == true){
		setCookie('saveToolSettings', value);
		setCookie('region', region);
		setCookie('product', product);
		setCookie('industry', industry);
	}
	else{
		deleteCookie('region');
		deleteCookie('product');
		deleteCookie('industry');
		deleteCookie('saveToolSettings');
	}
}

function mapOver(iRegion){
	if (iRegion != ''){
		oRegion = getObjectFromArray(gaRegions, iRegion);
		changeImage('toolRegionMap', oRegion.Image);	
	}
	else
		changeImage('toolRegionMap', '../PublishingImages/Tool/map.gif')
}

function handleRegionClick(iRegion){
	region = iRegion;
	setCookie('region', iRegion);
	
	var QS = new QueryString();	
	QS.Read();	
	if (iRegion != '')
		QS.SetValue('r', region);
	else
		QS.SetValue('r', null);
		
	location = location.pathname + QS.ToString();
	return false;	
}

function doRegionRender(iRegion){
	if(iRegion== ''){
		hideLayer('toolRegionBio');
		showLayer('toolRegionSelect');
	}
	else{
		hideLayer('toolRegionSelect');
		showLayer('toolRegionBio');
		
		oRegion = getObjectFromArray(gaRegions, iRegion);
		regionBio = oRegion.Bio;
		
		changeImage('toolRegionBioPhoto', oRegion.Bio.Image);
		document.getElementById('toolRegionBioH1').innerHTML = oRegion.Title;
		document.getElementById("toolRegionBioH2").innerHTML = oRegion.Bio.Name;
		document.getElementById("toolRegionBioH3").innerHTML = oRegion.Bio.Title;
	}	
}

function handleIndustryClick(iIndustry){
	industry = iIndustry;
	setCookie('industry', industry);

	var QS = new QueryString();
	QS.Read();
	if (iIndustry != '')
		QS.SetValue('i', industry);
	else
		QS.SetValue('i', null);

	location = location.pathname + QS.ToString();
	return false;		
}

function doIndustryRender(iIndustry){

	if (oProduct == null || oProduct == undefined){
		showLayer('toolIndustrySelect');
	}	
	else {	    
		document.getElementById("toolIndustryText").innerHTML = oProduct.IndustryLayerText;  //Could be toolIndustrySelect or toolIndustryNoUI
		showLayer(oProduct.IndustryLayer);
	}		
	
	if (iIndustry == ''){
		setCookie('industry', '');
		hideLayer('toolIndustryBio');
	}
	else{
		hideLayer('toolIndustrySelect');
		showLayer('toolIndustryBio');
		
		try {
			oIndustry = getObjectFromArray(gaIndustries, iIndustry);
		
			industryBio = oIndustry.Bio;
		
			changeImage('toolIndustryBioPhoto', oIndustry.Bio.Image);
			document.getElementById('toolIndustryBioH1').innerHTML = oIndustry.Title;
			document.getElementById("toolIndustryBioH2").innerHTML = oIndustry.Bio.Name;
			document.getElementById("toolIndustryBioH3").innerHTML = oIndustry.Bio.Title;			
		}
		catch (err) {}
	}
}

function handleProductChange(iProduct){
	product = iProduct;
	setCookie('product', product);
}

function doProductRender(iProduct){
	if(iProduct== ''){
		hideLayer('toolProductBio');
		showLayer('toolProductIntroduction');
	}
	else{
		hideLayer('toolProductIntroduction');
		showLayer('toolProductBio');
				
		try {	
			oProduct = getObjectFromArray(gaProducts, iProduct);	
			
			productBio = oProduct.Bio;
			changeImage('toolProductBioPhoto', oProduct.Bio.Image);
			document.getElementById('toolProductBioH1').innerHTML = oProduct.Bio.Name;
			document.getElementById("toolProductBioH2").innerHTML = oProduct.Bio.Title;
			document.getElementById("toolProductBioP").innerHTML = oProduct.Bio.Tagline;	
		}
		catch (err) {}
	}
}

function setMenu(){

	sLocation = '../PublishingImages/Navigation/';
	aProductImages = ['DGSAboutUs', 'DGSProducts', 'DGSContractVehicles', 'DGSSupplierDiversity'];

	// Restore the menu.
	changeImage('nav0', sLocation + 'DGSAboutUs.gif');
	changeImage('slashLeft0', sLocation + 'slashLeft.gif');
	changeImage('slashRight1', sLocation + 'slashRight.gif');

	changeImage('nav1', sLocation + 'DGSProducts.gif');
	changeImage('slashLeft1', sLocation + 'slashLeft.gif');
	changeImage('slashRight2', sLocation + 'slashRight.gif');
	
	changeImage('nav2', sLocation + 'DGSContractVehicles.gif');
	changeImage('slashLeft2', sLocation + 'slashLeft.gif');
	changeImage('slashRight3', sLocation + 'slashRight.gif');

	changeImage('nav3', sLocation + 'DGSSupplierDiversity.gif');
	//changeImage('slashLeft3', sLocation + 'slashLeft.gif');
	//changeImage('slashRight4', sLocation + 'slashRight.gif');
		
	//changeImage('nav4', sLocation + 'fairsEvents.gif');	

	if(product != ''){
		var index = getObjectIndexFromArray(gaProducts, product);	
		// Left Slash
		if(product != 'DGSAboutUs'){
			changeImage('slashLeft' + (index - 1), sLocation + aProductImages[index] + '_blank.gif');
			changeImage('slashRight' + (index), sLocation + aProductImages[index] + '_blank.gif');
		}
		
		// Belly
		changeImage('nav' + index, sLocation + aProductImages[index] + '_on.gif');
		
		// Right Slash
		if(product != 'DGSSupplierDiversity'){
			changeImage('slashLeft' + (index), sLocation + aProductImages[index] + '_blank.gif');
			changeImage('slashRight' + (index - 0 + 1), sLocation + aProductImages[index] + '_blank.gif');
		}
	}
}

function showBio(oBio){
	document.getElementById("bioH1").innerHTML = oBio.Name;
	document.getElementById("bioH2").innerHTML = oBio.Title;
	document.getElementById("bioP").innerHTML = oBio.Biography + "... <a href='javascript:gotoContentPage(&quot;" + oBio.Url + "&quot;);'>Read Complete Bio</a>";
	document.getElementById("bioContainer").style.display = "block";
}

function closeBio() {
	document.getElementById("bioContainer").style.display = "none";
}

function displayContactInformation(){

	if (product > 1){
		industry = ''
	}

	if (product.length + region.length + industry.length == 0)
		sObjectToUse = 'oDefaultContact';
	else{
		try {
			var oProd = getObjectFromArray(gaProducts, product);
			var oReg = getObjectFromArray(gaRegions, region);
			var oInd =  getObjectFromArray(gaIndustries, industry);
			sProduct = (product != '') ? oProd.Id : product;
			sRegion = (region != '') ? oReg.Id : region;
			sIndustry = (industry != '') ? oInd.Id : industry;
			sObjectToUse = 'o' + sProduct + sRegion + sIndustry;
		}
		catch (err) {}
	}
	
	try {
		oContact = eval(sObjectToUse);
	}
	catch(err){
		// No matching object found
		return false;
	}
	// Currently next block is implemented by hard coding contact info in page

	//document.getElementById("contactPhone").innerHTML = oContact.Phone;
	//document.getElementById("contactFax").innerHTML = oContact.Fax;
	//document.getElementById("contactEmail").innerHTML = "<a href=\"mailto:" + oContact.Email + "\">" + oContact.Email + "</a>";
	
	//document.getElementById("contactPhoneContainer").style.display = (oContact.Phone == '') ? "none" : "block";
	//document.getElementById("contactFaxContainer").style.display = (oContact.Fax == '') ? "none" : "block";
	//document.getElementById("contactEmailContainer").style.display = (oContact.Email == '') ? "none" : "block";

}

function gotoServiceRollup(sProduct){
	handleProductChange(sProduct);
	
	var QS = new QueryString();
	QS.Read();
	QS.SetValue('v', null);
	QS.SetValue('p', sProduct);
	
	oProduct = getObjectFromArray(gaProducts, sProduct);
	if (oProduct.IndustryLayer == "toolIndustryNoUI"){
		industry = '';
		setCookie('industry', '');
		QS.SetValue('i', null);
	}
	
	location.href = 'Agility_ServiceRollup.aspx' + QS.ToString();
}

function gotoContentPage(sPage){
	var QS = new QueryString();
	QS.Read();
	if (isDefaultPage(sPage)){
		QS.SetValue('p', null);
		setCookie('product', '');
	}
		
	location.href = sPage + QS.ToString();
}

function isDefaultPage(sPage){
	if (sPage.toUpperCase().indexOf(DEFAULT_PAGE.toUpperCase()) != -1)
		return true;
	else
		return false;
}

function isDefaultPageType(){
	return isPageType("Default");
}

function isServiceRollupPageType(){
	return isPageType("ServiceRollup");
}

function isServiceDetailPageType(){
	return isPageType("ServiceDetail");
}

function isAboutUsPageType(){
	return isPageType("AboutUs");
}

function isPageType(pageType){
	return (pageLayoutType == pageType) ? true : false;
}

function searchAgility(txtControl){
	window.location = "Agility_SearchResults.aspx?s=" + encodeURI(document.getElementById(txtControl).value);
}

function handleTracking(txtControl){
	window.open("Agility_Tracking.html", "Agility");
}

function handleCustomerLogin(txtControl_1, txtControl_2){
    window.open("Agility_Customer_Login.html.aspx", "Agility");
}

function handleLanguage(){
	if(document.getElementById("languageSelect").selectedIndex == 0)
		location.href = "/EN/GIL/Pages/Default.aspx";
		
	else
		location.href = "/FR/GIL/Pages/Default.aspx";;
}

function showLogin(){
	document.getElementById("logIn").style.display = "inline";
}

function enableLogin(username,password,chk)
{
	var chkconditions = document.getElementById(chk).checked;
	var txtusername = document.getElementById(username).value;
	var txtpassword = document.getElementById(password).value;
	var btnLogin = document.getElementById('LoginUC1_1_ImgBtnLogin');

	if(txtusername.length > 0 && txtpassword.length > 0 && chkconditions)
	{
		btnLogin.style.cursor="hand";
		btnLogin.src = "../PublishingImages/Login/logIn.gif";
		btnLogin.disabled = false;
	}
	else
	{
		btnLogin.style.cursor="default";
		btnLogin.src = "../PublishingImages/Login/logIn_disable.gif";
		btnLogin.disabled = true;
	}
}

function menuOn(sMenuItem, sImage){

	setMenu();

	sLocation = '../PublishingImages/Navigation/';

	var index = getObjectIndexFromArray(gaProducts, product);
	
	iProduct = '';
	if(product != ''){
		iProduct = index;
	}
	
	// Get the nav position.
	iIndex = sMenuItem.substring(3) - 0;
	iPrevious = iIndex - 1;
	iNext = iIndex + 1;
		
	// Handle the left slash.
	if (iIndex != 0 && iIndex != iProduct + 1 && iIndex != iProduct ){
		changeImage('slashLeft' + iPrevious, sLocation + sImage + '_blank.gif');
		changeImage('slashRight' + iIndex, sLocation + sImage + '_blank.gif');
	}

	// Handle the right slash.
	if (iIndex != 3 && iIndex != iProduct -1 && iIndex != iProduct ){
		changeImage('slashLeft' + iIndex, sLocation + sImage + '_blank.gif');
		changeImage('slashRight' + iNext, sLocation + sImage + '_blank.gif');
	}
	
	if(iIndex != iProduct){
		changeImage(sMenuItem, sLocation + sImage + '_on.gif');
	}
}


