

// Gets the browser specific XmlHttpRequest Object
// AJAX
function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest(); //Not IE
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP"); //IE
	} else {
		//Display your error message here. 
		alert("Your browser doesn't support the XmlHttpRequest object.  Better upgrade to IE 6 or better.");
	}
}			




// Remove white space from front of string
function trimLeft(sString) 
{
    // Spaces
    while (sString.substring(0,1) == ' ') { 
        sString = sString.substring(1, sString.length);
    }
    
    // Tabs
    while (sString.substring(0,1) == '	') { 
        sString = sString.substring(1, sString.length);
    }

    return sString;
}

// Remove white space from end of string
function trimRight(sString) 
{
    // Spaces
    while (sString.substring(sString.length-1, sString.length) == ' ') {
        sString = sString.substring(0,sString.length-1);
    }

    // Tabs
    while (sString.substring(sString.length-1, sString.length) == '	') {
        sString = sString.substring(0,sString.length-1);
    }

    return sString;
}

// Remove white space from front and back end of string
function trim(sString) 
{
    sString = trimLeft(sString);
    return trimRight(sString);
}


// Return the file name off the end of a file path
// e.g. 'c:\_temp\stuff.txt' would return 'stuff.txt'
//
function getFileName(sFilePath)
{
    // replace back slashs with forward slashs
    var sNormalizedPath = sFilePath.replace(/\\/g, '/'); // text to replace goes between the /'s.  The /g means global replace.
    var nLastSlash = sNormalizedPath.lastIndexOf('/');
    var sFileName = sNormalizedPath.slice(nLastSlash + 1, sNormalizedPath.length);
    return sFileName;
    
}

// Return the file name prefix for a file name.
// E.g. 'document.txt' would return 'document'
// NOTE: The file name must have a dot extension.
//
function getFileNamePrefix(sFileName)
{
    var nDotIndex = sFileName.lastIndexOf('.');
    if (nDotIndex == -1)
        return sFileName;
        
    var sFileNamePrefix = sFileName.slice(0, nDotIndex);
    return sFileNamePrefix;
}


// Add this filter to the ONKEYPRESS in a text input control to filter out apostrophies.
// E.g.	<input type="text" ONKEYPRESS="SQL_Filter(event);"></input>
function SQL_Filter(theEvent)
{
	//alert("theEvent.keyCode=" + theEvent.keyCode);
	if (theEvent.keyCode == 39) // 39='
	  theEvent.returnValue = false;
}



// Filter out any chars that are not between 0 and 9 or a minus '-' or a decimal point '.'
// NOTE: Currently the minus can go anywhere and there can be more than one - or decimal
// E.g.	<input type="text" ONKEYPRESS="FloatFilter(event);"></input>
function FloatFilter(theEvent,theControl)
{
	if (
	     (
	       (theEvent.keyCode < 48) || // 48='0'
	       (theEvent.keyCode > 57)
	      ) && // 57='9'
	      (theEvent.keyCode != 45) && // not 45='-'
	      (theEvent.keyCode != 46) // 46='.'
	    )
	    {
	      theEvent.returnValue = false;
	    }
}

// Filter out any chars that are not between characters, numbers or '
//
function SiteNameFilter(theEvent,theControl)
{
	if (theEvent.keyCode == 34) // 34 is double quotes.  We don't allow that in the site names currently.
    {
        theEvent.returnValue = false;
    }
}

// Filter out anything that is not a letter.
//
function AccountIDFilter(theEvent,theControl)
{
    return AlphaNumericFilter(theEvent, theControl);
}


// Filter out anything that is not a letter or number
//
function AlphaNumericFilter(theEvent,theControl)
{
    var bIsValid = false;

	if ((theEvent.keyCode >=48) && (theEvent.keyCode <=57))  // 0-9
        bIsValid = true;
	if ((theEvent.keyCode >=65) && (theEvent.keyCode <=90))  // A-Z
        bIsValid = true;
	if ((theEvent.keyCode >=97) && (theEvent.keyCode <=122)) // a-z
        bIsValid = true;

    if (bIsValid != true)
        theEvent.returnValue = false;
}




