$(document).ready(function(){

	// add context to date of birth fields.
    $('input[name^=dob]').each(function(){

		if( $(this).val() == '' ) {
			$(this).val( $(this).attr('name').substring(3) )
				   .addClass('fieldname')
		}
		$(this).bind('focus', function(){

					if( $(this).val() == $(this).attr('name').substring(3) )
						$(this).val('').removeClass('fieldname')

				})
			   .bind('blur', function(){

						if( $(this).val() == '' )
							$(this).val( $(this).attr('name').substring(3) ).addClass('fieldname')
						else
							$(this).removeClass('fieldname')

					})

    }).siblings('span').hide()

});

function updatePartyTotal()
{
    var adults = (document.booking.adults.options[document.booking.adults.selectedIndex].text=='')?0:parseInt(document.booking.adults.options[document.booking.adults.selectedIndex].text)
    var children = (document.booking.children.options[document.booking.children.selectedIndex].text=='')?0:parseInt(document.booking.children.options[document.booking.children.selectedIndex].text)
    var partyTotal = document.getElementById("partyTotal").getElementsByTagName('span')[0]
    var total = (adults + children)
    if ( partyTotal != null) partyTotal.innerHTML = total
    partyTotal.style.color = ( total > 12 )?'#ff0000':'#000'
    partyTotal.parentElement.parentElement.getElementsByTagName('th')[1].style.color = ( total > 12 )?'#ff0000':'#000'
}

function updateBookingFee(e, price)
{
    if ( e.checked )
    {
        document.getElementById('bookingFee').getElementsByTagName('span')[0].innerHTML = "&pound;" + addCommas(price.toFixed(2))
    }
}

function addCommas(nStr)
{
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
	    x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}

function validateBooking(f)
{
    var msg = 'The following errors occured...\n';

    // Booking Details
    var departure = null;
    if ( f.departure.value == undefined )
    {
        for ( i=0; i<f.departure.length; i++ )
            if (f.departure[i].checked) departure = f.departure[i].value
    }
    else if ( f.departure.value != '' )
        departure = f.departure.value

    if ( departure == null ) msg = msg + '\n    A departure date has not been selected.'

    // Group Leader Contact Details
    if ( f.fullname.value == '' ) msg = msg + '\n    Your name is required.'
    if ( f.address.value == '' ) msg = msg + '\n    Your address is required.'
    if ( f.postcode.value == '' ) msg = msg + '\n    Your postcode address is required.'
    if ( !checkMail(f.email.value) ) msg = msg + '\n    Your email address is invalid.'

	// D.O.B.
	var d = new Date();
	var yyyy = parseInt(f.dobyyyy.value)
	var mm = parseInt(f.dobmm.value)
	var dd = parseInt(f.dobdd.value)

	if( isNaN(yyyy) || isNaN(mm) || isNaN(dd) )
		msg = msg + '\n    Your Date of Birth is not a valid date.'
	else {
		d.setFullYear( yyyy, mm-1, dd );
		if( d.getUTCDate() != dd )// handles months without 31/30/29 etc
			msg = msg + '\n    Your Date of Birth is not a valid date.'
	}

    if ( f.telhome.value + f.telwork.value + f.telmobile.value == '' ) msg = msg + '\n    At least one phone number is required.'

    // Drivers licence
    if ( f.licenceNo.value == '' ) msg = msg + '\n    Your driving licence number is required.'

    var licencetype = false
    for ( i=0; i<f.licencetype.length; i++ )
        if ( f.licencetype[i].checked ) licencetype = true
    if ( !licencetype ) msg = msg + '\n    A Licence Type has not been selected.'
    
    if( f.endorsements.value == '' ) msg = msg + '\n    The number of endorsements is required. (0 if none)'
    if( f.bans.value == '' ) msg = msg + '\n    The number of bans is required. (0 if none)'
    

    // Confirmation
    if ( f.iagree != null )
    {
        if ( f.iagree.value != 'I Agree' ) msg = msg + '\n    The confirmation value needs to be \'I Agree\'.'
    }

    if ( msg != 'The following errors occured...\n' )
    {
        alert(msg)
        var e = $(f).find(':input[value=],input[name=dobyyyy][value=yyyy],input[name=dobmm][value=mm],input[name=dobdd][value=dd]').filter(':first')
        $.scrollTo( e.parents('table:first') , 500)
        return false;
    }
    return true;
}
function checkMail(email)
{
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,6})+$/;
	return filter.test(email)
}