// infra_component.js
//
// Author:      J. Robar/Uniband, Inc.
// Created:     17 July, 2000
// Updated:     5 October, 2004; J Robar - added window modality code for all popup windows
//              12/13/2006; J Beyer - changed IHS_Component function to utilize https for SSL
// Purpose:     create 'one' calling for all popup components
// Arguments:   Comp (component name) & URL arguments

var found = 99;
var fnd;
var site_args;
var args;
var tmp_baseURL;
var tmp_orig_baseURL;
var offset;
var baseURL;
var win_comp;
var ihs_comp = new Array(5);

//<cfif 1 EQ 1>
// <cfset whereWeBeAt1 = cgi.https>
// <cfset whereWeBeAt2 = cgi.server_name>
// <cfset whereWeBeAt3 = cgi.script_name>
// <cfoutput>"JBDebug<br>wWBA1:" & #whereWeBeAt1# & "<br>wWBA2:" & #whereWeBeAt2# & "<br>wWBA3:" & #whereWeBeAt3# & "<br>"</cfoutput>
//</cfif>

// set configuration types
var default_config = "scrollbars=yes,location=no,toolbar=no,directories=no,menubar=no,status=yes,resizable=yes,alwaysraised";

// Global for brower version branching.
var Nav4 = ((navigator.appName == "Netscape") && (parseInt(navigator.appVersion) == 4))
var Nav5 = ((navigator.appName == "Netscape") && (parseInt(navigator.appVersion) == 5))

// One object tracks the current modal dialog opened from this window.
// Replaced the following with new URL as shown below (KMS 7/2007):
//ihs_comp[1] = new IHSComp_Struct("login","/generalweb/webcomponents/infra/login.cfm",default_config,600,600,"IHS_Login/Logout"); 

var dialogWin = new Object()

ihs_comp[0] = new IHSComp_Struct("error","/generalweb/webapps/webprofile/indexcomperror.cfm",default_config,600,600,"Window_Error");
ihs_comp[1] = new IHSComp_Struct("login","/generalweb/webapps/webprofile/indexpop.cfm",default_config,600,600,"IHS_Login/Logout");
ihs_comp[2] = new IHSComp_Struct("weblogin","/generalweb/webapps/webprofile/indexpop.cfm",default_config,600,600,"IHS_Login/Logout");
ihs_comp[3] = new IHSComp_Struct("profile","/generalweb/webapps/webprofile/register/indexpop2.cfm",default_config,810,950,"Web_Account");
ihs_comp[4] = new IHSComp_Struct("webaccount","/generalweb/webapps/webprofile/register/indexpop2.cfm",default_config,810,950,"Web_Account");

function IHSComp_Struct(Comp,URL,Config,Width,Height,Desc)
{
 this.comp = Comp
	this.url = URL
	this.config = Config
	this.width = Width
	this.height = Height
	this.desc = Desc
}

function IHS_Component(Comp,Args)
{

 var tmp_baseURL = window.location.href;

 if (Args.indexOf("jimtest") > -1){
  ihs_comp[4] = new IHSComp_Struct("webaccount","/generalweb/webapps/webprofile/register/indexpop2.cfm",default_config,810,950,"Web_Account");
 }
 
 if (Args.indexOf("rs") > -1) {
 	if (tmp_baseURL.indexOf("dev") > -1) tmp_baseURL = "https://wwwdev.ihs.gov";
 	else if (tmp_baseURL.indexOf("stage") > -1) tmp_baseURL = "https://wwwstage.ihs.gov";
 	else tmp_baseURL = "https://www.ihs.gov"; 
 } else {
 	if (tmp_baseURL.indexOf("dev") > -1) tmp_baseURL = "http://wwwdev.ihs.gov";
 	else if (tmp_baseURL.indexOf("stage") > -1) tmp_baseURL = "http://wwwstage.ihs.gov";
 	else tmp_baseURL = "http://www.ihs.gov"; 
 }

 offset = tmp_baseURL.indexOf(".gov")+4;
 baseURL = tmp_baseURL.substring(0,offset);

 // find component; if not found, set 'fnd' to 0
	for (var i=0; i<ihs_comp.length; i++) { if (Comp == ihs_comp[i].comp) { found = i; }	}
	if (found == 99) { fnd = 0; }	else { fnd = found; }

 // set argument list
	if (IHS_Component.arguments.length == 1) { site_args  = baseURL + ihs_comp[fnd].url + "?ihs_component=" + ihs_comp[fnd].comp;	}
	else { site_args  = baseURL + ihs_comp[fnd].url + "?ihs_component=" + ihs_comp[fnd].comp + "&" + Args; }

 if (Args.indexOf("rs") > -1) {
   // branch in current window -jb-
   window.location=site_args;
 } else {
   // do the popup -jb-
   // Initialize properties of the modal dialog object.
  	if (!dialogWin.win || (dialogWin.win && dialogWin.win.closed)) {
   //		dialogWin.returnFunc = returnFunc
   //		dialogWin.args = args
  		dialogWin.returnFunc = ""
  		dialogWin.returnedValue = ""
  		dialogWin.args = ""
  		dialogWin.url = site_args
  		dialogWin.width = ihs_comp[fnd].width
  		dialogWin.height = ihs_comp[fnd].height
  
    // Keep name unique so Navigator doesn't overwrite an existing dialog.
  		dialogWin.name = (new Date()).getSeconds().toString()
  
    // Assemble window attributes and try to center the dialog.
  		if (Nav4 || Nav5) {
  
     // Center on the main window.
  			dialogWin.left = window.screenX + ((window.outerWidth - ihs_comp[fnd].width) / 2)
  			dialogWin.top = window.screenY + ((window.outerHeight - ihs_comp[fnd].height) / 2)
  			var attr = "screenX=" + dialogWin.left + ",screenY=" + dialogWin.top + ",width=" + dialogWin.width + ",height=" + dialogWin.height + "," + ihs_comp[fnd].config
  
  		} else {
  
     // The best we can do is center in screen.
  			dialogWin.left = (screen.width - dialogWin.width) / 2
  			dialogWin.top = (screen.height - dialogWin.height) / 2
     //			var attr = "left=" + dialogWin.left + ",top=" + dialogWin.top + ",width=" + dialogWin.width + ",height=" + dialogWin.height + "," + ihs_comp[fnd].config
  			var attr = "width=" + ihs_comp[fnd].width + ",height=" + ihs_comp[fnd].height + "," + ihs_comp[fnd].config
  		}
  
    // Generate the dialog and make sure it has focus.
    // var win_name = window.open(site_args,win_comp,ihs_comp[fnd].config);
    //alert ("DEBUGGING: url: " + dialogWin.url + " IHS_Component.arguments.length: " + IHS_Component.arguments.length)
  		dialogWin.win = window.open(dialogWin.url, dialogWin.name, attr)
  		dialogWin.win.focus()
  	} else {
  		dialogWin.win.focus()
  	}
 }
}

