var formCollection = new Object();
var errorBeginComment = "<!-- Errors BEGIN -->";
var errorEndComment = "<!-- Errors END -->";

function formProc(){
	formCollection = new Object();
	formCollection.ajaxPostInquiry = true;
	
	$('input',$('#inquiryCommand')).each(function(){
		var dataName = $(this).get(0).name;
		var dataType = $(this).get(0).type;
		if( dataType == 'checkbox') {
			var dataChecked = $(this).get(0).checked;
		    if( dataChecked ) {
		    	formCollection[dataName] = $(this).val();
		    }
		}
		else {
			formCollection[dataName] = $(this).val();
		}
		
	});
	$('select',$('#inquiryCommand')).each(function(){
		var dataName = $(this).get(0).name;
		formCollection[dataName] = $(this).val();
	});
	$('textarea',$('#inquiryCommand')).each(function(){
		var dataName = $(this).get(0).name;
		formCollection[dataName] = $(this).val();
	});

	$.ajax({
		url: $('#inquiryCommand').get(0).action,
		type: 'POST',
		data: formCollection,
		success: handleData,
		dataType: 'html'
	});
	
	return false;
}

var handleData = function(data) {
	if (data.indexOf('id=\"inquiryCommand\"') > -1) {
		// Errors on post, update the form
		handleErrorData(data);
	} else {
		// Update and show the dialogs
		handleInquiryConfirmationData(data);
	}
}

function handleInquiryConfirmationData(data) {
	$.facebox.loading();
	clearValidationErrors();
	var popup = $('<div style="width: 800px"></div>');
	popup.html(data);
	$.facebox.reveal(popup.show(), '');
	showInquiryConfirmationAd();
	sendInquiryConfirmationOmniture();
	return false;
}

function showInquiryConfirmationAd() {
	if($('#inquiryConf-ad').attr('src') == '') {
		$('#inquiryConf-ad').attr('src', $('#inquiryConf-ad').attr('longdesc'));
	}
}

function sendInquiryConfirmationOmniture() {
	if (!(window.s === undefined)) {
		s.pageName = "inquiry_confirmation";
		s.events = "event7";
        s.channel = "inquiry_confirmation";
        s.eVar31 = $("#listing_id").val();
		var s_code=s.t();
        if (s_code) {
        	document.write(s_code);
        }
	}
}

function handleErrorData(data) {
	var errorData = $(data);
	clearValidationErrors();
	$('#inquiryFormErrors').html($('#inquiryFormErrors', errorData).html());
	$('.errorfield', errorData).each(function() {
		var fieldId = $(this).attr('id');
		$('#' + fieldId).addClass('errorfield');
	});
	$.scrollTo($('#inquiryFormErrors'));
}

function clearValidationErrors() {
	$('#inquiryFormErrors').html('');
	$('.errorfield').removeClass('errorfield');
}

function updateDepartureDate(arrivalDate) {
	var date = $.datepicker.parseDate('mm/dd/yy', arrivalDate);
	date.setDate(date.getDate() + 1);
	var formated_date = $.datepicker.formatDate('mm/dd/yy', date);
	document.getElementById('departureString').value = formated_date;
	$('#departureString').datepicker('option', {
		minDate: $.datepicker.parseDate('mm/dd/yy', formated_date)
	});
}

$(document).ready(function(){
	$('#inquiryCommand').submit(formProc);
	
	$('#arrivalString').datepicker({ 
		mandatory: true, 
		changeFirstDay: false, 
		constrainInput: true, 
		onSelect: updateDepartureDate,
		showOn: "both", 
	    buttonImage: "/resources/images/vrfresh/calendar-icon-small.gif", 
	    buttonImageOnly: true,
	    yearRange: '-0:+10',
	    minDate: '+0',
	    maxDate: '+10y',
	    prevText: ' ',
	    nextText: ' '
	});
	
	$('#departureString').datepicker({ 
		mandatory: true, 
		changeFirstDay: false, 
		constrainInput: true,
		onSelect: function(){},
		showOn: "both", 
	    buttonImage: "/resources/images/vrfresh/calendar-icon-small.gif", 
	    buttonImageOnly: true,
	    yearRange: '-0:+10',
	    minDate: '+1',
	    maxDate: '+10y',
		prevText: ' ',
		nextText: ' '
	});

    $('.calendarField').each(function() {
        $(this).attr('autocomplete', 'off');
    });
});