var inProgress = false;

/**
 * Validates the form.  Returns true if valid, false if not.
 */
function validate()
{
    var isValid = true;

    if (document.donorForm.transType.value == "check")
    {
        document.donorForm.checkSSN.value =
            new String(document.donorForm.checkSSN1.value) + 
            new String(document.donorForm.checkSSN2.value) + 
            new String(document.donorForm.checkSSN3.value);

        document.donorForm.checkDOB.value =
            new String(document.donorForm.dobMonth.value) + "/" + 
            new String(document.donorForm.dobDay.value) + "/" + 
            new String(document.donorForm.dobYear.value);
    }
    
    if (isValid)
    {
        isValid = validateRequirement();
        if (isValid)
        {
            isValid = validateContents();
        }
        if (isValid)
        {
            if (!inProgress)
            {
                inProgress = true;
                isValid = true;
            }
            else
            {
                alert("Please wait.  Your transaction is in progress.");
                isValid = false;
            }
        }
    }

    return isValid;
}

/**
 * Makes sure a vaild e-mail address was given.
 * Returns true if valid, false if not.
 * based on code from: http://www.chalcedony.com/javascript3e/
 */
function validEmail(email)
{
	invalidChars = " /:,;"
	
	// does it contain any invalid characters?
	for (i=0; i<invalidChars.length; i++)
	{
		badChar = invalidChars.charAt(i)
		if (email.indexOf(badChar,0) > -1)
		{
			return false;
		}
	}

	// there must be one "@" symbol
	atPos = email.indexOf("@", 1)
	if (atPos == -1)
	{
		return false;
	}
	// and only one "@" symbol
	if (email.indexOf("@", atPos+1) != -1)
	{	
		return false;
	}
	
	// and at least one "." after the "@"
	periodPos = email.indexOf(".", atPos)
	if (periodPos == -1)
	{					
		return false;
	}

	// must be at least 2 characters after the "."
	if (periodPos+3 > email.length)
	{	
		return false;
	}
	
	return true;
}


/**
 * Validates the form field requirements.
 * Returns true if valid, false if not.
 */
