$(document).ready(function(){
    $('.textInput').focus(function(){
        $(this).css("border-color","gray");
    });

    $('.textInput').blur(function(){
        $(this).css("border-color","#cccccc");
    });

    $('.textAreaInput').focus(function(){
        $(this).css("border-color","gray");
    });

    $('.textAreaInput').blur(function(){
        $(this).css("border-color","#cccccc");
    });

    $('.tool').click(function(){
        $('#toolAction').val($(this).attr('id'));
    });

    var checkedTool = $('input:checked').parent().parent().attr('id');

    if(checkedTool == ''){
        checkedTool = $('input:checked').parent().attr('id');
    }

    $('#toolAction').val(checkedTool);

    // New tool dialog
    $('#newToolDialog').dialog({
        autoOpen: false,
        resizable: false,
        width: 400,
        buttons: {
            "Cancel": function() {
                $(this).dialog("close");
            },
            "Request": function() {
                requestNewTool();
            }
        }
    });

    $('#newToolLink').click(function(){
        $('#newToolDialog').dialog('open');
        return false;
    });

    // Feedback dialog
    $('#feedbackDialog').dialog({
        autoOpen: false,
        resizable: false,
        width: 400,
        buttons: {
            "Cancel": function() {
                $(this).dialog("close");
            },
            "Send": function() {
                sendFeedback();
            }
        }
    });

    $('#feedbackLink').click(function(){
        $('#feedbackDialog').dialog('open');
        return false;
    });

    $('#toolSubmitInput').submit(function(){
        $('pre').html('Loading... Thanks for waiting!');
    });


    $(function() {
        if($("#serviceTabs").length > 0){

            $("#serviceTabs").tabs({
                ajaxOptions: {
                    error: function(xhr, status, index, anchor) {
                        $(anchor.hash).html("Couldn't load this tab. We'll try to fix this as soon as possible. If this wouldn't be a demo.");
                    }
                }
            });
        }
    }
    );



        
});

function getRequestParameter(paramName) {
    paramName = paramName.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexPattern = "[\\?&]" + paramName + "=([^&#]*)";
    var regexSearch = new RegExp(regexPattern);
    var matchResults = regexSearch.exec(window.location.href);

    if (matchResults == null) {
        return "";
    } else {
        return matchResults[1];
    }
}

function requestNewTool(){
    var toolName = $('#newToolName').val();
    var toolDescription = $('#newToolDescription').val();
    var toolEmail = $('#newToolEmail').val();

    $.post('/?clientAction=requestNewTool',
    {
        newToolName: toolName,
        newToolDescription: toolDescription,
        newToolEmail: toolEmail
    },function(){
        $('#newToolDialog').dialog('close');
    });
}

function sendFeedback(){
    var feedbackEmail = $('#feedbackEmail').val();
    var feedbackComments = $('#feedbackComments').val();

    $.post('/?clientAction=sendFeedback',
    {
        feedbackEmail : feedbackEmail,
        feedbackComments: feedbackComments
    },function(){
        $('#feedbackDialog').dialog('close');
    });
}


