//Globals
//gets set with the mnuItem of the dropdown box that's currently being looked at
var g_bEnableCollapse = true;
var g_sCurrentMenu = '';
var g_sFormerMenu = '';
var g_iTimeoutID = 0; g_iAutoSaveTimer = 0;
var g_bSelectedAllOptions = false
var g_bCancelPleaseWait = false;

//Autologout after 60 minutes of inactivity
//var iTimer = window.setTimeout("logout()",6000000);
//var iTimer = window.setTimeout("logout()",100000);


function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function disableAutoLogout(){
	if (!isNaN(iTimer)) {
		clearTimeout(iTimer);
	}
}

//This starts the autosave ticker
function startAutoSaveTimer(){
	g_iAutoSaveTimer = window.setTimeout("autoSaveStory()",300000);
	//g_iAutoSaveTimer = window.setTimeout("autoSaveStory()",30000);
}

//This stops the auto-save timer
function cancelAutoSaveTimer(){
	if (!isNaN(g_iAutoSaveTimer)) {
		clearTimeout(g_iAutoSaveTimer);
	}
}

//This allows the Story to autosave every 5 minutes
function autoSaveStory(){
	if (isObject(document.frmManageCaseStoryAutoSave) && isObject(parent.document.frmManageCaseStory)){
		document.frmManageCaseStoryAutoSave.hidStoryAutoSave.value = parent.document.frmManageCaseStory.txtStory.value;
		document.frmManageCaseStoryAutoSave.submit();
	}
}

//100 Neediest Functions
function logout(){
	window.location = 'login.aspx?sUserMessage=Your%20session%20has%20timed%20out%20due%20inactivity';
}

//This allows the Story Sub page to LOAD the story FROM the Cases Main page INTO the Story Sub page
function loadStory(){
	if (isObject(document.frmManageCaseStory) && isObject(parent.document.frmManageCase)){
		document.frmManageCaseStory.txtStory.value = parent.document.frmManageCase.txtStory.value;
	}
}

//This allows the Case main page to GRAB the story FROM the Story Sub page INTO the Case Main page
function grabStory(){
	if(isObject(fmePane.document.frmManageCaseStory) && isObject(document.frmManageCase)) {
		document.frmManageCase.txtStory.value = fmePane.document.frmManageCaseStory.txtStory.value;
	}
}

//This allows the Story Sub page to THROW the story FROM the Story Sub page INTO the Case Main page
function throwStory(){
	var sStory = '', bSaveStory = false;
	if (isObject(document.frmManageCaseStory)){
		sStory = document.frmManageCaseStory.txtStory.value;
		bSaveStory = true;
	}
	if (bSaveStory){
		if(isObject(parent.document.frmManageCase)) {
			parent.document.frmManageCase.txtStory.value = sStory;
		} 
	}
}

function enableAgencyMessage(chkBx){
	if (chkBx.checked == false){
		alert('After you enable this Agency you must manually enable each of this Agency\'s users.  Or else none of the users for this Agency will be able to login to the website.\n\nTo enable this Agencie\'s users go to Database | Manage Users, then Edit each User for this Agency that you want enabled.');
	}	
}

function saveAgencyContact(){
	if (document.all.fmePane.contentWindow.document.all.frmManageAgencyContacts.hidAction.value = 'saveform'){
		if (!document.all.fmePane.contentWindow.document.all.frmManageAgencyContacts.btnSave == null){
			document.all.fmePane.contentWindow.document.all.frmManageAgencyContacts.btnSave.click();
		}
	}
}

function setCasePane(sPane, sURL){
	setCaseTabColors(sPane);
	getTheObject('fmePane').src = sURL;
}

function setCaseTabColors(sPane){
	setBGColor('celStory','#C0C0C0');
	setBGColor('celFamily','#C0C0C0');
	setBGColor('celIncome','#C0C0C0');
	setBGColor('celExpense','#C0C0C0');
	setBGColor('celNeeds','#C0C0C0');
	setBGColor('celDonations','#C0C0C0');
	setBGColor(sPane,'Gray');
}

function openNewReportWindow(sURL) {
g_bCancelPleaseWait=true;
	launchWindow(sURL,700,500,'ReportWindow',true,true);
}

