
var applicationCode = "";

if (document.addEventListener)
{
	document.addEventListener('click',hideDD,false); 
} 
else if (document.attachEvent)
{
	document.attachEvent('onclick', hideDD);
}

function hideDD() 
{
    var dealerGroupDiv = document.getElementById("dp3_groupDealerRest");
    var dealerGroupDivL = document.getElementById("dp3_groupDealerLeft");
    
	if (document.getElementById("dp3_groupDealerRest"))
    {
        if (dealerGroupDiv.style.display == 'inline')
        {
            dealerGroupDiv.style.display = 'none';
            clearTimeout(timer);
        }
    }
    
    if (document.getElementById("dp3_groupDealerLeft"))
    {
        if (dealerGroupDivL.style.display == 'inline')
        {
            dealerGroupDivL.style.display = 'none';
            clearTimeout(timer);
        }
    }
}

function showGroupDealerships(){
	    
    clearTimeout(timer);
    
    var dealerGroupDiv = document.getElementById("dp3_groupDealerRest");
	if (dealerGroupDiv.style.display != 'inline')
    {
		dealerGroupDiv.style.display = 'inline';
	}
    
    var dealerGroupDivL = document.getElementById("dp3_groupDealerLeft");
	if (dealerGroupDivL.style.display != 'inline')
    {
		dealerGroupDivL.style.display = 'inline';
	}
    
}

function hideGroupDealerships()
{	    
    document.getElementById("dp3_groupDealerRest").style.display = 'none';
    document.getElementById("dp3_groupDealerLeft").style.display = 'none';
    clearTimeout(timer);
}

var timer;
function timehide(onoff)
	{
		if(onoff == 1)
		{
			timer = setTimeout("hideGroupDealerships()",1500);
		}
		else
		{
			clearTimeout(timer);
		}
	}

function dp3_insertDynamicJavaScript(url, headElementName)
{
	if(url.indexOf("?") == -1)
	{
		url = url + "?";
	}
	else
	{
		url = url + "&";
	}
	url = url + "cachebuster=" + parseInt(Math.random() * 99999999);
	var headElement = document.getElementsByTagName("head").item(0);
	var headerScript = document.createElement("script");
	headerScript.setAttribute("id", headElementName);
	headerScript.setAttribute("type", "text/javascript");
	headerScript.setAttribute("src", url);
	headElement.appendChild(headerScript);
}



function dp3_logOutValidation(logOutUrl)
{
	var answer = confirm ("Are you sure you want to log out of the Dealer Portal?")
	if(answer)
	{
		window.location.href = logOutUrl;
		window.event.returnValue = false;
	}
}
/*
	This function will open the FAQ window.
*/

function dp3_openHelpPopupWindow(url) 
{
	window.open(url, "help", "width=788px,height=610px,scrollbars=yes,menubar=no,status=no,toolbar=no,resizable=no");
}

function dp3_openNewWindow(urlLink)
{
	formGo = document.createElement("form");
    formGo.method = "GET";
    formGo.action = urlLink;
    formGo.target = "_blank";
    document.body.appendChild(formGo);
    formGo.submit();
}

function dp3_qpl_callOmnitureFunction(applicationName)
{
	applicationName = applicationName.split(' ').join(''); // removed spaces

	var s_qpl=s_gi(s_account);
	s_qpl.linkTrackVars='eVar32';
	s_qpl.eVar32='QuickBarNav:' + applicationName;
	s_qpl.tl(this,'o','QuickBarNav:' + applicationName);
}

/*
	This function calls the required HBX function. It first checks that if
	function is available i.e. the standard hbx js file has already been referenced  
	in the page or not. If not then it includes the JS file and calls the required function.
*/
function dp3_qpl_callHBXFunction(applicationName)
{	
	applicationName = applicationName.split(' ').join(''); // removed spaces
		
	if(typeof _hbLink == 'function')
	{		
		_hbLink(applicationName,"/portal/QPL");		
	}
	
	if (typeof s_doPlugins == 'function'){
		dp3_qpl_callOmnitureFunction(applicationName);
	}
}

