var ajax = '/ajax/publicBackend.php';
var comment = { };
function submitComment() {
if ($('commentName').value != ''){
  $('errorMessage').innerHTML = '';
  comment.postId = $('commentPostId').value;
  comment.name   = $('commentName').value;
  comment.email  = $('commentEmail').value;
  comment.website = $('commentWebsite').value;
  comment.comment = $('commentComment').value;
  if ($('commentSubmit')) { $('commentSubmit').disabled = true; }
  new Ajax.Request(ajax, {
    parameters: {
        pos: 'postcomment',
        postid: comment.postId,
        name: comment.name,
        email: comment.email,
        website: comment.website,
        comment: comment.comment
    },
    onSuccess:handlerSubmitComment,
    onFailure:errFunc,
    onException:errFunc
  });
}else {  
    $('errorMessage').innerHTML = "You must enter your name. Please try again.";
    new Effect.Highlight('commentName', {startcolor:'#dd0000', endcolor:'#ffffff'});
      }
}
function handlerSubmitComment(t) {
  if (t.responseText.match("%%SUCCESS%%") || t.responseText.match("%%MODERATE%%")) {
    $('commentName').value = '';
    $('commentEmail').value = '';
    $('commentWebsite').value = '';
    $('commentComment').value = '';
    if ($('commentSubmit')) { $('commentSubmit').disabled = false; }
    var commentEl = document.createElement('DIV');
    var rand = Math.floor(Math.random()*10000);
    commentEl.id = "comment"+rand;
    comment.comment = comment.comment.replace(new RegExp("\n", "g"), "
");
    comment.website = "http://"+comment.website.replace(/http:\/\//, "");
    comment.website = comment.website == "http://" ? "" : "";
    comment.endTag  = comment.website == "http://" ? "" : "";
    comment.name    = comment.name    == "" ? "Anonymous" : comment.name;
    if (t.responseText.match("%%MODERATE%%")) {
      comment.moderate = "Note: This comment is currently being held for moderation awaiting approval.
";
    } else {
      comment.moderate = "";
    }
    commentEl.innerHTML = comment.moderate+""+comment.website+comment.name+comment.endTag+"
"+comment.comment+"
   
";
    commentEl.style.display = 'none';
    $('lastComment').appendChild(commentEl);
    Effect.Appear(commentEl);
    // Reset fields
    $('commentName').value = '';
    $('commentEmail').value = '';
    $('commentWebsite').value = '';
    $('commentComment').value = '';
  } else {
    if ($('commentSubmit')) { $('commentSubmit').disabled = false; }
    if (t.responseText.match("ERROR:")) {
      $('errorMessage').innerHTML = t.responseText.replace("ERROR:", "");
    } else {
      $('errorMessage').innerHTML = "There was an error submitting your comment. Please try again";
    }
  }
}
function blogCommentDisplayForm(url, wrapperId, commentId) {
    var wrapper = $(wrapperId),
        isOpen = wrapper.retrieve('isReplyFormOpen') || false,
        replyButton = wrapper.previous('.reply-comment').select('span')[0],
        cancelText = /*tl(*/'Cancel Reply'/*)tl*/;
    if (wrapper.retrieve('locked')) return;
    wrapper.store('locked', true);
    var replyText = wrapper.retrieve('replyText');
    if (!replyText) {
        replyText = replyButton.innerHTML;
        wrapper.store('replyText', replyText);
    }
    if (isOpen) {
        replyButton.innerHTML = replyText;
        wrapper.store('isReplyFormOpen', false);
        Effect.SlideUp(wrapper, {
            afterFinish: function() { wrapper.store('locked', false); }    
        });
    } else {
        replyButton.innerHTML = cancelText;
        wrapper.store('isReplyFormOpen', true);
        Effect.SlideDown(wrapper, {
            afterFinish: function() { wrapper.store('locked', false); }    
        });
    }
    var iframe = $$('#'+wrapperId+' iframe')[0];
    if (!iframe) {
        var iframe = '';
        $$('#'+wrapperId+' > div > div')[0].update(iframe);
    }
    return false;
}
function blogCommentCreateCancelOverlay(wrapperId, commentId) {
    var wrapper = $(wrapperId);
    var buttonId = 'comment_cancel_'+commentId;
    var button = $(buttonId);
    if (button) {
        button.show();
    } else {
        button = new Element('button', {
            id: buttonId,
            'class': 'blogCommentReplyOverlay'
        }).update(/*tl(*/'Cancel'/*)tl*/).observe('click', function() {
            var siblings = wrapper.previousSiblings();
            var replyButton = siblings.findAll(function(sibling) {
                return sibling.hasClassName('reply-comment');
            });
            replyButton[0].onclick();
            return false;
        });
        wrapper.insert(button);
    }
    
    var left = Math.round((wrapper.getWidth()-442)/2);
    var offset = {'top': 372, 'left': left+381};
    button.setStyle({
        position: 'absolute', 
        top: offset.top+'px', 
        left: offset.left+'px'
    });
};
function blogCommentHideCancelOverlay(wrapperId, commentId) {
    var cancel = $('comment_cancel_'+commentId);
    cancel && cancel.hide();
}
function errFunc(t) {
    $('errorMessage').innerHTML = "There was an error submitting your comment. Please try again";
}
var stylePrefix = window.STYLE_PREFIX || 'weebly';
function updateForm(){
    if (window.location.href.match(/posted=(.*)$/)) {
        var posted = $H(decodeURIComponent(window.location.href.match(/posted=(.*)$/)[1].replace(/\+/g, ' ')).evalJSON());
        $$('form').each(
            function(form){
                posted.each(
                    function(pair){
                        if(typeof(pair.value) === 'object'){
                            $H(pair.value).each(function(subpair){
                                form.getInputs().each(function(input){
                                    if(input.name.replace(/_u\d*/, '') == pair.key+'['+subpair.key+']'
                                        || input.name == pair.key+'['+subpair.key+']'){
                                        if(input.type === 'checkbox'){
                                            input.checked = 1;
                                        }
                                        else{
                                            input.value = subpair.value;
                                        }
                                    }
                                });
                            });
                        }
                        else{
                            form.getElements().each(function(input){
                                if(input.name.replace(/_u\d*/, '') == pair.key
                                    || input.name == pair.key){
                                    var realName = input.name;
                                    if(form[realName][0] && form[realName][0].type === 'radio'){
                                        form.getInputs('radio', realName).each(function(radioinput){
                                            if(radioinput.value == pair.value){
                                                radioinput.checked = true;
                                            }
                                        });
                                    }
                                    else{
                                        input.value = pair.value;
                                    }
                                }
                            });
                        }
                    }
                )
            }
        );
    }
    if (window.location.href.match(/form-errors=(.*?)&/) && window.location.href.match(/ucfid%22%3A%22(.*?)%/) ) {
        var errors = window.location.href.match(/form\-errors=(.*?)&/)[1].split(',');
        var ucfid = window.location.href.match(/ucfid%22%3A%22(.*?)%/)[1];
        var form = $('form-'+ucfid);
        errors.each(function(field){
            field = decodeURIComponent(field);
            form.getElements().each(function(input){
                if(input.name.replace(/_u\d*/, '') == field 
                    || input.name.replace(/.*_u/, '_u') == field
                    || input.name.replace(/\[.*\]$/, '') == field){
                    input.addClassName('form-input-error');
                    input.up('.'+stylePrefix+'-form-field').addClassName('form-field-error');
                }
            });
        });
        $(ucfid+'-form-parent').insert({'after':'Please correct the highlighted fields
'});
    }
    if (window.location.href.match(/success\=1/) && window.location.href.match(/ucfid\=(.*)/) ) {
        var ucfid = window.location.href.match(/ucfid\=(.*?)&/)[1];
        var form = $('form-'+ucfid);
        var confText = 'Your data was successfully submitted.';
        var textMatch = window.location.href.match(/text=(.*?)&/);
        if(textMatch){
            confText = decodeURIComponent(textMatch[1].replace(/\+/g, ' '));
        }
        form.update(''+confText+'
');
    }
}
document.observe('dom:loaded', updateForm);
document.observe('dom:loaded', function(){
    function receiveMessage(message) {
        var response = message.data.evalJSON();
        switch (response.action) 
        {
            case "finished" :
                var ucfid = response.data.ucfid;
                var form = $("form-" + ucfid);
                form.hide();
                if ($(ucfid+'-msg'))
                    $(ucfid+'-msg').update(response.data.message);
                else
                    form.insert({'after':''+response.data.message+'
'});
                new Effect.ScrollTo($(ucfid+'-msg'),{ queue:{scope: 'loading',location:'end'} });
                new Effect.Highlight($(ucfid+'-msg'),{ duration:2, queue:{scope: 'loading',location:'end'} });
                return;
            case "redirect" :
                window.location = response.data.location;
                return;
            case "error" :
                var errors = response.data['error-fields'];
                var ucfid = response.data.ucfid;
                var form = $("form-" + ucfid);
                form.getElements().each(function(input){
                    if(input.hasClassName('form-input-error')) {
                        input.removeClassName('form-input-error');
                        input.up('.'+stylePrefix+'-form-field').removeClassName('form-field-error');
                    } 
                });
                errors.each(function(field){
                    form.getElements().each(function(input){
                        if(input.name.replace(/_u\d*/, '') == field 
                            || input.name.replace(/.*_u/, '_u') == field
                            || input.name.replace(/\[.*\]$/, '') == field){
                            input.addClassName('form-input-error');
                            input.up('.'+stylePrefix+'-form-field').addClassName('form-field-error');
                        } 
                    });
                });
                if ($(ucfid+'-msg'))
                    $(ucfid+'-msg').update(response.data.message);
                else
                    form.insert({'after':''+response.data.message+'
'});
                return;
        }
    }
    var listening = false;
    $$('form').each(
        function(form) {
            if (form.action.match(/formSubmit\.php$/))
            {
                form.action = form.action.replace(/(.*)\/formSubmit\.php$/,window.location.protocol + "//" + window.location.host +"/ajax/apps/formSubmitAjax.php");
                form.acceptCharset = "UTF-8";
                var iframe, 
                    name = form.id + "-target";
                try {
                    iframe = document.createElement('