function toggle_visibility(id) {
var e = document.getElementById(id);
if(e.style.display == 'none')
e.style.display = 'block';
else
e.style.display = 'none';
}

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];}
}

// st brigid donation scripts

//check for valid e-mail address
function isEmail(argvalue) {

  if (argvalue.indexOf(" ") != -1)
    return false;
  else if (argvalue.indexOf("@") == -1)
    return false;
  else if (argvalue.indexOf("@") == 0)
    return false;
  else if (argvalue.indexOf("@") == (argvalue.length-1))
    return false;

  arrayString = argvalue.split("@");
  

  if (arrayString[1].indexOf(".") == -1)
    return false;
  else if (arrayString[1].indexOf(".") == 0)
    return false;
  else if (arrayString[1].charAt(arrayString[1].length-1) == ".") {
    return false;
  }

  return true;

}

function checkform(theForm) {
        //check donation information
		if (theForm.donation_amount.value == "") 
		{
                alert("Please enter a donation amount.");
                theForm.donation_amount.focus();
                return (false);
        }
		
		if (theForm.donation_fund_id.selectedIndex == "") 
		{
 				alert("Please select a donation fund.");
                theForm.donation_fund_id.focus();
                return (false);
        }
		
		if (theForm.donation_from.value == "") 
		{
                alert("Please specify who the donation is from.");
                theForm.donation_from.focus();
                return (false);
        }
		
		if (theForm.donation_email_address.value != "") 
		{
                if(!isEmail(theForm.donation_email_address.value)) 
				{  
                alert("Please enter a valid email address.");
                        theForm.donation_email_address.focus();
                        return (false);
                }
        }
		
		if (theForm.donation_notes.value == "")  
		{
				alert("Please enter who the donation is in honor/memory of.");
				theForm.donation_notes.focus();
				return(false);
		}
		
		//check credit card information
		if (theForm.cNameBfirst.value == "") 
		{
			alert("Please enter the first name appearing on the credit card.");
			theForm.cNameBfirst.focus();
			return (false);
		}
		if (theForm.cNameBlast.value == "") 
		{
			alert("Please enter the last name appearing on the credit card.");
			theForm.cNameBlast.focus();
			return (false);
		}
		if (theForm.CreditCardType.value == "" ) 
		{
				alert("Please select a Credit Card.");
				return (false);
		}
		if (theForm.cardNum.value == "") 
		{
			alert("\"Credit Card Number\" must be completed.");
			theForm.cardNum.focus();
			return (false);
		}

		if (!checkCreditCard(theForm.cardNum.value, theForm.CreditCardType.value))
		{
				alert("You have entered an invalid credit card number - please check.");
				theForm.cardNum.focus();
				return (false);
		}
		if (mask(theForm.cardExp.value, "####") == false) 
		{
               alert("Please enter invoice date in MMYY format.");
			   theForm.cardExp.value = "";
               theForm.cardExp.focus();
			   return (false);

        }
		if (theForm.CCV.value == "") 
		{
			alert("Please enter the CVV code.");
			theForm.CCV.focus();
			return (false);
		}
		if (theForm.address.value == "") 
		{
                alert("Please enter your mailing address.");
                theForm.address.focus();
                return (false);
        }
        if (theForm.city.value == "") 
		{
                alert("Please enter your city.");
                theForm.city.focus();
                return (false);
        }
		if (theForm.State.selectedIndex == "") 
		{
                alert("Please select your state.");
                theForm.State.focus();
                return (false);
        }
        if (theForm.zip.value == "") 
		{
                alert("Please enter your zip code.");
                theForm.zip.focus();
                return (false);
        }
		if (theForm.country.value == "") 
		{
                alert("Please enter your country.");
                theForm.country.focus();
                return (false);
        }
//check contact information
		if (theForm.Sender.value == "") 
		{
                alert("Please enter your Name.");
                theForm.Sender.focus();
                return (false);
        }
		if (theForm.Phone.value == "") 
		{
			alert("Please enter your telephone number.");
			theForm.Phone.focus();
			return (false);
		}
		if (theForm.emailfromaddress.value !="") 
		{
			if(!isEmail(theForm.emailfromaddress.value)) 
			{  
                alert("Please enter a valid email address.");
                        theForm.emailfromaddress.focus();
                        return (false);
			}
                
        }
		if (theForm.d_address.value == "") 
		{
                alert("Please enter your mailing address.");
                theForm.d_address.focus();
                return (false);
        }
        if (theForm.d_city.value == "") 
		{
                alert("Please enter your city.");
                theForm.d_city.focus();
                return (false);
        }

        if (theForm.d_state.selectedIndex == "") 
		{
                alert("Please select your state.");
                theForm.d_state.focus();
                return (false);
        }
        if (theForm.d_zip.value == "") 
		{
                alert("Please enter your zip code.");
                theForm.d_zip.focus();
                return (false);
        }
		if (theForm.d_country.value == "") 
		{
                alert("Please enter your zip code.");
                theForm.d_country.focus();
                return (false);
        }                
        return (true);		
		
}

function mask (InString, Mask)  {
        // used to check a field value against an approved format
        // examples
        // date     : if (!mask(form.date.value, "##/##/##"))
        // zip code : if (!mask(form.zip.value, "#####-####"))
        if (((InString.length ==0) || (Mask.length ==0)) || (InString.length!= Mask.length))
                return false;
        TempString=""
        for (Count=0; Count<=InString.length; Count++)  {
                StrChar = InString.substring(Count, Count+1);
                MskChar = Mask.substring(Count, Count+1);
                if (MskChar=='#')
                {
                        if(isNaN(StrChar))
                                return false;
                }
                else if (MskChar=='*') {
                }
                else {
                        if (MskChar!=StrChar)
                                return false;
                }
        }
        return true;
}
function textBuild() {
//used to combine the 4 input boxes for cc number to one value for validation
     document.donation.cardNum.value =  document.donation.cardNum1.value +  document.donation.cardNum2.value +  document.donation.cardNum3.value +  document.donation.cardNum4.value;
}
function KeyPress(what,e,max,action) {
//  used to autotab the cc fields
    if (document.layers) {
        if (e.target.value.length >= max)
            eval(action);
    }
    else if (document.all) {
        if (what.value.length > (max-1))
            eval(action);
    }
}

function fillInfo()
{
	theForm = document.donation;
	theForm.d_city.value = theForm.City.value;
	theForm.d_zip.value = theForm.Zip.value;
	theForm.d_address.value = theForm.Address.value;
	theForm.d_state.value = theForm.State.value;
}
