/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function displayNeuStoreDialog(location,title){
    var loc = location+" #modal_vid_stream_display";
    var popupDiv = "<div id='popupDialog' class='hidden' title='"+title+"'><h3>Loading, Please wait...</h3></div>";
    $("#page").append(popupDiv);
    $("#popupDialog").load(loc,function(vals){
        $("#popupDialog").html(vals);
    });
    $("#popupDialog").dialog({
        modal: true,
        height:500,
        width:600
    });
    $('#popupDialog').bind('dialogclose', function() {
        $("#popupDialog").remove();
    });
}

$("document").ready(function(){
    $('.addonPricing').hide();
    $(".hiddenGem").css('cursor', 'pointer');
    $(".hiddenGem").prev().css("float","left");
    $(".hiddenGem").css("margin-top","14px");
    $(".handmeout").css('cursor', 'pointer');
    var parent = $(".hiddenGem").parent();
    var uls = $(parent).find("ul");
    $.each(uls, function (k,v){
        $(v).toggle();
    });
    $(".DisplayDialog").each(function(){
        var $href = $(this).attr("href");
        var $title = $(this).text();
        //var re = /^[A-z-' '-'-']*$/;  /* this will test for A through Z, spaces and dashes */
        var re = /^http*$/;  /* Check only to see if string starts with http */
        if (!re.test($href)) {
            $(this).click(function(event) {
                event.preventDefault();
                displayNeuStoreDialog("/layout/set/neustorepopup"+$href,$title);
            })
        }else{
            window.open($href);
        }
    });

$(".popup_noheader").bind('click', function(event) {
        event.preventDefault();
        //console.log(event);
        //return false;
        var Ohref = $(this).attr("href");
            //$(this).attr("href","#");
            var oShare  = ',scrollbars=yes,resizable=no,location=no,status=no'; // shared options
            var options = 'width=600,height=500' + oShare; // defaults
            var url = "/layout/set/popupnohead"+Ohref;
            window.open(url, "_blank", options);


});
   /* $(".popup_noheader").each(function(){
        $(this).click(function(event) {
            var $Ohref = $(this).attr("href");
            //$(this).attr("href","#");
            var oShare  = ',scrollbars=yes,resizable=no,location=no,status=no'; // shared options
            var options = 'width=600,height=500' + oShare; // defaults
            var $url = "/layout/set/popupnohead"+$Ohref;
            event.preventDefault();
            window.open($url, "_blank", options);
        });
});   */


    });
function showTac() {
    $( "#dialog" ).dialog({
        width: 790
    } );
}

function validatespecialchar(obj){
    /*console.log(nextEl); */
    var errChildren = $(obj).parent().children(".error");
    //console.log($(obj).parent());
    $(errChildren).each(function(){
        if (this.tagName != "INPUT"){
	//console.log("item to be removed"+this.tagName);
        $(this).remove();
}
    });
    var $match =  /^[A-z-' '-'-']*$/;
    if(!$match.test($(obj).val())){
        $(obj).after("<span class='error' style='margin-left: 204px; *margin-left: 10px;'>You entered invalid characters, please correct before continuing</span>");
    }
}

function validatefield(obj){
    /*console.log(nextEl); */
    var errChildren = $(obj).parent().children(".error");
    //console.log($(obj).parent());
    $(errChildren).each(function(){
        if (this.tagName != "INPUT"){
	//console.log("item to be removed"+this.tagName);
        $(this).remove();
        }
    });
    var $match =  /^[A-z0-9-' '-'-']*$/;
    if(!$match.test($(obj).val())){
        $(obj).after("<span class='error' style='margin-left: 204px; *margin-left: 10px;'>You entered invalid characters, please correct before continuing</span>");
    }
}

