/*
JavaScript Document for contributions.mittromney.com
Author: James Nicol, www.enilsson.com, April, 2007
 */
 
 var Site = {
	 
	 // set some function variables
	 fields : new Array,
	 // initialisation function
	 start : function()
	 	{
			$$('#fp_table .form_input').each(function(el){ Site.fields[el.name] = el.value; });
			Site.behaviour();
			Site.pulse();
		},
	// apply behaviours to elements via CSS selectors	
	behaviour : function()
		{
			// remove the field name from input
			$$('#fp_table .form_input').each(function(el) 
			{
				Event.observe(el,'focus',function() {
					if(Site.fields[el.name] == el.value)
					{ 
						el.value = ''; 
						el.style.color = '#000000'; 
					}
				});
				Event.observe(el,'blur',function() {
					if(el.value == '')
					{ 
						el.value = Site.fields[el.name];
						el.style.color = '#999999'; 
					}								 
				});
			});
			// clean signup data from post array on submit
			$$('#continue').each(function(el)
			{
				el.onclick = function() { $$('#fp_table .form_input').each(function(b) { if(Site.fields[b.name] == b.value){ b.value = ''; } }); }
			});
			// report card functions
			$$('#view_rc').each(function(el)
			{
				el.onclick = function()
				{ 
					Effect.toggle('send_reportcard','blind'); 
					return false; 
				}	
			});
			$$('#send_rc').each(function(el)
			{
				el.onclick = function(){ Site.sendRC(); return false; }	
			});
			$$('#rc_email').each(function(el)
			{
				el.onfocus = function(){ if(el.value == 'Your Email'){ el.value = ''; el.style.color = '#000000'; } }	
			});
		} ,
	// send report card
	sendRC : function()
		{
			if(!Site.validEmail($F('rc_email')))
			{
				msg = '<p class="fail_message">Please enter a valid email address!</p>';
				new Insertion.Bottom('send_reportcard',msg);
				Site.pulse();
				return;
			}
			new Ajax.Request(siteURL + 'reportcard/send',
				{ 
					method: 'post',
					postBody: 'rc_email=' + $F('rc_email'),
					onSuccess: function(t)
					{
						if(t.responseText == 'success')
						{
							msg = '<p class="success_message">Your report card has been sent successfully!</p>';
						}
						else
						{
							msg = '<p class="fail_message">' + t.responseText + '</p>';
						}
						Site.removeMSG();
						new Insertion.Bottom('send_reportcard',msg);
						Site.pulse();
					}
				}
			);			
		},
	// remove error messages from reportcard div
	removeMSG : function ()
	{
		$$('#send_reportcard .success_message','#send_reportcard .fail_message').each(function(el)
		{
			$('send_reportcard').removeChild(el);	
		});	
	},
	// validate email
	validEmail : function(str)
	{
		var at=str.indexOf("@");
		var dot=str.indexOf(".");
		var lastdot=str.lastIndexOf(".");
		
		if((dot < 0) || (at < 0)) {return false;}
		else if (lastdot < at) {return false;}
		else if (str.length < 3) {return false;}
		else if (lastdot+1 == str.length) {return false;}
		else {return true;}	
	},
	// pulse the error messages
	pulse : function()
	{
		$$('.fail_message','.success_message').each(function(el)
		{
			Effect.Pulsate(el, { duration: 2.0, from:0.2 });
		});
	}	
	 
 }
 
 Event.observe(window, 'load', Site.start ); 
