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

//======================================================
// *** 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]){
			
			// *** example exceptions 
			/*
			case "mygulfstream" :
				var newPhrase = "myGulfstream.com";
			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+1] = arrPhrase[j+1].toUpperCase();
					}
		
					newPhrase += arrPhrase[j];
				}
			
			break;
			
		}

		arrURLDisplay[i] = newPhrase;
	}

	// *** now build end of breadcrumb - the title
	var strTitle = thisTitle.toString();
	var titleSeparatorPosition = strTitle.lastIndexOf("-");

	if(titleSeparatorPosition == null || titleSeparatorPosition == -1){
		titleSeparatorPosition = 0;
	} else {
		titleSeparatorPosition--;
	}
	
	var strNewTitle = strTitle.substr(0, titleSeparatorPosition);
	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";
	}	
}