/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
$(document).ready(function() {
    loadMenuListener();
    validateContactForm();
});


function loadMenuListener() {
        $(".menu_home").click(function() {
        hideMainContent();
        $('#home_inc').show('slow');
    });

    $('.menu_about').click(function() {
        hideMainContent();
        $('#about_inc').show('slow');
    });

    $('.menu_services').click(function() {
        hideMainContent();
        $('#services_inc').show('slow');
    });

    $('.menu_websites').click(function() {
        hideMainContent();
        $('#websites_inc').show('slow');
    });

    $('.menu_contact').click(function() {
        hideMainContent();
        $('#contact_inc').show('slow');
    });

}

/**
 * Hide main content
 */

function hideMainContent() {
    // hide all content
    $("div#home_inc").hide();
    $("div#about_inc").hide();
    $("div#services_inc").hide();
    $("div#websites_inc").hide();
    $("div#contact_inc").hide();
}

function isStringNullOrEmpty(str) {
    if ((str == null) || (str == "")) {
        return true;
    }

    return false;
}

function validateContactForm() {
    $('.error').hide();
    $("#submitAction").click(function() {
        // validate and process form here
        $('.error').hide();

        // Validate First Name
        var firstName = $("input#firstName").val();
        if (isStringNullOrEmpty(firstName)) {
            $("span#firstName_error").show();
            $("input#firstName").focus();
            return false;
        }

        // Validate Last Name
        var lastName = $("input#lastName").val();
        if (isStringNullOrEmpty(lastName)) {
            $("span#lastName_error").show();
            $("input#lastName").focus();
            return false;
        }

        // Validate Phone number
        var phone = $("input#phone").val();
        if (isStringNullOrEmpty(phone)) {
            $("span#phone_error").show();
            $("input#phone").focus();
            return false;
        } else if(!isPhoneValid(phone)) {
            $("span#phone_error").html('Please Enter a Valid Phone Number');
            $("span#phone_error").show();
            $("span#phone").focus();
            return false;
        }

        // Validate Email
        var email = $("input#email").val();
        if (isStringNullOrEmpty(email)) {
            $("span#email_error").show();
            $("input#email").focus();
            return false;
        } else if (!isEmailValid(email)) {
            $("span#email_error").html('Please Enter a Valid Email Address');
            $("span#email_error").show();
            $("input#email").focus();
            return false;
        }

        // Make Ajax call to post Form data
        postContactUs();

        return false;
    });
}

function postContactUs() {
    // Serialize form data
    var dataString = $('form').serialize();

    // Make ajax call, POST
    $.ajax({
        type: "POST",
        url: "/ContactUs",
        data: dataString,
        success: function(xml) {
            $('#contactus').html("<div id='message'></div>");
            $('#message').html("<h2>Contact Form Submitted!</h2>")
            .hide()
            .fadeIn(2000, function() {
                $('#message');
            });
        }
    });
}

/*
 * Verify Phone Number Format
 */
function isPhoneValid(phone) {
    var valid = true;
    var iCount = 0;

    var GoodChars = "0123456789()-+ ";

    for (iCount =0; iCount <= phone.length -1; iCount++) {
        if (GoodChars.indexOf(phone.charAt(iCount)) == -1) {
            valid = false;
        }
    }

    return valid;
}

function isEmailValid(email){
    var valid = true;
    var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;

    if (email.search(emailRegEx) == -1) {
        valid = false;
    }

    return valid;
}
