/* generates a registration form */
var OverlaidRegister =
{
	archetype:	null,
	button_style:'overlaid-register',
	id: 'div-overlaid-register'	//to avoid conflicts
};
OverlaidRegister.show=function()
{
	// positioning
	this.position();
	
	// cancel buttons
	this.add_cancels();
	
	this.sync_fields(this.archetype,$(this.id).down(),'refer');

	$$('#'+$(this.id).identify()+' input[type="button"]').each(function(item) {
		// add the submit handler
		Event.observe(item,"click",function(evt){
			
			var e=Event.element(evt);
			var form=e.form;

			Spinner.show();
			new Ajax.Request(form.action,{
				method:form.method,
				parameters:form.serialize(),
				onComplete:function(){
					Spinner.hide();
				}
			});
		});
	});
	
	// use google for long/lat
	LongLatGUI.init($('address'), {
		display_address:'display_address',
		country:'country',
		accuracy:'accuracy',
		country_code:'country_code',
		city:'city',
		region:'region_code',
		postal_code:'postal_code',
		longitude:'long',
		latitude:'lat'});
	
	$('address').observe('location:selected', function(e) {
		if (e.memo.location.AddressDetails.Accuracy > 4)
			$('display_only').show();
	});
	
	//dropdown testing
	if ($('birth_month')) Event.observe($('birth_month'),'change',this.fix_month.bind(this));
	if ($('birth_year')) Event.observe($('birth_year'),'change',this.fix_month.bind(this));
	
	try { pageTracker._trackPageview('/register1'); } catch(e) { }
};

OverlaidRegister.process=function(item)
{
	this.archetype=$(item.readAttribute('rel'));
	Event.observe(item,"click",function(){
		
		Overlay.show(this);
		this.archetype=$(item.readAttribute('rel'));

		new Ajax.Request(this.archetype.action+"/"+this.id,{
			method:this.archetype.method,
			parameters:this.archetype.serialize(),
			onComplete:this.show.bind(this)
		});
	}.bind(this));
};
OverlaidRegister.processAll=function()
{
	$$('.'+OverlaidRegister.button_style).each(OverlaidRegister.process.bind(OverlaidRegister));
};
OverlaidRegister.autoShow=function()	//called from the Login overlay
{
	Overlay.show(this);
	new Ajax.Request(this.archetype.action+"/"+this.id,{
		method:this.archetype.method,
		parameters:this.archetype.serialize(),
		onComplete:this.show.bind(this)
	});
};
OverlaidRegister.fix_month=function()
{
	// DD should be 1-31 except April, June, September, November (1-30) and Feburary should be 1-29.
	var stop_here;
	var days = stop_here = $('birth_day').options.length;
	var year = $('birth_year').value;
	switch($('birth_month').value) {
	case "2":
		stop_here=(year%4==0)?29:28;
		break;
	case "4":
	case "6":
	case "9":
	case "11":
		stop_here=30;
		break;
	default:
		stop_here=31;
	}

	if(days>stop_here) {
		for(var i=days; i>stop_here; i--)
			$('birth_day').options[i]=null;
	} else if(days<stop_here) {
		for(var i=days; i<stop_here; i++)
			$('birth_day').options[$('birth_day').options.length] = new Option(i+1,i+1);
	}
	$('birth_day').options.length=stop_here;
};

/* extend */
Massify.extend(OverlaidRegister,Overlaid);
/*
*/
Event.observe(window,"load",function()
{
	OverlaidRegister.processAll();
	//auto show the first one based on QS
	if (document.location.search.indexOf('?register') >-1 || document.location.search.indexOf('&register') >-1) {
		OverlaidRegister.autoShow();
	}
});