function ccvalidate(){
     var d=new Date();
     var cc_m=$('select[name="cc_mo"]').val();
     var cc_yr=$('select[name="cc_year"]').val();
     var errChdren=$('select[name="cc_year"]').parent().children(".error");

         $(errChdren).each(function(){
            if (this.tagName != "SELECT"){
            $(this).remove();
            }
         });

if(cc_yr !=""){
    if(cc_yr < d.getFullYear()){       
       $('select[name="cc_year"]').after("<span class='error' style='float: right; *margin-left: 10px;'>Your card is expired</span>");
       return false;
     }
    else if(cc_yr == d.getFullYear()){
      if(cc_m < (d.getMonth()+1)){       
        $('select[name="cc_year"]').after("<span class='error' style='float: right; *margin-left: 10px;'>Your card is expired</span>");
        return false;
       }
     }
      else if(cc_yr > d.getFullYear()){
        if(cc_m == ""){
        $('select[name="cc_year"]').after("<span class='error' style='float: right; *margin-left: 10px;'>Please enter expiration month</span>");
        return false;
        }
     }
}
}

function setDisp(e,showText,hideText){

    document.cookie = "viewPricing=true";
    var cookie_date = new Date ( );  // current date & time
    cookie_date.setTime ( cookie_date.getTime() - 1 );

    var parent = $(".hiddenGem").parent();
    var uls = $(parent).find("ul");
    if ($(e).hasClass("showingHidden") || !(getCookie('viewPricing'))){
        delete_cookie('viewPricing');
        $(e).removeClass("showingHidden").html(showText);
        $.each(uls, function (k,v){
            $(v).slideUp("50",function(e){
                $(e).hide();
            });
        });
    }
    else{
        $(e).addClass("showingHidden").html(hideText);
        $.each(uls, function (k,v){
            $(v).slideDown("50",function(e){
                $(e).show();
            });
        });
    }

}
$("document").ready(function(){
    if(getCookie('viewPricing')){
        testing = $(".hiddenGem");
        setDisp(testing,'View Pricing','Hide Pricing');
    }
});

/* Code copied from http://www.perlscriptsjavascripts.com/ */
function getCookie(w){
    cName = "";
    pCOOKIES = new Array();
    pCOOKIES = document.cookie.split('; ');
    for(bb = 0; bb < pCOOKIES.length; bb++){
        NmeVal  = new Array();
        NmeVal  = pCOOKIES[bb].split('=');
        if(NmeVal[0] == w){
            cName = unescape(NmeVal[1]);
        }
    }
    return cName;
}

function delete_cookie(cookie_name){
    var cookie_date = new Date ( );  // current date & time
    cookie_date.setTime ( cookie_date.getTime() - 1 );
    document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}

