﻿$(document).ready(function () {
    // main navigation drop downs
    if (document.all) {
        $("#navContainer li").hoverClass("sfHover");
    }

    // how it works info toggle
    $(".moreInfo").hide();
    $(".expander").children("a").html("+");

    // video player
    $('.videoPlayer').jqm({ closeClass: 'closeBtn', onHide: endVideo });

    $(".btnPlayVideoSmall").hoverClass("btnPlayVideoSmallOver");
    $(".btnPlayVideoLarge").hoverClass("btnPlayVideoLargeOver");

    // Port Number Form
    setupCheckNumberPortabilityForm();

    // Newsletter Signup Form
    setupNewsletterForm();
});

$.fn.hoverClass = function(c){
    return this.each(function(){
        $(this).hover(
            function(){$(this).addClass(c);},
            function(){$(this).removeClass(c);}
        );
    });
};

function toggleInfo(rowId, callerItem){
    if (callerItem.parent().attr("class") == "expander")
	    callerItem = callerItem.parent();
    if ($("#" + rowId).css("display") == "none"){
	    // hide all
	    $(".moreInfo").hide();
	    $(".moreInfo").parent().children().removeClass("activeHeader");
	    $(".expander").children("a").html("+");
	    $(".noBottomBorder").removeClass("noBottomBorder");

	    // show selected
	    $("#" + rowId).show();
	    $(callerItem).parent().children(".expander").children("a").html("&ndash;");
	    $(callerItem).parent().parent().addClass("activeHeader");
	    if ($(callerItem).parent().parent().prev().attr("class") != "moreInfo")
		    $(callerItem).parent().parent().prev().addClass("noBottomBorder");
	    else $(callerItem).parent().parent().prev().prev().addClass("noBottomBorder");
    } else {
	    // hide selected
	    $("#" + rowId).hide();
	    $(callerItem).parent().children(".expander").children("a").html("+");
	    $(callerItem).parent().parent().removeClass("activeHeader");
	    $(".noBottomBorder").removeClass("noBottomBorder");
    }
}

function showVideo(videoName){
    $('#videoPlayer_' + videoName).jqmShow();
    return false;
}
function hideVideo(){
    $('.videoPlayer').jqmHide();
}
function endVideo(hash){
    try{
        var flashObj = document.getElementById(hash.w.attr("id").replace("videoPlayer", "flashVideoPlayer"));
        if (flashObj){
            flashObj.endVideo();
        }
    } catch (err){}
    hash.w.hide();
    hash.o.remove();
}

function submitHostedReceptionistDemoForm(){
    var strName = "";
    var strEmail = "";
    var strPhone = "";
    var btnValue = "";
    
    strName = $("#contactName").val();
    strPhone = $("#contactPhone").val();
    strEmail = $("#contactEmail").val();

    btnValue = $("#contactBtnSubmit").val();
    $("#contactBtnSubmit").val("Loading...");
    $("#contactBtnSubmit").attr("disabled", "true");

    $.ajax({
        type: "POST",
        url: "/Receptionist-LiveDemo-Form.aspx/SubmitReceptionistLiveDemo",
        data: "{ContactName: " + JSON.stringify(strName) + ", ContactPhone: " + JSON.stringify(strPhone) + ", ContactEmail: " + JSON.stringify(strEmail) + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",

        success: function(data){
            try{
                json = JSON.parse(data.d);

                if (json[0].Success){
                    return __doPostBack('SaveHostedReceptionistDemoSessionVar', $("#hostedReceptionistDemoURL").val());
                } else if (json[0].ErrorMessage){
                    $("#contactFormErrorMsg").html(json[0].ErrorMessage);
                    $("#contactFormErrorMsg").show();
                    $("#contactBtnSubmit").val(btnValue);
                    $("#contactBtnSubmit").attr("disabled", "");
                } else {
                    alert("Sorry, we were unable to submit your information.  Please try again later.");
                    $("#contactBtnSubmit").val(btnValue);
                    $("#contactBtnSubmit").attr("disabled", "");
                }
            } catch(err){
                alert("Error: " + err);
                $("#contactBtnSubmit").val(btnValue);
                $("#contactBtnSubmit").attr("disabled", "");
            }
        },
        
        error: function(XMLHttpRequest, textStatus, errorThrown){
            alert("Sorry, we were unable to submit your information.  Please try again later.");
            $("#contactBtnSubmit").val(btnValue);
            $("#contactBtnSubmit").attr("disabled", "");
        }
    });

}