function validateRequirement()
{
    var isValid = true;

	// every form needs a vaild e-mail address
	// check to see if a valid e-mail was given
	if ( !validEmail(document.donorForm.email.value) )
	{
		isValid = false;
		alert("Please enter a valid e-mail address.");
		document.donorForm.email.focus();
		document.donorForm.email.select();
	}
    // check for registration required fields
    else if (document.donorForm.transType.value == "registration")
    {
		if ((document.donorForm.accountName.value == "") &&
			((document.donorForm.firstName.value == "") ||
			(document.donorForm.lastName.value == "")))
		{
			alert("Please complete the account name or both first and last names.");
			isValid = false;
		}
    }
    // check for credit card donation required fields
    else if (document.donorForm.transType.value == "donation")
    {
		if(document.donorForm.firstName.value == "")
		{
			isValid = false;
			alert("Please enter your first name.");
			document.donorForm.firstName.focus();
		}
		else if(document.donorForm.lastName.value == "")
		{
			isValid = false;
			alert("Please enter your last name.");
			document.donorForm.lastName.focus();
		}
		else if(document.donorForm.address.value == "")
		{
			isValid = false;
			alert("Please enter your address.");
			document.donorForm.address.focus();
		}
		else if(document.donorForm.city.value == "")
		{
			isValid = false;
			alert("Please enter your city.");
			document.donorForm.city.focus();
		}
		else if(document.donorForm.state.selectedIndex == 0)
		{
			isValid = false;
			alert("Please select your state.");
			document.donorForm.state.focus();
		}
		else if(document.donorForm.postalCode.value == "")
		{
			isValid = false;
			alert("Please enter your postal (zip) code.");
			document.donorForm.postalCode.focus();
		}
		else if(document.donorForm.phone.value == "")
		{
			isValid = false;
			alert("Please enter your phone number.");
			document.donorForm.phone.focus();
		}
		else if(document.donorForm.cardNumber.value == "")
		{
			isValid = false;
			alert("Please enter your credit card number.");
			document.donorForm.cardNumber.focus();
		}
		else if(document.donorForm.cardExpMonth.value == "")
		{
			isValid = false;
			alert("Please enter the month your credit card expires.");
			document.donorForm.cardExpMonth.focus();
		}
		else if(document.donorForm.cardExpYear.value == "")
		{
			isValid = false;
			alert( "Please enter the year your credit card expires.");
			document.donorForm.cardExpYear.focus();
		}
		else if(document.donorForm.cardCVV2 != null && document.donorForm.cardCVV2.value == "")
		{
			isValid = false;
			alert("Please enter the security (CVV2) code for your credit card.");
			document.donorForm.cardCVV2.focus();
		}
		else if(document.donorForm.amount.value == "")
		{
			isValid = false;
			alert("Please enter the amount you wish to donate.");
			// only focus on amount if it is not readOnly
			if(!document.donorForm.amount.readOnly)
			{
				document.donorForm.amount.focus();
			}
		}
    }
    // check for check/eft donation required fields
    else if (document.donorForm.transType.value == "check")
    {
		if(document.donorForm.firstName.value == "")
		{
			isValid = false;
			alert("Please enter your first name.");
			document.donorForm.firstName.focus();
		}
		else if(document.donorForm.lastName.value == "")
		{
			isValid = false;
			alert("Please enter your last name.");
			document.donorForm.lastName.focus();
		}
		else if(document.donorForm.address.value == "")
		{
			isValid = false;
			alert("Please enter your address.");
			document.donorForm.address.focus();
		}
		else if(document.donorForm.city.value == "")
		{
			isValid = false;
			alert("Please enter your city.");
			document.donorForm.city.focus();
		}
		else if(document.donorForm.state.selectedIndex == 0)
		{
			isValid = false;
			alert("Please select your state.");
			document.donorForm.state.focus();
		}
		else if(document.donorForm.postalCode.value == "")
		{
			isValid = false;
			alert("Please enter your postal (zip) code.");
			document.donorForm.postalCode.focus();
		}
		else if(document.donorForm.phone.value == "")
		{
			isValid = false;
			alert("Please enter your phone number.");
			document.donorForm.phone.focus();
		}
		else if(document.donorForm.checkRoutingNum.value == "")
		{
			isValid = false;
			alert("Please enter your Bank Routing Number.");
			document.donorForm.checkRoutingNum.focus();
		}
		else if(document.donorForm.checkAcctNum.value == "")
		{
			isValid = false;
			alert("Please enter your Bank Account Number.");
			document.donorForm.checkAcctNum.focus();
		}
		else if(document.donorForm.checkAcctType.value == "")
		{
			isValid = false;
			alert("Please mark what kind of bank account you are using.");
			document.donorForm.checkAcctType.focus();
		}
		else if(document.donorForm.checkSSN.value == "")
		{
			isValid = false;
			alert("Please enter your Social Security Number.");
			// this is more than one field, do not focus
		}
		else if(document.donorForm.checkStateAbr.value == "")
		{
			isValid = false;
			alert("Please enter the state your Driver's License is from.");
			document.donorForm.checkStateAbr.focus();
		}
		else if(document.donorForm.checkLicenseNum.value == "")
		{
			isValid = false;
			alert("Please enter your Driver's License Number.");
			document.donorForm.checkLicenseNum.focus();
		}
		else if(document.donorForm.checkDOB.value == "")
		{
			isValid = false;
			alert("Please enter your Date of Birth.");
			// this is more than one field, do not focus
		}
		else if(document.donorForm.amount.value == "")
		{
			isValid = false;
			alert("Please enter the amount you wish to donate.");
			// only focus on amount if it is not readOnly
			if(!document.donorForm.amount.readOnly)
			{
				document.donorForm.amount.focus();
			}
		}
    }
    // check for pledge required fields
    else if (document.donorForm.transType.value == "pledge")
    {
		if(document.donorForm.firstName.value == "")
		{
			isValid = false;
			alert("Please enter your first name.");
			document.donorForm.firstName.focus();
		}
		else if(document.donorForm.lastName.value == "")
		{
			isValid = false;
			alert("Please enter your last name.");
			document.donorForm.lastName.focus();
		}
		else if(document.donorForm.address.value == "")
		{
			isValid = false;
			alert("Please enter your address.");
			document.donorForm.address.focus();
		}
		else if(document.donorForm.city.value == "")
		{
			isValid = false;
			alert("Please enter your city.");
			document.donorForm.city.focus();
		}
		else if(document.donorForm.state.selectedIndex == 0)
		{
			isValid = false;
			alert("Please select your state.");
			document.donorForm.state.focus();
		}
		else if(document.donorForm.postalCode.value == "")
		{
			isValid = false;
			alert("Please enter your postal (zip) code.");
			document.donorForm.postalCode.focus();
		}
		else if(document.donorForm.phone.value == "")
		{
			isValid = false;
			alert("Please enter your phone number.");
			document.donorForm.phone.focus();
		}
		else if(document.donorForm.amount.value == "")
		{
			isValid = false;
			alert("Please enter the amount you wish to donate.");
			// only focus on amount if it is not readOnly
			if(!document.donorForm.amount.readOnly)
			{
				document.donorForm.amount.focus();
			}
		}
    }

    return isValid;
}

