﻿//::: 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>Founded in 1996, Colorado-based Total Merchant Services is one of the fastest growing credit card merchant account ";
        html += "acquirers in the nation. We've quickly become a leader in the merchant bankcard business by providing innovative and ";
        html += "cost-effective solutions to merchants like you who need to accept credit cards for the payment of their goods and services.</p>";
    } else {
        html += "<h3>Contact Total Merchant Services</h3>";
        html += "<p><strong>For Customer Service call 888.848.6825</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 888.848.6825</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 = "img/btn_contact_on.png";
            },
            function() {
                this.src = "img/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();
});
