$(document).ready(function() {

    $("#rfm_cta_field").focus();

//     $("#tab_right").addClass("hometab_r");
//     $("#tab_rightb").addClass("hometab_rb");

     $("#tab_right").mouseover(function() {
//        $("#tab_left").addClass("hometab_l");
//        $("#tab_right").removeClass("hometab_r");
//        $("#tab_lefti").show();
//        $("#tab_righti").hide();
//        $("#rfm").hide();
//        $("#rfe").show();
        $("#rfe_cta_field").focus();
    });

    $("#tab_left").mouseover(function() {
//         $("#tab_right").addClass("hometab_r");
//         $("#tab_left").removeClass("hometab_l");
//         $("#tab_lefti").hide();
//         $("#tab_righti").show();
//         $("#rfm").show();
//         $("#rfe").hide();
        $("#rfm_cta_field").focus();
    });


    $(".home_rfm_li, .home_rfe_li").mouseover(function() {
        //$(this).css("background", "#F6F6F6");
    }).mouseout(function() {
        //$(this).css("background", "#FFF");
    }).click(function() {
        var prd = this.id.split("_")[0];
        $("#" + prd + "_cta_con").hide();
        $("#" + prd + "_fb_con").hide();
        $("#" + prd + "_pp_con").hide();
        $("#" + prd + "_signup_con").hide();
        $("#" + this.id + "_con").show();

        $("#" + prd + "_cta").removeClass("li_sel");
        $("#" + prd + "_fb").removeClass("li_sel");
        $("#" + prd + "_pp").removeClass("li_sel");
        $(this).addClass("li_sel");
        //$("."+ prd + "_li_sel").hide();
        //$(this).find("."+ prd + "_li_sel").show();
    });

    $("#rfm_fnb_cta_btn, #rfe_fnb_cta_btn").click(function() {
        var prd = this.id.split("_")[0];
        $("#" + prd + "_cta_con").show();
        $("#" + prd + "_fb_con").hide();
        $("#" + prd + "_pp_con").hide();
        $("#" + prd + "_signup_con").hide();
        //$("."+ prd + "_li_sel").hide();

        $("#" + prd + "_cta").addClass("li_sel");
        $("#" + prd + "_fb").removeClass("li_sel");
        $("#" + prd + "_pp").removeClass("li_sel");
        //$("#" + prd + "_cta").find("."+ prd + "_li_sel").show();
        //$("#"+ prd + "_cta_field").focus();
    });

    $("#rfm_signup_free, #rfm_signup_basic, #rfm_signup_pro, #rfm_signup_ent").click(function() {
        var pkg = this.id.split("_")[2];
        if(pkg == "free") {
            $("#rfm_su_pkg_name").text("Free");
            $("#rfm_su_pkg_pm").text("$0.00");
        } else if(pkg == "basic") {
            $("#rfm_su_pkg_name").text("Basic");
            $("#rfm_su_pkg_pm").text("$4.49");
        } else if(pkg == "pro") {
            $("#rfm_su_pkg_name").text("Pro");
            $("#rfm_su_pkg_pm").text("$6.95");
        } else if(pkg == "ent") {
            $("#rfm_su_pkg_name").text("Enterprise");
            $("#rfm_su_pkg_pm").text("$13.95");
        }

        if(pkg == "free") {
            $("#rfm_su_pkg_opts").hide();
            $("#rfm_su_fine_print").hide();
            $(".rfm_su_btn_txt").text("Signup Free");
            $("#rfm_su_btn_val").val("Signup Free");
        } else {
            $("#rfm_su_pkg_opts").show();
            $("#rfm_su_def_cycle").show();
            $("#rfm_su_cycle_basic, #rfm_su_cycle_pro, #rfm_su_cycle_ent").hide();
            $("#rfm_su_cycle").val(1);
            $("#rfm_su_fine_print").show();
            $(".rfm_su_btn_txt").text("Signup & Start Trial");
            $("#rfm_su_btn_val").val("Signup & Start Trial");
        }
        $("#rfm_su_pkg").val(pkg);
        $("#rfm_pp_con").hide();
        $("#rfm_signup_con").show();
    });

    $("#rfm_su_pkg_change").click(function() {
        $("#rfm_pp_con").show();
        $("#rfm_signup_con").hide();
    });

    $("#rfm_su_cycle_change").click(function() {
        $("#rfm_su_cycle_basic, #rfm_su_cycle_pro, #rfm_su_cycle_ent").hide();
        $("#rfm_su_cycle_"+$("#rfm_su_pkg").val()).show();
        $("#rfm_su_def_cycle").hide();
    });

    $("#rfm_su_cycle_basic, #rfm_su_cycle_pro, #rfm_su_cycle_ent").change(function() {
        $("#rfm_su_cycle").val($(this).val());
    });

    // rfe su

    $("#rfe_signup_free, #rfe_signup_pro").click(function() {
        var pkg = this.id.split("_")[2];
        if(pkg == "free") {
            $("#rfe_su_pkg_name").text("Free");
            $("#rfe_su_pkg_pm").text("$0.00");
        } else if(pkg == "pro") {
            $("#rfe_su_pkg_name").text("Pro");
            $("#rfe_su_pkg_pm").text("$4.49");
        }

        if(pkg == "free") {
            $("#rfe_su_pkg_opts").hide();
            $("#rfe_su_fine_print").hide();
            $(".rfe_su_btn_txt").text("Signup Free");
            $("#rfe_su_btn_val").val("Signup Free");
        } else {
            $("#rfe_su_pkg_opts").show();
            $("#rfe_su_def_cycle").show();
            $("#rfe_su_cycle_pro").hide();
            $("#rfe_su_cycle").val(1);
            $("#rfe_su_fine_print").show();
            $(".rfe_su_btn_txt").text("Signup & Start Trial");
            $("#rfe_su_btn_val").val("Signup & Start Trial");
        }
        $("#rfe_su_pkg").val(pkg);
        $("#rfe_pp_con").hide();
        $("#rfe_signup_con").show();
    });


    $("#rfe_su_pkg_change").click(function() {
        $("#rfe_pp_con").show();
        $("#rfe_signup_con").hide();
    });


    $("#rfe_su_cycle_change").click(function() {
        $("#rfe_su_cycle_pro").show();
        $("#rfe_su_def_cycle").hide();
    });

    $("#rfe_su_cycle_pro").change(function() {
        $("#rfe_su_cycle").val($(this).val());
    });



    $("#rfm_cta_frm").submit(function(e) {

        if($("#rfm_cta_frm").attr("action") != "") {
            return true;
        }

        if($("#rfm_cta_field").val() == "") {
            $("#rfm_msg").text("Feed title is required!").css("color", "red");
        } else {
          var urlregex = new RegExp("^(http|https)\:\/\/+[a-z0-9_-]+(\.[a-z0-9_-]+)+([_~#?&%+./;:,=./a-z0-9_-])+$");
          if(urlregex.test($("#rfm_cta_field").val())) {
              $("#rfm_msg").html("You have entered a URL instead of a title for your feed. If you are trying to display this feed on your website; please use <b>FeedEmbed</b> by clicking the Right-Hand-Side tab on this page. Or <a class='green_lnks jmp_inpage' href='javascript:jump_inpage(\"a_rfe_cta\")'>Click Here</a>").css("color", "red");
          } else {
              $("#rfm_cta_s1").hide();
              $("#rfm_cta_s2").show();
              $("#rfm_msg").html("");
              $("#rfm_cta_email").focus();
          }
        }
        if($("#rfm_cta_email").val() != "") {
            $("#rfm_msg").text("Creating RSS Feed...").css("color", "green");
            $.ajax({
             type: "POST",
             url: "/ax_rfm_signup/",
             data: "email="+ $("#rfm_cta_email").val() +"&feed_title=" + $("#rfm_cta_field").val(),
             success: function(msg){
               rtype = msg.split(":")[0];
               msg = msg.split(":")[1]
               if(rtype == "r") {
                   $("#rfm_cta_frm").attr("action", msg);
                   $("#rfm_cta_frm").submit();
                   //location.href = msg;
               } else {
                   $("#rfm_msg").text(msg).css("color", "red");
               }
             }
           });
        }
        e.preventDefault();
        return false;
    });



    $("#rfe_cta_frm").submit(function(e) {

        if($("#rfe_cta_frm").attr("action") != "") {
            return true;
        }

        if($("#rfe_s").val() == 1) {
            if($("#rfe_cta_field").val() == "") {
                $("#rfe_msg").text("Please Enter a Feed URL").css("color", "red");
            } else  {
                $("#rfe_msg").text("Generating Feed Preview...").css("color", "green");
                $.ajax({
                    type: "POST",
                    url: "/ax_rfe_signup/",
                    data: "do=preview"+"&feed_url=" + escape($("#rfe_cta_field").val()),
                    success: function(msg){
                        rtype = msg.split(":")[0];
                        rmsg = msg.split(":")[1]
                        if(rtype == "e") {
                            $("#rfe_msg").text(rmsg).css("color", "red");
                        } else {
                            $("#rfe_preview").html(msg).show();
                            $("#rfe_cta_s1").hide();
                            $("#rfe_cta_s2").show();
                            $("#rfe_msg").text("");
                            $("#rfe_s").val(2);
                        }
                    }
                });
            }
        } else if($("#rfe_s").val() == 2) {
            if($("#rfe_cta_email").val() != "") {
                $("#rfe_msg").text("Generating Code...").css("color", "green");
                $.ajax({
                    type: "POST",
                    url: "/ax_rfe_signup/",
                    data: "do=signup"+"&mysiteid="+ $("#rfe_mysiteid").val() + "&email="+ $("#rfe_cta_email").val(),
                    success: function(msg){
                        rtype = msg.split(":")[0];
                        msg = msg.split(":")[1]
                        if(rtype == "r") {
                            $("#rfe_cta_frm").attr("action", msg);
                            $("#rfe_cta_frm").submit();
                            //location.href = msg;
                        } else {
                            $("#rfe_msg").text(msg).css("color", "red");
                        }
                    }
                });
            }
        }
        e.preventDefault();
        return false;
    });



    $("#rfm_su_frm, #rfe_su_frm").submit(function(e) {
        var prd = this.id.split("_")[0];
        $.ajax({
             type: "POST",
             url: "/ax_signup/",
             data: "product="+ prd +
                   "&pkg="+ $("#"+prd+"_su_pkg").val() +
                   "&cycle="+ $("#"+prd+"_su_cycle").val() +
                   "&fname="+ $("#"+prd+"_su_fname").val() +
                   "&lname="+ $("#"+prd+"_su_lname").val() +
                   "&email="+ $("#"+prd+"_su_email").val() +
                   "&pwd="+ $("#"+prd+"_su_pwd").val() +
                   "&repwd="+ $("#"+prd+"_su_repwd").val(),
             beforeSend:function() {
                 $("#"+prd+"_su_msg").text("Processing ...").css("color", "green");
             },
             success: function(data){
                 rtype = data.split(":")[0];
                 msg = data.split(":")[1]
                 if(rtype == "e") {
                     $("#"+prd+"_su_msg").text(msg).css("color", "red");
                 } else if(rtype == "r") {
                     $("#"+prd+"_su_msg").text("Redirecting to PayPal ...").css("color", "green");
                     location.href = data.substring(2);
                 }
             }
        });
        e.preventDefault();
        return false;
    });


    $(".jmp_inpage").click(function(e) {
        jump_inpage($(this).attr("href").split("#")[1]);
        $('html, body').animate({scrollTop:0}, 'slow');
        e.preventDefault();
        return false;
    });

    apage = window.location.href.split("#")[1];

    if(apage) {
        jump_inpage(apage);
    }




});