/**
 * Validates the form field content.
 * Returns true if valid, false if not.
 */
function validateContents()
{
    var isValid = true;
    var msg = "";	

    // check credit card donation content 
    if (document.donorForm.transType.value == "donation")
    {
	
	    // cvv2 code
		if(document.donorForm.cardCVV2 != null)   // skip if not implemented on the page yet
        {
            // Validate that the field is the right length
            if( document.donorForm.cardCVV2.value.length < 3 || document.donorForm.cardCVV2.value.length > 4 )
            {
                isValid = false;
                msg = "Valid security (CVV2) codes are 3 or 4 digits.";
            }
 
            // If we're still okay, make sure all characters in the code are numbers.
            if( isValid )
            {
                 for( i = 0; i < document.donorForm.cardCVV2.value.length; i++ )
                 {
                      if ((document.donorForm.cardCVV2.value.charAt(i) < "0") ||
                          (document.donorForm.cardCVV2.value.charAt(i) > "9"))
                      {
                          isValid = false;
                          msg = "The security (CVV2) code entered is not a valid 3 or 4 digit number.";
                      }
                 }
            }
        }   
		
        // card expiration month 
        if (document.donorForm.cardExpMonth.value.length < 2)
        {
            isValid = false;
            msg = "Valid credit card expiration month values are 01-12.";
        }
        if (isValid)
        {
            var month = new Number(document.donorForm.cardExpMonth.value);
            if ((month <= 0) || (month > 12))
            {
                isValid = false;
                msg = "Valid credit card expiration month values are 01-12.";
            }
            if (isNaN(month))
            {
                isValid = false;
                msg = "The credit card expiration month entered is not valid.";
            }
        }

        // card expiration year
        if (isValid)
        {
            var year = new Number(document.donorForm.cardExpYear.value);
            var now = new Date();
            if (year < now.getYear())
            {
                isValid = false;
                msg = "The credit card expiration year entered is in the past.";
            }
            if (isNaN(year))
            {
                isValid = false;
                msg = "The credit card expiration year entered is not valid.";
            }
        }
		
		// credit card number
		if (document.donorForm.cardNumber.value.length < 14)
		{
			isValid = false;
			msg = "Valid credit card numbers are at least 14 digits.";
		}
	
		for (i=0; i < document.donorForm.cardNumber.value.length; i++)
		{
			if ((document.donorForm.cardNumber.value.charAt(i) < "0") ||
				(document.donorForm.cardNumber.value.charAt(i) > "9"))
			{
				isValid = false;
				msg = "The credit card number entered is not valid";
			}
		}

    }
    else if (document.donorForm.transType.value == "check")
    {
        // routing number 
        if (document.donorForm.checkRoutingNum.value.length != 9)
        {
            isValid = false;
            msg = "The bank routing number you entered does not contain nine digits.  Please check and re-enter the number.";
        }
        if (isValid)
        {
            // ssn
            if ((document.donorForm.checkSSN1.value.length != 3) ||
                (document.donorForm.checkSSN2.value.length != 2) ||
                (document.donorForm.checkSSN3.value.length != 4))
            {
                isValid = false;
                msg = "Social Security numbers are 9 digits.";
            }
        }
		for (i=0; i < document.donorForm.checkRoutingNum.value.length; i++)
		{
			if ((document.donorForm.checkRoutingNum.value.charAt(i) < "0") ||
				(document.donorForm.checkRoutingNum.value.charAt(i) > "9"))
			{
				isValid = false;
				msg = "The check routing number entered is not valid";
			}
		}
    }

    // amount
    if (isValid)
    {
        if ((document.donorForm.transType.value == "donation") ||
            (document.donorForm.transType.value == "check"))
        {
            var amount = new Number(document.donorForm.amount.value);
            if ((amount <= 0) || (isNaN(amount)))
            {
                isValid = false;
                msg = "The amount entered must be greater than 0 and not contain a currency symbol.";
            }
        }
    }
    if (!isValid)
    {
        alert(msg);
    }
    return isValid;
}

