var $j = jQuery.noConflict();

var fnames = new Array();var ftypes = new Array();fnames[0]='EMAIL';ftypes[0]='email';fnames[1]='FNAME';ftypes[1]='text';fnames[2]='LNAME';ftypes[2]='text';var err_style = '';

$j(document).ready( function() {
  var options = { errorClass: 'mce_inline_error', errorElement: 'div', errorStyle: err_style, onkeyup: function(){}, onfocusout:function(){}, onblur:function(){}  };
  var mce_validator = $j("#mc-embedded-subscribe-form").validate(options);
  options = { url: 'http://byboe.us2.list-manage.com/subscribe/post-json?u=4992af8c99ff5b6a221b4ea24&id=096be8bf36&c=?', type: 'GET', dataType: 'json', contentType: "application/json; charset=utf-8",
                beforeSubmit: function(){
                    $j('#mce_tmp_error_msg').remove();
                    $j('.datefield','#mc_embed_signup').each(
                        function(){
                            var txt = 'filled';
                            var fields = new Array();
                            var i = 0;
                            $j(':text', this).each(
                                function(){
                                    fields[i] = this;
                                    i++;
                                });
                            $j(':hidden', this).each(
                                function(){
                                	if ( fields[0].value=='MM' && fields[1].value=='DD' && fields[2].value=='YYYY' ){
                                		this.value = '';
									} else if ( fields[0].value=='' && fields[1].value=='' && fields[2].value=='' ){
                                		this.value = '';
									} else {
	                                    this.value = fields[0].value+'/'+fields[1].value+'/'+fields[2].value;
	                                }
                                });
                        });
                    return mce_validator.form();
                }, 
                success: mce_success_cb
            };
  $j('#mc-embedded-subscribe-form').ajaxForm(options);

});
function mce_success_cb(resp){
    if (resp.result=="success"){
        openNewsDialog(resp.msg);
        $j('#mc-embedded-subscribe-form').each(function(){
            this.reset();
    	});
    } else {
        var index = -1;
        var msg;
        try {
            var parts = resp.msg.split(' - ',2);
            if (parts[1]==undefined){
                msg = resp.msg;
            } else {
                i = parseInt(parts[0]);
                if (i.toString() == parts[0]){
                    index = parts[0];
                    msg = parts[1];
                } else {
                    index = -1;
                    msg = resp.msg;
                }
            }
        } catch(e){
            index = -1;
            msg = resp.msg;
        }
        try{
            if (index== -1){
            openErrorDialog(msg);
            } else {
                err_id = 'mce_tmp_error_msg';
                html = '<div id="'+err_id+'" style="'+err_style+'"> '+msg+'</div>';
                
                var input_id = '#mc_embed_signup';
                var f = $j(input_id);
                if (ftypes[index]=='address'){
                    input_id = '#mce-'+fnames[index]+'-addr1';
                    f = $j(input_id).parent().parent().get(0);
                } else if (ftypes[index]=='date'){
                    input_id = '#mce-'+fnames[index]+'-month';
                    f = $j(input_id).parent().parent().get(0);
                } else {
                    input_id = '#mce-'+fnames[index];
                    f = $j().parent(input_id).get(0);
                }
                if (f){
                    $j(f).append(html);
                    $j(input_id).focus();
                } else {
                    openErrorDialog(msg);
                }
            }
        } catch(e){
            openErrorDialog(msg);
        }
    }
}

function openNewsDialog(message) {
            $j("#newsletterDialog").remove();
            $j("body").append("<div id='newsletterDialog' style='height:161px; width:340px; background-image:url(/assets/mailsignup/signup.gif);'> <div style='color:#000; font-size:14px; font-family:Helvetica, Arial, sans-serif; width: 320px; text-align:center; font-weight: normal; line-height: normal; padding-top: 30px;'>"+message+"</div> <div style='padding-top: 20px; margin-left: 113px; width: 74px; height: 24px;'> <a id='newsletterDialogClose' href='#'><img src='/assets/mailsignup/close-btn.gif' border='0'/></a></div> </div>");
            $j("#newsletterDialog").dialog({ bgiframe: true, resizable: false, height: 161, width: 340, modal: true, open: function (e) {
                $j("#newsletterDialogClose").focus();
                $j("#newsletterDialogClose").click(function (e) {

                    $j("#newsletterDialog").dialog("close");
                });
                $j('#newsletterDialogClose').keypress(function(e) {
                   if(e.which == 13) {
                       $j("#newsletterDialog").dialog("close");
                   }
                 });
            }
            });
}

function openErrorDialog(message) {
            $j("#invalidEmailDialog").remove();
            $j("body").append("<div id='invalidEmailDialog' style='height:161px; width:340px; background-image:url(/assets/mailsignup/signup-error.gif);'> <div style='color:#000; font-size:14px; font-family:Helvetica, Arial, sans-serif; width: 320px; text-align:center; font-weight: normal; line-height: normal; padding-top: 40px;'>"+message+"</div> <div style='padding-top: 30px; margin-left: 113px; width: 74px; height: 24px;'> <a id='invalidEmailDialogClose' href='#'><img src='/assets/mailsignup/close-btn.gif' border='0'/></a></div> </div>");
            $j("#invalidEmailDialog").dialog({ bgiframe: true, resizable: false, height: 161, width: 340, modal: true, open: function (e) {
                $j("#invalidEmailDialogClose").focus();
                $j("#invalidEmailDialogClose").click(function (e) {

                    $j("#invalidEmailDialog").dialog("close");
                });
                $j('#invalidEmailDialogClose').keypress(function(e) {
                   if(e.which == 13) {
                       $j("#invalidEmailDialog").dialog("close");
                   }
                 });

            }
            });
}

