﻿$(function() {
    $('.error').hide();

    $("#contactbutton").click(function() {

        $('.error').hide();
        $(".textbox").css("border", "1px solid #e3e9ef").css("background", "#ffffff");

        if ($("input#name").val() == "") {

            $("input#name").css("border", "1px solid red").css("background", "#ffdbdb");
            $("label#name_error").text("name required");
            $("label#name_error").show();
            $("input#name").focus();
            return false;
        }

        if ($("input#email").val() == "") {

            $("input#email").css("border", "1px solid red").css("background", "#ffdbdb");
            $("label#email_error").text("email address required");
            $("label#email_error").show();
            $("input#email").focus();
            return false;
        }

        if (isValidEmailAddress($("input#email").val())) {
        }
        else {
            $("input#email").css("border", "1px solid red").css("background", "#ffdbdb");
            $("label#email_error").text("invalid email address");
            $("label#email_error").show();
            $("input#email").focus();
            return false;
        }

        if ($("textarea#review").val() == "") {

            $("textarea#review").css("border", "1px solid red").css("background", "#ffdbdb");
            $("label#review_error").text("comments required");
            $("label#review_error").show();
            $("textarea#review").focus();
            return false;
        }

        $.ajax({
            type: "POST",
            url: "/pages/contactus.php",
            data: "name=" + $("input#name").val() + "&email=" + $("input#email").val() + "&comment=" + escape($("textarea#review").val()),
            success: function(msg) {
                $('#form').html("<b>Email Sent!</b>")
                    .append("<p>Thank you " + msg.d + ", We will contact you within 24 hours.</p>")
                    .hide()
                    .fadeIn(1500, function() {
                        $('.form').append("<img id='checkmark' src='/images/check.png' />");
                    });
            }
        });

        return false;
    });
});

function isValidEmailAddress(emailAddress) {
    var pattern = 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);
    return pattern.test(emailAddress);
}

function initialize() {
    if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(52.843625, -3.041728), 13);
        map.setUIToDefault();

        // Create our "tiny" marker icon
        var myIcon = new GIcon();
        myIcon.image = '/images/markers/image.png';
        myIcon.printImage = '/images/markers/printImage.gif';
        myIcon.mozPrintImage = '/markers/mozPrintImage.gif';
        myIcon.iconSize = new GSize(20, 22);
        myIcon.shadow = '/images/markers/shadow.png';
        myIcon.transparent = '/images/markers/transparent.png';
        myIcon.shadowSize = new GSize(31, 22);
        myIcon.printShadow = '/images/markers/printShadow.gif';
        myIcon.iconAnchor = new GPoint(10, 22);
        myIcon.infoWindowAnchor = new GPoint(10, 0);
        myIcon.imageMap = [12, 0, 14, 1, 15, 2, 17, 3, 17, 4, 17, 5, 18, 6, 19, 7, 19, 8, 19, 9, 19, 10, 19, 11, 19, 12, 19, 13, 19, 14, 19, 15, 18, 16, 17, 17, 16, 18, 15, 19, 13, 20, 10, 21, 5, 21, 5, 20, 2, 19, 1, 18, 1, 17, 1, 16, 0, 15, 0, 14, 0, 13, 0, 12, 0, 11, 1, 10, 0, 9, 0, 8, 0, 7, 1, 6, 1, 5, 1, 4, 3, 3, 3, 2, 4, 1, 7, 0]

        // Set up our GMarkerOptions object
        markerOptions = { icon: myIcon };

        // Add 10 markers to the map at random locations
        var bounds = map.getBounds();
        var southWest = bounds.getSouthWest();
        var northEast = bounds.getNorthEast();
        var lngSpan = northEast.lng() - southWest.lng();
        var latSpan = northEast.lat() - southWest.lat();
        //for (var i = 0; i < 10; i++) {
        var latlng = new GLatLng(52.843625, -3.041728);
        map.addOverlay(new GMarker(latlng, markerOptions));
        //}
    }
}