var states = [ 
    new Array("", ""),
    new Array("Alabama", "AL"),
    new Array("Alaska", "AK"),
    new Array("Alberta", "AB"),
    new Array("American Samoa", "AS"),
    new Array("Arizona", "AZ"),
    new Array("Arkansas", "AR"),
    new Array("British Columbia", "BC"),
    new Array("California", "CA"),
    new Array("Colorado", "CO"),
    new Array("Connecticut", "CT"),
    new Array("Delaware", "DE"),
    new Array("District Of Columbia", "DC"),
    new Array("Federated States Of Micronesia", "FM"),
    new Array("Florida", "FL"),
    new Array("Georgia", "GA"),
    new Array("Guam", "GU"),
    new Array("Hawaii", "HI"),
    new Array("Idaho", "ID"),
    new Array("Illinois", "IL"),
    new Array("Indiana", "IN"),
    new Array("Iowa", "IA"),
    new Array("Kansas", "KS"),
    new Array("Kentucky", "KY"),
    new Array("Louisiana", "LA"),
    new Array("Maine", "ME"),
    new Array("Manitoba", "MB"),
    new Array("Marshall Islands", "MH"),
    new Array("Maryland", "MD"),
    new Array("Massachusetts", "MA"),
    new Array("Michigan", "MI"),
    new Array("Minnesota", "MN"),
    new Array("Mississippi", "MS"),
    new Array("Missouri", "MO"),
    new Array("Montana", "MT"),
    new Array("Nebraska", "NE"),
    new Array("Nevada", "NV"),
    new Array("New Brunswick", "NB"),
    new Array("New Hampshire", "NH"),
    new Array("New Jersey", "NJ"),
    new Array("New Mexico", "NM"),
    new Array("New York", "NY"),
    new Array("Newfoundland ", "NF"),
    new Array("North Carolina", "NC"),
    new Array("North Dakota", "ND"),
    new Array("Northern Mariana Islands", "MP"),
    new Array("Nova Scoita", "NS"),
    new Array("Ohio", "OH"),
    new Array("Oklahoma", "OK"),
    new Array("Ontario", "ON"),
    new Array("Oregon", "OR"),
    new Array("Palau", "PW"),
    new Array("Pennsylvania", "PA"),
    new Array("Prince Edward Island", "PE"),
    new Array("Puerto Rico", "PR"),
    new Array("Quebec", "PQ"),
    new Array("Rhode Island", "RI"),
    new Array("Saskatchewan", "SK"),
    new Array("South Carolina", "SC"),
    new Array("South Dakota", "SD"),
    new Array("Tennessee", "TN"),
    new Array("Texas", "TX"),
    new Array("Utah", "UT"),
    new Array("Vermont", "VT"),
    new Array("Virgin Islands", "VI"),
    new Array("Virginia", "VA"),
    new Array("Washington", "WA"),
    new Array("West Virginia", "WV"),
    new Array("Wisconsin", "WI"),
    new Array("Wyoming", "WY")];

function writeStates()
{
    writeSelectOptions(states);
}

var countries = [ 
    new Array("United States", "US"),
    new Array("Australia", "AU"),
    new Array("Canada", "CA"),
    new Array("France", "FR"),
    new Array("Germany", "DE"),
    new Array("Israel", "IL"),
    new Array("Italy", "IT"),
    new Array("Japan", "JP"),
    new Array("Mexico", "MX"),
    new Array("New Zealand", "NZ"),
    new Array("Spain", "ES"),
    new Array("United Kingdom", "UK")];

