//***************************************************************************************
// *   File Name   :  DynamicContent_ajax.js		                            
// *   Description :  This javascript file uses AJAX to load the 
// *				  DynamicContent.aspx in the Homepage-5 template. 
// *				  It controls the visibility of the Dynamic Content 
// *				  depending on the success or failure of the ajax request
//***************************************************************************************
// *  Revision History:
// *  Date Created:   10/26/2007
// *  Author:         Sonny Boy G. Sacedor
// *
// *  Revision Trail:
// *  DATE            BY         SIR/CHANGE REQUEST       CHANGE DESCRIPTION
// *  10/26/2007      SGS        SIR-IDAQASRD             Dynamic Content on
// *                                                      Homepage-5 Template 
// *  12/20/2007	  SGS		 SIR-1080				  Add Tracking to the 
// *													  Content Targeting box 
// *  08/21/2008	  SGS		 SIR-1282				  Omniture Effort - Release 2
// *  10/21/2008	  FBPS		 SIR-1380				  Add Tracking for the 
// *													  Dynamic Content Links for Hitbox
// *  09/02/2009	  RPSD		 RMT 376				  Hitbox Control Code Cleanup for 
// *													  Dynamic Content
//***************************************************************************************

//***************************************************************************************
// The public variable to contain the XML Http Request object 
var xmlHttp;

//********************************************************************************
// FUNCTION CALLED BY THE HOMEPAGE-5 TEMPLATE TO LOAD THE DYNAMIC CONTENT
function showDynamicContent()
{
	// Unhide the Dynamic Content control and 
	// display the 2 html placeholders on initial load
	if (document.getElementById("trDynamicContentCheckBoxPlaceholder") != null)
	{
		document.getElementById("trDynamicContentCheckBoxPlaceholder").style.display="block";
	}

	if (document.getElementById("trCareersLocation") != null)
	{
		document.getElementById("trCareersLocation").style.display="block";
	}

	if (document.getElementById("trCareersDescription") != null)
	{
		document.getElementById("trCareersDescription").style.display="block";
	}

	
	// Instantiate the XML Http Request object
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){return;}
	
	// Defines the url to send to the server
	// a random code is added to make sure that a  
	// cached version is not retrieved
	var url=location.protocol + "//"
	url=url + location.host + "/Careers/ASPX/DynamicContent.aspx?sid="+Math.random();

	// Set stateChanged as the function to process the response from the server
	xmlHttp.onreadystatechange=stateChanged;
	
	// Sends off the request to the server
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);  
}

//********************************************************************************
// FUNCTION THAT WILL PROCESS THE RESPONSE FROM THE SERVER
function stateChanged() 
{
	// Continue process if the request is complete (4 - request is complete)  
	if (xmlHttp.readyState==4)
	{ 
		// Retrieve the page as a string 
		var sourceXML = xmlHttp.responseText;

		// Retrieve the ajaxedDynamicContent div section of the Homepage
		var targetId = document.getElementById("ajaxedDynamicContent_ON");

		// If the div is present, continue
		if (targetId != null)
		{
			// If the request is successful, continue
			if ( sourceXML.search(/DynamicControlCSS_Header/i) > -1 && (xmlHttp.status == 200 || xmlHttp.statusText == "OK") )
			{
				// Unhide the Dynamic Content and hide the two htmlplaceholders 
				if (document.getElementById("trDynamicContentCheckBoxPlaceholder") != null)
				{
					document.getElementById("trDynamicContentCheckBoxPlaceholder").style.display="block";
				}

				if (document.getElementById("trCareersLocation") != null)
				{
					document.getElementById("trCareersLocation").style.display="none";
				}

				if (document.getElementById("trCareersDescription") != null)
				{
					document.getElementById("trCareersDescription").style.display="none";
				}

				// Change table background color to green
				document.getElementById("tblDynamicContent").style.width="165px";
				document.getElementById("tblDynamicContent").style.backgroundColor="#66cc00";
			
				// Display the Dynamic Content 
				targetId.innerHTML = sourceXML;
				
				// Track the Dynamic Content box in Omniture 
				try
				{
					trackDynamicContentBox();
				}
				catch (e)
				{
				} 
				
			}
			
			else
			{
				// Hide the Dynamic Content and display the two htmlplaceholders 
				if (document.getElementById("trDynamicContentCheckBoxPlaceholder") != null)
				{
					document.getElementById("trDynamicContentCheckBoxPlaceholder").style.display="none";
				}

				if (document.getElementById("trCareersLocation") != null)
				{
					document.getElementById("trCareersLocation").style.display="block";
				}

				if (document.getElementById("trCareersDescription") != null)
				{
					document.getElementById("trCareersDescription").style.display="block";
				}
			}
		}

		// If the checkbox is unmarked 
		else if (document.getElementById("ajaxedDynamicContent_OFF") != null)
		{
			// Hide the Dynamic Content and display the two htmlplaceholders 
			if (document.getElementById("trDynamicContentCheckBoxPlaceholder") != null)
			{
				document.getElementById("trDynamicContentCheckBoxPlaceholder").style.display="none";
			}

			if (document.getElementById("trCareersLocation") != null)
			{
				document.getElementById("trCareersLocation").style.display="block";
			}

			if (document.getElementById("trCareersDescription") != null)
			{
				document.getElementById("trCareersDescription").style.display="block";
			}
		}

		// Handles the editing mode
		else
		{
			// Display the Checkbox and the two htmlplaceholders 
			if (document.getElementById("trDynamicContentCheckBoxPlaceholder") != null)
			{
				document.getElementById("trDynamicContentCheckBoxPlaceholder").style.display="block";
			}

			if (document.getElementById("trCareersLocation") != null)
			{
				document.getElementById("trCareersLocation").style.display="block";
			}

			if (document.getElementById("trCareersDescription") != null)
			{
				document.getElementById("trCareersDescription").style.display="block";
			}
		}
	}
}

