// +------------------------------------------------------------+
// | Created 011/04/2003              Last Modified 03/23/2007  |
// | Web Site:                 http://www.AmericanCatholic.org  |
// +------------------------------------------------------------+
// | Contains all the scripts for the ACO.                      |
// | Less clutter on pages				                        |
// |                                                            |
// |                            								|
// +------------------------------------------------------------+
/*-- When Google AdSense was added in late September 2003 the Remove Frames function interfered with it.
 All the global JavaScripts that were contained in ACO_JavaScript.inc 
  reside here for the 20 or so Google AdSense pages.*/


// THIS FUNCTION DOESN"T WORK WITH GOOGLE ADSENSE 
/* <SCRIPT LANGUAGE="JavaScript">
<!-- Javascript to keep site out of frames
setTimeout ("RemoveFrames()", 5000);
function RemoveFrames() {
  if (parent.frames.length) {
    top.location.href= document.location;	}
  }
 End Commented JavaScript -->
</script>
*/


// "SUBSCRIBE TO E-NEWSLETTERS POPUP" Script begins
<!--
var eSubscribe;
var eURL;
var winWidth;
var winHeight;
var options;

function openSubscribe(eURL) {
winWidth = 752;
winHeight = 209;
options = "width=" + winWidth + ",height=" + winHeight + ",scrollbars=no,resizable=yes,menubar=no,status=no,toolbar=no,location=no,directories=no";
 if (eURL) {  
  if (!eSubscribe || eSubscribe.closed){
    eSubscribe = window.open(eURL,'eSubscribewin', options);  // Open a new window and show the specified page
    eSubscribe.focus();
	eSubscribe.moveTo(10,40);   
   									  }
  else{
    eSubscribe.close();
	eSubscribe = window.open(eURL,'eSubscribewin', options);  // Open a new window and show the specified page
    eSubscribe.focus();
	eSubscribe.moveTo(10,40);
  	  }
 	    }
}

// Script Ends -->
// +------------------------------------------------------------+

 
// +------------------------------------------------------------+
// "openEmailForm" Script begins
var objEmailForm;
var fmURL;
var fmWinWidth;
var fmWinHeight;
var fmOptions;

function openEmailForm(fmURL) {
fmWinWidth = 440;
fmWinHeight = 520;
var fmOptions = "width=" + fmWinWidth + ",height=" + fmWinHeight + ",scrollbars=no,resizable=yes,menubar=no,status=no,toolbar=no,location=no,directories=no";
 if (fmURL) {  
  if (!objEmailForm || objEmailForm.closed){
    objEmailForm = window.open(fmURL,'EmailFormwin', fmOptions);  // Open a new window and show the specified page
    objEmailForm.focus();
	objEmailForm.moveTo(140,250);   
   									  }
  else{
    objEmailForm.close();
	objEmailForm = window.open(fmURL,'EmailFormwin', fmOptions);  // Open a new window and show the specified page
    objEmailForm.focus();
	objEmailForm.moveTo(140,250);
  	  }
 	    }
}

// Script Ends -->
// +------------------------------------------------------------+

//CHECKS EmailForm FORM starts the checking of the form data
function CheckEmailForm(objForm)
{
	if (IsEmailFormComplete("EmailForm") == true)
		return true;
    else
		return false;
}
//**************END FUNCTION****************************


/********Validates Email Form********************************
DESCRIPTION: IS FORM COMPLETE? Loops through all the form elements validating data..

PARAMETERS:
   FormName - form name 
   ElemName - form element name
   FormOk - boolean 

RETURNS:
   True if valid, otherwise false.
*************************************************/

function IsEmailFormComplete(FormName)
{
var x = 0;
var FormOk = true;

while ((x < document.forms[FormName].elements.length) && (FormOk))
   {  //if NO value in text box, and the form element isn't required or hidden element
      //&& (document.forms[FormName].elements[x].name!='expire_month') && (document.forms[FormName].elements[x].name!='expire_year')
     if ((document.forms[FormName].elements[x].value == '') && (document.forms[FormName].elements[x].name!='emailto') && (document.forms[FormName].elements[x].name!='CAPTCHABox') && (document.forms[FormName].elements[x].name!='form_action')) 
     {
        if (document.forms[FormName].elements[x].name == 'emailfrom')
		{
				alert('Please type your e-mail address and then try again.')
		}
        if (document.forms[FormName].elements[x].name == 'emailfrom_confirm')
		{
				alert('Please type your e-mail confirmation address and then try again.')
		}
        if (document.forms[FormName].elements[x].name == 'subject')
		{
				alert('Please type your subject and then try again.')
		}
        if (document.forms[FormName].elements[x].name == 'message')
		{
				alert('Please type your message and then try again.')
		}
        document.forms[FormName].elements[x].focus();
        FormOk = false;
     }
	 else // the field is not empty but needs further checking -------------------------------
	 {
		if (document.forms[FormName].elements[x].name == 'emailfrom')
		{
		        FormOk = IsEmailValid(FormName,document.forms[FormName].elements[x].name)
		}
		if (document.forms[FormName].elements[x].name == 'emailfrom_confirm')
		{        
			if (document.forms[FormName].emailfrom.value != document.forms[FormName].emailfrom_confirm.value)
				{
					alert('The e-mail addresses don\'t match! Please try again.');
					 FormOk = false;
				} else {
					 FormOk = true;
				}
		}
}
     x ++
   }
return FormOk
}
//**************END FUNCTION****************************



