(function($) {
window.addEvent('domready', function() {
	// CUSTOM PSEUDO SELECTORS
	 Slick.definePseudo('HPinput', function() {
		var $this = document.id(this);
		var $store = $this.retrieve('hp', new Object());
		return (($this.get('tag') == 'input' || $this.get('tag') == 'select' || $this.get('tag') == 'textarea') && typeof $store.validation != 'undefined');
	 });
	 Slick.definePseudo('HPrequired', function() {
		var $store = document.id(this).retrieve('hp', new Object());
		return (typeof $store.validation != 'undefined' && $store.validation.required == true);
	 });
	 Slick.definePseudo('HPvalid', function() {
		var $store = document.id(this).retrieve('hp', new Object());
		return (typeof $store.validation != 'undefined' && $store.validation.valid == true);
	 });
	 Slick.definePseudo('HPinvalid', function() {
		var $store = document.id(this).retrieve('hp', new Object());
		return (typeof $store.validation != 'undefined' && $store.validation.valid == false);
	 });
	 Slick.definePseudo('selected', function() {
		return (document.id(this).selected && document.id(this).get('tag') == 'option' && document.id(this).get('disabled') == false);
	 });
 
	
	// BLUR VALIDATION
	var $form = document.id('affForm'), $fName = document.id('fName'), $lName = document.id('lName'), $title = document.id('title'),
		$address1 = document.id('address1'), $address2 = document.id('address2'), $city = document.id('city'),
		$country = document.id('country'), $state = document.id('state'), $province = document.id('province'),
		$territory = document.id('territory'), $zip = document.id('zip'), $phone = document.id('phone'),
		$fax = document.id('fax'), $email = document.id('email'), $email2 = document.id('email2'), $imHandle = document.id('imHandle'),
		$imService = document.id('imService'), $organization = document.id('organization'),
		$paytoaddress1 = document.id('paytoaddress1'), $paytoaddress2 = document.id('paytoaddress2'),
		$paytocity = document.id('paytocity'), $paytocountry = document.id('paytocountry'),
		$paytostate = document.id('paytostate'), $paytoprovince = document.id('paytoprovince'),
		$paytoterritory = document.id('paytoterritory'), $paytozip = document.id('paytozip'),
		$taxclass = document.id('taxclass'), $taxid_ssn = document.id('taxid_ssn'), $paytocompany = document.id('payabletocompany'),
		$paytoperson = document.id('payabletoperson'), $url = document.id('url'), $sitecat = document.id('sitecat'),
		$sitemktg = document.id('sitemktg'), $comment = document.id('comment'), $uname = document.id('uname'),
		$pass = document.id('pass'), $pass2 = document.id('pass2'), $challengequestion = document.id('challengequestion'),
		$challengeanswer = document.id('challengeanswer'), $captchainput = document.id('captchainput'),
		$termsversion = document.id('termsversion'), $saa = document.id('saa'), $initials = document.id('initials'),
		$$customInputs = $$('dd.custominput input, dd.custominput select, dd.custominput textarea');

	$fName.store('hp', { 'validation' : { 'regex' : /^([A-z \-\']{2,34})$/i, 'replace' : /a^/, 'required' : true, 'errorMsg' : $$('dd.fName p.invalid')[0], 'valid' : false } });
	$lName.store('hp', { 'validation' : { 'regex' : /^([A-z \-\']{2,34})$/i, 'replace' : /a^/, 'required' : true, 'errorMsg' : $$('dd.lName p.invalid')[0], 'valid' : false } });
	$address1.store('hp', { 'validation' : { 'regex' : /^[\w $\\\/#'-.]{1,100}$/i, 'replace' : /a^/, 'required' : true, 'errorMsg' : $$('dd.address2 p.invalid')[0], 'valid' : false } });
	$address2.store('hp', { 'validation' : { 'regex' : /^[\w $\\\/#'-.]{1,100}$/i, 'replace' : /a^/, 'required' : false, 'errorMsg' : $$('dd.address2 p.invalid')[0], 'valid' : false } });
	$paytoaddress1.store('hp', { 'validation' : { 'regex' : /^[\w $\\\/#'-.]{1,100}$/i, 'replace' : /a^/, 'required' : true, 'errorMsg' : $$('dd.paytoaddress2 p.invalid')[0], 'valid' : false } });
	$paytoaddress2.store('hp', { 'validation' : { 'regex' : /^[\w $\\\/#'-.]{1,100}$/i, 'replace' : /a^/, 'required' : false, 'errorMsg' : $$('dd.paytoaddress2 p.invalid')[0], 'valid' : false } });
	$city.store('hp', { 'validation' : { 'regex' : /^[\w .\-\']{2,50}$/i, 'replace' : /a^/, 'required' : true, 'errorMsg' : $$('dd.city p.invalid')[0], 'valid' : false } });
	$paytocity.store('hp', { 'validation' : { 'regex' : /^[\w \-.]{2,50}$/i, 'replace' : /a^/, 'required' : true, 'errorMsg' : $$('dd.paytocity p.invalid')[0], 'valid' : false } });
	$territory.store('hp', { 'validation' : { 'regex' : /^[\w .\-\']{2,50}$/i, 'replace' : /a^/, 'required' : true, 'errorMsg' : $$('dd.state p.invalid')[0], 'valid' : false } });
	$paytoterritory.store('hp', { 'validation' : { 'regex' : /^[\w .\-\']{2,50}$/i, 'replace' : /a^/, 'required' : true, 'errorMsg' : $$('dd.paytostate p.invalid')[0], 'valid' : false } });
	$zip.store('hp', { 'validation' : { 'regex' : /^([\d]{2,10}|[A-z0-9]{4,10})$/, 'replace' : /[\W\- ]/gi, 'required' : true, 'errorMsg' : $$('dd.zip p.invalid')[0], 'valid' : false } });
	$paytozip.store('hp', { 'validation' : { 'regex' : /^([\d]{2,10}|[A-z0-9]{4,10})$/, 'replace' : /[\W\- ]/gi, 'required' : true, 'errorMsg' : $$('dd.paytozip p.invalid')[0], 'valid' : false } });
	$taxclass.store('hp', { 'validation' : { 'regex' : /^[\w]+$/i, 'replace' : /a^/, 'required' : true, 'errorMsg' : $$('dd.taxclass p.invalid')[0], 'valid' : false } });
	$taxid_ssn.store('hp', { 'validation' : { 'regex' : /^[\d]{9,15}$/, 'replace' : /[\D]/gi, 'required' : true, 'errorMsg' : $$('dd.taxid_ssn p.invalid')[0], 'valid' : false } });
	$phone.store('hp', { 'validation' : { 'regex' : /^[\d]{4,15}$/i, 'replace' : /[\D]/gi, 'required' : true, 'errorMsg' : $$('dd.phone p.invalid')[0], 'valid' : false } });
	$email.store('hp', { 'validation' : { 'regex' : /^[\w\-.]+@[\w\-.]+\.[A-Z]{2,6}$/i, 'replace' : /a^/, 'required' : true, 'errorMsg' : $$('dd.email p.invalid')[0], 'valid' : false } });
	$email2.store('hp', { 'validation' : { 'regex' : /^[\w\-.]+@[\w\-.]+\.[A-Z]{2,6}$/i, 'replace' : /a^/, 'required' : true, 'errorMsg' : $$('dd.email2 p.invalid')[0], 'valid' : false } });
	$paytocompany.store('hp', { 'validation' : { 'regex' : /^[\w]+$/i, 'replace' : /a^/, 'required' : true, 'errorMsg' : $$('dd.payableto p.invalid')[0], 'valid' : false } });
	$paytoperson.store('hp', { 'validation' : { 'regex' : /^[\w]+$/i, 'replace' : /a^/, 'required' : true, 'errorMsg' : $$('dd.payableto p.invalid')[0], 'valid' : false } });
	$organization.store('hp', { 'validation' : { 'regex' : /^[\w \-\.\!\&\'\\\/\@\!\(\)\,]{2,128}$/i, 'replace' : /a^/, 'required' : true, 'errorMsg' : $$('dd.organization p.invalid')[0], 'valid' : false } });
	$url.store('hp', { 'validation' : { 'regex' : /^[\w \\\:\/\-.]{5,150}$/i, 'replace' : /a^/, 'required' : true, 'errorMsg' : $$('dd.url p.invalid')[0], 'valid' : false } });
	$captchainput.store('hp', { 'validation' : { 'regex' : /^[\w]+$/i, 'replace' : /a^/, 'required' : true, 'errorMsg' : $$('dd.captchainput p.invalid')[0], 'valid' : false } });
	$initials.store('hp', { 'validation' : { 'regex' : /^[A-z]{2}$/, 'replace' : /a^/, 'required' : true, 'errorMsg' : $$('li.validationMsg p.invalid')[0], 'valid' : false } });
	$uname.store('hp', { 'validation' : { 'regex' : /^[\w\.\@]{6,25}$/, 'replace' : /a^/, 'required' : true, 'errorMsg' : $$('dd.uname p.invalid')[0], 'valid' : false } });
	$pass.store('hp', { 'validation' : { 'regex' : /^[\w\!\?\@\#\$\%\^\&\*]{6,15}$/, 'replace' : /a^/, 'required' : true, 'errorMsg' : $$('dd.pass p.invalid')[0], 'valid' : false } });
	$pass2.store('hp', { 'validation' : { 'regex' : /^[\w\!\?\@\#\$\%\^\&\*]{6,15}$/, 'replace' : /a^/, 'required' : true, 'errorMsg' : $$('dd.pass2 p.invalid')[0], 'valid' : false } });
	$challengequestion.store('hp', { 'validation' : { 'regex' : /^[\w]{2,50}$/, 'replace' : /a^/, 'required' : true, 'errorMsg' : $$('dd.challengequestion p.invalid')[0], 'valid' : false } });
	$challengeanswer.store('hp', { 'validation' : { 'regex' : /^[^\=\;\?]{2,50}$/, 'replace' : /a^/, 'required' : true, 'errorMsg' : $$('dd.challengeanswer p.invalid')[0], 'valid' : false } });
	$country.store('hp', { 'validation' : { 'required' : true, 'errorMsg' : $$('dd.country p.invalid'), 'valid' : false }, 'removedInputs' : { 'state' : $state.dispose(), 'province' : $province.dispose(), 'territory' : $territory, 'current' : $territory, 'taxes' : null } });
	$paytocountry.store('hp', { 'validation' : { 'required' : true, 'errorMsg' : $$('dd.paytocountry p.invalid'), 'valid' : false }, 'removedInputs' : { 'state' : $paytostate.dispose(), 'province' : $paytoprovince.dispose(), 'territory' : $paytoterritory, 'current' : $paytoterritory, 'taxes' : $$('dt.taxclass, dd.taxclass, dt.taxid_ssn, dd.taxid_ssn') } });
	$state.store('hp', { 'validation' : { 'required' : true, 'errorMsg' : $$('dd.state p.invalid'), 'valid' : false } });
	$province.store('hp', { 'validation' : { 'required' : true, 'errorMsg' : $$('dd.state p.invalid'), 'valid' : false } });
	$paytostate.store('hp', { 'validation' : { 'required' : true, 'errorMsg' : $$('dd.paytostate p.invalid'), 'valid' : false } });
	$paytoprovince.store('hp', { 'validation' : { 'required' : true, 'errorMsg' : $$('dd.paytostate p.invalid'), 'valid' : false } });
	$$customInputs.store('hp', { 'validation' : { 'regex' : /^(.|\r|\n)+$/i, 'replace' : /a^/, 'required' : true, 'errorMsg' : null, 'valid' : false } });
	$saa.store('hp', { 'removedValues' : { 'address1' : '', 'addresss2' : '', 'city' : '', 'country' : '', 'state' : '', 'province' : '', 'territory' : '', 'zip' : '' } });
	
	$title.store('hp', { 'validation' : { 'regex' : /^[^\=\;\?]{2,50}$/, 'replace' : /a^/, 'required' : false, 'errorMsg' : $$('dd.title p.invalid')[0], 'valid' : false } });
	$fax.store('hp', { 'validation' : { 'regex' : /^[\d]{4,15}$/i, 'replace' : /[\D]/gi, 'required' : false, 'errorMsg' : $$('dd.fax p.invalid')[0], 'valid' : false } });
	$imHandle.store('hp', { 'validation' : { 'regex' : /^[^\"\'\\\(\)\=\/]{4,25}$/, 'replace' : /a^/, 'required' : false, 'errorMsg' : $$('dd.imHandle p.invalid')[0], 'valid' : false } });
	$imService.store('hp', { 'validation' : { 'required' : false, 'errorMsg' : $$('dd.imService p.invalid'), 'valid' : false } });
	$comment.store('hp', { 'validation' : { 'regex' : /^(.|\r|\n){1,400}$/i, 'replace' : /a^/, 'required' : false, 'errorMsg' : $$('dd.comment p.invalid')[0], 'valid' : false } });
	$sitecat.store('hp', { 'validation' : { 'regex' : /^[^\=\;]{2,150}$/, 'replace' : /a^/, 'required' : false, 'errorMsg' : $$('dd.sitecat p.invalid')[0], 'valid' : false } });
	$sitemktg.store('hp', { 'validation' : { 'regex' : /^(.|\r|\n){1,150}$/i, 'replace' : /a^/, 'required' : false, 'errorMsg' : $$('dd.sitemktg p.invalid')[0], 'valid' : false } });

	$$('dd.optional_hidden input, dd.optional_hidden select, dd.optional_hidden textarea').each(function($this) {
		var $store = $this.retrieve('hp');

		$this.value = '';
		$this.setProperty('disabled', 'disabled');
		$store.validation.required = false;
		$store.validation.valid = true;
	});
	
	$$($fName, $lName, $title, $city, $paytocity, $territory, $paytoterritory, $zip, $paytozip, $taxid_ssn, $phone, $fax,
		$email, $imHandle, $organization, $url, $sitecat, $sitemktg, $comment, $challengeanswer).addEvent('blur', function() {
			
		var $this = document.id(this);
		var $store = $this.retrieve('hp');
		var regex = $store.validation.regex;
		var required = $store.validation.required;
		var $errorMsg = $store.validation.errorMsg;
		var trimmed = this.value.replace($store.validation.replace, '').trim();
		
		if(regex.test(trimmed) || ($this.value == '' && required == false)) {
			$store.validation.valid = true;
			$this.store('hp', $store);
			$errorMsg.fade('out');
		} else {
			$store.validation.valid = false;
			$this.store('hp', $store);
			$errorMsg.fade('in');
		}
	});

	// ADDRESSES	
	$$($address1, $paytoaddress1).addEvent('blur', function() {
		var $store = document.id(this).retrieve('hp');
		var regex = $store.validation.regex;
		var required = $store.validation.required;
		var $errorMsg = $store.validation.errorMsg;
		var $current = (document.id(this) == $address1) ? $address2 : $paytoaddress2;
		
		if(regex.test(this.value.trim()) && (regex.test($current.value) || $current.value == '')) {
			$store.validation.valid = true;
			this.store('hp', $store);
			$errorMsg.fade('out');
		} else {
			$store.validation.valid = false;
			this.store('hp', $store);
			$errorMsg.fade('in');
		}
	});
		
	$$($address2, $paytoaddress2).addEvent('blur', function() {
		var $store = document.id(this).retrieve('hp');
		var regex = $store.validation.regex;
		var $errorMsg = $store.validation.errorMsg;
		var required = $store.validation.required;
		var $current = (document.id(this) == $address2) ? $address1 : $paytoaddress1;
		
		if((regex.test(this.value.trim()) || this.value == '') && regex.test($current.value)) {
			$store.validation.valid = true;
			this.store('hp', $store);
			$errorMsg.fade('out');
		} else {
			$store.validation.valid = false;
			this.store('hp', $store);
			$errorMsg.fade('in');
		}
	});

	// SELECT BOXES
	$$($country, $paytocountry, $state, $paytostate, $province, $paytoprovince, $imService, $challengequestion, $taxclass).addEvents({
		blur : function() {
			var $this = document.id(this);
			var $store = $this.retrieve('hp');
			var $errorMsg = $store.validation.errorMsg;

			if($this.disabled == true || $this.getChildren('option:selected').length == 1 || $store.validation.required == false) {
				$store.validation.valid = true;
				$this.store('hp', $store);
				$errorMsg.fade('out');
			} else {
				$store.validation.valid = false;
				$this.store('hp', $store);
				$errorMsg.fade('in');
			}
		},
		change : function () {
			this.fireEvent('blur');
		}
	});

	// RADIOS
	$$($paytocompany, $paytoperson).addEvents({
		blur : function() {
			var $this = document.id(this);
			var $store = $this.retrieve('hp');
			var $errorMsg = $store.validation.errorMsg;

			if($this.get('checked') == true || $this.getSiblings('input:checked').length > 0) {
				$store.validation.valid = true;
				$this.store('hp', $store);
				$errorMsg.fade('out');
			} else {
				$store.validation.valid = false;
				$this.store('hp', $store);
				$errorMsg.fade('in');
			}
		},
		change : function() {
			this.fireEvent('blur');
		}
	});

	// CUSTOM
	$$customInputs.addEvents({
		blur : function() {
			var $this = document.id(this);
			var $store = $this.retrieve('hp');
			var regex = $store.validation.regex;
			var $errorMsg = $this.getSiblings('div.validationMsg').getChildren('p.invalid')[0];

			// NORMAL INPUTS
			if(($this.get('tag') == 'input' || $this.get('tag') == 'textarea') && ($this.get('type') != 'radio' && $this.get('type') != 'checkbox')) {
				if(regex.test($this.value.trim())) {
					$store.validation.valid = true;
					$this.store('hp', $store);
					$errorMsg.fade('out');
				} else {
					$store.validation.valid = false;
					$this.store('hp', $store);
					$errorMsg.fade('in');
				}
			// SELECTS AND RADIOS
			} else if($this.get('tag') == 'select' || ($this.get('tag') == 'input' && $this.get('type') == 'radio')) {
				if($this.get('checked') == true || $this.getSiblings('input:checked').length > 0 || ($this.getElement('option:selected') != null && $this.getElement('option:selected').get('value') != '')) {
					$store.validation.valid = true;
					$this.store('hp', $store);
					$errorMsg.fade('out');
				} else {
					$store.validation.valid = false;
					$this.store('hp', $store);
					$errorMsg.fade('in');
				}
			// CHECKBOXES
			} else if($this.get('tag') == 'input' && $this.get('type') == 'checkbox') {
				$store.validation.valid = true;
				$this.store('hp', $store);
				$errorMsg.fade('out');
			}
		},
		change : function() {
			document.id(this).fireEvent('blur');
		}
	});
	
// CHECK USERNAME AVAILABILITY
	$uname.request = new Request({
		url : '/usernamevalidator.php',
		method : 'post',
		link : 'chain',
		async : true,
		timeout : 10000,
		noCache : true,
		onSuccess : function(txt) {
			var $this = $uname;
			var $store = $this.retrieve('hp', new Object());
			var $errorMsg = $store.validation.errorMsg;
			var $msg = $$('dd.uname div.validationMsg p.unavailable');
			if(txt == 1) {
				$store.validation.valid = true;
				$this.store('hp', $store);
				$msg.fade('out');
				$errorMsg.fade('out');
			} else {
				$store.validation.valid = false;
				$this.store('hp', $store);
				$errorMsg.setStyle('display', 'none');
				$msg.setStyle('display', 'block');
				$msg.fade('in');
			}
		}
	});

	$uname.addEvent('blur', function() {
		var $store = document.id(this).retrieve('hp', new Object());
		var regex = $store.validation.regex;
		var $errorMsg = $store.validation.errorMsg;
		var $msg = $$('dd.uname div.validationMsg p.unavailable');
		
		if(regex.test(this.value.trim())) {
			$uname.request.send('u=' + this.value);
		} else {
			$store.validation.valid = false;
			this.store('hp', $store);
			$msg.setStyle('display', 'none');
			$errorMsg.fade('in');
		}
	});

// CHECK PASSWORD

	$pass.addEvent('blur', function() {
		var $this = document.id(this);
		var $store = $this.retrieve('hp', new Object());
		var $errorMsg = $store.validation.errorMsg;
		var $value = $this.get('value').trim();
		var regex = $store.validation.regex;
		var str = 0;
		var lc  = /[a-z]/;
		var uc  = /[A-Z\!\?\@\#\$\%\^\&\*]/;
		var num = /[0-9]/;
		str += ($value.test(lc)) ? 1 : 0;
		str += ($value.test(uc)) ? 1 : 0;
		str += ($value.test(num)) ? 1 : 0;
		
		if(regex.test( $value ) && str > 1) {
			$store.validation.valid = true;
			$this.store('hp', $store);
			$this.getParent('dd.pass').setStyle('height', '40px');
			$errorMsg.fade('out');
		} else {
			$store.validation.valid = false;
			$this.store('hp', $store);
			$errorMsg.fade('in');
			$this.getParent('dd.pass').setStyle('height', 'auto');
		}
		
		if($pass2.get('value') != '') {
			$pass2.fireEvent('blur');
		}
	});
	
	$pass2.addEvent('blur', function() {
		var $this = document.id(this);
		var $store = $this.retrieve('hp');
		var regex = $store.validation.regex;
		var $errorMsg = $store.validation.errorMsg;
		
		if(this.value.trim() == $pass.value.trim() && this.value.trim() != '') {
			$store.validation.valid = true;
			$this.store('hp', $store);
			$errorMsg.fade('out');
		} else if($pass.value.trim() == '') {
			$store.validation.valid = false;
			$this.store('hp', $store);
			$errorMsg.fade('out');
		} else {
			$store.validation.valid = false;
			$this.store('hp', $store);
			$errorMsg.fade('in');
		}
	});

// CHECK EMAIL
	$email2.addEvent('blur', function() {
		var $this = document.id(this);
		var $store = $this.retrieve('hp');
		var regex = $store.validation.regex;
		var $errorMsg = $store.validation.errorMsg;

		if($this.value.trim() == $email.value.trim() && $this.value.trim() != '') {
			$store.validation.valid = true;
			$this.store('hp', $store);
			$errorMsg.fade('out');			
		} else if($email.value.trim() == '') {
			$store.validation.valid = false;
			$this.store('hp', $store);
			$errorMsg.fade('out');
		} else {
			$store.validation.valid = false;
			$this.store('hp', $store);
			$errorMsg.fade('in');
		}
	});

// WHITELISTING POPUP
	$email.addEvents({
		focus : function() {
			document.id('whitelisting').fade('in');
		},
		blur : function() {
			(function() {
				document.id('whitelisting').fade('out')
			}).delay(10000);
			this.getNext('input');
		}
	});

// COUNTRY SELECTIONS
	$$($country, $paytocountry).addEvents({
		blur : function() {
			var $this = document.id(this), val = this.value, $zipBox = $$('dd.paytozip')[0];
			var $store = $this.retrieve('hp');
			var $label = ($this.get('id') == "country") ? $$('dt.state label') : $$('dt.paytostate label');
			var $container = ($this.get('id') == "country") ? $$('dd.state')[0] : $$('dd.paytostate')[0];

			switch(val) {
				case "us":
				case "united states":
					$label.set('html', '<sup class="required">*</sup>State:');
					$label.setProperty('for', $store.removedInputs.state.get('id'));

					var $oldInput = $container.getChildren('input, select');
					$oldInput.dispose();
					
					var $newInput = $store.removedInputs.state.inject($container, 'top');
					$newInput.setStyles({ display : 'block', opacity : '1', visibility : 'visible' });

					if($store.removedInputs.taxes != null) {
						var $taxOne = $store.removedInputs.taxes[0].inject($zipBox, 'after');
						var $taxTwo = $store.removedInputs.taxes[1].inject($taxOne, 'after');
						var $taxThree = $store.removedInputs.taxes[2].inject($taxTwo, 'after');
						var $taxFour = $store.removedInputs.taxes[3].inject($taxThree, 'after');
					}
					break;
				case "ca":
				case "canada":
					$label.set('html', '<sup class="required">*</sup>Province/Territory:');
					$label.setProperty('for', $store.removedInputs.province.get('id'));

					var $oldInput = $container.getChildren('input, select');
					$oldInput.dispose();
					
					var $newInput = $store.removedInputs.province.inject($container, 'top');
					$newInput.setStyles({ display : 'block', opacity : '1', visibility : 'visible' });
					
					$$('dt.taxclass, dd.taxclass, dt.taxid_ssn, dd.taxid_ssn').dispose();
					break;
				default:
					$label.set('html', '<sup class="required">*</sup>Province/Territory:');
					$label.setProperty('for', $store.removedInputs.territory.get('id'));
					
					var $oldInput = $container.getChildren('input, select');
					$oldInput.dispose();
					
					var $newInput = $store.removedInputs.territory.inject($container, 'top');
					$newInput.setStyles({ display : 'block', opacity : '1', visibility : 'visible' });
					
					$$('dt.taxclass, dd.taxclass, dt.taxid_ssn, dd.taxid_ssn').dispose();
					break;
			}
		},
		change : function() {
			document.id(this).fireEvent('blur');
		}
	});

// "SAME AS" SELECTIONS
	$saa.addEvents({
		click : function() {
			var $this = document.id(this);
			var $store = $this.retrieve('hp', new Object());
			
			if(this.checked == true) {
				$store.removedValues = { 'address1' : $paytoaddress1.value,
					'address2' : $paytoaddress2.value, 'city' : $paytocity.value,
					'country' : $paytocountry.value, 'state' : $paytostate.value,
					'province' : $paytoprovince.value, 'territory' : $paytoterritory.value,
					'zip' : $paytozip.value };
				this.store('hp', $store);

				$paytoaddress1.value = $address1.value;
				$paytoaddress2.value = $address2.value;
				$paytocity.value = $city.value;
				$paytozip.value = $zip.value;
				$paytocountry.value = $country.value;
				$paytocountry.fireEvent('change');
				$paytostate.value = $state.value;
				$paytoprovince.value = $province.value;
				$paytoterritory.value = $territory.value;
				
				switch($country.value) {
					case 'us':
					case 'united states':
					case 'United States':
						$paytostate.fireEvent('blur').setProperty('disabled', 'disabled');
						break;
					case 'ca':
					case 'canada':
					case 'Canada':
						$paytoprovince.fireEvent('blur').setProperty('disabled', 'disabled');
						break;
					default:
						$paytoterritory.fireEvent('blur').setProperty('disabled', 'disabled');
						break;
				}
				$$($paytoaddress1, $paytoaddress2, $paytocity, $paytozip, $paytocountry).fireEvent('change').fireEvent('blur').setProperty('disabled', 'disabled');
			} else {
				$$($paytoaddress1, $paytoaddress2, $paytocity, $paytozip, $paytocountry, $paytostate, $paytoprovince, $paytoterritory).removeProperty('disabled');
				$paytoaddress1.value = $store.removedValues.address1;
				$paytoaddress2.value = $store.removedValues.address2;
				$paytocity.value = $store.removedValues.city;
				$paytozip.value = $store.removedValues.zip;
				$paytocountry.value = $store.removedValues.country;
				$paytocountry.fireEvent('change');
				$paytostate.value = $store.removedValues.state;
				$paytoprovince.value = $store.removedValues.province;
				$paytoterritory.value = $store.removedValues.territory;
				
				switch($paytocountry.value) {
					case 'us':
					case 'united states':
					case 'United States':
						$paytostate.fireEvent('blur');
						break;
					case 'ca':
					case 'canada':
					case 'Canada':
						$paytoprovince.fireEvent('blur');
						break;
					default:
						$paytoterritory.fireEvent('blur');
						break;
				}
				$$($paytoaddress1, $paytoaddress2, $paytocity, $paytozip, $paytocountry).fireEvent('change').fireEvent('blur');
			}
		},
		change : function() {
			document.id(this).fireEvent('blur');
		}
	});

// CHECK CAPTCHA
	 var $captchaImage = document.id('captchaimage');
	 
	$captchainput.request = new Request({
		url : '/CAPTCHA/captchavalidator.php',
		method : 'post',
		link : 'chain',
		async : true,
		timeout : 10000,
		noCache : true,
		onSuccess : function(txt) {
			var $this = $captchainput;
			var $store = $this.retrieve('hp', new Object());
			var $errorMsg = $store.validation.errorMsg;
			
			if(txt == 1) {
				$store.validation.valid = true;
				$this.store('hp', $store);
				$errorMsg.fade('out');
			} else {
				var randomnumber = Math.floor(Math.random() * 1001);
				$store.validation.valid = false;
				$this.store('hp', $store);
				$captchaImage.src = '/CAPTCHA/captcha.php?reloadimage=' + randomnumber;
				$this.value = '';
				$errorMsg.fade('in');
			}
		}
	});
	
	$captchainput.addEvent('blur', function() {
		var $store = this.retrieve('hp');
		var $errorMsg = $store.validation.errorMsg;
		var regex = $store.validation.regex;
		
		if(regex.test(this.value)) {
			$store.validation.valid = true;
			this.store('hp', $store);
			$errorMsg.fade('out');
		} else {
			$store.validation.valid = false;
			this.store('hp', $store);
			$errorMsg.fade('in');
		}
	});

// SET INITIALS
	var $fInitial = document.id('fnameinitial'), $fComplete = document.id('completedfname'), $lInitial = document.id('lnameinitial'), $lComplete = document.id('completedlname');

	$fName.addEvent('blur', function() {
		var trimmed = this.value.replace(/[^A-Z]/gi, '');
		
		if(trimmed.length > 0) {
			$fInitial.set('text', trimmed.substr(0, 1).toUpperCase());
			$fComplete.set('text', trimmed.substr(1));
		}
	});
	
	$lName.addEvent('blur', function() {
		var trimmed = this.value.replace(/[^A-Z]/gi, '');
		
		if(trimmed.length > 0) {
			$lInitial.set('text', trimmed.substr(0, 1).toUpperCase());
			$lComplete.set('text', trimmed.substr(1));
		}
	});
	
	$initials.addEvent('blur', function() {
		var $store = this.retrieve('hp');
		var regex = $store.validation.regex;
		var $errorMsg = $store.validation.errorMsg;
		var finit = $fName.get('value').replace(/[^A-Z]/gi, '').substr(0, 1).toUpperCase();
		var linit = $lName.get('value').replace(/[^A-Z]/gi, '').substr(0, 1).toUpperCase();
		
		if(regex.test(this.value) && this.value.toUpperCase() == finit + linit) {
			
			this.value = this.value.toUpperCase();
			$store.validation.valid = true;
			this.store('hp', $store);
			$errorMsg.fade('out');
			
		} else {
			
			$store.validation.valid = false;
			this.store('hp', $store);
			$errorMsg.fade('in');
			
		}
	});


// Navigation
	var $submit = document.id('affSubmit'), $fieldsets = $$('#affForm fieldset'), $prev = $$('#affForm fieldset div.buttonBox a.previousBtn'), $next = $$('#affForm fieldset div.buttonBox a.nextBtn, #affForm fieldset div.buttonBox a.anchor'),
	$submit = document.id('affSubmit'), $appheader = $$('#affForm div.applicationheader '), $bullets = $$('#affForm div.applicationheader span.activestepID, #affForm div.applicationheader span.inactivestepID'),
	$headers = $$('#affForm div.applicationheader span.activestep, #affForm div.applicationheader span.inactivestep');
	
	$prev.each(function($this) {
		$this.addEvent('click', function() {
			var pgid = $this.getParent('fieldset').get('id').replace(/[\D]/gi, '');

			pgid--;

			var $bullet = document.id('bullet' + pgid);
			var $header = document.id('header' + pgid);
			var $current = document.id('step' + pgid);

			$bullets.removeClass('activestepID').addClass('inactivestepID');
			$headers.removeClass('activestep').addClass('inactivestep');
			
			$bullet.removeClass('inactivestepID').addClass('activestepID');
			$header.removeClass('inactivestep').addClass('activestep');
			
			$fieldsets.setStyle('display', 'none');
			$current.setStyles({ display : 'block', opacity : 1, visibility : 'visible' });
			document.id(window).scrollTo(0, 0);

			return false;
		});
	});
	
	$next.each(function($this) {
		if($this != $submit) {
			$this.addEvent('click', function() {
				var pgid = $this.getParent('fieldset').get('id').replace(/[\D]/gi, '');
				var $currentInputs = $this.getParent('fieldset').getElements('input, select:HPrequired, textarea');				
				$currentInputs.fireEvent('blur');
				
				// CHECK IF THE CAPTCHA SHOULD BE FIRED
				if($this.getParent('fieldset').get('id') == 'step4') {
					$captchainput.request.send('value=' + $captchainput.value);
				}

				var $invalidInputs = $currentInputs.filter(':HPinvalid');
				
				if($invalidInputs.length == 0) {
					pgid++;

					var $bullet = document.id('bullet' + pgid);
					var $header = document.id('header' + pgid);
					var $current = document.id('step' + pgid);

					$bullets.removeClass('activestepID').addClass('inactivestepID');
					$headers.removeClass('activestep').addClass('inactivestep');
					
					$bullet.removeClass('inactivestepID').addClass('activestepID');
					$header.removeClass('inactivestep').addClass('activestep');
					
					$fieldsets.setStyle('display', 'none');
					$current.setStyles({ display : 'block', opacity : 1, visibility : 'visible' });
					document.id(window).scrollTo(0, 0);
				} else {
					$invalidInputs[0].focus();
				}
				
				return false;
			});
		}
	});

// Form Submission
	// Submit Button Functionality
	$submit.addEvent('click', function() {
		var $inputs = $form.getElements('input:HPrequired, select:HPrequired, textarea:HPrequired');
		$inputs.fireEvent('blur');
		$captchainput.request.send('value=' + $captchainput.value);
		
		var $invalidInputs = $inputs.filter(':HPinvalid');
		if($invalidInputs.length > 0) {
			if($appheader.getStyle('display') == 'block') {
				var $current = $invalidInputs[0].getParent('fieldset');
				var pgid = $current.get('id').replace(/[\D]/gi, '');
				
				if(pgid < 5) {
					pgid++;
					var $prevBtn = document.id('step' + pgid).getElements('a.previousBtn');
					$prevBtn.fireEvent('click');
				}
			}
			
			$invalidInputs[0].focus();
		} else {
			$form.submit();
		}
		
		return false;
	});
	
	$form.addEvent('submit', function() {
		this.submit();
	});
});

})(document.id);

