//======================================================
// *** Core Scripts
//======================================================

//======================================================
// *** init
// -----------------------------------------------------
// *** performs any initialization functions
//======================================================
function init(){

	//alert('init');
	// *** build toolbar
	buildUtilityToolbar();
}

//======================================================
// *** openNewWindow
// -----------------------------------------------------
// *** opens new window with specified variables
//======================================================
function openNewWindow(URLtoOpen, windowName, windowFeatures) {
	newWindow=window.open(URLtoOpen, windowName, windowFeatures); 
}

//======================================================
// *** MM_jumpMenu
// -----------------------------------------------------
// *** creates jump menu
//======================================================
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  	eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  	if (restore) selObj.selectedIndex=0;
}

//======================================================
// *** generateBreadcrumb
// -----------------------------------------------------
// *** generates breadcrumb links for page
//======================================================
function generateBreadcrumb(){

	var thisURL = window.location;
	var thisTitle = document.title;
	
	// *** first build main part of breadcrumb
	var strURL = thisURL.toString();
	var arrURL = strURL.split("/");
	// *** pop out the first four items in the array - 
	// *** the root of the url string (http:, /, /, [domain]
	
	// *** remove the http://www.gulfstream.com/ string
	// *** from the array
	arrURL.reverse();
	for(var i=0; i<3; i++){
		arrURL.pop(i);
	}
	// *** reorder to correct order
	arrURL.reverse();
	// *** get rid of trailing slashes
	var lastItem = arrURL.length-1;
	arrURL.pop(lastItem);

	// *** create an array to contain the pieces of the breadcrumb string
	var arrURLDisplay = [];

	// *** now loop through the arrURL array and capitalize the first letter of 
	// *** each word in each array item
	for(var i=0; i<arrURL.length; i++){
		
		// *** create an array of characters from this phrase
		
		switch(arrURL[i]){
		
			case "mygulfstream" :
				var newPhrase = "myGulfstream.com";
			break;
			
			case "mygulfstream.com" :
				var newPhrase = "myGulfstream.com";
			break;
			
			case "evs" :
				var newPhrase = "EVS";
			break;
			
			case "bbml" :
				var newPhrase = "BBML";
			break;
			
			case "planeconnect" :
				var newPhrase = "PlaneConnect";
			break;
			
			case "planebook" :
				var newPhrase = "PlaneBook";
			break;
			
			case "planeview" :
				var newPhrase = "PlaneView";
			break;
			
			case "svpfd" :
				var newPhrase = "SV-PFD";
			break;
		
			
			default :
				var arrPhrase = arrURL[i].split("");
				var newPhrase = "";
				// *** loop through characters in phrase array
				// *** and capitalize the first character, and
				// *** any characters that follow a space (" ")
				for(var j=0; j<arrPhrase.length; j++){
					
					// *** handle first char
					if(j==0){
						arrPhrase[j] = arrPhrase[j].toUpperCase();
					}
					
					// *** handle subsequent chars
					if(arrPhrase[j] == "_" || arrPhrase[j] == "-"){
						
						arrPhrase[j] = " ";
						arrPhrase[j+1] = arrPhrase[j+1].toUpperCase();
					}
		
					newPhrase += arrPhrase[j];
				}
			
			break;
			
		}

		arrURLDisplay[i] = newPhrase;
	}

	// *** now build end of breadcrumb - the title
	var strTitle = thisTitle.toString();
	/*
	var titleColonPosition = strTitle.lastIndexOf(":");
	if(titleColonPosition == null || titleColonPosition == -1){
		titleColonPosition = 0;
	} else {
		titleColonPosition++;
	}
	*/
	var titleDashPosition = strTitle.lastIndexOf("-");
	if(titleDashPosition == null || titleDashPosition == -1){
		titleDashPosition = 0;	
	} else {
		titleDashPosition--;
	}
	
	var strNewTitle = strTitle.substr(0, titleDashPosition);
	strNewTitle = cleanString(strNewTitle);

	// *** now compose breadcrumb
	var breadcrumb = "<a href=\"/\">Home</a> / ";
	
	// *** loop through array
	for(var j=0; j<arrURL.length+1; j++){
		
		// *** if this is last item in array
		if(j==arrURL.length){
			var section = "<span>"+strNewTitle+"</span>";
		} else {
			
			// *** get this section url
			var sectionURL = "";
			for(var m=0; m<=j; m++){
				//alert('arrURL['+m+'] = ' + arrURL[m]);
				sectionURL += "/"+arrURL[m]; 
				
			}
			var section = "<a href="+sectionURL+">"+arrURLDisplay[j]+"</a> / ";
			
		}
		breadcrumb += section;
	}
	
	// *** return the final string
	// *** example:  <a href="/" class="footerLinkWhite">Home</a> / <a href="/g150/">G150</a> / <span style="font-weight:bold;">G150 Overview</span>;
	//alert(breadcrumb);
	return(breadcrumb);
}


