
function Querystring() 
{
	// get the query string, ignore the ? at the front.
	var querystring=location.search.substring(1,location.search.length);

	// parse out name/value pairs separated via &
	var args = querystring.split('&');

	// split out each name = value pair
	for (var i=0;i<args.length;i++)
	{
		var pair = args[i].split('=');

		// Fix broken unescaping
		temp = unescape(pair[0]).split('+');
		temp0 = temp.join(' ');
		
		temp = unescape(pair[1]).split('+');
		temp1 = temp.join(' ');
		
		this[temp0]=temp1;
	}

	this.get=Querystring_get;
}

function Querystring_get(strKey,strDefault)
{
	var value=this[strKey];
	if (value==null){value=strDefault;}
	return value;
}

function getLink()
{
	qs = new Querystring();
	pagelink=qs.get('link');
	
	// if pagelink is undefined, 
	if (pagelink){
			changeClass(pagelink);
	}	
	else {
			//run getPageName to get the page name and set any default values
			setDefaultClass();
	}	
	
	// set the top nav tab based on the current page
	setTopNav();
	
}

function getPageName()
{
	// returns the page name without the extension
	var pathname = location.pathname;
	var lastSlash = pathname.lastIndexOf('/');
	var lastDot = pathname.lastIndexOf('.');
	return pathname.substring(lastSlash+1,lastDot);
}

function getFoldName()
{
	// returns the folder that the file is stored in
	var foldname = location.pathname;
	var finalSlash = foldname.lastIndexOf('/');
	var nextSlash = foldname.lastIndexOf('/',finalSlash-1);
	return foldname.substring(nextSlash+1,finalSlash);
}	
		
function changeClass(id) 
{
	// highlight the active tab
	// note - it's the great-grandparent of the id
	document.getElementById(id).parentNode.parentNode.parentNode.className = "leftNavTier1_item-active";
	
	// highlight the active link
	selectedLink = document.getElementById(id);
	selectedLink.className = "active";
	
	// show the selected copy section
	selectedCopy = document.getElementById(id + "_copy");
	selectedCopy.style.display = "block";
}

function setDefaultClass()
{
	
	// sets default active tab for the current page.
	// do this when there is no 'link' parameter in querystring
	// assumes that the page name and the defaults tab ID are the same.
	
	pageName = getPageName();
	
	if (document.getElementById(pageName)){		
		var defaultTab = document.getElementById(pageName);
			
		// highlight the default tab
		defaultTab.className = "leftNavTier1_item-active";
		
		// highlight the first link within the defaultTab
		defaultTab.getElementsByTagName("li")[0].className = "active"; 
	}
	
	// highlight the default copy div
	if (document.getElementById("copyContainer")) {document.getElementById("copyContainer").getElementsByTagName("div")[0].style.display = "block"; }
}

function setTopNav()
{
	foldName = getFoldName();
	
	topNavTab = 
		foldName == "prodserv" ? "products" : 	// products & services pages
		foldName == "training" ? "training" :	// training pages
		foldName == "resource" ? "resource" :	// resource center
		foldName == "manage" ? "manage" :		// manage users
		"welcome";								// aka: app root, welcome page for unregistered, non-SA users
		
		if (document.getElementById(topNavTab)){ document.getElementById(topNavTab).className = "active";}
		// Need this section because the enrollment tab can have several different tab ids
		if (foldName == "enrollment") 
			{ 
			if (document.getElementById("Topnav_enrollment"))
				{
				document.getElementById("Topnav_enrollment").className = "active";
				}
			if (document.getElementById("Topnav1_enrollment"))
				{
				document.getElementById("Topnav1_enrollment").className = "active";
				}
			if (document.getElementById("Topnav2_enrollment"))
				{
				document.getElementById("Topnav2_enrollment").className = "active";
				}
			}
}	

//                  FAQS 

var openQuestions = new Array; // array to hold open FAQ questions

function toggleFAQ(thisQuestion){
	
	var classValue = "active";
	
	if (openQuestions.length > 0){
		 // there are open FAQs
		for (var i = 0; i < openQuestions.length; i++) 
		{
			if (thisQuestion == openQuestions[i]){
				classValue = "inactive";		// item is open, prepare to close it
				openQuestions.splice(i,1);	// remove this item from the openQuestions array
			}
		}
	}
	
	var defTerms = document.getElementById("faqs").getElementsByTagName("dt");
	var defData = document.getElementById("faqs").getElementsByTagName("dd");
	
	thisQuestion = thisQuestion ? thisQuestion : 0; // first FAQ is open as default
	
	// set the class
	defTerms[thisQuestion].className = classValue;
	defData[thisQuestion].className = classValue;

	if (classValue == "active") {openQuestions.push(thisQuestion);} // add this item to the openQuestions array
}

