// +------------------------------------------------------------+
// | Created 10/20/2004              Last Modified 04/11/2007   |
// | Web Site:                 http://ads.AmericanCatholic.org  |
// +------------------------------------------------------------+
// | Contains all the scripts for the ads site;	                |
// | Less clutter on page				                        |
// |                                                            |
// | 								                            |
// +------------------------------------------------------------+

// "View Ad Image" Script begins
<!--
var objAdImage;
var adURL;
var adWinWidth;
var adWinHeight;
var adOptions;


function viewAdImage(adURL,adWinWidth,adWinHeight) {
//adWinWidth = 420;
//adWinHeight = 580;
var adOptions = "width=" + adWinWidth + ",height=" + adWinHeight + ",scrollbars=no,resizable=yes,menubar=no,status=no,toolbar=no,location=no,directories=no";
 if (adURL) {  
  if (!objAdImage || objAdImage.closed){
    objAdImage = window.open(adURL,'AdImagewin', adOptions);  // Open a new window and show the specified page
    objAdImage.focus();
	objAdImage.moveTo(400,40);   
   									  }
  else{
    objAdImage.close();
	objAdImage = window.open(adURL,'AdImagewin', adOptions);  // Open a new window and show the specified page
    objAdImage.focus();
	objAdImage.moveTo(400,40);
  	  }
 	    }
}
// Script Ends -->
// +------------------------------------------------------------+
//CHECK FORM starts the checking of the form data
function CheckForm(objForm)
{
	if (IsFormComplete("Advertisers") == 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')&& (document.forms[FormName].elements[x].name!='phone')&& (document.forms[FormName].elements[x].name!='yearlyBudget')&& (document.forms[FormName].elements[x].name!='advertisingObjectives')&& (document.forms[FormName].elements[x].name!='advertiseOnline') && (document.forms[FormName].elements[x].name!='CAPTCHABox')) 
     {
        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 == 'company')
		{
				alert('Please type your company name and then try again.')
		}
        if (document.forms[FormName].elements[x].name == 'companyURL')
		{
				alert('Please type your company URL 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.')
		}
        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****************************
// -->

/********   END  functions********************************/