//======================================================
// *** cleanString
// -----------------------------------------------------
// *** 
//======================================================
function cleanString(str){
	
	var re = /[\/_*]/g;
	str = str.replace(re, " ");
	return(str);
	
}


//======================================================
// *** valideateForm
// -----------------------------------------------------
// *** 
//======================================================
function validateForm(which){

	var tf = which;
	for (i=0; i<tf.length; i++){
		var te = tf.elements[i];
		var ten = te.name;
		var tev = te.value;
		var teid = te.id;
		var tet = te.type;
		
		// *** get element name prefix
		var teprefix = ten.substr(0, 2);
		//alert('hey teprefix = ' + teprefix);
		
		if(teprefix=='RE'){
			
			
			if(tev == ''){
				alert('Please make sure the "'+teid+'" field is completed.');
				return(false);
			}
			
		}
		

	}
	
	return(true);
}

//======================================================
// *** expandSidebarMenu
// -----------------------------------------------------
// ***
//======================================================
function expandSidebarMenu(){
	var thisURL = window.location;		
	var strURL = thisURL.toString();
	var arrURL = strURL.split("/");
	for(var i=0; i<3; i++){
		arrURL.shift(i);
	}
	arrURL.pop();
	var lastItem = arrURL[arrURL.length-1];
	var submenuID = "sidebarMenuSubmenu_"+lastItem;
	var submenuParentID = "sidebarMenuItem_"+lastItem;
	var submenu = document.getElementById(submenuID);
	var submenuParent = document.getElementById(submenuParentID);
	if(submenu && submenuParent){
		submenu.style.display = "block";
		submenuParent.style.backgroundColor = "#eeeeee";
		submenuParent.style.fontWeight = "bold";
	}	
}

//======================================================
// *** buildUtilityToolbar
// -----------------------------------------------------
// *** tests for necessity for utility toolbar
//======================================================
function buildUtilityToolbar(){
	
	//alert('buildUtilityToolbar');
	var cc = "";
	if(document.getElementById('textContentContainer') != null){
		cc = document.getElementById('textContentContainer').innerHTML;
	}
	if(document.getElementById('textContentContainerOneColumn') != null){
		cc = document.getElementById('textContentContainerOneColumn').innerHTML;
	}
	if(document.getElementById('featurepageTextContentContainer') != null){
		cc = document.getElementById('featurepageTextContentContainer').innerHTML;
	}
	
	cc = cc.toLowerCase();
	//alert('cc = ' + cc);
	
	// *** if there is text content on the page, 
	// *** then build print screen toolbar
	var c = '<a href="#" onClick="return openPrintView();" title="Open Printable View">Open Printable View</a>&nbsp;<a href="#" onClick="return openPrintView();" title="Open Printable View" alt="Open Printable View"><img src="/_images/icon_printer_16x16.jpg" width="16" height="16" align="absmiddle" border="0" /></a>';
	if(cc.indexOf("<p>") != -1){
		// *** place toolbar contents
		if(document.getElementById('utilityContainer') != null){
			var tc = document.getElementById('utilityContainer');
			tc.innerHTML = c;
		}
	}
	
	
	
}


