


// NEWSLETTER FORMS
function csNewsletterForm(form){
	
	form.observe("submit", function(evt){

		evt.stop();

		s_formvalues = {};

		form.getInputs().each( function(input){
			console.log(input.retrieve("init_val"));

			if ( input.getValue() == input.retrieve("init_val") ){
				value = "";
			} else {
				value = input.getValue();
			}

			s_formvalues[input.name] = value;
		});

		new Ajax.Request(form.action, {
				parameters: s_formvalues,
				onSuccess: function(response){
					if ( response.headerJSON.complete ){
						form.update('<div class="form_response success">Thank you. A confirmation URL email has been emailed to you. Please follow the instructions in the email to complete your subsription.</div>');
					} else {
						if ( response.headerJSON.firstname_msg ){
							form.down('.firstname_msg').update(response.headerJSON.firstname_msg);
							form.down('.firstname_msg').show();
							form.down('.firstname_msg').addClassName("error");
						} else {
							form.down('.firstname_msg').hide();
						}
						if ( response.headerJSON.lastname_msg ){
							form.down('.lastname_msg').update(response.headerJSON.lastname_msg);
							form.down('.lastname_msg').show();
							form.down('.lastname_msg').addClassName("error");
						} else {
							form.down('.lastname_msg').hide();
						}
						if ( response.headerJSON.email_msg ){
							form.down('.email_msg').update(response.headerJSON.email_msg);
							form.down('.email_msg').show();
							form.down('.email_msg').addClassName("error");
						} else {
							form.down('.email_msg').hide();
						}
					}
				}
			});

	});

	form.select('input[type=text]').each( function(input){
		textFieldClearOnFocus(input);
	});

}

a_formfield_init_values = new Array();

function textFieldClearOnFocus(input){
	
	input.store("init_val", input.getValue());

	input.observe("focus", function(evt){
		if ( input.retrieve("init_val") == input.getValue() ){
			input.value = "";
		}
	});

	input.observe("blur", function(evt){
		if ( input.getValue().length == 0 ){
			input.value = input.retrieve("init_val");
		}		
	});

}


// USER INTERFACE INITIALISE

function csUiInit(scope){

	scope.select('form.newsletter_form').each( function(form){
		csNewsletterForm(form);
	})

}


// DOCUMENT LOAD

document.observe("dom:loaded", function() {

	$$('body').each(function(elt){
		csUiInit(elt);
	});

});
