// JavaScript Document

//::: Show Error Msg :::
function fnShowErrorMsg(message) {
    $("#phone").css("display", "none");
    $("#lblErrorMsg").text(message).css("display", "block");
}

//::: Hide Error Msg :::
function fnHideErrorMsg() {
    $("#phone").css("display", "block");
    $("#lblErrorMsg").css("display", "none");
}

//::: Show Popup :::
function fnShowPopup(position, id) {
    //REFACTOR TO USE AJAX??
    var html = "<a class=\"close top\" href=\"javascript: fnHidePopup();\">X</a><div>";
    
    if (id == "about") {
        html += "<h3>About Total Merchant Services, Inc.</h3>";
        html += "<p>Total Merchant Services of Aspen, Inc. is a Total Merchant Services, Inc. Official Sales Office. Total Merchant Services provides the most cost effective solutions for merchants to accept the preferred method of payment for the sales of their goods and services , credit, debit, check and gift card processing solutions.</p>";
        html += "<p>Through our partnerships with bankcard industry leaders, we have quickly become one of the largest acquirers in the nation. This scale allows us to pass along the cost savings to our existing and new merchants, like you, as activated.</p>";
        html += "<p>Total Merchant Services of Aspen prides itself as a customer service based facility offering a dedicated point of contact to each merchant for the life of their account.</p>";
    } else {
        html += "<h3>Contact Total Merchant Services</h3>";
        html += "<p><strong>For Customer Service call 1-888-848-6821</strong>: Our representatives can help you with questions about transfers ";
        html += "of money into your designated checking account, monthly statements, chargeback and retrieval requests, and other issues that ";
        html += "face merchant account holders every day.</p><p><strong>For Terminal Help Desk Support call 1-888-848-6821</strong>: If you have ";
        html += "any questions about your credit card terminal, need to schedule telephone training, need to replace equipment, or need supplies, ";
        html += "our terminal help desk is here 24/7 to keep your point of sale terminals up and running.</p>";
    }

    html += "</div><a class=\"close bottom\" href=\"javascript: fnHidePopup();\">Close</a>";

    var offset = $("div.header").offset();

    if (position == "top") {
        $("#popup").css("top", "30px").css("left", (offset.left + 451) + "px").html(html).fadeIn("normal");
    } else {
        $("#popup").css("top", "450px").css("left", offset.left + "px").html(html).fadeIn("normal");
    }

    pageTracker._trackPageview("popup/" + id);
}

//::: Hide Popup :::
function fnHidePopup() {
    $("#popup").fadeOut("normal").html("");
}

//::: Validate Form :::
function fnValidateForm() {
    var txtFullName = $("#txtFullName").val();
    var txtCompanyName = $("#txtCompanyName").val();
    var txtEmail = $("#txtEmail").val();
    var txtPhone1 = $("#txtPhone1").val();
    var txtPhone2 = $("#txtPhone2").val();
    var txtPhone3 = $("#txtPhone3").val();
    var txtCity = $("#txtCity").val();
    var txtState = $("#selState").val();

    fnHideErrorMsg();

    //check for required fields
    if (!txtFullName || txtFullName == "") {
        fnShowErrorMsg("Your name is a required field!");
        return false;
    }

    if (!txtEmail || txtEmail == "") {
        fnShowErrorMsg("Your email address is a required field!");
        return false;
    }

    if (!txtPhone1 || txtPhone1 == ""
            || !txtPhone2 || txtPhone2 == ""
            || !txtPhone2 || txtPhone3 == "") {
        fnShowErrorMsg("Your phone number is a required field!");
        return false;
    }

    if (!txtState || txtState == "") {
        fnShowErrorMsg("Your state is a required field!");
        return false;
    }

    //pattern match
    var namePattern = /^[a-zA-Z-\.]+( [a-zA-Z-\.]+)? [a-zA-Z-\.]+$/g;
    var emailPattern = /^[\w-\.]+@[\w-\.]+\.[a-zA-Z]{2,4}$/g;
    var phonePatternA = /^[0-9][0-9][0-9]$/g;
    var phonePatternB = /^[0-9][0-9][0-9][0-9]$/g;

    if (txtFullName.match(namePattern) == null) {
        fnShowErrorMsg("Please enter a valid full name!");
        return false;
    }

    if (txtEmail.match(emailPattern) == null) {
        fnShowErrorMsg("Please enter a valid email address!");
        return false;
    }

    if (txtPhone1.match(phonePatternA) == null
            || txtPhone2.match(phonePatternA) == null
            || txtPhone3.match(phonePatternB) == null) {
        fnShowErrorMsg("Please enter a valid 10-digit phone number!");
        return false;
    }
    
    document.form1.submit();
}

//::: Initialize Form :::
function fnInitializeForm() {
    $("#btnContact").hover(
            function() {
                this.src = "http://www.total-merchant-services.com/wp-content/themes/Total-merchant/images/btn_contact_on.png";
            },
            function() {
                this.src = "http://www.total-merchant-services.com/wp-content/themes/Total-merchant/images/btn_contact.png";
            });

    $("#btnContact").click(function() {
        /*
        $("#form1").fadeOut("normal", function() {
        $("#thanks").fadeIn("normal");
        });
        */

        fnValidateForm();

        return false;
    });

    $("#txtPhone1").keyup(function() {
        if (this.value.length == 3) {
            $("#txtPhone2").focus();
        }
    });

    $("#txtPhone2").keyup(function() {
        if (this.value.length == 3) {
            $("#txtPhone3").focus();
        }
    });
}

//::: Initialize Popup Links :::
function fnInitializePopupLinks() {
    $("#tnav-about").click(function(event) {
        event.preventDefault();
        fnShowPopup("top", "about");
    });

    $("#tnav-contact").click(function(event) {
        event.preventDefault();
        fnShowPopup("top", "contact");
    });

    $("#fnav-about").click(function(event) {
        event.preventDefault();
        fnShowPopup("bottom", "about");
    });

    $("#fnav-contact").click(function(event) {
        event.preventDefault();
        fnShowPopup("bottom", "contact");
    });
}

function fnShowService(id) {
    $("#panelServices h3").removeClass("over");
    $("#panelServices p").removeClass("over");

    $("#" + id).addClass("over");

    $("#" + id.replace("h3", "p")).addClass("over");
}

//::: Initialize Popup Links :::
function fnInitializeServicesMenu() {
    $("#panelServices h3").mouseover(function() {
        fnShowService(this.id);
    });

    $("#h3-check").addClass("over");
    $("#p-check").addClass("over");    
}

//::: Document Ready :::
$("document").ready(function() {
    fnInitializeForm();
    fnInitializePopupLinks();
    fnInitializeServicesMenu();
});