//======================================================
// *** openPrintView
// -----------------------------------------------------
// *** opens body copy of page in a printable window
//======================================================
function openPrintView(){
	//alert('open printable view');
		
	// *** header content
	var c = '';
	if(document.getElementById('subpageHeaderContainer')!=null){
		c += document.getElementById('subpageHeaderContainer').innerHTML;
	}
	if(document.getElementById('headerContainer')!=null){
		c += document.getElementById('headerContainer').innerHTML;
	}
	
	// *** body copy content
	if(document.getElementById('textContentContainer')!=null){
		c += document.getElementById('textContentContainer').innerHTML; 
	}
	if(document.getElementById('textContentContainerOneColumn')!=null){
		c += document.getElementById('textContentContainerOneColumn').innerHTML; 
	}
	if(document.getElementById('featurepageTextContentContainer')!=null){
		c += document.getElementById('featurepageTextContentContainer').innerHTML;
	}
	
	//alert('c = ' + c);
	var printWindow = window.open('','printView','width=580,height=600,scrollbars=yes,resizable=yes,location=no,status=no,menubar=yes');
	
	var html = '<html><head><title>' + document.title + '</title></head><body><a name="textContentTop"></a><div style="margin:0px; padding:30px; font-family:Arial,Sans-Serif; font-size:11px;"><div style="float:left; width:150px; margin:0px; padding:0px;"><img src="/_images/logo_gulfstream_150px.gif" width="150" height="26" /></div><div style="float:right; text-align:right; width:200px; margin:0px; padding:0px; font-size:10px;"><a href="#" onClick="window.print();return false;">Print This Page</a>&nbsp;<a href="#" onClick="window.print();return false;"><img src="/_images/icon_printer_16x16.jpg" width="16" height="16" border="0" title="Print This Page" alt="Print This Page" align="absmiddle" /></a></div><div style="clear:both; line-height:0px; font-size:0px; height:0px;">&nbsp;</div>' + c + '<p style="font-size:10px;">Copyright &copy; 2009 Gulfstream Aerospace Corporation.  All rights reserved. <a href="http://www.generaldynamics.com/">A General Dynamics company</a>.</p></div></body></html>';
	
	// *** variable name of window must be included for all three of the following methods so that
	// *** javascript knows not to write the string to this window, but instead to the new window
	printWindow.document.open();
	printWindow.document.write(html);
	printWindow.document.close();

}

