// JavaScript Document
function echeck(str) {
    var at="@";
    var dot=".";
    var lat=str.indexOf(at);
    var lstr=str.length;
    var ldot=str.indexOf(dot);
    if (str.indexOf(at)==-1){
        return false;
    }
    if (str.indexOf(at)==-1 || str.indexOf(at)===0 || str.indexOf(at)==lstr){
        return false;
    }
    if (str.indexOf(dot)==-1 || str.indexOf(dot)===0 || str.indexOf(dot)==lstr){
        return false;
    }
    if (str.indexOf(at,(lat+1))!=-1){
        return false;
    }
    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
        return false;
    }
    if (str.indexOf(dot,(lat+2))==-1){
        return false;
    }
    if (str.indexOf(" ")!=-1){
        return false;
    }
    return true;
}
function startFreeTrial() {
    configureAccountReset();
    $('#configureAccount').dialog('open');
    pageTracker._trackPageview('->configureAccount');
		
    $('#configureAccount input[title]').each(function() {
        $(this).focus();
    });
    $('#configureAccount input:not([title]):first').focus();
    return false;
}
	
function doContactUsForm() {
    validateValue = true;

    if (!echeck($('#contactUsForm #email').val()) || $('#contactUsForm #email').val()==="") {
        validateValue = false;
        $('#contactUsForm #email').css('background','#fff6c6');
    } else {
        $('#contactUsForm #email').css('background','white');
    }
	
    if ($('#contactUsForm #name').val()==='') {
        validateValue = false;
        $('#contactUsForm #name').css('background','#fff6c6');
    } else {
        $('#contactUsForm #name').css('background','white');
    }
	
    if ($('#contactUsForm #additionalcomments').val()==='') {
        validateValue = false;
        $('#contactUsForm #additionalcomments').css('background','#fff6c6');
    } else {
        $('#contactUsForm #additionalcomments').css('background','white');
    }
	
    if (validateValue === true) {
        name = $('#contactUsForm #name').val();
        email = $('#contactUsForm #email').val();
        phone = $('#contactUsForm #phone').val();
        additionalcomments = $('#contactUsForm #additionalcomments').val();

        $.post("/public/contact_us/", {
            name: name,
            email: email,
            additionalcomments : additionalcomments,
            phone: phone,
            authenticity_token : form_authenticity_token
        },function(data){
            if (data=="ok") {
                $('#contactUsErrorMsg').hide();
                $("#contactUs").dialog("close");
				
            } else {
                $('#contactUsErrorMsg').hide();
                alert("Something went wrong: " + data);
            }
            $('.wait_loading').hide();
        });
    } else {
        $('#contactUsErrorMsg').show();
        $('.wait_loading').hide();
    }
    return true;
}
function askAnotherQuestion() {
    $('#askQuestionEmail').val("");
    $('#askQuestionMessage').val('');
    $('#askQuestionRest').fadeIn();
    $('#askQuestionOkMsg').fadeOut();
}
function askQuestion() {
    if ((!echeck($('#askQuestionEmail').val()) || ($('#askQuestionEmail').val()==="") || ($('#askQuestionMessage').val()===""))) {
        $('#askQuestionErrorMsg').show();
    } else {
		
        $('#askQuestionErrorMsg').hide();
        email = $('#askQuestionEmail').val();
        message = $('#askQuestionMessage').val();

        $.post("/home/askQuestion/", {
            email: email,
            message: message
        },function(data){
            if (data=="ok") {
                $('#askQuestionRest').fadeOut();
                $('#askQuestionOkMsg').fadeIn();
            } else {
                $('#askQuestionOkMsg').hide();
                alert("Something went wrong: " + data);
            }
        });
    }
}

function forwardThisForm() {
    validateValue = true;

    if (!echeck($('#forwardToFriendemail').val()) || $('#forwardToFriendemail').val()==="") {
        validateValue = false;
        $('#forwardToFriendemail').css('background','#fff6c6').next().show();
    } else {
        $('#forwardToFriendemail').css('background','white').next().hide();
    }
	
    if ($('#forwardToFriendname').val()==='') {
        validateValue = false;
        $('#forwardToFriendname').css('background','#fff6c6').next().show();
    } else {
        $('#forwardToFriendname').css('background','white').next().hide();
    }
	
    if (validateValue === true) {
        name = $('#forwardToFriendname').val();
        email = $('#forwardToFriendemail').val();
        message = $('#forwardToFriendcomments').val();

        $.post("/home/forwardsite/", {
            name: name,
            email: email,
            message: message,
            authenticity_token : form_authenticity_token
        },function(data){
            if (data=="ok") {
                $('#forwardToFriendError').hide();
                $("#forwardToFriend").dialog("close");
                $("#thanksForward").dialog("open");
				
            } else {
                $('#forwardToFriendError').hide();
                $('#forwardToFriendOk').hide();
                alert("Something went wrong: " + data);
            }
            $('.wait_loading').hide();
        });
    } else {
        $('#forwardToFriendError').show();
        $('.wait_loading').hide();
    }
    return true;
}	

var newwindow;
function poptastic(url) {
    newwindow=window.open(url,'name','height=600,width=680,scrollbars=yes');
    if (window.focus) {
        newwindow.focus();
    }
}