var allCountries = [ 
    new Array("United States", "US"),
    new Array("Afghanistan", "AF"),
    new Array("Albania", "AL"),
    new Array("Algeria", "DZ"),
    new Array("American Samoa", "AS"),
    new Array("Andorra", "AD"),
    new Array("Angola", "AO"),
    new Array("Anguilla", "AI"),
    new Array("Antarctica", "AQ"),
    new Array("Antigua And Barbuda", "AG"),
    new Array("Argentina", "AR"),
    new Array("Armenia", "AM"),
    new Array("Aruba", "AW"),
    new Array("Australia", "AU"),
    new Array("Austria", "AT"),
    new Array("Azerbaijan", "AZ"),
    new Array("Bahamas", "BS"),
    new Array("Bahrain", "BH"),
    new Array("Bangladesh", "BD"),
    new Array("Barbados", "BB"),
    new Array("Belarus", "BY"),
    new Array("Belgium", "BE"),
    new Array("Belize", "BZ"),
    new Array("Benin", "BJ"),
    new Array("Bermuda", "BM"),
    new Array("Bhutan", "BT"),
    new Array("Bolivia", "BO"),
    new Array("Bosnia-Herzegovina", "BA"),
    new Array("Botswana", "BW"),
    new Array("Bouvet Island", "BV"),
    new Array("Brazil", "BR"),
    new Array("British Indian Ocean Territory", "IO"),
    new Array("Brunei Darussalam", "BN"),
    new Array("Bulgaria", "BG"),
    new Array("Burkina Faso", "BF"),
    new Array("Burundi", "BI"),
    new Array("Cambodia", "KH"),
    new Array("Cameroon", "CM"),
    new Array("Canada", "CA"),
    new Array("Cape Verde", "CV"),
    new Array("Cayman Islands", "KY"),
    new Array("Central African Republic", "CF"),
    new Array("Chad", "TD"),
    new Array("Chile", "CL"),
    new Array("China", "CN"),
    new Array("Christmas Island", "CX"),
    new Array("Cocos (Keeling) Islands", "CC"),
    new Array("Colombia", "CO"),
    new Array("Comoros", "KM"),
    new Array("Congo", "CG"),
    new Array("Cook Islands", "CK"),
    new Array("Costa Rica", "CR"),
    new Array("Cote D'ivoire", "CI"),
    new Array("Croatia (Local Name: Hrvatska)", "HR"),
    new Array("Cuba", "CU"),
    new Array("Cyprus", "CY"),
    new Array("Czech Republic", "CZ"),
    new Array("Denmark", "DK"),
    new Array("Djibouti", "DJ"),
    new Array("Dominica", "DM"),
    new Array("Dominican Republic", "DO"),
    new Array("East Timor", "TP"),
    new Array("Ecuador", "EC"),
    new Array("Egypt", "EG"),
    new Array("El Salvador", "SV"),
    new Array("Equatorial Guinea", "GQ"),
    new Array("Eritrea", "ER"),
    new Array("Estonia", "EE"),
    new Array("Ethiopia", "ET"),
    new Array("Falkland Islands (Malvinas)", "FK"),
    new Array("Faroe Islands", "FO"),
    new Array("Fiji", "FJ"),
    new Array("Finland", "FI"),
    new Array("France", "FR"),
    new Array("France, Metropolitan", "FX"),
    new Array("French Guiana", "GF"),
    new Array("French Polynesia", "PF"),
    new Array("French Southern Territories", "TF"),
    new Array("Gabon", "GA"),
    new Array("Gambia", "GM"),
    new Array("Georgia", "GE"),
    new Array("Germany", "DE"),
    new Array("Ghana", "GH"),
    new Array("Gibraltar", "GI"),
    new Array("Greece", "GR"),
    new Array("Greenland", "GL"),
    new Array("Grenada", "GD"),
    new Array("Guadeloupe", "GP"),
    new Array("Guam", "GU"),
    new Array("Guatemala", "GT"),
    new Array("Guinea", "GN"),
    new Array("Guinea-bissau", "GW"),
    new Array("Guyana", "GY"),
    new Array("Haiti", "HT"),
    new Array("Heard And Mc Donald Islands", "HM"),
    new Array("Honduras", "HN"),
    new Array("Hong Kong", "HK"),
    new Array("Hungary", "HU"),
    new Array("Iceland", "IS"),
    new Array("India", "IN"),
    new Array("Indonesia", "ID"),
    new Array("Iran (Islamic Republic Of)", "IR"),
    new Array("Iraq", "IQ"),
    new Array("Ireland", "IE"),
    new Array("Israel", "IL"),
    new Array("Italy", "IT"),
    new Array("Jamaica", "JM"),
    new Array("Japan", "JP"),
    new Array("Jordan", "JO"),
    new Array("Kazakhstan", "KZ"),
    new Array("Kenya", "KE"),
    new Array("Kiribati", "KI"),
    new Array("Korea, Democratic People's Republic Of", "KP"),
    new Array("Korea, Republic Of", "KR"),
    new Array("Kuwait", "KW"),
    new Array("Kyrgyzstan", "KG"),
    new Array("Lao People's Democratic Republic", "LA"),
    new Array("Latvia", "LV"),
    new Array("Lebanon", "LB"),
    new Array("Lesotho", "LS"),
    new Array("Liberia", "LR"),
    new Array("Libyan Arab Jamahiriya", "LY"),
    new Array("Liechtenstein", "LI"),
    new Array("Lithuania", "LT"),
    new Array("Luxembourg", "LU"),
    new Array("Macau", "MO"),
    new Array("Macedonia, The Former Yugoslav Republic Of", "MK"),
    new Array("Madagascar", "MG"),
    new Array("Malawi", "MW"),
    new Array("Malaysia", "MY"),
    new Array("Maldives", "MV"),
    new Array("Mali", "ML"),
    new Array("Malta", "MT"),
    new Array("Marshall Islands", "MH"),
    new Array("Martinique", "MQ"),
    new Array("Mauritania", "MR"),
    new Array("Mauritius", "MU"),
    new Array("Mayotte", "YT"),
    new Array("Mexico", "MX"),
    new Array("Micronesia, Federated States Of", "FM"),
    new Array("Moldova, Republic Of", "MD"),
    new Array("Monaco", "MC"),
    new Array("Mongolia", "MN"),
    new Array("Montserrat", "MS"),
    new Array("Morocco", "MA"),
    new Array("Mozambique", "MZ"),
    new Array("Myanmar", "MM"),
    new Array("Namibia", "NA"),
    new Array("Nauru", "NR"),
    new Array("Nepal", "NP"),
    new Array("Netherlands", "NL"),
    new Array("Netherlands Antilles", "AN"),
    new Array("New Caledonia", "NC"),
    new Array("New Zealand", "NZ"),
    new Array("Nicaragua", "NI"),
    new Array("Niger", "NE"),
    new Array("Nigeria", "NG"),
    new Array("Niue", "NU"),
    new Array("Norfolk Island", "NF"),
    new Array("Northern Mariana Islands", "MP"),
    new Array("Norway", "NO"),
    new Array("Oman", "OM"),
    new Array("Pakistan", "PK"),
    new Array("Palau", "PW"),
    new Array("Panama", "PA"),
    new Array("Papua New Guinea", "PG"),
    new Array("Paraguay", "PY"),
    new Array("Peru", "PE"),
    new Array("Philippines", "PH"),
    new Array("Pitcairn", "PN"),
    new Array("Poland", "PL"),
    new Array("Portugal", "PT"),
    new Array("Puerto Rico", "PR"),
    new Array("Qatar", "QA"),
    new Array("Reunion", "RE"),
    new Array("Romania", "RO"),
    new Array("Russian Federation", "RU"),
    new Array("Rwanda", "RW"),
    new Array("Saint Kitts And Nevis", "KN"),
    new Array("Saint Lucia", "LC"),
    new Array("Saint Vincent And The Grenadines", "VC"),
    new Array("Samoa", "WS"),
    new Array("San Marino", "SM"),
    new Array("Sao Tome And Principe", "ST"),
    new Array("Saudi Arabia", "SA"),
    new Array("Senegal", "SN"),
    new Array("Seychelles", "SC"),
    new Array("Sierra Leone", "SL"),
    new Array("Singapore", "SG"),
    new Array("Slovakia (Slovak Republic)", "SK"),
    new Array("Slovenia", "SI"),
    new Array("Solomon Islands", "SB"),
    new Array("Somalia", "SO"),
    new Array("South Africa", "ZA"),
    new Array("South Georgia And The South Sandwich Islands", "GS"),
    new Array("Spain", "ES"),
    new Array("Sri Lanka", "LK"),
    new Array("St. Helena", "SH"),
    new Array("St. Pierre And Miquelon", "PM"),
    new Array("Sudan", "SD"),
    new Array("Suriname", "SR"),
    new Array("Svalbard And Jan Mayen Islands", "SJ"),
    new Array("Swaziland", "SZ"),
    new Array("Sweden", "SE"),
    new Array("Switzerland", "CH"),
    new Array("Syrian Arab Republic", "SY"),
    new Array("Taiwan, Republic Of China", "TW"),
    new Array("Tajikistan", "TJ"),
    new Array("Tanzania, United Republic Of", "TZ"),
    new Array("Thailand", "TH"),
    new Array("Togo", "TG"),
    new Array("Tokelau", "TK"),
    new Array("Tonga", "TO"),
    new Array("Trinidad And Tobago", "TT"),
    new Array("Tunisia", "TN"),
    new Array("Turkey", "TR"),
    new Array("Turkmenistan", "TM"),
    new Array("Turks And Caicos Islands", "TC"),
    new Array("Tuvalu", "TV"),
    new Array("Uganda", "UG"),
    new Array("Ukraine", "UA"),
    new Array("United Arab Emirates", "AE"),
    new Array("United Kingdom", "UK"),
    new Array("United States Minor Outlying Islands", "UM"),
    new Array("Uruguay", "UY"),
    new Array("Uzbekistan", "UZ"),
    new Array("Vanuatu", "VU"),
    new Array("Vatican City State (Holy See)", "VA"),
    new Array("Venezuela", "VE"),
    new Array("Viet Nam", "VN"),
    new Array("Virgin Islands (British)", "VG"),
    new Array("Virgin Islands (U S)", "VI"),
    new Array("Wallis And Futuna Islands", "WF"),
    new Array("Western Sahara", "EH"),
    new Array("Yemen", "YE"),
    new Array("Zaire", "ZR"),
    new Array("Zambia", "ZM"),
    new Array("Zimbabwe", "ZW")];