$("document").ready(function(){
    $('select[name="acct_state"]').change(function () {
         var state = $("#statesList1").val();
        if(state == "OT"){
          $("#otherState1").show();
          $('input[name="acct_other_state"]').addClass("required");
          $("#phoneareacode").attr('minlength','1');
          $('input[name="acct_zip"]').attr('maxlength','7');
          $('input[name="acct_zip"]').removeClass("number");
          $("#phonecountrycode").val('+');
         }
         else{
          $("#otherState1").hide();
          $('input[name="acct_other_state"]').removeClass("required");
          $("#phoneareacode").attr('minlength','3');

          if($("#countrylist").val() != "CA"){
          $("#countrylist").val("US");
          $('input[name="acct_zip"]').attr('maxlength','5');
          $('input[name="acct_zip"]').addClass("number");
            }
          $("#phonecountrycode").val('+1');
          $("#phonecountrycode").hide();
         }
});

$('select[name="acct_country"],select[name="acct_state"]').change(function () {
    var errChildren = $('select[name="acct_country"],select[name="acct_state"]').parent().children(".error");
        $(errChildren).each(function(){
            if (this.tagName != "INPUT" && this.tagName != "SELECT"){
            //console.log("item to be removed"+this.tagName);
            $(this).remove();
            }
        }); 
});

$('select[name="bill_country"],select[name="bill_state"]').change(function () {
    var errChildren = $('select[name="bill_country"],select[name="bill_state"]').parent().children(".error");
        $(errChildren).each(function(){
            if (this.tagName != "INPUT" && this.tagName != "SELECT"){
            //console.log("item to be removed"+this.tagName);
            $(this).remove();
            }
        });
});

$('select[name="bill_state"]').change(function (){
         var state = $("#statesList2").val();
        if(state == "OT"){
          $("#otherState2").show();
          $('input[name="bill_other_state"]').addClass("required");
          $('input[name="bill_zip"]').attr('maxlength','7');
          $('input[name="bill_zip"]').removeClass("number");
          $('input[name="bill_zip"]').attr('minlength','5');
         }
         else{
          $("#otherState2").hide();
          $('input[name="bill_other_state"]').removeClass("required");
          if($("#countrylist2").val() != "CA"){
          $("#countrylist2").val("US");
          $('input[name="bill_zip"]').attr('maxlength','5');
          $('input[name="bill_zip"]').addClass("number");
            }

         }
});



$('select[name="acct_country"]').change(function () {
         var country = $("#countrylist").val();
        // alert(country);
        if(country == "US" || country == "CA"){
            $("#phonecountrycode").val('+1');
            $("#phonecountrycode").hide();
            $("#otherState1").hide();
            $('input[name="acct_other_state"]').removeClass("required");            
            if($("#statesList1").val() == "OT"){
            $("#statesList1").val(""); }            
            $("#phoneareacode").attr('minlength','3');

         }else{
         $("#phonecountrycode").val('+');
         $("#phonecountrycode").show();
         $("#otherState1").show();
         $('input[name="acct_other_state"]').addClass("required");
         $("#statesList1").val("OT");         
         $("#phoneareacode").attr('minlength','1');
         }
});

$('select[name="acct_country"]').change(function () {
         var country = $("#countrylist").val();
        if(country == "US"){
            $('input[name="acct_zip"]').attr('maxlength','5');
            $('input[name="acct_zip"]').addClass("number");
        }else{
            $('input[name="acct_zip"]').attr('maxlength','7');
            $('input[name="acct_zip"]').removeClass("number");
         }
});

$('select[name="bill_country"]').change(function () {
         var country = $("#countrylist2").val();
        // alert(country);
        if(country == "US" || country == "CA"){
             $("#otherState2").hide();
             $('input[name="bill_other_state"]').removeClass("required");
             if($("#statesList2").val() == "OT"){
             $("#statesList2").val(""); }
         }else{
             $("#otherState2").show();
             $('input[name="bill_other_state"]').addClass("required");
             $("#statesList2").val("OT");
         }
});

$('select[name="bill_country"]').change(function () {
         var country = $("#countrylist2").val();
        // alert(country);
        if(country == "US"){
            $('input[name="bill_zip"]').attr('maxlength','5');
            $('input[name="bill_zip"]').addClass("number");

         }else{
            $('input[name="bill_zip"]').attr('maxlength','7');
            $('input[name="bill_zip"]').removeClass("number");
         }
});

$('select[name="acct_state"]').change(function () {
   var state = $("#statesList1").val();
   if(state == "OT"){
     $("#otherState1").show();
     $('input[name="acct_other_state"]').addClass("required");
     $('input[name="acct_zip"]').attr('maxlength','7');
     $('input[name="acct_zip"]').removeClass("number");
    }
});

$('select[name="bill_state"]').change(function (){
   var state1 = $("#statesList2").val();
   if(state1 == "OT"){
     $("#otherState2").show();
     $('input[name="bill_other_state"]').addClass("required");
     $('input[name="bill_zip"]').attr('maxlength','7');
     $('input[name="bill_zip"]').removeClass("number");
    }
});

 $('select[name="acct_country"]').change(function () {
   var contryselect=$("#countrylist").val();
     if(contryselect == "US" || contryselect == "PR" || contryselect == "CA"){
            $("#phonecountrycode").val('+1');
            $("#phonecountrycode").hide();

         }
 });

});