function printAllCases(bGotoFirstCase, bGotoNextCase, iPrintDialogTimeout){
	if (bGotoFirstCase) {
		alert('IMPORTANT INSTRUCTIONS:\n\n1. Do not close the "Print Cases" window until all your printing is complete.  Your printing will not finish if you close the "Print Cases" window prematurely.\n\n2. You must allow all ActiveX controls to run, otherwise you may get an error when printing.  If an "ActiveX" error occurs then follow these steps in Internet Explorer 6.0:\n\ta) In the web browser menu bar, select "Tools".\n\tb) Then select "Internet Options".\n\t\c) Then select the "Security" tab.\n\td) Then click the "Custom Level" button.\n\te) Find the option that says "Initialize and script ActiveX controls not marked as safe"\n\tf) Click "Enable".\n\t\g) Click OK all the way out, then click the "Print All Cases" button again.\n\nIf you have trouble with these instructions then please contact your administrator.');
		document.forms[0].selMultiCaseIDs.selectedIndex = 0;
		document.forms[0].hidShowPrintDialog.value = 'true';
		document.forms[0].hidGotoNextCase.value = 'true';
		document.forms[0].hidPrintNextCase.value = 'true';
		document.forms[0].submit();
	} else if(bGotoNextCase) {
		if(document.forms[0].selMultiCaseIDs.selectedIndex < (document.forms[0].selMultiCaseIDs.options.length-1)){
			document.forms[0].selMultiCaseIDs.selectedIndex += 1;
			document.forms[0].hidGotoNextCase.value = 'true';
			document.forms[0].hidPrintNextCase.value = 'true';
			setTimeout('document.forms[0].submit();',iPrintDialogTimeout)
		}else{
			alert('Printing Complete!');
		}
	}
}

function openPrintCaseWindow(sURL) {
	g_bCancelPleaseWait=true;
	launchWindow(sURL,770,500,'',true,false);
}

function relayMultiCaseIDs(){
	var frmPrintCases = document.forms[0];
	frmPrintCases.hidMultiCaseIDs.value = window.opener.document.forms[0].hidIDs.value;
	frmPrintCases.hidPrintCaseURL.value = window.opener.document.forms[0].hidRedirURL.value;
	frmPrintCases.submit();
}

function printSelectedCases(frm, sURL, iMultiPrintPurpose) {
	var iNumCasesSelected = 0;
	
	//clear out the form field
	frm.hidIDs.value = '';
	
	//Show the please wait message for long selections...
	pleaseWait_Now();
	
	if (ConfirmLongSelection(frm.elements.length, 'printing')){
		//set the IDs into the form field
		for (i=0;i<frm.elements.length;i++) {
			if (frm.elements[i].type == 'checkbox'){
				if (frm.elements[i].checked == true){
					var sID = frm.elements[i].name.substring(frm.elements[i].name.indexOf('Fld')+3);
					frm.hidIDs.value = frm.hidIDs.value + ',' + sID;
					iNumCasesSelected++
					if (iNumCasesSelected > 1000){
						alert('Due to system limitations you can not print more than 1000 Cases at a time.  Please decrease the number of Cases you\'re trying to print to 400 or less.');
						hidePleaseWait();
						return;
					}
				}
			}
		}
		//clip the first character (,) off
		if (frm.hidIDs.value.length > 0){
			frm.hidIDs.value =  frm.hidIDs.value.substring(1);
		}
		
		if (frm.hidIDs.value.length > 0){
			//Open the print cases window named 'ChildWindow'
			//sURL = sURL + '&MultiPrintPurpose=' + iMultiPrintPurpose
			//sURL = sURL + '&MultiIDs=' + frm.hidIDs.value + '&MultiPrintPurpose=' + iMultiPrintPurpose
			frm.hidRedirURL.value = sURL + '&MultiPrintPurpose=' + iMultiPrintPurpose;
			openPrintCaseWindow('PrintCases_Multi.aspx');
		}else{
			alert('You have not selected any cases to Print... You must select at least one Case.');
		}
	}
	
	//Hide the please wait message...
	hidePleaseWait();
}