function writeCountries()
{
    writeSelectOptions(countries);
}

function writeAllCountries()
{
    writeSelectOptions(allCountries);
}

function writeSelectOptions(selectData)
{
    for (var i = 0; i < selectData.length; i++)
    {
        document.write("<option value=\"" + selectData[i][1] + "\"");
        if ((selectData[i][2] != null) && (selectData[i][2] == "true"))
        {
            document.write(" selected");
        }
        document.writeln(">" + selectData[i][0] + "</option>");
    }
}

function writeDays()
{
    for (var i = 1; i <= 31; i++)
    {
        var s = new Number(i).toString();
        if (s.length == 1)
        {
            s = new String("0") + s;
        }
        document.writeln("<option value=\"" + s + "\">" + s + "</option>");
    }
}

function writeMonths()
{
    for (var i = 1; i <= 12; i++)
    {
        var s = new Number(i).toString();
        if (s.length == 1)
        {
            s = new String("0") + s;
        }
        document.writeln("<option value=\"" + s + "\">" + s + "</option>");
    }
}

function writeYears(start, num)
{
    var startYear;
    if (Number(start) == -1)	// start at current year
    {
        startYear = getCurrentYear();
    }
    else
    {
        startYear = start;
    }

    for (var i = startYear; i <= startYear+num; i++)
    {
        document.writeln("<option value=\"" + i + "\">" + i + "</option>");
    }
}