function changepop(id, newClass) {

	if (newClass=="canseepop"){
	
		// before opening any new layers, let's close any open ones
		
		var popLayers = document.getElementsByTagName("span");
		for (var i = 0; i < popLayers.length; i++) 
		{
			if (popLayers[i].className=="popup" || popLayers[i].className=="poptd"){
				popLayers[i].firstChild.className='cantseepop';
			}
		}
	}	
	
	identity=document.getElementById(id);
	identity.className=newClass;
}

/***************************************************
Toggles availability of Lead Providers dropdown list
on the services page. It should only be available if
the user has chosen the full Leads package.
****************************************************/
function bindToggleLeadProvider(){
  if(document.servicesForm.GMOSRadioBtn != null){
	document.servicesForm.GMOSRadioBtn[0].onclick = toggleLeadProvider;
	document.servicesForm.GMOSRadioBtn[1].onclick = toggleLeadProvider;
  }
}

function toggleLeadProvider(){
  var f = document.servicesForm;
  if(f.GMOSclmpDDL != null && f.GMOSRadioBtn[1].checked == true)
  {  
	f.GMOSclmpDDL.disabled = false;
	f.GMOSaddlSvcRadioBtn_0.disabled = true;
	f.GMOSaddlSvcRadioBtn_1.disabled = true;
	f.GMOSaddlSvcRadioBtn_2.disabled = true;
}
  else{
	if(f.GMOSclmpDDL != null){
	    f.GMOSclmpDDL.disabled = true;
	    f.GMOSaddlSvcRadioBtn_0.disabled = false;
		f.GMOSaddlSvcRadioBtn_1.disabled = false;
		f.GMOSaddlSvcRadioBtn_2.disabled = false;
	}    
  }
}

function toggleGMDWcontent(){
  var f = document.servicesForm;
  var content =document.getElementById("GMDWPackageContent");
  var contact =document.getElementById("GMDWContact");
  var chkMessage = document.getElementById("gmdwChkMessage");
  var unchkMessage = document.getElementById("gmdwUnChkMessage");

  if(f.GMDWchk != null && f.GMDWchk.checked == true)
  {  
	content.className="canseediv";
	contact.className="cantseediv";
	unchkMessage.className="cantseediv";
	chkMessage.className="canseespan";
  }
  else{
	if(f.GMDWchk != null)
	{
		content.className="cantseediv";
		contact.className="canseediv";
		chkMessage.className="cantseediv";
		unchkMessage.className="canseespan";
	}
  }

}

/*

*/
function checkEditMode(){
	if(editModeFlag == 1)
	{
		alert("Before you can edit the dealership services, please save or cancel your\n edits for the above dealership by clicking on the 'Save' or 'Cancel' buttons");
		return false;
	}
	return true;
}


/* pop-up function. only needs a url and winName parameter -- attributes are defaulted. 
it is recommended that call to function at least include width and height attributes.
any attribute passed in will take precedence over default. -- dtw 8/5/05 */
function popUpWindow(url,winName,attributes) {
	if (attributes.indexOf("directories") == -1) { attributes += ",directories=0"; }
	if (attributes.indexOf("location") == -1) {	attributes += ",location=0"; }
	if (attributes.indexOf("menubar") == -1) { attributes += ",menubar=0"; }
	if (attributes.indexOf("resizable") == -1) { attributes += ",resizable=0"; }
	if (attributes.indexOf("scrollbars") == -1) { attributes += ",scrollbars=1"; }
	if (attributes.indexOf("status") == -1) { attributes += ",status=0"; }
	if (attributes.indexOf("toolbar") == -1) { attributes += ",toolbar=0"; }
	if (attributes.indexOf("top") == -1) { attributes += ",top=80";	}
	if (attributes.indexOf("left") == -1) { attributes += ",left=50"; }
	
	var newWindow = window.open(url,winName,attributes);
	if (window.focus) { newWindow.focus() }
}

/* Custom Link Tracking */

function clickTrack(oArg) {		
	s_linkType="o";
	if (typeof(oArg.s_pageName) == "string") {	s_pageName = oArg.s_pageName;}
	if (typeof(oArg.s_linkName) == "string") {	s_linkName = oArg.s_linkName;}
	if (typeof(oArg.s_prop1) == "string") {	s_prop1 = oArg.s_prop1;}
	if (typeof(oArg.s_prop2) == "string") {	s_prop2 = oArg.s_prop2;}
	ns=s_account;	
	s_lnk=true;
	void(s_gs(ns));			
}