//======================================================
// *** loginFormInputFieldFocusHandler
// -----------------------------------------------------
// *** 
//======================================================
function loginFormInputFieldFocusHandler(id){
	// *** handle field values
	switch(id.id){
		case "loginFormUsername" :
			id.value ='';
			break;
		case "loginFormPassword" :
			id.value ='';
			break;
		case "loginFormPasswordLabel" :
			id.style.display = 'none';
			$('loginFormPassword').setStyle({'display':'inline'});
			$('loginFormPassword').focus();
			break;
		default :
			break;
	}
}
//======================================================
// *** loginFormInputFieldBlurHandler
// -----------------------------------------------------
// *** 
//======================================================
function loginFormInputFieldBlurHandler(id){
	if(id.value == ''){
		switch(id.id){
			case 'loginFormPassword' :
				break;
			case 'loginFormPasswordLabel' :
				break;
			case 'loginFormUsername' :
				break;
			default :
				break;
		}
	}
}
//======================================================
// *** loginFormSubmitHandler
// -----------------------------------------------------
// *** 
//======================================================
function loginFormSubmitHandler(id){
	
	// *** reset styles
	if($('loginFormUsername').hasClassName('formFieldError')){
		$('loginFormUsername').removeClassName('formFieldError');	
	}
	if($('loginFormPassword').hasClassName('formFieldError')){
		$('loginFormPassword').removeClassName('formFieldError');	
	}
	if($('loginFormDestinationSite').hasClassName('formFieldError')){
		$('loginFormDestinationSite').removeClassName('formFieldError');	
	}
	
	
	// *** username
	if($('loginFormUsername').value == '' || $('loginFormUsername').value == 'Username'){
		alert('Please make sure the username field is completed.');
		$('loginFormUsername').addClassName('formFieldError');
		return(false);
	}
	// *** password
	if($('loginFormPassword').value == '' || $('loginFormPassword').value == 'Password'){
		alert('Please make sure the password field is completed.');
		$('loginFormPassword').addClassName('formFieldError');
		return(false);
	}
	// *** destination
	if($('loginFormDestinationSite').value == ''){
		alert('Please make sure the the destination selector is completed.');
		$('loginFormDestinationSite').addClassName('formFieldError');
		return(false);
	}
	
	
	// *** handle processing
	switch($('loginFormDestinationSite').value){
		case "livelink" : 
		
			// *** add hidden form elements
			var ppe = document.createElement('input');
			ppe.type = 'hidden';
			ppe.name = 'proxypath';
			ppe.value = 'reverse';
			
			var cte = document.createElement('input');
			cte.type = 'hidden';
			cte.name = 'context';
			cte.value = 'default';
			
			var urle = document.createElement('input');
			urle.type = 'hidden';
			urle.name = 'url';
			urle.value = 'https://km.mygulfstream.com/';
			
			var arre = [ppe,cte,urle];
			for(i=0; i<arre.length; i++){
				$('loginForm').appendChild(arre[i]);
			}
			$('loginForm').action = 'https://km.mygulfstream.com/ICSLogin/auth-up';
			
			return(true);
			break;

		case "mygulfstream" : 
		
			// *** add hidden form elements
			var ppe = document.createElement('input');
			ppe.type = 'hidden';
			ppe.name = 'proxypath';
			ppe.value = 'reverse';
			
			var cte = document.createElement('input');
			cte.type = 'hidden';
			cte.name = 'context';
			cte.value = 'default';
			
			var urle = document.createElement('input');
			urle.type = 'hidden';
			urle.name = 'url';
			urle.value = 'https://www.mygulfstream.com/';
			
			var arre = [ppe,cte,urle];
			for(i=0; i<arre.length; i++){
				$('loginForm').appendChild(arre[i]);
			}
			$('loginForm').action = 'https://www.mygulfstream.com/ICSLogin/auth-up';
			
			return(true);
			break;
			
		case "careers" :
			
			$('loginFormUsername').name = "email";
			$('loginFormPassword').name = "password";
			$('loginForm').action = 'http://www.resumeware.net/gac_rw/login.cfm?rtype=&comingfrom=external';
			return(true);
			break;

		case "supplier" :

			// *** add hidden form elements
			var ope = document.createElement('input');
			ope.type = 'hidden';
			ope.name = 'option';
			ope.value = 'credential';
			
			var tge = document.createElement('input');
			tge.type = 'hidden';
			tge.name = 'target';
			tge.value = 'https://auth-https-int.gulfstream.com:443/LAGBroker?%22https://supplier.mygulfstream.com:443/%22';
			
			var arre = [ope,tge];
			for(i=0; i<arre.length; i++){
				$('loginForm').appendChild(arre[i]);
			}
			
			$('loginFormUsername').name = "Ecom_User_ID";
			$('loginFormPassword').name = "Ecom_Password";
			$('loginForm').action =  'https://idp.gulfstream.com/nidp/idff/sso?sid=1';
			
			return(true);
			break;

		default :

			break;
	}
	return(true);
}
//======================================================
// *** swapImage
// -----------------------------------------------------
// *** 
//======================================================
function swapImage(id, url){
	id.src = url;
}