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

//======================================================
// *** Global Vars
// -----------------------------------------------------
// *** 
//======================================================

// *** detect iOS devices
var _isiPad = navigator.userAgent.match(/iPad/i)  != null;
var _isiPhone = navigator.userAgent.match(/iPhone/i)  != null;

// *** Flash Player major version
var _flashPlayerVersion = swfobject.getFlashPlayerVersion().major;

//======================================================
// *** 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<4; 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 = "\u0421\u0438\u0441\u0442\u0435\u043c\u0430 \u0441\u0431\u043e\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 PlaneConnect";
			break;
			
			case "planebook" :
				var newPhrase = "\u042d\u043b\u0435\u043a\u0442\u0440\u043e\u043d\u043d\u044b\u0439 \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432 PlaneBook";
			break;
			
			case "planeview" :
				var newPhrase = "\u041a\u0430\u0431\u0438\u043d\u0430 \u044d\u043a\u0438\u043f\u0430\u0436\u0430 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=\"/\">\u0413\u043b\u0430\u0432\u043d\u0430\u044f</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('\u041f\u043e\u0436\u0430\u043b\u0443\u0439\u0441\u0442\u0430, \u0443\u0431\u0435\u0434\u0438\u0442\u0435\u0441\u044c \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u043f\u043e\u043b\u0435 \042'+teid+'\042 \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u043e.');
				return(false);
			}
			
		}
		

	}
	
	return(true);
}

