
/* ~~ BITCH SLAP SAFARI ~~ */

// send focus to, or check, the passed element
function sendFocus(e)
{
	var obj = $(Event.element(e).getAttribute('for'));
	obj.focus();

	// send focus or check it - but only if it exists
	if((obj.type == 'radio' || obj.type == 'checkbox') && obj.checked == false) { obj.checked = true; }
	else if(obj.type == 'checkbox' && obj.checked == true) { obj.checked = false; }

	// trigger onclick event
	obj.click();
}

// tag labels so that Safari 2 acts like other browsers in that clicking on a form label focuses on that element
// hopfully one day Apple will fix this, but I won't turn blue waiting for it
// function modified to add a help popup based on a span within the label
function tagLabels()
{
	// browser sniff, only worry about Safari 2, Safari 1 users SHOULD UPGRADE THEIR OS
	if(navigator.userAgent.indexOf('Safari') != -1 && navigator.appVersion.indexOf('2.0') != -1)
	{
		// get all labels in the document
		var labels = $A(document.getElementsByTagName('label'));
		// grab all the label elements
		labels.each(function(label) {
			Event.observe(label,'click',sendFocus.bindAsEventListener(this),false);
		});
	}
}


/* ~~ SET SAMPLE VALUE BASED ON TITLE ATTRIBUTE ~~*/

// loop through elements and autofill sample text based on the title attribute
function altTextSample()
{
	// inputs
	inputs = document.getElementsByTagName('input');

	for(var i = 0; i < inputs.length; i++)
	{
		if(inputs[i].type == 'text' && inputs[i].title != '')
		{
			if(inputs[i].value == '' || !inputs[i].value) { setAltValue(inputs[i]); }

			inputs[i].onfocus = function() { toggleAltValue(this); }
			inputs[i].onblur = function() { toggleAltValue(this); }
		}
	}

	// textareas
	textareas = document.getElementsByTagName('textarea');

	for(var i = 0; i < textareas.length; i++)
	{
		if(textareas[i].title != '')
		{
			if(textareas[i].value == '' || !textareas[i].value) { setAltValue(textareas[i]); }

			textareas[i].onfocus = function() { toggleAltValue(this); }
			textareas[i].onblur = function() { toggleAltValue(this); }
		}
	}
}

// toggle the value of the selected input
function toggleAltValue(obj)
{
	if(obj.value.length < 1) { setAltValue(obj); }
	else if(obj.value == obj.title) { unsetAltValue(obj); }
}

// set value to that of the title attribute
function setAltValue(obj)
{
	obj.value = obj.title;
	obj.style.color = 'gray';
}

// clear value to accept user input
function unsetAltValue(obj)
{
	obj.value = '';
	obj.style.color = 'black';
}


/*~~ CALENDAR HELPER FUNCTIONS ~~*/

// see if today is a special day
function dateIsSpecial(year,month,day)
{
	var m = SPECIAL_DAYS[month];
	if(!m) { return false; }

	for(var i in m)
	{
		if(m[i] == day) { return true; }
	}

	return false;
}

// pass this function name for dateStatusFunc when setting up a calendar
//		Calendar.setup({
//			...
//			dateStatusFunc : calendarSpecialDayStatus
//		});
// and include a version of
//		 var SPECIAL_DAYS = { month : [day,day,day] };
// to enable highlighting of special days. Month is zero based - January = 0
// display is controlled by css via .special { ... }
function calendarSpecialDayStatus(date,y,m,d)
{
	if(dateIsSpecial(y,m,d)) { return 'special'; }
	else { return false; } // return true to disable other dates
}


/*~~ PREP THE FORM FOR DUTY ~~*/

// form onsubmit action to clear any generic values before submission
function prep_submit()
{
	for(var i = 0; i < inputs.length; i++)
	{
		if(inputs[i].value == inputs[i].title) { unsetAltValue(inputs[i]); }
	}

	for(var i = 0; i < textareas.length; i++)
	{
		if(textareas[i].value == textareas[i].title) { unsetAltValue(textareas[i]); }
	}

	return true;
}

// if a form fails js validation the alt-tag-preview needs to be re-enabled
function unprep_submit()
{
     for(var i = 0; i < inputs.length; i++)
     {
     	if(inputs[i].value == '') { setAltValue(inputs[i]); }
     }

     for(var i = 0; i < textareas.length; i++)
     {
     	if(textareas[i].value == '') { setAltValue(textareas[i]); }
     }

     return true;
}

/*~~ HELP ~~*/
function setHelp()
{
	var help_imgs = $$('img.form_help');
	help_imgs.each(function(img) {
			new Control.Modal(img,{contents:img.next('span.help_text').innerHTML, 
												 hover:true, 
												 position:'mouse',
												 offsetTop:10,
												 offsetLeft:10});			
		});
}

// prepare the form
function prep_form()
{
	tagLabels();
	altTextSample();
	setHelp();
}

Event.observe(window,'load',prep_form,false);