function getCurrentYear()
{
    var year;

    var d = new Date();
    if (navigator.appName.indexOf("Netscape") >= 0)
    {
        var str = new Number(d.getYear()).toString();
        str = "20" + str.substring(1);
        year = new Number(str);
    }
    else
    {
        year = new Number(d.getYear());
    }

    return year;
}

function getStringDate()
{
    var d = new Date();
    return (d.getMonth()+1) + "/" + d.getDate() + "/" + getCurrentYear();
}

function updatePaymentType()
{
    if (document.donorForm.paymentType[0].checked)
    {
        document.donorForm.transType.value = "donation";
    }
    else if (document.donorForm.paymentType[1].checked)
    {
        document.donorForm.transType.value = "check";
    }
}

function getElementByName(eName)
{
    var e = null;

    for (var i = 0; i < document.donorForm.elements.length; i++)
    {
        if (document.donorForm.elements[i].name == eName)
        {
            e = document.donorForm.elements[i];
            break;
        }
    }

    return e;
}

function getElementStartsWithName(eName)
{
    var e = null;

    for (var i = 0; i < document.donorForm.elements.length; i++)
    {
        if (document.donorForm.elements[i].name.indexOf(eName) >= 0)
        {
            e = document.donorForm.elements[i];
            break;
        }
    }

    return e;
}