function emailSelectedCases(frm, sAllowableStatuses) {
	var iNumCasesToEmail = 0;
	//clear out the form field
	frm.hidIDs.value = '';
	
	//Show the please wait message for long selections...
	pleaseWait_Now();
	
	if (ConfirmLongSelection(frm.elements.length, 'emailing')){
		//set the IDs into the form field
		for (i=0;i<frm.elements.length;i++) {
			if (frm.elements[i].type == 'checkbox'){
				if (frm.elements[i].checked == true){
					iNumCasesToEmail++;
					var sID = frm.elements[i].name.substring(frm.elements[i].name.indexOf('Fld')+3);
					var sCaseStatus = eval('frm.hidCaseStatusFld' + sID + '.value;');
					if (sAllowableStatuses.indexOf(sCaseStatus) > -1){
						frm.hidIDs.value = frm.hidIDs.value + ',' + sID;
					} else {
						alert('You can not email Cases with a status of: ' + sCaseStatus + '. Please change the Case Status to either ' + sAllowableStatuses + ' AND CLICK SAVE then you can email.\n\nThe # ' + iNumCasesToEmail + ' checked Case has a Current Status of : ' + sCaseStatus + '.');
						frm.hidIDs.value = '';
						return false;
					}
				}
			}
		}
		//clip the first character (,) off
		if (frm.hidIDs.value.length > 0){
			frm.hidIDs.value =  frm.hidIDs.value.substring(1);
		}
		
		if (!(frm.hidIDs.value.length > 0)){
			alert('You have not selected any cases to Email... You must select at least one Case.');
		}
	}
	
	//Hide the please wait message...
	hidePleaseWait();
}

function emailAllocatorCases(frm) {
	var iNumCasesToEmail = 0;
	//clear out the form field
	frm.hidIDs.value = '';
	
	//Show the please wait message for long selections...
	pleaseWait_Now();
	
	if (ConfirmLongSelection(frm.elements.length, 'emailing')){		
		//set the IDs into the form field
		for (i=0;i<frm.elements.length;i++) {
			if (frm.elements[i].type == 'checkbox'){
				if (frm.elements[i].checked == true){
					iNumCasesToEmail++;
					var sID = frm.elements[i].name.substring(frm.elements[i].name.indexOf('Fld')+3);
						frm.hidIDs.value = frm.hidIDs.value + ',' + sID;
				}
			}
		}
		//clip the first character (,) off
		if (frm.hidIDs.value.length > 0){
			frm.hidIDs.value =  frm.hidIDs.value.substring(1);
		}
		
		if (!(frm.hidIDs.value.length > 0)){
			alert('You have not selected any cases to Email... You must select at least one Case.');
		}
	}
	
	//Hide the please wait message...
	hidePleaseWait();
}


function openChildWindow(sURL) {
	launchWindow(sURL,500,400,'ChildWindow',true,false);
}

function openDonationWindow(sURL) {
	launchWindow(sURL,755,325,'DonationWindow',true,false);
}

function printReport(sAction) {
	if (sAction == 'print') {
		printThenCloseThisWindow();
	}
}

function printWindow(bPrompt, bShowPrintDialog){
	//{ if ((navigator.appName == "Netscape")) { window.print() ; } else { var 
	//WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 
	//CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>'; 
	//document.body.insertAdjacentHTML('beforeEnd', WebBrowser); WebBrowser1.ExecWB(6, 
	//-1); WebBrowser1.outerHTML = ""; } }

	if (bPrompt){
		if(bShowPrintDialog || navigator.appName == "Netscape") {
			window.print();
		} else {
			var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH="0" HEIGHT="0" CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
			document.body.insertAdjacentHTML('beforeEnd', WebBrowser); 
			WebBrowser1.ExecWB(6, -1); 
			WebBrowser1.outerHTML = ""; 
		}
	}
}

function closePrintWindow(bPrompt){
	if (bPrompt && confirm('Would you like to close this case profile?\n\nClick OK to close this window, Cancel will leave this window open.')) {
		window.opener.document.forms[0].submit();
		window.opener.focus();
		window.close();
	}
}

function closeDonationWindow(bPrompt, bForce){
	if (bForce || (bPrompt && confirm('Would you like to close this window?\n\nClick OK to close this window, Cancel will leave this window open.'))){
		window.opener.document.forms[0].submit();
		window.opener.focus();
		window.close();
	}
}