// Open TinyEdit edit window so the user can enter new content.
//
//  The EditContentForm is defined in MainTemplate.jsp (or needs to be defined somewhere if not there.)
//
function OnEditContent(sContentCode, sDisplayName, sEditorType, sAccountID)
{
    var editWindow = window.open("","EditContentWindow","directories=no,status=no,location=no,menubar=yes,toolbar=no,resizable=yes,height=640,width=850");
    editWindow.focus();

    // Set the values to submit to the EditContentWindow
    //
    document.EditContentForm.target = "EditContentWindow";
    document.EditContentForm.content_code.value = sContentCode;
    document.EditContentForm.account_id.value   = sAccountID;
    document.EditContentForm.display_name.value = sDisplayName;
    document.EditContentForm.editor_type.value  = sEditorType;
    document.EditContentForm.submit();
}


// vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv Prompt Function vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
// Replaces the javascript prompt() dialog with a better looking HTML prompt.
// This function will call a callback function specified in the first parameter of the call.
// The callback function will be passed the string entered by the user.
// IMPORTANT NOTE: The callback function will need to 'unescape' the string that gets passed back.
//
//  sCallbackFunctionName    - The name of the function to call after the user inputs a string and clicks OK.
//  sPromptText              - The prompt to display to the user.
//  sDialogTitle (optional)  - The title for the prompt dialog.
//  sDefaultValue (optional) - Default text value
//  sOKButtonLabel (optional) - Label for the OK button.  Will be 'OK' by default
//  sCancelButtonLabel (optional) - Label for the Cancel button.  Will be 'Cancel' by default.
//
var gPromptWindow = null;
function HtmlPrompt(sCallbackFunctionName, sPromptText, sDialogTitle, sDefaultValue, sOKButtonLabel, sCancelButtonLabel)
{
    if (sOKButtonLabel == null)
        sOKButtonLabel = "OK";
    if (sCancelButtonLabel == null)
        sCancelButtonLabel = "Cancel";
    if (sDefaultValue == null)
        sDefaultValue = "";
    if (sDialogTitle == null)
        sDialogTitle = "";

    // If there is already an HtmlPrompt window open then close it.
    if((gPromptWindow != null) && (!gPromptWindow.closed))
        gPromptWindow.close();

    gPromptWindow = window.open("","PromptWindow","location=no,menubar=no,toolbar=no,resizable=yes,height=250,width=600");
    gPromptWindow.focus();
    gPromptWindow.target="HTMLPrompt.jsp";

    gPromptWindow.document.writeln('<HTML><HEAD>');

    gPromptWindow.document.writeln('<SCRIPT language="Javascript" src="ChurchSiteJavaScript.js"></SCRIPT>');

    gPromptWindow.document.writeln('<SCRIPT language="Javascript">');
    gPromptWindow.document.writeln('function SetFocus() {');
    gPromptWindow.document.writeln('    document.MainForm.htmlPromptInputText.focus();');
    gPromptWindow.document.writeln('}');
    gPromptWindow.document.writeln('</SCRIPT>');

    gPromptWindow.document.writeln('<LINK REL="StyleSheet" HREF="DefaultLook.css" TYPE="text/css" TITLE="" MEDIA="screen, print">');
    gPromptWindow.document.writeln('<title>' + sDialogTitle + '</title>');
    gPromptWindow.document.writeln('</HEAD>');
    gPromptWindow.document.writeln('<BODY>');
    gPromptWindow.document.writeln('<form name="MainForm" onSubmit="OnHtmlPromptOK();return false;">');
    gPromptWindow.document.writeln('<center>');
    gPromptWindow.document.writeln('<h3>' + sDialogTitle + '</h3>');
    gPromptWindow.document.writeln(sPromptText + '<p/>');
    gPromptWindow.document.writeln('<input type="text" name="htmlPromptInputText" size="60" value="' + sDefaultValue + '"></input><p>');
    gPromptWindow.document.writeln('<INPUT TYPE="submit" VALUE="'+sOKButtonLabel+'">'); // Will call OnHtmlPromptOK()
    gPromptWindow.document.writeln('<INPUT TYPE="button" VALUE="'+sCancelButtonLabel+'" onClick="window.close();">');
    gPromptWindow.document.writeln('<INPUT TYPE="hidden" NAME="CallBackFunctionName" VALUE="'+sCallbackFunctionName+'">');
    gPromptWindow.document.writeln('</center>');
    gPromptWindow.document.writeln('</form>');
    gPromptWindow.document.writeln('</BODY></HTML>');
//    gPromptWindow.document.MainForm.htmlPromptInputText.focus();
//    gPromptWindow.setTimeout('SetFocus()', 1000);
//    gPromptWindow.SetFocus();

}
function OnHtmlPromptOK() // This is part of the HtmlPrompt functionality.
{
    var sCallbackFunctionName = document.MainForm.CallBackFunctionName.value;
    var sInputText = document.MainForm.htmlPromptInputText.value;
    eval("window.opener." + sCallbackFunctionName + "('" + escape(sInputText) + "');"); // Must escape input for &\ etc.
    window.close();
}
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


