$(function() {

    var Days = ["11/28/2009"];
    function naDays(date) {
        var sDate = (date.getMonth() + 1).toString() + "/" + date.getDate().toString() + "/" + date.getFullYear().toString();
        if ($.inArray(sDate, Days) != -1) return [false, "", "Not available"];
        else return [true, ""];
    }
    $("#datepicker").datepicker({beforeShowDay:  naDays});
});

$(function() {

    var Days = ["11/28/2009"];
    function naDays(date) {
        var sDate = (date.getMonth() + 1).toString() + "/" + date.getDate().toString() + "/" + date.getFullYear().toString();
        if ($.inArray(sDate, Days) != -1) return [false, "", "Not available"];
        else return [true, ""];
    }
    $("#datepickerend").datepicker({beforeShowDay:  naDays});
});


$(function() {
    var name = $("#name"),
    email = $("#email")
    allFields = $([]).add(name).add(email),
    tips = $("#validateTips");

    function updateTips(t) {
        tips.text(t).effect("highlight", {},
        1500);
    }

    function checkLength(o, n, min, max) {
        if (o.val().length > max || o.val().length < min) {
            o.addClass('ui-state-error');
            updateTips("Length of " + n + " must be between " + min + " and " + max + ".");
            return false;
        } else {
            return true;
        }

    }

    function checkRegexp(o, regexp, n) {
        if (! (regexp.test(o.val()))) {
            o.addClass('ui-state-error');
            updateTips(n);
            return false;
        } else {
            return true;
        }

    }

    $("#dialog").dialog({
        bgiframe: true,
        autoOpen: false,
        width: 600,
        height: 600,
        modal: true,
        buttons: {
            'Send Form': function() {
                var bValid = true;
                allFields.removeClass('ui-state-error');

                bValid = bValid && checkLength(name, "name", 1, 50);
                bValid = bValid && checkLength(email, "email", 6, 80);
                bValid = bValid && checkRegexp(email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "Please enter a valid email address. We need it to confirm your reservation and to send details regarding your tour. eg. myname@gmail.com");

                if (bValid) {
                    if ($('#rentalform').text()) {
                        sendrentalform();
                    } else if ($('#tourform').text()) {
                        sendtourform();
                    }
                    $(this).dialog('close');
                }
            },
            Cancel: function() {
                $(this).dialog('close');
            }
        },
        close: function() {
            allFields.val('').removeClass('ui-state-error');
        }
    });

    $('#schedule_tupelo').click(function() {
        $('#tourid').text("BLACK RIVER CYPRESS AND TUPELO SWAMP TOUR");
        $('#dialog').dialog('open');
    }).hover(function() {
        $(this).addClass("ui-state-hover");
    },
    function() {
        $(this).removeClass("ui-state-hover");
    }).mousedown(function() {
        $(this).addClass("ui-state-active");
    }).mouseup(function() {
        $(this).removeClass("ui-state-active");
    });
    
    $('#schedule_huntington').click(function() {
        $('#tourid').text("HUNTINGTON BEACH SALT MARSH CREEKS");
        $('#dialog').dialog('open');
    }).hover(function() {
        $(this).addClass("ui-state-hover");
    },
    function() {
        $(this).removeClass("ui-state-hover");
    }).mousedown(function() {
        $(this).addClass("ui-state-active");
    }).mouseup(function() {
        $(this).removeClass("ui-state-active");
    });
    
    $('#schedule_georgetown').click(function() {
        $('#tourid').text("HISTORIC HARBOR & SEAPORT OF GEORGETOWN");
        $('#dialog').dialog('open');
    }).hover(function() {
        $(this).addClass("ui-state-hover");
    },
    function() {
        $(this).removeClass("ui-state-hover");
    }).mousedown(function() {
        $(this).addClass("ui-state-active");
    }).mouseup(function() {
        $(this).removeClass("ui-state-active");
    });

    $('#schedule_chicora').click(function() {
        $('#tourid').text("CHICORA WOOD PLANTATION");
        $('#dialog').dialog('open');
    }).hover(function() {
        $(this).addClass("ui-state-hover");
    },
    function() {
        $(this).removeClass("ui-state-hover");
    }).mousedown(function() {
        $(this).addClass("ui-state-active");
    }).mouseup(function() {
        $(this).removeClass("ui-state-active");
    });
        $('#schedule_sandy').click(function() {
        $('#tourid').text("SANDY ISLAND NATURE PRESERVE");
        $('#dialog').dialog('open');
    }).hover(function() {
        $(this).addClass("ui-state-hover");
    },
    function() {
        $(this).removeClass("ui-state-hover");
    }).mousedown(function() {
        $(this).addClass("ui-state-active");
    }).mouseup(function() {
        $(this).removeClass("ui-state-active");
    });

        $('#rentalform').click(function() {
        $('#tourid').text("KAYAK AND CANOE RENTALS");
        $('#dialog').dialog('open');
    }).hover(function() {
        $(this).addClass("ui-state-hover");
    },
    function() {
        $(this).removeClass("ui-state-hover");
    }).mousedown(function() {
        $(this).addClass("ui-state-active");
    }).mouseup(function() {
        $(this).removeClass("ui-state-active");
    });
    
});