//CHECKS FeedbackForm FORM starts the checking of the form data
function CheckFeedbackForm(objForm)
{
	if (validateFeedbackForm("FeedbackForm") == true)
		return true;
    else
		return false;
}
//**************END FUNCTION****************************


/********Validates Feedback Form********************************
DESCRIPTION: IS FORM COMPLETE? 
Is there a comment and a valid e-maill address on the feedback <form>
	/Feedback/default.asp.

PARAMETERS:
   FormName - form name 
   ElemName - form element name

RETURNS:
   True if valid, otherwise false.
*************************************************/
function validateFeedbackForm(FormName) {
		var FeedbackFormOK = false;
		
		if (document.forms[0].elements['feedback'].value.length<1)
		        {
				alert("You forgot to add your comment.");
				}
		else if ((document.forms[0].elements['email'].value.length<1))
		        {
				alert('Please type your e-mail address and then try again.');
				}
				//IsEmailValid(FormName,document.forms[FormName].elements[x].name)
		else if ((IsEmailValid('FeedbackForm', document.forms[0].elements['email'].name)==false))
		        {
				 FeedbackFormOK = true;
				}
		//else 
			//    {
				//document.forms[0].submit();
			//    }
}
//**************END FUNCTION****************************


/******** Counts characters in a textbox ********************************
DESCRIPTION: Counts textbox characters for length. Truncates to correct user-defined max limit.
e.g.,
onKeyDown="textCounter(this.form.feedback,this.form.remainder,400);" onKeyUp="textCounter(this.form.feedback,this.form.remainder,400);
<input type="HIDDEN" name="remainder" value="400">

PARAMETERS:
   textarea - form's textarea element name 
   countfield - remainder defined in a HIDDEN form element
   maxlimit - maximum number of characters

RETURNS:
   If valid no alert box, otherwise truncates user-defined max limit.
*************************************************/
function textCounter(textarea, countfield, maxlimit) {
	    if (textarea.value.length > maxlimit)         // if too long...trim it!	
		 {
		 textarea.value = textarea.value.substring(0, maxlimit);
		 alert('You have input too many characters into the text box! \nThere is a maximum of '+(maxlimit)+' characters. Please, try again.');
        }
		else 		// otherwise, update 'characters left' counter
		{
		countfield.value = maxlimit - textarea.value.length;
	    }
	}
//**************END FUNCTION****************************



/******** Validates Length and IsNumeric of other_textbox ********************************
DESCRIPTION: Validates other_textbox textbox for length and custom IsNumber() function below
			Has to be a number between 10 and 1000.
PARAMETERS:
   FormName - form name 
   ElemName - form element name


RETURNS:
   True if valid, otherwise false.
*************************************************/
function ValidNumericLength(FormName,ElemName) {
var iTextBox = document.forms[FormName].elements[ElemName].value; //value of other_textbox text box
if (isNumber(iTextBox)==false)
  {
    alert("Error... not a number! \nPlease type in a donation amount between 1 and 1000 dollars. \nJust the number with no dollar sign, please.");
    return false;
  }
else
  {
	  if (parseInt(iTextBox) < 1 || parseInt(iTextBox) > 1000)
	  {
		alert("Please type in a donation amount between 1 and 1000 dollars. \nJust a whole number with no dollar sign, please.");
        document.forms[FormName].elements[ElemName].value=parseInt(iTextBox); //(Sender or Recipient) e-mail text box excised of all spaces
		return false;
	  }
  }
  
  return true;
}
//**************END FUNCTION****************************


//************ TRIM FUNCTIONS **************************
// Description: Trim functions trim strings of spaces
//     on (1)both ends, (2)left, (3)right and (4)All spaces 
//     whether on the ends or between characters in the string. 
//     
// Returns:String
//
// Trim(i_value);
function Trim(String) {
 if (String == null) {
    return (false);
   }
    return String.replace(/(^\s+)|(\s+$)/g,"");
   }
// TrimAllDashes(i_value);
function TrimAllDashes(str) {
 if (str == null) {
    return (false);
   }
    return str.replace(/(\-+)/g,"");
   }

// TrimAll(i_value);
function TrimAll(str) {
 if (str == null) {
    return (false);
   }
    return str.replace(/(\s+)/g,"");
   }

// TrimLeft(i_value);
function trimLeft(str) {
	// remove all spaces on left
	var newstr = str.replace(/[ ]*/,"");
	return (newstr);
}

// TrimRight(i_value);
function trimRight(str) 
{
	// remove all spaces on right
	var newstr = str.replace(/[ ]*$/,"");
	return (newstr);
}