function submitCheckNumberPortabilityForm() {
    var txtPhone1 = "";
    var txtPhone2 = "";
    var txtPhone3 = "";

    txtPhone1 = $("#NumberPortabilityForm #txtPhone1").val();
    txtPhone2 = $("#NumberPortabilityForm #txtPhone2").val();
    txtPhone3 = $("#NumberPortabilityForm #txtPhone3").val();

    btnValue = $("#NumberPortabilityForm input[name='submit']").val();
    $("#NumberPortabilityForm input[name='submit']").val("Loading...");
    $("#NumberPortabilityForm input[name='submit']").attr("disabled", "true");
    $("#NumberPortabilityForm .loadingImg").show();

    $.ajax({
        type: "POST",
        url: "/CheckNumberPort.aspx/CheckNumberPortAvailability",
        data: "{PhoneNumber1: " + JSON.stringify(txtPhone1) + ", PhoneNumber2: " + JSON.stringify(txtPhone2) + ", PhoneNumber3: " + JSON.stringify(txtPhone3) + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",

        success: function (data) {
            try {
                json = JSON.parse(data.d);

                if (json[0].Success) {
                    // Port Number Test performed correctly
                    $("#NumberPortabilityFormMessageContainer").html(json[0].Message);
                    $("#NumberPortabilityFormMessageContainer").show();

                    $("#NumberPortabilityForm input[name='submit']").val(btnValue);
                    $("#NumberPortabilityForm input[name='submit']").attr("disabled", "");
                    $("#NumberPortabilityForm .loadingImg").hide();
                } else if (json[0].ErrorMessage) {
                    // Server reported error
                    $("#NumberPortabilityFormMessageContainer").html(json[0].ErrorMessage);
                    $("#NumberPortabilityFormMessageContainer").show();

                    $("#NumberPortabilityForm input[name='submit']").val(btnValue);
                    $("#NumberPortabilityForm input[name='submit']").attr("disabled", "");
                    $("#NumberPortabilityForm .loadingImg").hide();
                } else {
                    $("#NumberPortabilityFormMessageContainer").hide();
                    alert("Sorry, we were unable to submit your information.  Please try again later.");

                    $("#NumberPortabilityForm input[name='submit']").val(btnValue);
                    $("#NumberPortabilityForm input[name='submit']").attr("disabled", "");
                    $("#NumberPortabilityForm .loadingImg").hide();
                }
            } catch (err) {
                $("#NumberPortabilityFormMessageContainer").hide();
                alert("Sorry, we were unable to submit your information.  Please try again later.");

                $("#NumberPortabilityForm input[name='submit']").val(btnValue);
                $("#NumberPortabilityForm input[name='submit']").attr("disabled", "");
                $("#NumberPortabilityForm .loadingImg").hide();
            }
        },

        error: function (XMLHttpRequest, textStatus, errorThrown) {
            $("#NumberPortabilityFormMessageContainer").hide();
            alert("Sorry, we were unable to submit your information.  Please try again later.");

            $("#NumberPortabilityForm input[name='submit']").val(btnValue);
            $("#NumberPortabilityForm input[name='submit']").attr("disabled", "");
            $("#NumberPortabilityForm .loadingImg").hide();
        }
    });
}

function setupCheckNumberPortabilityForm(){
    // only allow numbers on text box
    $("#NumberPortabilityForm input[type='text']").keypress(function (event) { return keyEntry_DigitsOnly(event); });
    
    // tell cursor to go to next box
    $("#NumberPortabilityForm #txtPhone1").keyup(function (event) { gotoNextTextBox(event, this, 'txtPhone2'); });
    $("#NumberPortabilityForm #txtPhone2").keyup(function (event) { gotoNextTextBox(event, this, 'txtPhone3'); });
}

function keyEntry_DigitsOnly(event){
    var charCode = (window.event) ? event.keyCode : evt.which;

    if (charCode > 31 && (charCode < 48 || charCode > 57)){
        return false;
    }
        
    return true;
}

function gotoNextTextBox(evt, currentTextBox, nextTextBox) {
    var charCode = (window.event) ? event.keyCode : evt.which;

    if (charCode > 31) {
        if (currentTextBox.value.length >= currentTextBox.maxLength) {
            var txtBox = document.getElementById(nextTextBox);
            if (txtBox) {
                txtBox.focus();
            }
        }
    }
}

function setupNewsletterForm() {
    $("#newsletterSignup input").focus(function () {
        $(this).removeClass("helpText");
        if ($(this).val() == $(this).attr("title")) {
            $(this).val("");
        }
    });

    $("#newsletterSignup input").blur(function () {
        if ($(this).val() == "") {
            $(this).val($(this).attr("title"));
            $(this).addClass("helpText");
        }
    });

    $("#newsletterSignup input").blur();
}

function newsletterSignup() {
    if ($("#txtNewsletterName").val() == "" || $("#txtNewsletterName").val() == $("#txtNewsletterName").attr("title")) {
        $("#txtNewsletterName").focus();
        alert("Please enter your name");
    } else if ($("#txtNewsletterEmail").val() == "" || $("#txtNewsletterEmail").val() == $("#txtNewsletterEmail").attr("title")) {
        $("#txtNewsletterEmail").focus();
        alert("Please enter your email address");
    } else {
        var strName = $("#txtNewsletterName").val();
        var strEmail = $("#txtNewsletterEmail").val();

        $("#txtNewsletterName").attr("disabled", "disabled");
        $("#txtNewsletterName").val("Loading...");

        $("#txtNewsletterEmail").attr("disabled", "disabled");
        $("#txtNewsletterEmail").val("");

        $.ajax({
            url: "NewsletterSignup.aspx/SaveNewsletterSignup",
            data: "{name: " + JSON.stringify(strName) + ", email: " + JSON.stringify(strEmail) + ", pageSubmittedFrom: " + JSON.stringify(document.location.href) + "}",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                var json = JSON.parse(data.d);
                if (json[0].ErrorMessage) {
                    alert(json[0].ErrorMessage);

                    $("#txtNewsletterName").attr("disabled", "");
                    $("#txtNewsletterName").val(strName);

                    $("#txtNewsletterEmail").attr("disabled", "");
                    $("#txtNewsletterEmail").val(strEmail);
                } else {
                    alert("Thank you for you submission.");

                    $("#txtNewsletterName").attr("disabled", "");
                    $("#txtNewsletterName").val("");

                    $("#txtNewsletterEmail").attr("disabled", "");
                    $("#txtNewsletterEmail").val("");

                    $("#newsletterSignup input").blur();
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert("We are unable to save your request at this time. Please try again later.");


                $("#txtNewsletterName").attr("disabled", "");
                $("#txtNewsletterName").val(strName);

                $("#txtNewsletterEmail").attr("disabled", "");
                $("#txtNewsletterEmail").val(strEmail);
            }
        });
    }
}
