ONEHAT.widget.Donate = {}; // namespace
ONEHAT.widget.Donate.patterns = {
	alpha: { 
		pattern	: /^[a-z]{1,}$/i,
		errmsg	: 'only letters (no spaces or special characters)'
	},
	alnum: {
		pattern	: /^[a-z0-9]{1,}$/i,
		errmsg	: 'only letters or numbers'
	},
	alnumSpace: {
		pattern	: /^[\w\s]{1,}$/,
		errmsg	: 'only letters, numbers, spaces, or the underscore'
	},
	alnumUnderscore: {
		pattern	: /^[\w]{1,}$/,
		errmsg	: 'only letters, numbers, or the underscore'
	},
	digit: {
		pattern	: /^[0-9]{1,}$/,
		errmsg	: 'only numbers'
	},
	yesno: {
		pattern	: /^1|0|y|n|yes|no$/i,
		errmsg	: 'yes or no'
	},
	bool: {
		pattern	: /^1|0|true|false$/i,
		errmsg	: 'a boolean value'
	},
	words: {
		pattern	: /^[a-z\s]$/i,
		errmsg	: 'letters or spaces'
	},
	title: {
		pattern	: /^[\w\s\-\/\'\",\.!]{1,}$/,
		errmsg	: 'only letters, numbers, dashes, quotes, exclamation marks, or the underscore'
	},
	message: {
		pattern	: /^[\w\s\.\,\'\"\?\!]{1,}$/,
		errmsg	: 'letters, numbers, spaces, or the following: .,\'"?!'
	},
	paragraph: {
		pattern	: /^[\w\s\.\(\)\'\"\!\?\$\/,:;&@<>#\-]{1,}$/,
		errmsg	: 'letters, numbers, spaces, or standard punctuation'
	},
	email: {
		pattern	: /^([\w\.\-\_]+)@[a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,4}$/,
		errmsg	: 'a valid email address'
	},
	url: {
		pattern	: new RegExp('^' +
					'((http|https|ftp|ftps|news)\:\/\/|~\/|\/)?' + // protocol
					'([\\w\\.]+:[\\w\\.]+@)?' + // username:password@
					'([a-zA-Z]{1}([\\w\\-]+\\.)+([\\w]{2,5}))?' + // domain
					'(:[0-9]{1,5})?' + // port
					'((\/?[\\w~]+\/?)+|\/?)' + // directory
					'(\\w+\.[\\w]{2,4})?' + // file & extension
					'((\\?\\w+=[\\w\\.%]+)?((&|&amp;)\\w+=[\\w\\.%]+)*)?' + // query string
					'(#[\\w\\-]+)?' + // anchor
					'$', 'i'),
		errmsg	: 'a valid URL'
	},
	zip: {
		pattern	: /^([0-9]{5})(-[0-9]{4})?$/,
		errmsg	: 'a valid zip code'
	},
	stateAbbr: {
		pattern	: /^A[LKSZR]|C[AOT]|D[EC]|F[ML]|G[AU]|HI|I[DLNA]|K[SY]|LA|M[EHDAINSOT]|N[EVHJMYCD]|MP|O[KKR]|P[WAR]|RI|S[CD]|T[NX]|UT|V[TIA]|W[AIVY]$/,
		errmsg	: 'a valid US state abbreviation'
	},
	street: {
		pattern	: /^[\w\s\.\-\/]{1,128}$/,
		errmsg	: 'a valid US street address'
	},
	phone: {
		pattern	: /^(\(?[0-9]{3}\)?)?[\s\.\-]?[0-9]{3}[\s\.\-]?[0-9]{4}$/,
		errmsg	: 'a valid US phone number'
	},
	ip: {
		pattern	: /^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$/,
		errmsg	: 'a valid IP address'
	},
	html: {
		pattern	:  /.*/,  // /(<\/?)(?i:(?<element>a(bbr|cronym|ddress|pplet|rea)?|b(ase(font)?|do|ig|lockquote|ody|r|utton)?|c(aption|enter|ite|(o(de|l(group)?)))|d(d|el|fn|i(r|v)|l|t)|em|f(ieldset|o(nt|rm)|rame(set)?)|h([1-6]|ead|r|tml)|i(frame|mg|n(put|s)|sindex)?|kbd|l(abel|egend|i(nk)?)|m(ap|e(nu|ta))|no(frames|script)|o(bject|l|pt(group|ion))|p(aram|re)?|q|s(amp|cript|elect|mall|pan|t(r(ike|ong)|yle)|u(b|p))|t(able|body|d|extarea|foot|h|itle|r|t)|u(l)?|var))(\s(?<attr>.+?))*>/, // Matches all valid HTML 4.01
		errmsg	: 'only html tags and content'
	},
	'file-html': {
		pattern	:  /^.*\.(php|htm|html|txt)$/,
		errmsg	: 'a file with html content (.php, .htm, .html, .txt)'
	},
	file: {
		pattern	:  /^[\w\s\.\-_]*\.[a-zA-Z0-9](3)$/,
		errmsg	: 'a file with html content (.php, .htm, .html, .txt)'
	},
	loose: {
		pattern	:  /.*/,
		errmsg	: 'anything!'
	},
	'date ISO-8601': {
		pattern	:  /^([0-9]{4})[\/-]{1}([0-9]{1,2})[\/-]{1}([0-9]{1,2})$/,
		errmsg	: 'a date in the form YYYY/MM/DD or YYYY-MM-DD'
	},
	time: {
		pattern	:  /^([0-9]{1,2})[:]{1}([0-9]{2})[\s]{0,3}(am|pm|AM|PM)$/,
		errmsg	: 'a time in the format "HH:MM pm"'
	}
	
	
} // end patterns
ONEHAT.widget.Donate.checkform = function() {
	var obj = {
	 	init: function(){
			var oThis = this;
			var n, id, submenu, ul;
			
			// Get elements to work with - manual form
			this.form = Ext.get('donateForm');
			this.personName = Ext.get('personName');
			this.personAddress = Ext.get('personAddress');
			this.personCity = Ext.get('personCity');
			this.personState = Ext.get('personState');
			this.personZip = Ext.get('personZip');
			this.personPhone = Ext.get('personPhone');
			this.personEmail = Ext.get('personEmail');
			this.donationType = Ext.get('donationType');
			this.designation = Ext.get('designation');
			this.prefix = Ext.get('prefix');
			this.moreInfo = Ext.get('moreInfo');
			this.direct = Ext.get('direct');
			this.comments = Ext.get('comments');


			// Get elements to work with - Paypal form
			this.form1 = Ext.get('paypalForm');
			this.donation = this.form1.dom.recur[0];
			this.subscribe = this.form1.dom.recur[1];
			this.hostedButton = Ext.get(this.form1.dom.hosted_button_id);
			this.amount = Ext.get(this.form1.dom.amount);
			this.business = Ext.get(this.form1.dom.business);
			this.item_name = Ext.get(this.form1.dom.item_name);
			this.sra = Ext.get(this.form1.dom.sra);
			this.src = Ext.get(this.form1.dom.src);
			this.srt = Ext.get(this.form1.dom.srt);
			this.no_shipping = Ext.get(this.form1.dom.no_shipping);
			this.a3 = Ext.get(this.form1.dom.a3);
			this.p3 = Ext.get(this.form1.dom.p3);
			this.t3 = Ext.get(this.form1.dom.t3);
			this.no_note = Ext.get(this.form1.dom.no_note);
			this.cmd = Ext.get(this.form1.dom.cmd);		
			
			// Assign event handlers
			this.form.on('submit', function(e) {
				oThis.checkForm(e);
			});
			this.form1.on('submit', function(e) {
				oThis.sendDonationForm(e);
			});
		},
		
		sendDonationForm: function(e) {
		//	e.stopEvent();
			if (this.donation.checked == true) { // one-time donation	
				// remove inputs that don't have to do with this
				this.business.remove();
				this.item_name.remove();
				this.sra.remove();
				this.no_shipping.remove();
				this.src.remove();
				if (this.srt) {
					this.srt.remove();
				}
				this.a3.remove();
				this.p3.remove();
				this.t3.remove();
				this.no_note.remove();

				this.cmd.dom.value = '_s-xclick';

			} else { // recurring donation	
						
				// remove inputs that don't have to do with this
				this.a3.dom.value = this.amount.dom.value;
				this.hostedButton.remove();
				this.cmd.dom.value = '_xclick-subscriptions';
			}
		//	debugger;
		},
		
		checkForm: function(e) {
			try {
				// make sure required elements are present
			//	if (this.prefix.dom.value == '') { throw('Please enter your name prefix.'); }
			//	if (!this.prefix.dom.value.match(ONEHAT.widget.Donate.patterns.alnum.pattern)) { throw('Your name prefix number contains illegal characters. Please limit to ' + ONEHAT.widget.Donate.patterns.alnum.errmsg); }

				if (this.personName.dom.value == '') { throw('Please enter your name.'); }
				if (!this.personName.dom.value.match(ONEHAT.widget.Donate.patterns.title.pattern)) { throw('Your name contains illegal characters. Please limit to ' + ONEHAT.widget.Donate.patterns.title.errmsg); }

				if (this.personAddress.dom.value == '') { throw('Please enter your address.'); }
				if (!this.personAddress.dom.value.match(ONEHAT.widget.Donate.patterns.paragraph.pattern)) { throw('Your address contains illegal characters. Please limit to ' + ONEHAT.widget.Donate.patterns.paragraph.errmsg); }
			
				if (this.personCity.dom.value == '') { throw('Please enter your city.'); }
				if (!this.personCity.dom.value.match(ONEHAT.widget.Donate.patterns.title.pattern)) { throw('Your city contains illegal characters. Please limit to ' + ONEHAT.widget.Donate.patterns.title.errmsg); }

				if (this.personState.dom.value == '') { throw('Please enter your state.'); }
				if (!this.personState.dom.value.match(ONEHAT.widget.Donate.patterns.title.pattern)) { throw('Your state contains illegal characters. Please limit to ' + ONEHAT.widget.Donate.patterns.title.errmsg); }

				if (this.personZip.dom.value == '') { throw('Please enter your zip code.'); }
				if (!this.personZip.dom.value.match(ONEHAT.widget.Donate.patterns.zip.pattern)) { throw('Your zip code contains illegal characters. Please limit to ' + ONEHAT.widget.Donate.patterns.zip.errmsg); }

				if (this.personPhone.dom.value == '') { throw('Please enter your phone number.'); }
				if (!this.personPhone.dom.value.match(ONEHAT.widget.Donate.patterns.phone.pattern)) { throw('Your phone number contains illegal characters. Please limit to ' + ONEHAT.widget.Donate.patterns.phone.errmsg); }

			//	if (this.personEmail.dom.value == '') { throw('Please enter your email address.'); }
			//	if (!this.personEmail.dom.value.match(ONEHAT.widget.Donate.patterns.email.pattern)) { throw('Event phone number contains illegal characters. Please limit to ' + ONEHAT.widget.Donate.patterns.email.errmsg); }

				if (this.donationType.dom.value == '') { throw('What type of donation is this?'); }
				if (!this.donationType.dom.value.match(ONEHAT.widget.Donate.patterns.alnum.pattern)) { throw('Your donation type contains illegal characters. Please limit to ' + ONEHAT.widget.Donate.patterns.alnum.errmsg); }

				if (this.designation.dom.value == '') { throw('Please select a gift designation.'); }
				if (!this.designation.dom.value.match(ONEHAT.widget.Donate.patterns.alnum.pattern)) { throw('Your gift designation contains illegal characters. Please limit to ' + ONEHAT.widget.Donate.patterns.alnum.errmsg); }

			//	moreInfo
			//	direct

			//	if (this.comments.dom.value == '') { throw('Please add comments.'); }
			//	if (!this.comments.dom.value.match(ONEHAT.widget.Donate.patterns.paragraph.pattern)) { throw('Your comments contain illegal characters. Please limit to ' + ONEHAT.widget.Donate.patterns.paragraph.errmsg); }

			} catch(err) {
				e.stopEvent();
				Ext.Msg.show({
					title: 'Form Error',
					msg: err,
					buttons: Ext.Msg.OK,
					width: 400
				});
			}
			
		} // end checkForm()
		
	};
	obj.init();
	return obj;
}();