// TrimBOTH(i_value);   !!!// Trim(i_value); above is better!!!
function trimBoth(str) {
	// reomve all spaces on both side
	var newstr = trimRight(trimLeft(str));
	return (newstr);
}
//**************END FUNCTIONS****************************


/**************** EMAIL VALIDATION FUNCTION *************
DESCRIPTION: validates email

TECH NOTES:
-----------------------------------------------------------------------------
These "regexpFilter" variables use the RegExp object and its functions to find patterns
created with a sequence of characters and symbols
var regexpFilter = 
				//   /............/ creates a new RegExp object
				// ^ Start of a string. 
			 	// ^.  -Match Any character at beginning of string
				// $ End of a string. 
				// . Any character (except \n newline) 
				// | Alternation. 
				// {...} Explicit quantifier notation. 
            	// .{2,4}$  -At least two characters but no more than four at the end of the string
				// [...] Explicit set of characters to match. 
				// (...) Logical grouping of part of an expression. 
				// * 0 or more of previous expression. 
				// + 1 or more of previous expression. 
				// ? 0 or 1 of previous expression; also forces minimal matching when an expression might match several strings within a search string. 
				// \ Preceding one of the above, it makes it a literal instead of a special character. Preceding a special matching character, see below. 
            	// \.  -a period (.)
				// . Matches any character except \n. If modified by the Singleline option, a period character matches any character. For more information, see Regular Expression Options. 
				// [aeiou] Matches any single character included in the specified set of characters. 
				// [^aeiou] Matches any single character not in the specified set of characters. 
				// [0-9a-fA-F] Use of a hyphen (–) allows specification of contiguous character ranges. 
				// \w Matches any word character. Equivalent to the Unicode character categories [\p{Ll}\p{Lu}\p{Lt}\p{Lo}\p{Nd}\p{Pc}]. If ECMAScript-compliant behavior is specified with the ECMAScript option, \w is equivalent to [a-zA-Z_0-9]. 
				// \W Matches any nonword character. Equivalent to the Unicode categories [^\p{Ll}\p{Lu}\p{Lt}\p{Lo}\p{Nd}\p{Pc}]. If ECMAScript-compliant behavior is specified with the ECMAScript option, \W is equivalent to [^a-zA-Z_0-9]. 
				// \s Matches any white-space character. Equivalent to the Unicode character categories [\f\n\r\t\v\x85\p{Z}]. If ECMAScript-compliant behavior is specified with the ECMAScript option, \s is equivalent to [ \f\n\r\t\v]. 
				// \S Matches any non-white-space character. Equivalent to the Unicode character categories [^\f\n\r\t\v\x85\p{Z}]. If ECMAScript-compliant behavior is specified with the ECMAScript option, \S is equivalent to [^ \f\n\r\t\v]. 
				// \d Matches any decimal digit. Equivalent to \p{Nd} for Unicode and [0-9] for non-Unicode, ECMAScript behavior. 
				// \D Matches any nondigit. Equivalent to \P{Nd} for Unicode and [^0-9] for non-Unicode, ECMAScript behavior. 
-----------------------------------------------------------------------------
PARAMETERS: FormName, ElemName

RETURNS: EmailOk
   True if valid, otherwise false.
*************************************************/
function IsEmailValid(FormName,ElemName)
{
var EmailOk  = true;
var strTextBox = document.forms[FormName].elements[ElemName].value; //value of Sender or Recipient text box

var regexpFilter = /^([a-zA-Z0-9_\+\&\-])+(\.([a-zA-Z0-9_\+\&\-])+)*@((\[(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5]))\]))|((([a-zA-Z0-9])+(([\-])+([a-zA-Z0-9])+)*\.)+([a-zA-Z])+(([\-])+([a-zA-Z0-9])+)*))$/;
				// modified from http://regexlib.com/RETester.aspx?regexp_id=284
				
strTextBox=TrimAll(strTextBox); //value of Sender e-mail or Recipient e-mail text box excised of all spaces
document.forms[FormName].elements[ElemName].value=strTextBox; //(Sender or Recipient) e-mail text box excised of all spaces
//strTextBox=dropSpaces(strTextBox)
// Checks for the general format of e-mail addresses (x@x.xx or x@x.xxx)
if (!regexpFilter.test(strTextBox)) 
	{
      alert('Please enter a valid e-mail address. \n\nExample: stanthony@franciscanmedia.org')
      EmailOk = false;
	}
return (EmailOk);
}
//**************END FUNCTION****************************


/********   MACROMEDIA  functions   ********************************/
<!--
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_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_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];}
}



// +------------------------------------------------------------+
        //NS resize bug fix
         if(!window.saveInnerWidth) {
       window.onresize = resizeIt;
       window.saveInnerWidth = window.innerWidth;
       window.saveInnerHeight = window.innerHeight;
     }
// +------------------------------------------------------------+
     function resizeIt() {
         if (saveInnerWidth < window.innerWidth || 
             saveInnerWidth > window.innerWidth || 
             saveInnerHeight > window.innerHeight || 
             saveInnerHeight < window.innerHeight ) 
         {
             window.history.go(0);
         }
     }
// +------------------------------------------------------------+

/********   END  functions   ********************************/
//-->