function sendtourform() {
    var tour = $('#tourid').text();
    var name = $('input[name=name]');
    var email = $('input[name=email]');
    var phone= $('input[name=phone]');
    var address = $('input[name=address]');
    var datepicker = $('input[name=datepicker]');
    var numpeople= $('input[name=numpeople]');
    var weight= $('input[name=weight]');
    var minor = $('input[name=minor]');
    var ages = $('input[name=ages]');
    var origin= $('input[name=origin]');
    var how= $('textarea[name=how]');
    var questions = $('textarea[name=questions]');
 
    var data = 'name=' + name.val() + '&email=' + email.val() + '&phone=' + phone.val() +
               '&address=' + address.val()  + '&datepicker=' + datepicker.val() + 
	       '&numpeople=' + numpeople.val() + '&weight=' + weight.val() +
               '&minor=' + minor.val() + '&ages=' + ages.val() + '&origin=' + origin.val() + '&tour=' + tour +
               '&how=' +encodeURIComponent(how.val()) + '&questions=' +encodeURIComponent(questions.val());   
    $.ajax({
	//this is the php file that processes the data and send mail
	url: "form_tour.php",	
	//GET method is used
	type: "GET",
	//pass the data			
	data: data,		
	//Do not cache the page
	cache: false,
	//success
	success: function (html) {				
	//if process.php returned 1/true (send mail success)
	    if (html==1) {					
                alert('Your information has been received. We will contact you soon to finalize the tour reservation.');
	    } else alert('Sorry, unexpected error. Please try again later.');				
	    }		
	});    
}

function sendrentalform() {
    var name = $('input[name=name]');
    var email = $('input[name=email]');
    var phone= $('input[name=phone]');
    var address = $('input[name=address]');
    var datepicker = $('input[name=datepicker]');
    var numpeople= $('input[name=numpeople]');
    var weight= $('input[name=weight]');
    var minor = $('input[name=minor]');
    var ages = $('input[name=ages]');
    var how= $('textarea[name=how]');
    var questions = $('textarea[name=questions]');
    var localaddress = $('input[name=localaddress]');
    var license = $('input[name=license]');
    var datepickend = $('input[name=datepickerend]');
    var boattype = $('select[name=boattype]');
    var delivered = $('select[name=delivered]');
    var deli_instr= $('textarea[name=deli_instr]');
 
    var data = 'name=' + name.val() + '&email=' + email.val() + '&phone=' + phone.val() +
               '&address=' + address.val()  + '&datepicker=' + datepicker.val() +
               '&localaddress=' + localaddress.val()  + '&datepickend=' + datepickend.val() +
               '&license=' + license.val()  + '&boattype=' + boattype.val() + 
	       '&numpeople=' + numpeople.val() + '&weight=' + weight.val() +
               '&minor=' + minor.val() + '&ages=' + ages.val() + 
               '&deli_instr=' +encodeURIComponent(deli_instr.val()) + '&bdelivered=' + encodeURIComponent(delivered.val()) + 
               '&how=' +encodeURIComponent(how.val()) + '&questions=' +encodeURIComponent(questions.val());   
    $.ajax({
	//this is the php file that processes the data and send mail
	url: "form_rental.php",	
	//GET method is used
	type: "GET",
	//pass the data			
	data: data,		
	//Do not cache the page
	cache: false,
	//success
	success: function (html) {				
	//if process.php returned 1/true (send mail success)
	    if (html==1) {					
                alert('Your information has been received. We will contact you soon to finalize the process.');
	    } else alert('Sorry, unexpected error. Please try again later.');				
	    }		
	});    
}