/* COPYRIGHT INFORMATION All pages within a SITE are the property of DTE Energy or its Affiliate, unless other ownership is 
indicated. All information and content including any software programs available on or through a SITE is protected by copyright. A 
user is permitted to download information from a SITE which, by its nature or context, is to be copied in order to use SITE-related 
services. A user is permitted to download information from a SITE for private personal use. A user is prohibited from modifying, copying, 
distributing, transmitting, displaying, publishing, selling, licensing, creating derivative works or using any Content available on or 
through a SITE for any commercial or public purpose unless the user has the prior written permission of DTE Energy or its Affiliate. 
*/

/* Global variables */
//var refSearch = "http://idevsearch.dteenergy.com";
//var secureBaseURL;
var unsecureBaseURL;
var args = getArgs();
//var wasLastFieldValid = true;
//var fieldToValidate = "";



  // The following 6 steps are  used to create the bread crumbs at the top of relevant pages within the Energy Services Site
	// 1.  Find which page the user is on and identify the corresponding unique ID in menu.js 
		winLocation = window.document.URL.toString();
		//alert(winLocation);

	// 2.  Re-write above variable to string so we can compare to items in menu.js (not sure why we must do this, but it only works this way)
		var windowLocation = "";
		for(var f=0; f < winLocation.length; f++)
		{
			windowLocation = windowLocation+winLocation.charAt(f);
		}
		
		// 3.  If the document path does not include a document name assume it is index.html
		var winLength=winLocation.length-1;
		if(windowLocation.charAt(winLength)=="/")
		{
			windowLocation = windowLocation+"index.html";
		}
		
	// define global var that is the html filename of current page
	var currentPageHTMLFileName = "";
	
	if (windowLocation.indexOf('?') > 0)
	{
		currentPageHTMLFileName = windowLocation.substring(windowLocation.lastIndexOf('/') + 1, windowLocation.indexOf('?'));
	}
	else
	{
		currentPageHTMLFileName = windowLocation.substring(windowLocation.lastIndexOf('/') + 1, windowLocation.length);
	}
	
/* 
   Some AJAX functions that will allow us to store the reference to the complimentary secured URL prefix 
   in a single place - namely a .html file - that can be acessed both by the browser at runtime via a simple
   AJAX call or a server side include in the case where value of the variable is needed from within an html or 
   server side evaluation.
*/