// page author must write a matrix variable "costs" as such
//
// var costs = new Array();
// costs[i] = new Array(amount, eventName, variableName, UDFname);
//
// e.g.
// var costs = new Array();
// costs[0] = new Array(25, "Event 1", "Activities", "Entity_SetElement|Activities|Event 1");
// costs[1] = new Array(50, "Event 2", "Activities", "Entity_SetElement|Activities|Event 2");
//
// in the page call writeCosts(costs, start, end, inputType)
// e.g. <script>writeCosts(costs, 0, 1, "radio")</script>
//
// in validation call getCosts(costs, baseFee)
function writeCosts(costs, start, end, inputType)
{
	for(i=start; i<(end + 1); i++)
	{
		document.write("<input type= \"" + inputType + "\" name=\"" + costs[i][2] + "\" value=\"" + costs[i][0] + "\">" + costs[i][1] + " - $" + costs[i][0] + "<br>");
		document.write("<input type=\"hidden\" name=\"" + costs[i][3] + "\">");
	}
}

function getCosts(costs, baseFee)
{
	var total = parseFloat(baseFee);
	for(i=0; i<costs.length; i++)
	{
		for(c=0; c<(document.donorForm.elements.length - 1); c++)
		{
			if(document.donorForm.elements[c+1].name == costs[i][3])
			{
				if(document.donorForm.elements[c].checked)
				{
					document.donorForm.elements[c+1].value = "true";
					total = total + parseFloat(document.donorForm.elements[c].value);
				}
				if(document.donorForm.elements[c].checked != true)
				{
						document.donorForm.elements[c+1].value = "false"
				}
			}
		}
	}
	document.donorForm.amount.value = total;
}

// this function changes fields from the donorForm into UDF format
// this will most likely be use when a UDF can only take one value
// 
// form requirements: hidden fields for the UDF's which follow the UDF naming conventions
//
// naming conventions: the UDF category will be the name and the UDF value will be the value.
// e.g. if a company has a UDF named Grades with values A, B, C, D, and F
// <input type="radio" name="Grades" value="A">A
// <input type="radio" name="Grades" value="B">B
// <input type="radio" name="Grades" value="C">C
// <input type="radio" name="Grades" value="D">D
// <input type="radio" name="Grades" value="F">F
// <input type="hidden" name="Entity_SetElement|Grades|A">
// <input type="hidden" name="Entity_SetElement|Grades|B">
// <input type="hidden" name="Entity_SetElement|Grades|C">
// <input type="hidden" name="Entity_SetElement|Grades|D">
// <input type="hidden" name="Entity_SetElement|Grades|F">
//
// possible input types are radio and select box
//
// <script>setUDFValue(document.donorForm.Grades, "Grades")</script>

function setUDFValue(fieldName, eventName)
{
	for (i=0; i < fieldName.length; i++)
	{
		if (fieldName[i].checked || fieldName[i].selected)
		{
			for (var c = 0; c < document.donorForm.elements.length; c++)
			{
				if (document.donorForm.elements[c].name.indexOf("_SetElement|" + eventName + "|" + fieldName[i].value) >= 0)
				{
					document.donorForm.elements[c].value = "true";
				}
			}
		}
		else if(fieldName[i].checked != true && fieldName[i].selected != true)
		{
			for (var c = 0; c < document.donorForm.elements.length; c++)
			{
				if (document.donorForm.elements[c].name.indexOf("_SetElement|" + eventName + "|" + fieldName[i].value) >= 0)
				{
					document.donorForm.elements[c].value = "false";
				}
			}
		}
	}
}