//********************************************************************************
// FUNCTION THAT INSTANTIATE THE APPROPRIATE XML HTTP OBJECT 
// BASED ON THE CURRENT BROWSER
function GetXmlHttpObject()
{
	var xmlHttpObject = null;
	try
	{
		// For Firefox, Opera 8.0+, Safari
		xmlHttpObject = new XMLHttpRequest();
	}
	catch (e)
	{
		// For Internet Explorer
		try
		{
			xmlHttpObject = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xmlHttpObject = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttpObject;
}

//********************************************************************************
// FUNCTION TO INCREMENT THE HITS TO OMNITURE
// OF THE DYNAMIC CONTENT BOX 
function trackDynamicContentBox() {
	var s=s_gi(getOmnitureAccount());
	var dcBox='Dynamic Content Views';

	// Override tracked variables 
	s.linkTrackVars='prop20';
	s.linkTrackEvents='None';
	
	// Set Custom Insight Variable 20 
	s.prop20=dcBox;
	
	// Send request to populate the Custom Link Report 
	s.tl(this,'o',dcBox);
	
	// Reset variables to avoid double hits 
	s.prop20='';
	s.linkTrackVars='None';
}

//********************************************************************************
// FUNCTION TO INCREMENT THE HITS TO OMNITURE
// OF THE DYNAMIC CONTENT LINKS 
function trackDynamicContentLink(countryCode, obj) {
	var s=s_gi(getOmnitureAccount());
	var dcLink=countryCode + ' || ' + obj.href.toLowerCase();
	var dcLink_hb=countryCode + ' | ' + obj.href.toLowerCase();

	// Override tracked variables
	s.linkTrackVars='prop25';
	s.linkTrackEvents='None';
	
	// Set Custom Insight Variable 25 
	s.prop25=dcLink;
	
	// Send request to populate the Custom Link Report 
	s.tl(this,'o','Dynamic Content Links');
	
	// Reset variables to avoid double hits
	s.prop25='';
	s.linkTrackVars='None';
}


//********************************************************************************
// RETRIEVES THE OMNITURE REPORTING SUITE TO BE USED
// BASED ON THE CURRENT SERVER
function getOmnitureAccount() {
	var accountOmniture='';
	
	// Assign the appropriate Reporting Suite accounts based on the current URL 
	if (window.location.href.toLowerCase().indexOf('careers3.accenture.com') != -1) {
		if (window.location.href.toLowerCase().indexOf('dev') != -1) {
			accountOmniture='acccareersdev,accglobaldev';
		}
		else if (window.location.href.toLowerCase().indexOf('test') != -1) {
			accountOmniture='acccareerstest,accglobaltest';
		}
		else if (window.location.href.toLowerCase().indexOf('stage') != -1) {
			accountOmniture='acccareersstage,accglobalstage';
		}	
		else if (window.location.href.toLowerCase().indexOf('//careers3.accenture.com') != -1) {
			accountOmniture='acccareers,accglobal';
		}
	} 
	// Set to an empty string for external domains 
	else {
		accountOmniture='';
	}
	
	return accountOmniture;
}

