/*
	2008.09.17	gricke
				<holdSearchTerm> Required if using google search (set through config)
				
	2007.12.20	gricke
				<loginAdmin>
				Updated sSite so as not to pass to search form and back
				Add rev number so other versions can be checked and upgraded
				Add check to have login link only appear for dev sites "orbisdesign.net" is in url
				
	NOTES
	
	SEEALSO
				css/header.css 
								<Login>
								<loginLink>
*/

window.onload = initialize; 

function initialize () {
	clickLogo();
	setupFooterNav();
	loginAdmin();
	addStyleSheet();
}
function addStyleSheet(){
	/*
	var sSite = window.location;
		sSite = sSite + ' ';
		sSite = sSite.toLowerCase();
	if(sSite.indexOf('bradfoote.') != -1){
		var cssNode = document.createElement('link');
		cssNode.type = 'text/css';
		cssNode.rel = 'stylesheet';
		cssNode.href = '/css/bradFooteSite.css';
		cssNode.media = 'screen';
		cssNode.title = 'dynamicLoadedSheet';
		document.getElementsByTagName("head")[0].appendChild(cssNode);
	}
	*/
}


var functionLoop = 0;
function sideBanner(){
	if(document.getElementById('underSubNav')){
		document.getElementById('underSubNav').innerHTML = '<a href="/GearboxServices_8491.aspx"><img src="/images/content/sideBanner_gearboxServices.png" width="206" height="128" ></a>';
	}else{
		functionLoop += 1;
		if(window.functionLoop < 50){
			setTimeout('sideBanner()',10);
		}	
	}
}
sideBanner();

function clickLogo(){
	var logoWidth = getStyle("logo","width");
	var logoHeight = getStyle("logo","height");
	document.getElementById('logo').innerHTML = "<a href=\"/home_2.aspx\"><img alt=\"Home\" height=\"" + logoHeight + "\" src=\"/images/trans.gif\" width=\"" + logoWidth + "\" /></a>";
}
function setupFooterNav(){
	var footerNav = document.getElementById('FooterImage_1');
	var HTMLDisp = '';
	HTMLDisp = HTMLDisp + '<div id=\"legal\">'
	HTMLDisp = HTMLDisp + '<div id="privacy"><a href=\"Privacy_7443.aspx\">Privacy</a></div>';
	HTMLDisp = HTMLDisp + ' | ';
	HTMLDisp = HTMLDisp + '<div id="usage"><a href=\"Legal_1375.aspx\">Terms of use</a></div>';
	HTMLDisp = HTMLDisp + '</div>'
	HTMLDisp += '<div id="followTwitter" onMouseOver="followHover(\'twitter\');" onMouseOut="followHover(\'\');"><a href="http://twitter.com/broadwind"><img src="/images/content/followlink_twitter2.png" width="29" height="28" /></a></div>';
	HTMLDisp += '<div id="followFacebook" onMouseOver="followHover(\'face\');" onMouseOut="followHover(\'\');"><a href="http://www.facebook.com/pages/Broadwind-Energy/187989077887588"><img src="/images/content/followlink_facebook2.png" width="29" height="29" /></a></div>';
	HTMLDisp += '<div id="followHover"></div>';
	
	footerNav.innerHTML = HTMLDisp;
}
function followHover(which){
	if(which == 'face'){
		document.getElementById('followHover').innerHTML = 'Join us on facebook';
	}else if(which == 'twitter'){
		document.getElementById('followHover').innerHTML = 'Join us on Twitter';
	}else{
		document.getElementById('followHover').innerHTML = '';
	}
}
function holdSearchTerm(){
	var searchTerm = document.searchform.SearchText.value;
	SetGetCookie('searchEntered',searchTerm);
}
/* ~~~~~~~~~~~~~~~~~~~~[ login functions ] ~~~~~~~~~~~~~~~ */
function loginAdmin(){
	var rev = 20100414;
	var sSite = window.location;
	sSite = sSite + ' ';
	
	if(sSite.indexOf('orbisdesign.net') != -1){
	
		var divParent = document.getElementById('main');		
		var newdiv = document.createElement('div');
		var divIdName = 'logInDiv';
		
		newdiv.setAttribute('id',divIdName);
		newdiv.innerHTML = '';
		divParent.appendChild(newdiv);		
	
		var LoginBox = document.getElementById('logInDiv');
		var HTMLDisp = '';
		var sPath = window.location.pathname;
		var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
		
		if(sPage.indexOf('.aspx') > 0){
			sPage = sPage.substring(0,(sPage.length - 1));
		}
		
		if(sPage.length < 3){
			sPage = 'home_2.asp';	//Correct for root
		}
		
		sSite = sSite.substring(sSite.indexOf("/")+2);
		sSite = sSite.substring(0,sSite.indexOf("/"));
		sSite = sSite.substring(0,sSite.indexOf("."));

		var sUser = sSite + 'User';
		var sPass = sSite + 'Pass';		

		if(getCookie(sSite + 'AdminLoggedIn') == 'True' && sPath.indexOf('.aspx') > 0){
			HTMLDisp = HTMLDisp + '<a href="#" id=\"loginLink\" onClick="DispLogin();return false;" style=\"\text-decoration:none;font-size:8px;">Log In</a>';
		}else if(getCookie(sSite + 'AdminLoggedIn') == 'True' && sPath.indexOf('.aspx') == -1){
			HTMLDisp = HTMLDisp + '<a href="#" id=\"loginLink\" onClick="LogOut(\'' + sSite + '\');return false;" style=\"\text-decoration:none;font-size:8px;">Log Out</a>';
		}else{
			HTMLDisp = HTMLDisp + '<a href="#" id=\"loginLink\" onClick="DispLogin();return false;" style=\"\text-decoration:none;font-size:8px;">LOG IN</a>';
		}
		HTMLDisp = HTMLDisp + '<div id=\"Login\">';
		HTMLDisp = HTMLDisp + '<form id=\"adminLoginForm\" method=\"post\" action=\"/Admin/Default.asp?OnTo=' + sPage + '" name=\"Security\" style=\"display:inline;\" >';
		HTMLDisp = HTMLDisp + '<input class=\"txtInput\" type=\"text\" name=\"txtUserID\">';
		HTMLDisp = HTMLDisp + '<input class=\"txtInput\" type=\"password\" name=\"txtPassword\">';
		HTMLDisp = HTMLDisp + '<div id=\"loginSubmit\"><input type=\"image\" width=\"40\" height=\"20\" src=\"/images/trans.gif\" value=\"log in\" name=\"Login2\" onClick=\"submitLogin();"></div>';
		HTMLDisp = HTMLDisp + '<div id=\"remember\"><input type=\"checkbox\" name=\"savePass\" value =\"true\"></div>';
		HTMLDisp = HTMLDisp + '<input type=\"hidden\" name=\"FormSubmitted\" value =\"true\">';
		HTMLDisp = HTMLDisp + '<input type=\"hidden\" name=\"ByPassAdmin\" value =\"true\">';
		HTMLDisp = HTMLDisp + '<input type=\"hidden\" name=\"siteUrlSec\" value =\"\">';
		HTMLDisp = HTMLDisp + '<a href="#" id=\"closeLink\" onClick="hideLogin();return false;"><img src=\"/images/trans.gif\" width=\"20\" height=\"20\" /></a>';
		HTMLDisp = HTMLDisp + '</form>';
		HTMLDisp = HTMLDisp + '</div>';
		HTMLDisp = HTMLDisp + '';
   		LoginBox.innerHTML = HTMLDisp;
		
		if(getCookie(sUser) != null){
			document.forms['Security'].txtUserID.value = getCookie(sUser);
			document.forms['Security'].txtPassword.value = getCookie(sPass);
		}
	}	
}
function submitLogin(){
	var sSite = window.location;
	document.forms['Security'].siteUrlSec.value = sSite;
	sSite = document.forms['Security'].siteUrlSec.value;
	sSite = sSite.substring(sSite.indexOf("/")+2);
	sSite = sSite.substring(0,sSite.indexOf("/"));
	sSite = sSite.substring(0,sSite.indexOf("."));
	var sUser = sSite + 'User';
	var sPass = sSite + 'Pass';
	
	if(document.Security.savePass.checked == true){
		SetGetCookie(sUser,document.forms['Security'].txtUserID.value);
		SetGetCookie(sPass,document.forms['Security'].txtPassword.value);
		document.forms['Security'].submit();
	}else{
		document.forms['Security'].submit();
	}
}
function DispLogin(){
	var Login = document.getElementById('Login');
	Login.style.display = 'block';
	document.Security.txtUserID.focus();
}
function hideLogin(){
	var Login = document.getElementById('Login')
	Login.style.display = 'none';
}
/* ~~~~~~~~~~~~~~~~~~~~[ END: login functions ] ~~~~~~~~~~~~~~~ */

