	
	var ignoreStoreValidation = false;
	
	window.addEvent('domready', function(){				
		
		if(currentForm){
			switch(currentForm){
				case 'form_registration':
					setValidateSubmitHandler();				
					handleRegistration();
					break;
				case 'form_process':
					handleProcess();	
					break;
			}
		}

	});
	
	function validateForm(){
		var valid = formValidator.validate();
		if(valid){
			return true;
		}else{
			return false;
		}
	}
	
	function submitForm(){
		showLoadingProgress();
		document.id(currentForm).submit();
	}
	
	function setSubmitHandler(){
		
		$$('#' + currentForm + ' input[type=submit]').addEvent('click', function(e){
			this.fade('out');
			submitForm();
			var e = new Event(e).stop();					
		});	

	}
	
	function setValidateSubmitHandler(){

		$$('#' + currentForm + ' input[type=submit]').addEvent('click', function(e){
			
			if(ignoreStoreValidation === false){

				if($('LocalStoreWeekend').get('value') == 'prompt' || $('LocalStoreWeekend').get('value')==''){
					alert('Please tell us your local weekend store.');
					var e = new Event(e).stop();	
					return false;
				}
				
				if($('LocalStoreWeekday').get('value') == 'prompt' || $('LocalStoreWeekday').get('value')==''){
					alert('Please tell us your local weekday store.');
					var e = new Event(e).stop();	
					return false;
				}
			
			}
			
			var result = validateForm();
			if(result){
				this.fade('out');
				submitForm();
			}
				
			var e = new Event(e).stop();					
		});	
	
	}
	
	function showLoadingProgress(){
		document.id('loadingProgress').setStyle('display','block');
		document.id('loadingProgress').setStyle('opacity',0);
		document.id('loadingProgress').fade('in');
	}
	
	function hideLoadingProgress(){
		document.id('loadingProgress').setStyle('display','none');
	}
	
	// REGO PAGE -------------------------------------------------------------
		
	function handleRegistration(){
		
		setStores();
		
		$('Post_Code').addEvent('blur',function(){
			setStores();
		});
		
		$('VicRegionW').addEvent('change',function(){
			if(this.get('value')=='prompt') return false;
			setWVicRegion();
		});
		
		$('VicRegionL').addEvent('change',function(){
			if(this.get('value')=='prompt') return false;
			setLVicRegion();
		});	
		
		$('v_c_LocalStoreWeekday').addEvent('blur',function(){
			setWeekdayValue(this);			
		});
		$('v_c_LocalStoreWeekend').addEvent('blur',function(){
			setWeekendValue(this);
		});
		
		$('v_w_LocalStoreWeekday').addEvent('blur',function(){
			setWeekdayValue(this);
		});
		$('v_w_LocalStoreWeekend').addEvent('blur',function(){
			setWeekendValue(this);
		});
		
		$('v_e_LocalStoreWeekday').addEvent('blur',function(){
			setWeekdayValue(this);
		});
		$('v_e_LocalStoreWeekend').addEvent('blur',function(){
			setWeekendValue(this);
		});
		
		$('v_s_LocalStoreWeekday').addEvent('blur',function(){
			setWeekdayValue(this);
		});
		$('v_s_LocalStoreWeekend').addEvent('blur',function(){
			setWeekendValue(this);
		});
		$('v_r_LocalStoreWeekday').addEvent('blur',function(){
			setWeekdayValue(this);
		});
		$('v_r_LocalStoreWeekend').addEvent('blur',function(){
			setWeekendValue(this);
		});		
		
		$('n_LocalStoreWeekday').addEvent('blur',function(){
			setWeekdayValue(this);
		});
		$('n_LocalStoreWeekend').addEvent('blur',function(){
			setWeekendValue(this);
		});
		
		$('q_LocalStoreWeekday').addEvent('blur',function(){
			setWeekdayValue(this);
		});
		$('q_LocalStoreWeekend').addEvent('blur',function(){
			setWeekendValue(this);
		});
		
		$('w_LocalStoreWeekday').addEvent('blur',function(){
			setWeekdayValue(this);
		});
		$('w_LocalStoreWeekend').addEvent('blur',function(){
			setWeekendValue(this);
		});	
			
		
	}
	
	function setWeekdayValue(field){
		var storeWeekdayField = $('LocalStoreWeekday');
		var fieldValue = field.get('value');
		
		storeWeekdayField.set('value',fieldValue);
		//alert(storeWeekdayField.get('value'))
	}
	
	function setWeekendValue(field){
		var storeWeekendField = $('LocalStoreWeekend');
		var fieldValue = field.get('value');
		
		storeWeekendField.set('value',fieldValue);
		//alert(storeWeekdayField.get('value'))
	}
	
	function clearStoreValues(field){
		$('LocalStoreWeekday').set('value','');
		$('LocalStoreWeekend').set('value','');
		//alert('cleared')
	}
	
	function setStores(){
		
		$('nsw_stores').hide();
		$('vic_stores').hide();
		$('qld_stores').hide();
		$('wa_stores').hide();		
		
		clearStoreValues();
		ignoreStoreValidation = false;
		
		var postCodeValue = $('Post_Code').get('value');
		var firstChar = postCodeValue.substring(0,1)

		switch(firstChar){
			case '3':
				//vic
				$('vic_stores').show();
				$('VicRegionW').focus();
				break;
			case '4':
				//qld
				$('qld_stores').show();
				$$('#qld_stores select')[0].focus();
				break;
			case '6':
				//wa
				$('wa_stores').show();
				$$('#wa_stores select')[0].focus();
				break;				
			case '2':
				//nsw
				$('nsw_stores').show();
				$$('#nsw_stores select')[0].focus();
				break;
			default:
				// message for non grilld states?
				ignoreStoreValidation = true; 
				break;						
		}
	}
	
	function setWVicRegion(){
		
		var region = $('VicRegionW').get('value');
		
		$('w_c_region').hide();
		$('w_w_region').hide();
		$('w_e_region').hide();
		$('w_s_region').hide();
		
		//clearStoreValues();
		
		$('w_'+region+'_region').show();
	}
	
	function setLVicRegion(){
		
		var region = $('VicRegionL').get('value');
		
		$('l_c_region').hide();
		$('l_w_region').hide();
		$('l_e_region').hide();
		$('l_s_region').hide();
		
		//clearStoreValues();
		
		$('l_'+region+'_region').show();
	}
	
	// PROCESS PAGE -------------------------------------------------------------
	
	function handleProcess(){
		//alert("PROCESS");
		hideLoadingProgress();
	}