/*
* javascript code for APG membership form
* Kaundra Melber & Mark Willis  03/01/2009
*/

var ut = mawUtility;

var badAlphaExp = new RegExp("[^A-Za-z0-9 _,.#@()]");
var badAlphaMessage = "Please enter only letters, numbers, or _,.#@() characters.";
var badDecimalExp = new RegExp("[^0-9 ,.]");
var badDecimalMessage = "Invalid characters in number field.";
var badNumericExp = new RegExp("[^0-9]");
var badNumericMessage = "Please enter numbers only in this field.";
var badPhoneExp = new RegExp("[^0-9-() ]");
var badPhoneMessage = "Invalid characters in phone number.";

function getCheckedRadioValue(radioObjName) {
	if(!radioObjName) return "";
	var radioObj = document.getElementsByName(radioObjName);
	if(!radioObj) return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function hasInvalidCharacters(field, regexp, message) {
	if (field.value.match(regexp)) {
		alert(message);
		field.focus();
		return true;
	}
	return false;
}

function cannotBeBlank(field, message)
{
	if (field.value == "")
	{
		alert(message);
		field.focus();
		return true;
	}
	return false;
}


// Member data
var mData = {
    name: Object = null,
    address: Object = null,
    aptNumber: Object = null,
    city: Object = null,
	stateCode: Object = null,
	zipCode: Object = null,
	phoneDay: Object = null,
	phoneNight: Object = null,
	email: Object = null,
	membershipType: String = null,
	membershipLevel: String = null,
	otherAmount: Object = null,	
	levelDescription: String = null,
	amount: String = null,

	loadFromInputForm:function()	{
	    this.name = $("member_name");
		this.mName = $("member_name");
		this.address = $("member_address");
		this.aptNumber = $("apt_nbr");
		this.city = $("city_name");
		this.stateCode = $("state_cd");
		this.zipCode = $("postal_cd");
		this.phoneDay = $("phone_day");
		this.phoneNight = $("phone_night");
		this.email = $("email_addr");
		this.membershipType = getCheckedRadioValue("member_status");
		// the level value in the form combines the description and cost like "student,25"
		this.membershipLevel = getCheckedRadioValue("member_level");
		this.otherAmount = $("other_amt");

		var tmpArray = this.membershipLevel.split(",");
		this.levelDescription = tmpArray[0];
		if (this.levelDescription == "other") {
			this.amount = this.otherAmount.value;  // not validated yet...
		} else {
			this.amount = tmpArray[1];
		}
	},
	
	getInfoString:function() {
		var infoString =
			"name:" + this.name.value + ";" +
			"addr:" + this.address.value + ";" +
			"apt:" + this.aptNumber.value + ";" +
			"city:" + this.city.value + ";" +
			"st:" + this.stateCode.value + ";" +
			"zip:" + this.zipCode.value + ";" +
			"pD:" + this.phoneDay.value + ";" +
			"pN:" + this.phoneNight.value + ";" +
			"email:" + this.email.value + ";" +
			"type:" + this.membershipType + ";" +
			"level:" + this.levelDescription + ";"
		return infoString;
	},
	
	isValid:function() {
		// validation rules...
		if (this.membershipLevel != "other") this.otherAmount.value = "";
		if (cannotBeBlank(this.name, "Please enter your name.")) return false;
		if (hasInvalidCharacters(this.name, badAlphaExp, badAlphaMessage)) return false;
		if (cannotBeBlank(this.address, "Please enter your address.")) return false;
		if (hasInvalidCharacters(this.address, badAlphaExp, badAlphaMessage)) return false;
		// apartment number can be blank...
		if (hasInvalidCharacters(this.aptNumber, badAlphaExp, badAlphaMessage)) return false;
		if (cannotBeBlank(this.city, "Please enter your city.")) return false;
		if (hasInvalidCharacters(this.city, badAlphaExp, badAlphaMessage)) return false;
		if (cannotBeBlank(this.stateCode, "Please select your state.")) return false;
		if (cannotBeBlank(this.zipCode, "Please enter your zip code.")) return false;
		if (hasInvalidCharacters(this.zipCode, badNumericExp, badNumericMessage)) return false;
		// phone numbers can be blank
		if (hasInvalidCharacters(this.phoneDay, badPhoneExp, badPhoneMessage)) return false;
		if (hasInvalidCharacters(this.phoneNight, badPhoneExp, badPhoneMessage)) return false;
		if (cannotBeBlank(this.email, "Please enter your email for online membership.")) return false;
		if (hasInvalidCharacters(this.email, badAlphaExp, badAlphaMessage)) return false;
		if (this.membershipLevel == "other") {
			if (cannotBeBlank(this.otherAmount, "Please enter a donation amount.")) return false;
			if (hasInvalidCharacters(this.otherAmount, badNumericExp, badNumericMessage)) return false;
			var amount = parseInt(this.otherAmount.value);
			// should this be limited to > $35 ???
			if (amount < 35)
			{
				alert("Other donation amount should be greater than $35.");
				return false;
			}
		}
		return true; // ok
	}
};


function submitPaypalForm()
{
	var ppForm = $("paypalForm");
	ppForm.item_name.value = "APG " + mData.levelDescription + " membership";
	ppForm.amount.value = mData.amount + ".00";  //??? verify this
	ppForm.os0.value = mData.getInfoString();
	ppForm.submit();
}


//
// this function is called when the user clicks on the "Buy Now" button
//
function buynow_hit()
{
	mData.loadFromInputForm();
	if ( mData.isValid() )
	{
		submitPaypalForm();
	}
}