function rfe_panelselect() {

        $.ajax({
             type: "POST",
             url: "/ax_rfe_signup/",
             data: "do=pannelselect"+"&mysiteid="+ $("#rfe_mysiteid").val() +"&flavor=" + $("#rfe_theme").val() + "&color=" + $("#rfe_color").val(),
             beforeSend: function() {
                 $("#rfe_panelloading").text("Loading ...");
             },
             success: function(data){
                 rtype = data.split(":")[0];
                 msg = data.split(":")[1]
                 if(rtype == "e") {
                     $("#rfe_msg").text(msg).css("color", "red");
                 } else {
                     $("#rfe_panel").html(data);
                 }
             }
        });
}

function rfe_getcode(lang_id) {

        $.ajax({
             type: "POST",
             url: "/ax_rfe_signup/",
             data: "do=getcode"+"&mysiteid="+ $("#rfe_mysiteid").val() +"&lang=" + lang_id,
             beforeSend: function() {
                 $("#rfe_codeloading").text("Loading ...");
             },
             success: function(data){
                 rtype = data.split(":")[0];
                 msg = data.split(":")[1]
                 if(rtype == "e") {
                     $("#rfe_codeloading").text(msg).css("color", "red");
                 } else {
                     $("#rfe_code_area").text(data);
                 }
                 $("#rfe_codeloading").text("Copy and paste the below ready to use code in your webpage HTML.");
             }
        });
}