/*
	This fucntion will be called when user will select a link from dropdwn list.It'll take the user to 
	the selected link.
*/
function dp3_goToUrl() 
{
	var appLink = document.getElementById("QPL").value;
	var applicationName = document.getElementById('QPL').options[document.getElementById('QPL').selectedIndex].text;
	var newWindowFlag = document.getElementById('QPL').options[document.getElementById('QPL').selectedIndex].id;
	switch(appLink)
	{
		case "":
			alert("There are some problems with the system. Please refresh the page.");
			break;
		case "selectApp":
			alert("Please select an application then click Go.");
			break;
		default:
			dp3_qpl_callHBXFunction(applicationName);
			 if(newWindowFlag == "1")
			{
				dp3_openNewWindow(appLink);
			}
			else
			{
				window.location = appLink;		
			}
			
	}
}

/*
	This function is reponsible for displaying/not displaying quick product links dropdown in 
	Quick product links toolbar.
	Two options are acceptable:
		1 means Dont show Dropdown
		2 means Show Dropdown
*/


function dp3_switchQPLDisplay(optionNo)
{
	switch (optionNo)
	{
	case 0:	
	  document.getElementById("dp3_back_v2").style.display = "block";
	  document.getElementById("dp3_help_v2").style.display = "none";
	  document.getElementById("dp3_go_v2").style.display = "none";
	  document.getElementById("dp3_dropdown_v2").style.display = "none";
	  break;
	case 1:
	  document.getElementById("dp3_back_v2").style.display = "none";
	  document.getElementById("dp3_help_v2").style.display = "block";
	  document.getElementById("dp3_go_v2").style.display = "block";
	  document.getElementById("dp3_dropdown_v2").style.display = "block";
	  break;
	default:
	  document.write("Shouldn't see this!");
	}
}

/*
	This function will insert a java script file dynamically in the header of the current 
	document.
*/
function insertDP3QPLInserter(appCode) 
{
	applicationCode = appCode;
	var url = "http://dealerportal.autotrader.co.uk/qpl/insertQPL.js?requesterAppCode=" + appCode;  
	dp3_insertDynamicJavaScript(url, "DP3QPLDynamics");
	
	if (typeof s_doPlugins == 'function'){
		var sitecatUrl = "http://dealerportal.autotrader.co.uk/script/s_code.js";  
		dp3_insertDynamicJavaScript(sitecatUrl, "head");
	}
}

var tabName = "";
/*
	This function will insert a java script file dynamically in the header of the current 
	document.
*/
function insertDP3HeaderInserter(appCode, activeTabName) 
{
	applicationCode = appCode;
	tabName			= activeTabName;
	var url = "http://dealerportal.autotrader.co.uk/insertHeader.js?activeTabName=" + activeTabName;
	url  +=   "&requesterAppCode=" + appCode;
	dp3_insertDynamicJavaScript(url, "DP3HeaderDynamic");
}
/*
	This function will insert a java script file dynamically in the header of the current 
	document.
*/
function insertDP3FooterInserter(appCode) 
{
	applicationCode = appCode;
	url = "http://dealerportal.autotrader.co.uk/insertFooter.js?";
	url  +=   "requesterAppCode=" + appCode;
	dp3_insertDynamicJavaScript(url, "DP3FooterDynamic");
}

/*
	This function will replace the inner html of QPL div with the html data 
	passed to this function.
*/
function dp3_insertQPL(data) 
{
	if (document.getElementById("dp3_qpl") == null) return;
	
	document.getElementById("dp3_qpl").innerHTML = data;
	
		
		
			if(document.getElementById("QPL2") != null)
			{
				dp3_switchQPLDisplay(0);
			}
		
	
}

/*
	This function will replace the inner html of the div with the html data 
	passed to this function.
*/
function dp3_insertHeader(data) 
{
	document.getElementById("dp3_header").innerHTML = data;
	insertDP3QPLInserter(applicationCode);
}

/*
	This function will replace the inner html of the div with the html data 
	passed to this function.
*/	
	
function dp3_insertFooter(data) 
{
	document.getElementById("dp3_footer").innerHTML = data;
}