//======================================================
// *** expandSidebarMenu
// -----------------------------------------------------
// ***
//======================================================
function expandSidebarMenu(){
	var thisURL = window.location;		
	var strURL = thisURL.toString();
	var arrURL = strURL.split("/");
	for(var i=0; i<4; 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="\u0412\u0435\u0440\u0441\u0438\u044f \u0434\u043b\u044f \u043f\u0435\u0447\u0430\u0442\u0438">\u0412\u0435\u0440\u0441\u0438\u044f \u0434\u043b\u044f \u043f\u0435\u0447\u0430\u0442\u0438</a>&nbsp;<a href="#" onClick="return openPrintView();" title="\u0412\u0435\u0440\u0441\u0438\u044f \u0434\u043b\u044f \u043f\u0435\u0447\u0430\u0442\u0438" alt="\u0412\u0435\u0440\u0441\u0438\u044f \u0434\u043b\u044f \u043f\u0435\u0447\u0430\u0442\u0438"><img src="http://www.gulfstream.com/_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="http://www.gulfstream.com/_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;">\u0420\u0430\u0441\u043f\u0435\u0447\u0430\u0442\u0430\u0442\u044c \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443</a>&nbsp;<a href="#" onClick="window.print();return false;"><img src="http://www.gulfstream.com/_images/icon_printer_16x16.jpg" width="16" height="16" border="0" title="\u0420\u0430\u0441\u043f\u0435\u0447\u0430\u0442\u0430\u0442\u044c \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443" alt="\u0420\u0430\u0441\u043f\u0435\u0447\u0430\u0442\u0430\u0442\u044c \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443" align="absmiddle" /></a></div><div style="clear:both; line-height:0px; font-size:0px; height:0px;">&nbsp;</div>' + c + '<p style="font-size:10px;">\u041a\u043e\u043f\u0438\u0440\u0430\u0439\u0442 \251 2010 \u041a\u043e\u0440\u043f\u043e\u0440\u0430\u0446\u0438\u044f Gulfstream Aerospace. \u0412\u0441\u0435 \u043f\u0440\u0430\u0432\u0430 \u0437\u0430\u0449\u0438\u0449\u0435\u043d\u044b. <a href="http://www.generaldynamics.com/">\u041a\u043e\u043c\u043f\u0430\u043d\u0438\u044f General Dynamics</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){
	//alert('id.id = ' + id.id);
	// *** handle field values
	switch(id.id){
		case "loginFormUsername" :
			id.value ='';
			break;
		case "loginFormPassword" :
			id.value ='';
			break;
		case "loginFormPasswordLabel" :
			id.style.display = 'none';
			//$('#loginFormPassword').toggleClass('displayInline');
			document.getElementById('loginFormPassword').style.display = 'inline';
			//$('#loginFormPassword').style.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').hasClass('formFieldError')){
		$('#loginFormUsername').removeClass('formFieldError');	
	}
	if($('#loginFormPassword').hasClass('formFieldError')){
		$('#loginFormPassword').removeClass('formFieldError');	
	}
	if($('#loginFormDestinationSite').hasClass('formFieldError')){
		$('#loginFormDestinationSite').removeClass('formFieldError');	
	}
	
	// *** username
	if($('#loginFormUsername').val() == '' || $('#loginFormUsername').val() == 'Username'){
		alert('\u041f\u043e\u0436\u0430\u043b\u0443\u0439\u0441\u0442\u0430, \u0443\u0431\u0435\u0434\u0438\u0442\u0435c\u044c, \u0447\u0442\u043e \u043f\u043e\u043b\u0435 \u0418\u043c\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u043e.');
		$('#loginFormUsername').addClass('formFieldError');
		return(false);
	}
	// *** password
	if($('#loginFormPassword').val() == '' || $('#loginFormPassword').value == 'Password'){
		alert('\u041f\u043e\u0436\u0430\u043b\u0443\u0439\u0441\u0442\u0430, \u0443\u0431\u0435\u0434\u0438\u0442\u0435c\u044c, \u0447\u0442\u043e \u043f\u043e\u043b\u0435 \u041f\u0430\u0440\u043e\u043b\u044c \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u043e.');
		$('#loginFormPassword').addClass('formFieldError');
		return(false);
	}
	// *** destination
	if($('#loginFormDestinationSite').val() == ''){
		alert('\u041f\u043e\u0436\u0430\u043b\u0443\u0439\u0441\u0442\u0430, \u0443\u0431\u0435\u0434\u0438\u0442\u0435c\u044c, \u0447\u0442\u043e \u043e\u0434\u0438\u043d \u0438\u0437 \u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432 \u0432\u044b\u0431\u0440\u0430\u043d.');
		$('#loginFormDestinationSite').addClass('formFieldError');
		return(false);
	}
	
	
	// *** handle processing
	switch($('#loginFormDestinationSite').val()){
		case "livelink" : 
		
			// *** add hidden form elements
			var ppe = document.createElement('\u0432\u0432\u043e\u0434');
			ppe.type = '\u0441\u043a\u0440\u044b\u0442\u044b\u0439';
			ppe.name = '\u043f\u0443\u0442\u044c \u043a \u043f\u0440\u043e\u043a\u0441\u0438-\u0441\u0435\u0440\u0432\u0435\u0440\u0443';
			ppe.value = '\u043d\u0430\u0437\u0430\u0434';
			
			var cte = document.createElement('\u0432\u0432\u043e\u0434');
			cte.type = '\u0441\u043a\u0440\u044b\u0442\u044b\u0439';
			cte.name = '\u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442';
			cte.value = '\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e';
			
			var urle = document.createElement('\u0432\u0432\u043e\u0434');
			urle.type = '\u0441\u043a\u0440\u044b\u0442\u044b\u0439';
			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]);
				document.getElementById('loginForm').appendChild(arre[i]);
			}
			//$('#loginForm').action = 'https://km.mygulfstream.com/ICSLogin/auth-up';
			document.getElementById('loginForm').action = 'https://km.mygulfstream.com/ICSLogin/auth-up';
			
			return(true);
			break;

		case "mygulfstream" : 
		
			// *** add hidden form elements
			var ppe = document.createElement('\u0432\u0432\u043e\u0434');
			ppe.type = '\u0441\u043a\u0440\u044b\u0442\u044b\u0439';
			ppe.name = '\u043f\u0443\u0442\u044c \u043a \u043f\u0440\u043e\u043a\u0441\u0438-\u0441\u0435\u0440\u0432\u0435\u0440\u0443';
			ppe.value = '\u043d\u0430\u0437\u0430\u0434';
			
			var cte = document.createElement('\u0432\u0432\u043e\u0434');
			cte.type = '\u0441\u043a\u0440\u044b\u0442\u044b\u0439';
			cte.name = '\u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442';
			cte.value = '\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e';
			
			var urle = document.createElement('\u0432\u0432\u043e\u0434');
			urle.type = '\u0441\u043a\u0440\u044b\u0442\u044b\u0439';
			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]);
				document.getElementById('loginForm').appendChild(arre[i]);
			}
			//$('#loginForm').action = 'https://www.mygulfstream.com/ICSLogin/auth-up';
			document.getElementById('loginForm').action = 'https://www.mygulfstream.com/ICSLogin/auth-up';
			
			return(true); 
			break;
			
		case "\u0432\u0430\u043a\u0430\u043d\u0441\u0438\u0438" :
			
			$('#loginFormUsername').name = "email";
			$('#loginFormPassword').name = "\u043f\u0430\u0440\u043e\u043b\u044c";
			//$('#loginForm').action = 'http://www.resumeware.net/gac_rw/login.cfm?rtype=&comingfrom=external';
			document.getElementById('loginForm').action = 'http://www.resumeware.net/gac_rw/login.cfm?rtype=&comingfrom=external';
			return(true);
			break;

		case "\u043f\u043e\u0441\u0442\u0430\u0432\u0449\u0438\u043a" :

			// *** add hidden form elements
			var ope = document.createElement('\u0432\u0432\u043e\u0434');
			ope.type = '\u0441\u043a\u0440\u044b\u0442\u044b\u0439';
			ope.name = '\u0432\u044b\u0431\u043e\u0440';
			ope.value = '\u0438\u043c\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438 \u043f\u0430\u0440\u043e\u043b\u044c';
			
			/*
			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];
			for(i=0; i<arre.length; i++){
				//$('#loginForm').appendChild(arre[i]);
				document.getElementById('loginForm').appendChild(arre[i]);
			}
			
			$('#loginFormUsername').name = "Ecom_User_ID";
			$('#loginFormPassword').name = "Ecom_Password";
			//$('#loginForm').action =  'https://idp.gulfstream.com/nidp/idff/sso?sid=1';
			//document.getElementById('loginForm').action = 'https://idp.gulfstream.com/nidp/idff/sso?sid=1';
			document.getElementById('loginForm').action = 'https://idp.gulfstream.com/nidp/app/login?sid=1';
			
			return(true);
			break;

		default :

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