function paynow_rfm(pkg) {
    document.forms["paypal_frm"].item_number.value = "rfm" + pkg + "_" + $("input[name="+pkg+"]:checked").val();
    document.forms["paypal_frm"].item_name.value = package["rfm" + pkg]["pname"];
    document.forms["paypal_frm"].a3.value = package["rfm" + pkg][$("input[name="+pkg+"]:checked").val()];
    document.forms["paypal_frm"].p3.value = $("input[name="+pkg+"]:checked").val();
    document.forms["paypal_frm"].submit();
}


function paynow_rfe(pkg) {
    document.forms["paypal_frm"].item_number.value = "rfe" + pkg + "_" + $("input[name="+pkg+"]:checked").val();
    document.forms["paypal_frm"].item_name.value = package["rfe" + pkg]["pname"];
    document.forms["paypal_frm"].a3.value = package["rfe" + pkg][$("input[name="+pkg+"]:checked").val()];
    document.forms["paypal_frm"].p3.value = $("input[name="+pkg+"]:checked").val();
    document.forms["paypal_frm"].submit();
}


function jump_inpage(apage) {
    hash_page_anchor = apage.split("_");
    if(hash_page_anchor[1] == "rfm") {
//         $("#tab_right").addClass("hometab_r");
//         $("#tab_rightb").addClass("hometab_rb");
//         $("#tab_left").removeClass("hometab_l");
//         $("#tab_leftb").removeClass("hometab_lb");
//         $("#rfm").show();
//         $("#rfe").hide();
        $("#rfm_cta_field").focus();
    } else if(hash_page_anchor[1] == "rfe") {
//         $("#tab_left").addClass("hometab_l");
//         $("#tab_leftb").addClass("hometab_lb");
//         $("#tab_right").removeClass("hometab_r");
//         $("#tab_rightb").removeClass("hometab_rb");
//         $("#rfm").hide();
//         $("#rfe").show();
        $("#rfe_cta_field").focus();
    }
    var prd = hash_page_anchor[1];
    $("#" + prd + "_cta_con").hide();
    $("#" + prd + "_fb_con").hide();
    $("#" + prd + "_pp_con").hide();
    $("#" + prd + "_signup_con").hide();
    $("#" + prd + "_" + hash_page_anchor[2] + "_con").show();
    $("."+ prd + "_li_sel").hide();
    $("#" + prd + "_" + hash_page_anchor[2]).find("."+ prd + "_li_sel").show();
}