function printThenCloseThisWindow(){
	if (navigator.appName == 'Microsoft Internet Explorer') {
		window.opener.focus();
		window.print();
		window.close();
	}
}

function pleaseWait(){
	var top = screen.height / 2 - 150, left = screen.width / 2 - 275;
	//getStyleObject('tbl100NeediestBody').visibility = 'hidden';
	setTimeout('showPleaseWait('+top+','+left+',false)',500);
	return true;
}

function pleaseWait_Now(){
	var top = screen.height / 2, left = screen.width / 2 - 300;
	showPleaseWait(top, left, false);
	return true;
}

function pleaseWait_Pane(){
	var top = 70, left = 150;
	
	//getStyleObject('tbl100NeediestBody').visibility = 'hidden';
	setTimeout('showPleaseWait('+top+','+left+',true)',500);
	return true;
}

function showPleaseWait(top, left, bCancelPleaseWait){
	//alert(g_bCancelPleaseWait);
	if (g_bCancelPleaseWait == bCancelPleaseWait) {
		getStyleObject('divPleaseWait').top = top;
		getStyleObject('divPleaseWait').left = left;
		getStyleObject('divPleaseWait').visibility = 'visible';
	}
	//Reset the CancelPleaseWait flag so that it only gets used when explicitly called and not as some sort of 'cached-page-state'!!!
	g_bCancelPleaseWait = false;
}

function hidePleaseWait(){
	getStyleObject('divPleaseWait').visibility = 'hidden';
}

function SelectAllOptions(frm, btn){
	//Show the please wait message for long selections...
	pleaseWait_Now();
		
	if (ConfirmLongSelection(frm.elements.length, 'select')){
		//Start looping through the form elements...
		if (!g_bSelectedAllOptions) {
			for (i=0;i<frm.elements.length;i++) {
				if (frm.elements[i].type == 'checkbox'){
					frm.elements[i].checked = true;
				}
			}
			g_bSelectedAllOptions = true;
			btn.value = 'Un-Select All';
		}else{
			for (i=0;i<frm.elements.length;i++) {
				if (frm.elements[i].type == 'checkbox'){
					frm.elements[i].checked = false;
				}
			}
			g_bSelectedAllOptions = false;
			btn.value = 'Select All';
		}
	}
		
	//Hide the please wait message...
	hidePleaseWait();
}

function ConfirmLongSelection (iNumOfElements, sAction){
	if (iNumOfElements != null && iNumOfElements > 0){
		var iApproxNumCases = Math.round(iNumOfElements / 5.05);
		
		if (iApproxNumCases > 400){
			return confirm('Because there are so many Cases on this page (more than ' + iApproxNumCases + ' Cases) ' + sAction + ' these Cases could take a long time... Anywhere from 1 to 5 minutes.  Are you sure want to do continue?  Click \'OK\' if you want to continue otherwise click \'Cancel\' to stop.');
		}
	}
	return true;
}


//Menu Functions
function expandMenu(mnuItem)
{
	g_sCurrentMenu = mnuItem;
		
	if((g_sFormerMenu.length>0) && (g_sCurrentMenu!=g_sFormerMenu)) {
		clearTimeout(g_iTimeoutID);
		g_iTimeoutID = 0;
		collapse(g_sFormerMenu);
	}
	
	g_bEnableCollapse = false;
	
	var MenuObject = getStyleObject(mnuItem+'MnuBody');
	MenuObject.display = 'inline';
	MenuObject.visibility = 'visible';
	
	g_sFormerMenu = mnuItem;
	
	return true;
}

function collapseMenu(mnuItem)
{
	g_iTimeoutID = setTimeout("collapse('"+mnuItem+"')",500);
}

function collapse(mnuItem)
{
	//fires after waiting a half second to see if the dropdown menu is being looked at
	if (g_bEnableCollapse)
	{
		var MenuObject = getStyleObject(mnuItem+'MnuBody');
		MenuObject.visibility = 'hidden';
		MenuObject.display = 'none';
		g_sFormerMenu = '';
	} else {
		clearTimeout(g_iTimeoutID);
	}
}

function disableMenuClose()
{
	g_bEnableCollapse = false;
}

function enableMenuClose()
{
	g_bEnableCollapse = true;
}