$import('/javascriptO2/cookies.js');
//$import('/javascriptO2/sifr_v3.js');
//$import('/javascript/sifr_config.js');

function baseURL(){
	var sSite;
	sSite = window.location;
	sSite = sSite + ' ';
	sSite = sSite.replace(/^\s+|\s+$/g, '');
	sSite = sSite.toLowerCase();
	return sSite;
}
function isDotNet(){
	var checkDotNet;
	if(document.getElementById('searchform') != null){
		checkDotNet = document.searchform.isDotNet.value.indexOf('aspx');
		if(checkDotNet != -1){
			return '.aspx';
		}else{
			return '.asp';
		}
	}else if(document.getElementById('searchPageform') != null){
		checkDotNet = document.searchPageform.isDotNet.value.indexOf('aspx');
		if(checkDotNet != -1){
			return '.aspx';
		}else{
			return '.asp';
		}	
	}else{
		return '.aspx';
	}
}
function getStyle(el,styleProp){
	 /* http://www.quirksmode.org/dom/getstyles.html */ 
	 var x = document.getElementById(el);
	 if (x.currentStyle){
	  //ie
	  switch(styleProp)
	  {
	  case 'text-decoration':
	    styleProp = 'textDecoration';
	    break;    
	  default:
	  }
	  var y = x.currentStyle[styleProp];
	 }else if (window.getComputedStyle){
	  var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
	 }
	 return y;
} 
function $import(src){
	var scriptElem = document.createElement('script');
	scriptElem.setAttribute('src',src);
	scriptElem.setAttribute('type','text/javascript');
	document.getElementsByTagName('head')[0].appendChild(scriptElem);
}
