<!-- 
// +------------------------------------------------------------+
// | Created 05/12/2005              Last Modified 03/15/2006   |
// | Web Site:                     http://www.Franciscan.org    |
// +------------------------------------------------------------+
// | Contains some of the scripts for the Province Web site;   	|
// | Other scripts are in donate.js, SJBPglobalNav.js and 	    |
// |  SJBPglobalNavSSL.js.                                      |
// | Less clutter on page    									|
// +------------------------------------------------------------+

// +------------------------------------------------------------+

/********init() FUNCTION********************************
DESCRIPTION:  use instead of <body onLoad="checkForUpgrade()"> 
              can put in multiple functions
PARAMETERS:
   
RETURNS:
*************************************************/
function init() {
 checkForUpgrade();
 
}

window.onload = init; 
//**************END FUNCTION****************************


/********checkForUpgrade FUNCTION********************************
DESCRIPTION:  checks browser type and version then redirects to an upgrade suggestion 
              page if old browser. checkForUpgrade() uses functions: CreateCookie, ReadCookie and GetLang

PARAMETERS:
   

RETURNS:
   -redirects to /upgrade.html, if necessary 
   -sets 30 day cookie, to not redirect, if decide to use site with old browser 
*************************************************/

function CreateCookie(name,value,days,useLang) 
{
  var langString = useLang ? GetLang() : '';

  var cookie = name + langString + '=' + value + ';';

  if (days) 
  {
    var date = new Date();
    var MilliSecondsInDay = 86400000; // 24*60*60*1000
    date.setTime(date.getTime()+(days*MilliSecondsInDay));
    cookie += ' expires=' + date.toGMTString() + ';';
  }
  cookie += ' path=/';

  document.cookie = cookie;
}

function ReadCookie(name, useLang) 
{
  var langString = useLang ? GetLang() : '';

  var nameEQ = name + langString + '=';
  var ca = document.cookie.split(';');

  //alert("ca[0]: " + ca[0]);

  for(var i = 0; i < ca.length; i++) 
  {
    var c = ca[i];
    while (c.charAt(0) == ' ') 
    {
      c = c.substring(1, c.length);
    }

    if (c.indexOf(nameEQ) == 0) 
    {
	  //alert(c);
      return c.substring(nameEQ.length,c.length);
    }
  }
  return null;
}

function GetLang()
{
  var langString = '';

  if (document.documentElement){
    langString = document.documentElement.lang;
    if (langString != ''){
      langString = '-' + langString;
    }
  }  
  return langString;
}

function checkForUpgrade()
{
  //rv is the revison version   
  var rvValue = -1;

  if (navigator.product == 'Gecko')
  {
    rvValue = 0;
    var ua      = navigator.userAgent.toLowerCase();
    var rvStart = ua.indexOf('rv:');
    var rvEnd   = ua.indexOf(')', rvStart);
    var rv      = ua.substring(rvStart+3, rvEnd);
    var rvParts = rv.split('.');
    var exp     = 1;

    for (var i = 0; i < rvParts.length; i++)
    {
      var val = parseInt(rvParts[i]);
      rvValue += val / exp;
      exp *= 100;
    }
  
  //alert(ua);

}

  if (!document.getElementById || ( rvValue >= 0 && rvValue < 1.0))
  {
    var updateMessageShown = ReadCookie('upgrade');
    //alert(updateMessageShown);

    if (!updateMessageShown)
    {
     //alert(rvValue);
      CreateCookie('upgrade','1', 30);
      // check if cookie written. If not, don't redirect
      if (ReadCookie('upgrade'))
      {
        document.location = '/upgrade.html';
      }
    }
  }
}
//**************END FUNCTION****************************


/********CheckForm FUNCTION********************************
DESCRIPTION:  CHECK FORM starts the checking of the form data
             

PARAMETERS:
   FormName - form name 
   <form name="BecomingAFriarInfo">...</form>

RETURNS:
   True if valid, otherwise false.
*************************************************/
function CheckForm(objForm)
{
	if (IsFormComplete("BecomingAFriarInfo") == true)
		return true;
	else
		return false;
}
//**************END FUNCTION****************************

