function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function ClearInput(value, id){
	var input = document.getElementById(id);
	if(value == input.value){
		input.value = '';
	}
	else{
		input.value = input.value;
	}
}

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); }
String.prototype.startsWith = function(str) { return (this.match("^" + str) == str); }
function isValidEmail(email) {
    var regex = /([\w\d\-_]+)(\.[\w\d\-_]+)*@([\w\d\-_]+\.)([\w\d\-_]+\.)*([\w]{2,3})/;

    if (regex.test(email))
        return true;
    else
        return false;
}
function resetText(obj, text, isClick) {
    if (obj.val() == text && isClick)
        obj.val('');
    else if (obj.val() == '' && !isClick)
        obj.val(text);
}
/*start blog*/
$(document).ready(function() {

    //Set top banner to display

    
    $('.blog-reply').click(function() {
        $(this).next('.blog-reply-comment').html(
        '<div class="form replyForm"><div class="clearer high16">&nbsp;</div><h3>Reply</h3><div class="clearer"></div><p class="blog-reply-error"></p><input name="tbBlogCommentName" class="inputText" value="Your Name" type="text"><div class="clearer"></div><input name="tbBlogCommentEmail" class="inputText" value="Your Email [not displayed on the website]" type="text"><div class="clearer"></div><textarea name="tbBlogCommentComment" cols="20" rows="2" value="Your Comment">Your Comment</textarea><div class="clearer"></div><a href="javascript:void(0)" class="orangeButton repyButton"><span>SUBMIT</span></a><a href="javascript:void(0)" class="orangeButton blog-reply-cancel"><span>CANCEL</span></a><div class="clearer">&nbsp;</div></div>');
        $('.blog-reply-cancel').click(function() {
            $(this).parents('.blog-reply-comment').html('');
        });
        $('input[name=tbBlogCommentName]').click(function() {
            resetText($(this), 'Your Name', true);
        });
        $('input[name=tbBlogCommentName]').blur(function() {
            resetText($(this), 'Your Name', false);
        });
        $('input[name=tbBlogCommentEmail]').click(function() {
            resetText($(this), 'Your Email [not displayed on the website]', true);
        });
        $('input[name=tbBlogCommentEmail]').blur(function() {
            resetText($(this), 'Your Email [not displayed on the website]', false);
        });
        $('textarea[name=tbBlogCommentComment]').click(function() {
            resetText($(this), 'Your Comment', true);
        });
        $('textarea[name=tbBlogCommentComment]').blur(function() {
            resetText($(this), 'Your Comment', false);
        });
        $('.repyButton').click(function() {
            var name = $(this).siblings('input[name=tbBlogCommentName]').val();
            var email = $(this).siblings('input[name=tbBlogCommentEmail]').val();
            var comment = $(this).siblings('textarea[name=tbBlogCommentComment]').val();
            var root = $('#blogRootId').val();

            //validation
            $(this).siblings('p.blog-reply-error').html('');
            var isValid = true;
            var errorMsg = '';
            if (name.trim() == '' || name.trim().toLowerCase() == 'your name') {
                isValid = false;
                errorMsg += 'Please enter your name<br />';
            }
            if (email.trim() == '' || email.trim().toLowerCase() == 'your email [not displayed on the website]') {
                isValid = false;
                errorMsg += 'Please enter your email address<br />';
            }
            else if (!isValidEmail(email)) {
                isValid = false;
                errorMsg += 'Please enter a valid email address<br />';
            }
            if (comment.trim() == '' || comment.trim().toLowerCase() == 'your comment') {
                isValid = false;
                errorMsg += 'Please enter your comment<br />';
            }
            if (!isValid) {
                $(this).siblings('p.blog-reply-error').html(errorMsg);
                return false;
            }

            var commentID = $(this).parents('.comment').children('input:hidden').val();
            $(this).html('Submitting... Please Wait');
            jQuery.post('/handlers/BlogCommentReply.ashx', { commentid: commentID, name: name, email: email, comment: comment, root: root }, function(data) {
                var result = data;
                if (data.startsWith('Success')) {
                    if (data.startsWith("Success:posted")) {
                        $('#divBlogReplyComment-' + commentID).html('<p>Your reply has been posted.</p>');
                        var newCommentHTML = $('#ulBlogCommentReply-' + commentID).html() + '<li>' + comment + '<br /><cite>' + name + '</cite><small class="commentmetadata">' + data.split(':')[2] + '</small</li>';
                        $('#ulBlogCommentReply-' + commentID).html(newCommentHTML);
                    }
                    else {
                        $('#divBlogReplyComment-' + commentID).html('<p>Your reply is now submitted and pending approval.</p>');
                    }
                }
                else {
                    $('#divBlogReplyComment-' + commentID).html('An error had occurred. Please try again later.');
                }
            });
        });
    });
});
/*end blog*/

function showLogin() {
    $('#loginContainer').show('fast');
    $('#loginLink').hide();
    $('#closeLink').show();
}

function hideLogin() {
    $('#loginContainer').hide('fast');
    $('#closeLink').hide();
    $('#loginLink').show();
}

function initSecureLinks() {
    $('.secureLink').click(function() {
        showLBMain('lightBox2');
        //set url to go
        $('#GoURL').attr("value", this);
        window.location = '#top';
        return false;
    });
}