document.observe('dom:loaded', function(){
$$('.autoclear').each(function(item) {
        item.observe('focus', function() {
            item.removeClassName("greyText");
			item.removeClassName("autoclear");
			item.value = '';
        });
    });
});

document.observe('dom:changed', function(){
$$('.fieldError').each(function(item) {
        item.observe('change', function() {
			if(item.value != '')
			{
            	item.removeClassName("fieldError");
				item.removeClassName("autoclear");
			}
        });
    });
});


function validateForm(form)
{
	theForm = $(form);
	if(fieldsOk())
	{
		if(form == 'reservation-form')
		{
			confirmReservation(form);
		}
		else
		{
			theForm.submit();
		}
	}
	else
	{
		alert('Please fill in all required fields before submitting the form.');
		document.fire('dom:changed');
	}
}

function fieldsOk()
{
	result = true;
	fields = $$('.required');
	    fields.each(function(item) {
        item.removeClassName('fieldError');
        if((item.value == '') || (item.hasClassName('greyText')) || (item.value == 'Please select...'))
        {
            item.addClassName('fieldError');
            result = false;
        }
    });
    return result;
}

function confirmReservation(form)
{
	Lightview.show({href: 'confirm-reservation.html', rel: 'ajax', options: {autosize: true,topclose: true,ajax: {method: 'get',evalScripts: true}}});
}

var isNN = (navigator.appName.indexOf("Netscape")!=-1);

function autoTab(input,len, e) {
  var keyCode = (isNN) ? e.which : e.keyCode; 
  var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
  if(input.value.length >= len && !containsElement(filter,keyCode)) {
    input.value = input.value.slice(0, len);
    input.form[(getIndex(input)+1) % input.form.length].focus();
  }

  function containsElement(arr, ele) {
    var found = false, index = 0;
    while(!found && index < arr.length)
    if(arr[index] == ele)
    found = true;
    else
    index++;
    return found;
  }

  function getIndex(input) {
    var index = -1, i = 0, found = false;
    while (i < input.form.length && index == -1)
    if (input.form[i] == input)index = i;
    else i++;
    return index;
  }
  return true;
}

function confirmGroupReservation(selector)
{
	var theVal = selector.value;
	if(theVal != 'Please select...')
	{
		var people = parseInt(theVal);
		if(people > 5)
		{
			if($('accepted-group-terms').value == "0")
			{
				var theOriginalVal = $('people-original').value;
				var theUrl = 'confirm-group-reservation.php?o=' + theOriginalVal;
				Lightview.show({href: theUrl, rel: 'ajax', options: {autosize: true,topClose: false,overlayClose: false, closeButton: false, ajax: {method: 'get',evalScripts: true}}});
			}
		}
	}
}