/********Validates 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 IsFormComplete(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 Preview, DeliveryDate, failload (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!='comments')) 
     {
        if (document.forms[FormName].elements[x].name == 'first_name')
		{
				alert('Please type your first name and then try again.')
		}
        if (document.forms[FormName].elements[x].name == 'last_name')
		{
				alert('Please type your last name and then try again.')
		}
        if (document.forms[FormName].elements[x].name == 'address_one')
		{
				alert('Please type your street address and then try again.')
		}
        if (document.forms[FormName].elements[x].name == 'city')
		{
				alert('Please type your city and then try again.')
		}
        if (document.forms[FormName].elements[x].name == 'state_or_province')
		{
				alert('Please choose your state or province\-\-or INTERNATIONAL\-\-and then try again.')
		}
        if (document.forms[FormName].elements[x].name == 'postal_code')
		{
				alert('Please type your Zip\/Postal code and then try again.')
		}
        if (document.forms[FormName].elements[x].name == 'country')
		{
				alert('Please choose your country and then try again.')
		}
        if (document.forms[FormName].elements[x].name == 'phone')
		{
				alert('Please type the phone number and then try again.')
		}
        if (document.forms[FormName].elements[x].name == 'email')
		{
				alert('Please type your e-mail address and then try again.')
		}
        if (document.forms[FormName].elements[x].name == 'age')
		{
				alert('Please type your birth date and then try again.')
		}
        if (document.forms[FormName].elements[x].name == 'rRequestReason')
		{
				alert('Please type request reason 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 == 'email')
		{
		        FormOk = IsEmailValid(FormName,document.forms[FormName].elements[x].name)
		}
	 }
     x ++
   }
return FormOk
}
//**************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 FUNCTION****************************

/**************** 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@americancatholic.org')
      EmailOk = false;
	}
return (EmailOk);
}
//**************END FUNCTION****************************

//CHECK FORM starts the checking of the form data
function CheckCreateAccountForm(objForm)
{
	if (IsCreateAccountFormComplete("CreateAccount") == true)
		return true;
    else
		return false;
}
//**************END FUNCTION****************************
function IsCreateAccountFormComplete(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 username, password, verify_password, (hidden element)
     if ((document.forms[FormName].elements[x].value == '') && (document.forms[FormName].elements[x].name!='radioAccountType') && (document.forms[FormName].elements[x].name!='radioBillType') && (document.forms[FormName].elements[x].name!='form_action')) 
     {
        if (document.forms[FormName].elements[x].name == 'user_name')
		{
				alert('Please type your username and then try again.')
		}
        if (document.forms[FormName].elements[x].name == 'password')
		{
				alert('Please type your \"Password\" and then try again.')
		}
        if (document.forms[FormName].elements[x].name == 'verify_password')
		{
				alert('Please type your password again in \"Re-enter Password\" and then try again.')
		}
        if (document.forms[FormName].elements[x].name == 'bill_email')
		{
				alert('Please type your e-mail address and then try again.')
		}
        FormOk = false;
     }
	 else // the field is not empty but needs further checking -------------------------------
	 {
		if ((document.forms[FormName].elements['password'].value.length < 5))
		{
		        alert('Please enter a password that is 5 characters or longer.')
				FormOk = false;
		}
		if ((document.forms[FormName].elements['verify_password'].value.length < 5))
		{
		        alert('Please re-enter a password that is 5 characters or longer.')
				FormOk = false;
		}
		if (document.forms[FormName].elements['password'].value != document.forms[FormName].elements['verify_password'].value)
		{
		        alert('The passwords do not match.  Please re-enter passwords.')
				FormOk = false;
		}
		if (document.forms[FormName].elements[x].name == 'bill_email')
		{
		        FormOk = IsEmailValid(FormName,document.forms[FormName].elements[x].name)
		}
	 }
     x ++
   }
return FormOk
}
//**************END FUNCTION****************************


/********   END  functions   ********************************/
//-->