function OpenHelpWindow(sURL)
{
	window.open(sURL, "_blank", "directories=0,location=0,menubar=0,resizable=1,scrollbars=0,status=0,titlebar=0,toolbar=0");
}




// vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
// vvvv                          MODAL DIALOG FUNCTIONS                            vvvv
// vvvv           Prototype.js and Scriptaculous are required for these            vvvv

///////////////////////////////////////////
// Get the top Z index on the page.
//
function GetTopZIndex()
{
	var topZIndex = 0;
	var currentZIndex = undefined;
	
	// for each element in the document
	$$('*').each(function(currentElement) { 
	  currentZIndex = currentElement.getStyle('zIndex');
	  if (currentZIndex != undefined) {
	    if (currentZIndex > topZIndex)
	      topZIndex = currentZIndex;
	  }
	});
	return topZIndex;
}

////////////////////////////////////////////
// Disable the page by creating a div over it
//
function DisablePage()
{
	// Create DisablePageDiv is it doesn't exist
	var oDisablePageDiv = $('DisablePageDiv');
	var newZIndex = GetTopZIndex() + 1;

	if (oDisablePageDiv == undefined) {
		// Create the DisablePageDiv
		oDisablePageDiv = new Element("div", {id:'DisablePageDiv'});
		oDisablePageDiv.setStyle({zIndex: newZIndex, opacity:0,  position:'absolute', top:'0%', left:'0%', width:'100%', height:'100%', background:'#000'});
		var oBodyTag = $$('body')[0];
		oBodyTag.insert(oDisablePageDiv);
	} else {
		// Re-initialize DisablePageDiv
		oDisablePageDiv.setStyle({zIndex: newZIndex, opacity:0});
	    oDisablePageDiv.show();
	}
	
	new Effect.Opacity('DisablePageDiv', {duration:0.2, from:0, to: 0.2});
}

//////////////////////////////////////////////////////////////////
// Restore the page and enable the controls.
// SEE DisablePage() above
//
function EnablePage()
{
	if (!$("DisablePageDiv"))
		return;
		
    $("DisablePageDiv").hide();
    $("DisablePageDiv").setStyle({opacity:0});
}

function InitializeModalDialog(modalDialogDivID)
{
	// Initialize Modal Dialog Divs
    $(modalDialogDivID).setStyle({opacity:0});
    $(modalDialogDivID).hide();
    new Draggable(modalDialogDivID);
}


/////////////////////////////////////////////////////////////////
// Display the specified dialog (pass it the div ID for the dialog)
function ShowModalDialog(dialogDivID)
{
    DisablePage();
    var newZIndex = GetTopZIndex() + 1;
	var oDialogDiv = $(dialogDivID);
    oDialogDiv.setStyle({zIndex: newZIndex, opacity:0, top:'50%', left:'50%', width:'600px', marginLeft:'-300px', marginTop:'-50px'});
    oDialogDiv.show();
    new Effect.Opacity(dialogDivID, {duration:0.2, from:0, to: 1});
    new Draggable(dialogDivID);
}


//////////////////////////////////////////////////////////////////
// Cancel the specified dialog by hiding it.
function CancelModalDialog(dialogDivID)
{
    $(dialogDivID).hide();
    EnablePage();
}


// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ MODAL DIALOG FUNCTIONS ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


