﻿// JavaScript Document
window.addEvent('domready', function () {


	var aryInputValues = Array();
	$$('input').each(function (item, index) {
		aryInputValues[item.get('id')] = item.get('value');
	})


	$$('input').addEvent('focus', function () {
		if (this.get('value') == aryInputValues[this.get('id')]) {
			if (this.get('type') != 'submit') {
				this.set('value', '');
			}
		}
	});

	$$('input').addEvent('blur', function () {
		if (this.get('value') == "") {
			this.set('value', aryInputValues[this.get('id')]);
			if ($('rfv' + this.get('id'))) {
				validate(this, '');
			}
		}
	});



	if ($('frmOptions')) {
		buildData();
	}

	//Add ajax data gathering to form as its filled out in form builder
	if ($('frmBuilder')) {
		var formresponse = $('FormResponse').get('value');
		var formid = $('frmBuilder').get('name').replace("frm", "");
		$('frmBuilder').getElements('input').each(function (el) {
			el.addEvent('blur', function () {
				var myRequest = new Request({ method: 'post', url: '/form/process/' + formid });
				var formfield = el.get('name');
				myRequest.send('partial=true&formresponse=' + formresponse + '&' + formfield + '=' + el.get('value'));
			})
		})
	}

	if ($('frmSubscribe')) {
		$('frmSubscribe').set('send', {
			onSuccess: function (html, xml) {
				AlertText(html);
			}
		});

		$('frmSubscribe').addEvent('submit', function (e) {
			if (validate(document.getElementById('frmSubscribe'), '')) {
				new Event(e).stop();
				this.send();
			}
		});
	}


})


function AlertText(txt) {
    if ($('alert')) {
        $('alert').setStyle('display', 'block');
        $('alert').set('html', txt);
    }
}