// Event handler to inhibit Navigator form element 
// and IE link activity when dialog window is active.
function deadend() {
	if (dialogWin.win && !dialogWin.win.closed) {
		dialogWin.win.focus()
		return false
	}
}

// Since links in IE4 cannot be disabled, preserve 
// IE link onclick event handlers while they're "disabled."
// Restore when re-enabling the main window.
var IELinkClicks

// Disable form elements and links in all frames for IE.
function disableForms() {
	IELinkClicks = new Array()
	for (var h = 0; h < frames.length; h++) {
		for (var i = 0; i < frames[h].document.forms.length; i++) {
			for (var j = 0; j < frames[h].document.forms[i].elements.length; j++) {
				frames[h].document.forms[i].elements[j].disabled = true
			}
		}
		IELinkClicks[h] = new Array()
		for (i = 0; i < frames[h].document.links.length; i++) {
			IELinkClicks[h][i] = frames[h].document.links[i].onclick
			frames[h].document.links[i].onclick = deadend
		}
	}
}

// Restore IE form elements and links to normal behavior.
function enableForms() {
	for (var h = 0; h < frames.length; h++) {
		for (var i = 0; i < frames[h].document.forms.length; i++) {
			for (var j = 0; j < frames[h].document.forms[i].elements.length; j++) {
				frames[h].document.forms[i].elements[j].disabled = false
			}
		}
		for (i = 0; i < frames[h].document.links.length; i++) {
			frames[h].document.links[i].onclick = IELinkClicks[h][i]
		}
	}
}

// Grab all Navigator events that might get through to form
// elements while dialog is open. For IE, disable form elements.
function blockEvents() {
	if (Nav4 || Nav5) {
		window.captureEvents(Event.CLICK | Event.MOUSEDOWN | Event.MOUSEUP | Event.FOCUS)
		window.onclick = deadend
	} else {
		disableForms()
	}
	window.onfocus = checkModal
}
// As dialog closes, restore the main window's original
// event mechanisms.
function unblockEvents() {
	if (Nav4 || Nav5) {
		window.releaseEvents(Event.CLICK | Event.MOUSEDOWN | Event.MOUSEUP | Event.FOCUS)
		window.onclick = null
		window.onfocus = null
	} else {
		enableForms()
	}
}

// Invoked by onFocus event handler of EVERY frame,
// return focus to dialog window if it's open.
function checkModal() {
	if (dialogWin.win && !dialogWin.win.closed) {
		dialogWin.win.focus()	
	}
}

/**************************
  END MODAL DIALOG CODE
**************************/

// Function to run upon closing the dialog with "OK".
function setPrefs() {
	// We're just displaying the returned value in a text box.
	document.returned.searchURL.value = dialogWin.returnedValue
}

// Called by "Apply Settings" button to reload the page with
// search string data appended to URL.
function applySettings() {
	if (document.returned.searchURL.value) {
		location.href = "main.html" + document.returned.searchURL.value
	}
}

/*****************************************
      BEGIN CUSTOM PAGE STYLE CODE
******************************************/
// Convert location.search into an array of values
// indexed by name.
function getSearchAsArray() {
	var results = new Array()
	var input = unescape(location.search.substr(1))
	if (input) {
		var srchArray = input.split("&")
		var tempArray = new Array()
		for (i = 0; i < srchArray.length; i++) {
			tempArray = srchArray[i].split("=")
			results[tempArray[0]] = tempArray[1]
		}
	}
	return results
}
var srchData = getSearchAsArray()

