function submitform(form_id) {
	$('#'+form_id).submit();
}

function clearThis(elmId,search,replace) {
	var field = $('#'+elmId);
	var val = field.val();
	if(val == search) {
		field.val(replace);
	}
}

/**
 * Updates the Tour details (date, package id's) on the SearchBox. Without
 * filling in this info, packages cannot be booked correctly.
 * 
 * @param selTour Tour Selection list box element
 * @return void
 */
function updateTourDetails(selTour) {
	var details = selTour.value.split('|');
	$('#frmsearchbox input[name="startdate"]').val(details[0]);

	$('#tour-ticket, #tour-travel-only, #select-pkg-msg').each(function() {
		this.style.display = 'none';
	});
	
	$('#tour-ticket-pkg, #tour-travel-only-pkg').each(function() {
		this.value = '';
	});
	
	if(parseInt(details[1]) > 0) {
		$('#tour-travel-only').show();
		var opt = $('#tour-travel-only-pkg');
		opt.val(details[1]);
		opt.show();
		//If TicketTravel is not available, force checked the TravelOnly travelradio box
		if( ! parseInt(details[2]) > 0) {
			opt.attr('checked', 'checked');
		} else {
			opt.removeAttr('checked');
		}
	}

	if(parseInt(details[2]) > 0) {
		$('#tour-ticket').show();
		var opt = $('#tour-ticket-pkg');
		opt.val(details[2]);
		opt.show();
		opt.attr('checked', 'checked');
	}
	
	if(parseInt(details[3]) > 0 || parseInt(details[4]) > 0)
	{
		(parseInt(details[3]) > 0) ?
			$('#r1c-container').show() :
			$('#r1c-container').hide();
		
		(parseInt(details[4]) > 0) ?
			$('#r1i-container').show() :
			$('#r1i-container').hide();
		
		$('#r1a option').each(function(){
			this.text = this.text.replace('tickets', 'adult tickets');
		});
	}
	else
	{
		$('#r1a option').each(function(){
			this.text = this.text.replace('adult tickets', 'tickets');
		});
		$('#r1c-container, #r1i-container').hide();
		$('#r1c option[value=0], #r1i option[value=0]').attr('selected', 'selected');
	}
	
	if(typeof details[1] == 'undefined' || typeof details[2] == 'undefined') {
		$('#select-pkg-msg').show();
	}
}

function validateSearchBox(form) {
	var ticket_pkg = $('#tour-ticket-pkg');
	var coach_pkg = $('#tour-travel-only-pkg');
	
	var ticket_pkg_val = (typeof ticket_pkg != 'undefined' && ticket_pkg.length > 0) ? ticket_pkg.val() : 0;
	var coach_pkg_val = (typeof coach_pkg != 'undefined' && coach_pkg.length > 0) ? coach_pkg.val() : 0;
	
	if(form.startdate.value == '') {
		return false;
	} else if(ticket_pkg_val.length == 0 && coach_pkg_val.length == 0) {
		return false;
	} else if(form.r1a.value == 0 && form.r1c.value == 0 && form.r1i.value == 0) {
		return false;
	} else {
		return true;
	}
}

function validateAndSendContact()
{
    var in_name = $('#fld-name').val();
    var in_email = $('#fld-email').val();
    var in_event = $('#fld-event').val();
    var in_msg = $('#fld-msg').val();

    in_name = (in_name) ? in_name : '';
    in_email = (in_email) ? in_email : '';
    in_event = (in_event) ? in_event : '';
    in_msg = (in_msg) ? in_msg : '';

    var name_error = true;
    var email_error = true;
    var event_error = true;
    var msg_error = true;

    if (in_name.length > 0) {
        name_error = false;
    }
    if (in_email.length > 0) {
        var regExp = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);

        if (regExp.test(in_email)) {
            email_error = false;
        }
    }
    if (in_event.length > 0) {
        event_error = false;
    }
    if (in_msg.length > 0) {
        msg_error = false;
    }

    if (name_error == false && email_error == false && event_error == false && msg_error == false) {
        $.ajax({
            type: 'POST',
            url: '/contact/send',
            data: 'n='+in_name+'&e='+in_email+'&p='+in_event+'&m='+in_msg+'',
            success: function(msg) {
                $('#frm-contact-fields').hide();
                $('#contact-thankyou').show();
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
              alert('Sorry, there was a problem posting your message')
            }
        });
    } else {
        alert('Please provide your name, valid contact email and a friendly message so we can deal with your enquiry effectively');
    }
}