function getSecuredUnsecuredURLPrefix(securedUnsecured)
{
	var xmlHttpReq = false;
	
	// ref to html file on server that contains ref to base URL of secured portion of site
	var strURL;
	strURL = "/includes/refDTEEnergyVenturesUnsecured.html";
	
	
	
	// find the base url of this page
	var urlIndex = windowLocation.indexOf("//");
	var urlPart1 = windowLocation.substring(0, urlIndex + 2);
	var urlPartTemp = windowLocation.substring(urlIndex + 2, windowLocation.length);
	var urlPart2 = urlPartTemp.substring(0, urlPartTemp.indexOf("/"));
	var urlBase = urlPart1 + urlPart2;
	//alert("urlpart1-"+urlPart1+"--"+urlPart2);
	var responseStr;
	
    // Mozilla/Safari
    if (window.XMLHttpRequest)
	{
        xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject)
	{
        xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlHttpReq.open('POST', strURL, true);
    xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlHttpReq.onreadystatechange = function()
	{
        if (xmlHttpReq.readyState == 4)
		{
			unsecureBaseURL = xmlHttpReq.responseText;
        }
    }
	
    xmlHttpReq.send(urlBase + strURL);
}


function drawBorder(borderDiv) 
{
	var subMenuDiv = document.getElementById("technologyInitiatives");
	//alert(subMenuDiv.style.display);
	if (currentPageHTMLFileName == 'technologyInitiatives.html')
	{
		if (subMenuDiv.style.display == '' || subMenuDiv.style.display == 'none')
		{
			factor=81;
		}
		else
		{
			factor=-81;
		}
	}
	else
	{
		factor=20;
	}
	//alert("factor "+factor);
		var cn = (document.getElementById("contextNavigation").offsetHeight);
		//alert("cn "+cn);
		//var sn = (document.getElementById("siteNavigation").offsetHeight);
		var ca = (document.getElementById("contentArea").offsetHeight);
			//alert("ca "+cn);
		if (cn > ca) {
			document.getElementById("fixLeftBorder").style.height = cn+factor+"px";
		} 
		else {	
			document.getElementById("fixLeftBorder").style.height = ca+30+"px";
		} 
	//	alert(document.getElementById("fixLeftBorder").style.height);
}
// Function for toggling the display of a submenu, given the div id of that contains the submenu items.
function toggleSubMenu(subMenuDivId)
{
	var subMenuDiv = document.getElementById(subMenuDivId);
	var arrowImage = getSubSectionArrowImageRef(subMenuDivId);
	
	if (subMenuDiv.style.display == '' || subMenuDiv.style.display == 'none')
	{
		arrowImage.setAttribute('src','/images/arrowDown.gif');
		subMenuDiv.style.display = "block";
	}
	else
	{
		arrowImage.setAttribute('src','/images/arrow.gif');
		subMenuDiv.style.display = "";
	}
      
}

// helper function to retrieve a reference to the correct arrow image
function getSubSectionArrowImageRef(subMenuDivId)
{
	var arrowImages = getElementsByClassName(document, 
						"img", "sideNavArrow");

	for (k=0; k<arrowImages.length; k++)
	{
		if (arrowImages[k].parentNode.id.toUpperCase() == (subMenuDivId + "HeaderLink").toUpperCase())
		{
			//alert(arrowImages[k]);
			return arrowImages[k];
		}
	}
}

// This function will act as a main "router" to facilitate side navigation and coordination of 
// subsection body content stylings with this side navigation.  This function will be called from 
// the body tag's onload attribute for every page of the site and should not grow too large.  It 
// should only apply css classes to specific divs NOT specific stylings to specific divs
// for performance and maintainability reasons.  In doing this, the detailed stylings are still 
// maintained in the css file, whereas this function simply maps the subsection stylings to the 
// appropriate div tags
function venturesMain() 
{
 
	getSecuredUnsecuredURLPrefix('unsecured');
//alert("1");
	maintainStateOfLeftNav();
//alert("2");
	setWindowTitleAndPageTitleToLeftNavClickedLinkText(currentPageHTMLFileName);
	
	if (currentPageHTMLFileName == 'index.html')
	{
		document.getElementById("homeHeaderLink").style.display = "none";
	}
	/*if (currentPageHTMLFileName != 'technologyInitiatives.html')
	{
		document.getElementById("technologyInitiatives").style.display = "block";
}
	switch(currentPageHTMLFileName)
	{
		case 'technologyInitiatives.html':
		{
			toggleSubMenu('technologyInitiatives');
			break;
		}
	}*/
}

// intercepts left nav link targets and redirects to correct page passing parameters to maintain state 
// of expanded left nav subsections
function leftNavController(target)
{
	var urlIndex = windowLocation.indexOf("//");
	var urlPart1 = windowLocation.substring(0, urlIndex + 2);
	var urlPartTemp = windowLocation.substring(urlIndex + 2, windowLocation.length);
	var urlPartTemp1 = urlPartTemp.substring(urlPartTemp.indexOf("/"), windowLocation.length);
	var urlPart4 = urlPartTemp1.substring(0,urlPartTemp1.indexOf("?"));

	var newTarget;
//	alert("asdf");
	if (target.indexOf("technologyInitiatives.html") > 0 )
	{
		
	//	alert("old "+currentPageHTMLFileName);
		//target = urlPart4;
	//	toggleSubMenu('technologyInitiatives');
	}
	
	if(target.indexOf("?") > 0)
	{
		newTarget = target  + "&technologyInitiativesDisplay=" + document.getElementById('technologyInitiatives').style.display;
	}
	else
	{
		newTarget = target  + "?technologyInitiativesDisplay=" + document.getElementById('technologyInitiatives').style.display;
	}

  newTarget = unsecureBaseURL + newTarget;
	//alert("new "+ newTarget);
	window.location = newTarget;
  
}

// sets the window title and the pageTitle equal to the text of the left nav link, if one is found
// that has the current page as a target, otherwise sets the pageTitle and window title to the value
// entered via the document generic template otherwise leaves it blank.
function setWindowTitleAndPageTitleToLeftNavClickedLinkText(currentPageHTMLFileName)
{
	var leftNavText = getSubsectionTextByCurrentPage();
	var contentAreaBodyDiv = document.getElementById('contentAreaBody');
	var pageTitle;

	if (currentPageHTMLFileName.indexOf('techPark') >= 0)
	{
		document.title = "DTE Energy Hydrogen Technology Park";
		pageTitle = document.title;
	}
	else if (currentPageHTMLFileName.indexOf('solarDemo') >= 0)
	{
		document.title = "Solar Technology Demonstration";
		pageTitle = document.title;
	}
	else if (currentPageHTMLFileName.indexOf('technologyInitiatives') >= 0)
	{
		document.title = "Technology Initiatives";
		pageTitle = document.title;
	}
	else if (currentPageHTMLFileName.indexOf('contactUs') >= 0)
	{
		document.title = "Contact Us";
		pageTitle = document.title;
	}
	else if (currentPageHTMLFileName.indexOf('ourTeam') >= 0)
	{
		document.title = "Our Team";
		pageTitle = document.title;
	}
	else if (currentPageHTMLFileName.indexOf('index.html') >= 0)
	{
		// handle home page left nav as special case b/c we don't want to sync page title and window title in this case
		document.title = "";
		pageTitle = document.title;
	}
	else if (leftNavText != null && leftNavText.length > 0)
	{
		document.title = "DTE Energy Ventures - " + leftNavText;
		pageTitle = leftNavText;
	}
	
	insertPageTitle(pageTitle);
}

// This function returns the text of the left nav LOB subsection link determined by the current page
function getSubsectionTextByCurrentPage()
{
	var sideNavAnchorElems = getElementsByClassName(document.getElementById("sideNavigation"), "a", "");
	var sideNavAnchorTarget;
	var targetIndex;

	for (i=0; i<sideNavAnchorElems.length; i++)
	{
		var sideNavInnerHTML = sideNavAnchorElems[i].innerHTML;
		var sideNavText = sideNavInnerHTML.substring(sideNavInnerHTML.indexOf('>') + 1, sideNavInnerHTML.length)
		
		sideNavAnchorTarget = sideNavAnchorElems[i].href;
		targetIndex = sideNavAnchorTarget.indexOf(currentPageHTMLFileName);
		if (targetIndex >= 0)
		{
			return sideNavText
		}
	}
}
// inserts the page title into the body of the page
function insertPageTitle (pageTitle)
{
	var contentAreaBodyDiv = document.getElementById('contentAreaBody');
	var pageTitleDiv = document.createElement("div");
	
	pageTitleDiv.setAttribute("id", "pageTitle");
	pageTitleDiv.innerHTML = pageTitle;
	
	if (contentAreaBodyDiv != null && pageTitle.length > 0)
	{
		contentAreaBodyDiv.insertBefore(pageTitleDiv, contentAreaBodyDiv.childNodes[0]);
	}
}

function getArgs(){
	var args = new Object();
	var query = location.search.substring(1);
	var pairs = query.split("&");
	for (var i=0; i<pairs.length; i++)
	{
		var pos = pairs[i].indexOf('=');
		if (pos == -1) continue;
		var argname = pairs[i].substring(0,pos);
		var value=pairs[i].substring(pos+1);
		args[argname]=unescape(value);
	}
	return args;
}

function maintainStateOfLeftNav()
{
	
	var subSection = "";
	var leftNavStateMap = new Object();
//alert("asdfasdf")+args.technologyInitiativesDisplay;
	leftNavStateMap.technologyInitiatives = args.technologyInitiativesDisplay;
	//alert("asdfassssdf");
	for (subSection in leftNavStateMap)
	{
		//alert("22222"+leftNavStateMap[subSection]);
		var subMenuDiv = document.getElementById(subSection);
	//	alert("22222");
		var arrowImage = getSubSectionArrowImageRef(subSection);
	//	alert("22222");
		if (leftNavStateMap[subSection] == 'block')
		{
		//	alert("bl");
			arrowImage.setAttribute('src','/images/arrowDown.gif');
		}
		else
		{
		//	alert("none");
			arrowImage.setAttribute('src','/images/arrow.gif');
			//	alert("none2");
		}
				if (leftNavStateMap[subSection] == 'block')
				{
						//alert("none2");
					document.getElementById(subSection).style.display = leftNavStateMap[subSection];
				}
				drawBorder(document.getElementById("fixLeftBorder"));
	
		
	}
	//drawBorder(document.getElementById("fixLeftBorder"));
	//	alert("6");
}


// Utility function to retrieve all elements within an element and tag scope with a specified class name
function getElementsByClassName(oElement, strTagName, strClassName){
	var arrElements = (strTagName == "*" && oElement.all)? oElement.all : oElement.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++)
	{
		oElement = arrElements[i];
		if(oRegExp.test(oElement.className))
		{
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}

function next_focus(formName, elementName, elementLength, nextElement) {
        if(document.forms[formName].elements[elementName].value.length == elementLength)
                document.forms[formName].elements[nextElement].focus()
        return true
}