var slugIsOk = false;
function checkSlug() {
    configureAccountReset($('#configAccountTeam'));

    testSlug = $('#signUpSlug').val();
    if (testSlug==="") {
        slugIsOk= false;
        $('#configAccountLogin').addClass("fail");
    } else if (!testSlug.match(/^[\w-]+$/)) {
        slugIsOk= false;
        $('#configAccountLogin').addClass("fail");
    } else {
        $('#slugspinner').show();
        $.post("/public/check_slug/", {
            slug : testSlug
        },function(data){
            if (data=="ok") {
                $('#configAccountTeam').addClass("pass");
                $('#createAccountNowButton').removeAttr('disabled');
                slugIsOk = true;
            } else {
                $('#configAccountTeam').addClass("fail");
                $('#createAccountNowButton').attr('disabled','disabled');
                slugIsOk = false;
            }
            $('#slugspinner').hide();
        });
    }
}

function signUpforUpdatesFormSubmit() {
    errors=false;
    if ($('#gjhlh-gjhlh').val() ===""  || !echeck($('#gjhlh-gjhlh').val())) {
        errors=true;
        $('#signupUpdatesemail').css('background','#fff6c6');
    } else {
        $('#signupUpdatesemail').css('background','white');
    }
	
    if (errors) {
        $('#signupUpdateserrorMsg').show();
        $('#signupUpdatesOk').hide();
        $('.wait_loading').hide();
    } else {
        name = $('#signupUpdatesname').val();
        email = $('#signupUpdatesemail').val();
        $('#signUpforUpdatesForm').submit();
    }
}

function configureAccountReset(row) {
    $('#configAccountMainError').fadeOut('fast');
    if (row===undefined) {
        $('input').val('');
        $('.input_check_titles input').each(function() {
            $(this).val($(this).attr('title'));
        });
													 
        $('#configAccountMainError').hide();
        $('.configAccountRow').removeClass('pass fail');
        $('.configAccountRow').addClass('blank');

    } else {
		
        row.removeClass('pass fail');
        row.addClass('blank');
        row.nextAll().removeClass('pass fail');
        row.nextAll().addClass('blank');
    }
}
function configureAccountValidates(row) {
	
    configureAccountReset(row);
    vfield = $('#' + row.attr('id') + ' .validatedfield:first');
	
    if (vfield.attr('id')=='signUpName') {
        if (vfield.val()==="") {
            row.addClass('fail');
            return false;
        } else {
            row.addClass('pass');
            return true;
        }
    } else if (vfield.attr('id')=='signUpEmail'){
        if (vfield.val()==="" || !echeck(vfield.val())) {
            row.addClass('fail');
            return false;
        } else {
            row.addClass('pass');
            return true;
        }

	
    } else if (vfield.attr('id')=='signUpSlug'){
        if (vfield.val()===""  || vfield.val().length < 2) {
            row.addClass('fail');
            return false;
        } else {
            return true;
        }
	
    } else if (vfield.attr('id')=='signUpPassword') {
        if ($('#signUpPassword').val() === "" || $('#signUpPassword').val().length < 8) {
            row.addClass('fail');
            return false;
			
        } else {
            if ($('#signUpPassword').val()!=$('#signUpPassword2').val()) {
                row.addClass('fail');
                return false;
            } else {
                row.addClass('pass');
                return true;
            }
        }
    }
}

function validateNewAccountForm() {
    pageTracker._trackPageview('->validateStart');
    errors=true;
    checkSlug();

    if (configureAccountValidates($('#configAccountTeam'))) {
        errors=errors;
    } else {
        errors=false;
    }

    if (configureAccountValidates($('#configAccountName'))) {
        errors=errors;
    } else {
        errors=false;
    }

    if (configureAccountValidates($('#configAccountEmail'))) {
        errors=errors;
    } else {
        errors=false;
    }




    if (configureAccountValidates($('#configAccountPassword'))) {
        errors=errors;
    } else {
        errors=false;
    }

    if (errors){
        forceCloseConfig=true;
        pageTracker._trackPageview('->validateOk');
        pageTracker._trackPageview('/index.html/enter_docubuzz');
        $('#createAccountNowButton').hide().next().show();
        signUpAndEnterDb();

    } else {
        pageTracker._trackPageview('->validateFail');
        $('#configAccountMainError').fadeIn();
    }
}

function signUpAndEnterDb() {
    pageTracker._trackPageview('->enterDocubuzz');
    $('#signUpForm').submit();
}




$(document).ready(function() {
    $('input.simple_validated', $('#configureAccount')).bind('blur', function() {
        configureAccountValidates($(this).parents('.configAccountRow'));
    }).bind('focus', function() {
        configureAccountReset($(this).parents('.configAccountRow'));
    }).bind('change', function() {
        configureAccountReset($(this).parents('.configAccountRow'));
    });

    $('.startFreeTrial').click(function() {
        startFreeTrial();
    });

    $("#contactUs").dialog({
        autoOpen : false,
        modal : true,
        width : 450,
        height: 545
    });

    $("#forwardToFriend").dialog({
        autoOpen : false,
        modal : true,
        width : 400,
        height: 430
    });

    $("#signupUpdates").dialog({
        autoOpen : false,
        modal : true,
        width : 400,
        height: 320
    });
    $("#thanksNewsLetter").dialog({
        autoOpen : false,
        modal : true,
        width : 450,
        height: 220
    });


    $("#thanksForward").dialog({
        autoOpen : false,
        modal : true,
        width : 450,
        height: 190
    });

    $('.fg-button').hover(
        function(){
            $(this).addClass("ui-state-hover");
        },
        function(){
            $(this).removeClass("ui-state-hover");
        });

    selectedString=document.location.toString();
    if (selectedString.match('#')) { // the URL contains an anchor
        // click the navigation item corresponding to the anchor
        if (selectedString.split('#')[1] == "thanksNewsLetter") {
            $("#thanksNewsLetter").dialog("open");
        }
    }
});