// JavaScript Document
var registration_form = {
	default_values : [],
	monthly_total_template : '<p id="#{id}">Your Monthly Total: $<span>0.00</span></p>',
	my_validator : new Validator(),
	ala_carte : {
		mouseOverHandler : function(e){
			if(!this.hasClassName('locked')){
				this.addClassName('hover');
			}
		},
		mouseOutHandler : function(e){
			if(!this.hasClassName('locked')){
				this.removeClassName('hover');
			}
		},
		clickHandler : function(e){
			if(!this.hasClassName('locked')){
				var box = Element.select(this,'input')[0];
				var event_tag = Event.element(e).tagName.toLowerCase();
				if(Event.element(e).tagName.toLowerCase() != 'input'){
					if(box.hasClassName('image-override')){
						registration_form.ala_carte.toggleImageOverride(this);
					}
				}
				if(event_tag != 'label' && event_tag != 'input' && event_tag != 'span'){
					box.checked = !box.checked;
				}
				this.removeClassName('was-selected');
				registration_form.setMonthlyTotal();
			}
		},
		toggleImageOverride : function(list_item){
			list_item.toggleClassName('selected');
		}
	},
	registration_types : {
		mouseOverHandler : function(e){
			this.addClassName('hover');
		},
		mouseOutHandler : function(e){
			this.removeClassName('hover');
		},
		clickHandler : function(e){
			var radio_button = Element.select(this,'input')[0]
			radio_button.checked = true;
			
			if(radio_button.identify() == 'registration-type-4'){
				$$('ul#ala-carte-options li.add-on').each(function(addon){
					addon.addClassName('locked');
					var box = addon.select('input').first();
					if(box.checked){
						addon.addClassName('was-selected');	
					}
					box.checked = true;
					if(box.hasClassName('image-override')){
						addon.addClassName('selected');
					}
					addon.down('.price-wrapper').hide();
					var included = addon.down('span.included');
					if(included){
						included.show();
					} else {
						addon.down('.price-wrapper').insert({after: '<span class="included">Included</span>'});
					}
				});
			} else {
				$$('ul#ala-carte-options li.add-on').each(function(addon){
					if(addon.hasClassName('locked') && !addon.hasClassName('was-selected')){
						var box = addon.select('input').first();
						box.checked = false;
						if(box.hasClassName('image-override')){
							addon.removeClassName('selected');
						}
					}
					addon.removeClassName('locked');
					addon.down('.price-wrapper').show();
					if(addon.down('.included')){
						addon.down('.included').hide();
					}
				});
			}
			
			
			//if(radio_button.hasClassName('has-options')){
			//	Element.show('features-fieldset');
			//} else {
			//	Element.hide('features-fieldset');
			//}
			if(radio_button.hasClassName('image-override')){
				registration_form.registration_types.toggleImageOverride(this);
			}
			registration_form.setMonthlyTotal();
		},
		toggleImageOverride : function(list_item){
			$$('ul#registration-types li.selected').each(function(object,index){
				if(object.identify() != list_item.identify()){
					object.removeClassName('selected');
				}
			});
			list_item.addClassName('selected');
		}
	},
	default_fields : {
		focusHandler : function(e){
			var new_id = this.id.replace('-plain','');
			if($F(new_id) == registration_form.default_values[new_id]){
				$(new_id).value = '';
			}
			if($(this).hasClassName('password') && $(this).hasClassName('plain-text')){
				Element.hide(this.id);
				Element.show(new_id);
				$(new_id).focus();
			}
		},
		blurHandler : function(e){
			if(this.value == ''){
				this.value = registration_form.default_values[this.id];
				if($(this).hasClassName('password') && !$(this).hasClassName('plain-text')){
					Element.hide(this.id);
					Element.show(this.id + '-plain');
				} 
			}
		}
	},
	hint_fields : {
		focusHandler : function(e){
			var object = $(this);
			anchor_item = object;
			if(object.title != ''){
				Element.insert(anchor_item,{after: '<span class="hint" id="' + object.identify() + '-hint">' + object.title + '</span>'});
			}	
		},
		blurHandler : function(e){
			var object = $(this);
			$(object.identify() + '-hint').remove();
		}
	},
	handle : {
		blurHandler : function(e){
			var url = '/scripts/www/checkhandle.asp';
			var parameters = 'handle=' + encodeURI(this.value);
			if(this.value != '' && this.value != registration_form.default_values[this.id]){
				new Ajax.Request(url, {
					parameters: parameters,
					method: 'get',
					onSuccess: function (resp, data) {
						if($('handle-response')){
							Element.remove('handle-response');	
						}
						if($('error-handle')){
							Element.remove('error-handle');	
						}
						if(!data.is_available){		
							new Insertion.Bottom('handle-container','<span id="handle-response" class="error">This handle is already in use.</span>');
							if(!Element.hasClassName('handle','text-error')){
								Element.addClassName('handle','text-error');
							}
							Element.update('example-handle','handle');
						} else {
							new Insertion.Bottom('handle-container','<span id="handle-response" class="valid">This handle is available.</span>');
							if(Element.hasClassName('handle','text-error')){
								Element.removeClassName('handle','text-error');	
							}
							Element.update('example-handle',data.handle);
						}
					}
				});
			}
			return true;
		},
		changeHandler : function(e){
			if($('handle-response')){
				Element.remove('handle-response');	
			}
	 		return true;
		}
	},
	i_agree : {
		clickHandler : function(e){
			//$('submit-button').disabled = !$('i-agree').checked
			return true;
		}
	},
	initialize : function(){
		document.observe('dom:loaded',registration_form._init);
	},
	_init : function(){
		//Make Footer Links External
		if($('ft')){
			$('ft').select('a').each(function(object,index){
				Element.writeAttribute(object,{rel: 'external'});								  
			});
		}
		
		//Add Monthly Total Inidcator
		//var total_template = new Template(registration_form.monthly_total_template);
		//Element.insert($('create-my-account').select('h2').first(),{after: total_template.evaluate({id: 'monthly-total'})});
		
		/*		
		if($('registration-type-4').checked){
			$$('ul#ala-carte-options li.add-on').each(function(addon){
				addon.addClassName('locked');
				var box = addon.select('input').first();
				if(box.checked){
					addon.addClassName('was-selected');	
				}
				box.checked = true;
				if(box.hasClassName('image-override')){
					addon.addClassName('selected');
				}
				addon.down('.price-wrapper').hide();
				var included = addon.down('span.included');
				if(included){
					included.show();
				} else {
					addon.down('.price-wrapper').insert({after: '<span class="included">Included</span>'});
				}
			});
		}
		*/
	
		//Disable Submit Button
		//$('submit-button').disabled = !$('i-agree').checked;
		
		//Setup Default Values
		$$('.has-default').each(function(element,index){
			registration_form.default_values[element.id] = element.value;	 
		});
		
		//Convert Password Fields to plain text {
		$$('.password').each(function(field,index){
			Element.hide(field);
			new Insertion.After(field,'<input title="' + field.title + '" type="text" id="' + field.id + '-plain" value="' + field.value + '" name="' + field.name + '-overlay" class="has-default text password plain-text" />');
		});
		//}
		
		//Default Element Event Handlers
		$$('.has-default').each(function(element,index){
			if(!element.hasClassName('password')){
				Event.observe(element,'focus',registration_form.default_fields.focusHandler.bindAsEventListener(element));
				Event.observe(element,'blur',registration_form.default_fields.blurHandler.bindAsEventListener(element));
			} else {
				if(!element.hasClassName('plain-text')){
					Event.observe(element,'blur',registration_form.default_fields.blurHandler.bindAsEventListener(element));
				} else {
					Event.observe(element,'focus',registration_form.default_fields.focusHandler.bindAsEventListener(element));
				}
			}
		});
		
		//Hint Event Handlers
		$$('.has-hint').each(function(element,index){
			Event.observe(element,'focus',registration_form.hint_fields.focusHandler.bindAsEventListener(element));
			Event.observe(element,'blur',registration_form.hint_fields.blurHandler.bindAsEventListener(element));
		});
		
		/*
		$$('input.image-override').each(function(input,index){
			Element.hide(input);
			var list_item = Element.ancestors(input).first();
			list_item.addClassName('image-override-container');
			if(input.checked){
				list_item.addClassName('selected');
			}
		});
		*/
		
		//Observe Handle Field
		Event.observe('handle','blur',registration_form.handle.blurHandler.bindAsEventListener($('handle')));
		Event.observe('handle','change',registration_form.handle.changeHandler.bindAsEventListener($('handle')));
		
		//Observe I Agree button
		Event.observe('i-agree','click',registration_form.i_agree.clickHandler.bindAsEventListener($('i-agree')));

		//Setup Registration Types
		/*
		$$('ul#registration-types li').each(function(object,index){
			Event.observe(object,'mouseover',registration_form.registration_types.mouseOverHandler.bindAsEventListener(object));		
			Event.observe(object,'mouseout',registration_form.registration_types.mouseOutHandler.bindAsEventListener(object));
			Event.observe(object,'click',registration_form.registration_types.clickHandler.bindAsEventListener(object));
			
		});
		
		//Setup Ala Carte Options
		$$('ul#ala-carte-options li').each(function(object,index){
			Event.observe(object,'mouseover',registration_form.ala_carte.mouseOverHandler.bindAsEventListener(object));
			Event.observe(object,'mouseout',registration_form.ala_carte.mouseOutHandler.bindAsEventListener(object));		
			Event.observe(object,'click',registration_form.ala_carte.clickHandler.bindAsEventListener(object));
		});
		*/
		
		//Setup Monthly Total
		registration_form.setMonthlyTotal();
	},
	setMonthlyTotal : function(){
		
		var total = 0;
		
		/*
		$$('ul#registration-types li').each(function(container,index){
			
			var input_item = container.select('input').first();
			if(input_item){
				if(input_item.checked){
					var price = container.select('.price').first();
					if(price){
						var price = new Number(price.innerHTML);
						total += price;
					}
				}
			}
		});
		
		//Check if the optional items should be included
		var features_active = false;
		$$('.has-options').each(function(checkbox){
			if(checkbox.checked){
				features_active = true;
			}
		});
		
		if(features_active){
			$$('ul#ala-carte-options li').each(function(container,index){
				var input_item = container.select('input').first();
				if(input_item){
					if(input_item.checked){
						var price = container.select('.price').first();
						if(price){
							var price = new Number(price.innerHTML);
							total += price;
						}
					}
				}
			});	
		}
		*/
		//total = new Number(total);
		//$('monthly-total').select('span').first().update(total.toFixed(2));
	},
	validate : function(){
		
	}
}	

registration_form.initialize();
