function setErrorMesg(num, mode) {
    if(mode) {
        $("td.mesg:eq("+num+")").addClass("error");
    } else {
        $("td.mesg:eq("+num+")").removeClass("error");
    }
}

function validateLinkForm(formData, jqForm, options) {
    
    var errorcount = 0;
    
    /**/
    if($.trim($("#link").val()) == "") {
        setErrorMesg(0, true);
        errorcount = errorcount + 1;
    } else {
        setErrorMesg(0, false);
    }
    
    if($.trim($("#description").val()) == "") {
        setErrorMesg(1, true);
        errorcount = errorcount + 1;
    } else {
        setErrorMesg(1, false);
    }
    
    if($("#category").val() == 0) {
        setErrorMesg(2, true);
        errorcount = errorcount + 1;
    } else {
        setErrorMesg(2, false);
    }
    
    if($.trim($("#naam").val()) == "") {
        setErrorMesg(3, true);
        errorcount = errorcount + 1;
    } else {
        setErrorMesg(3, false);
    }
    
    if($.trim($("#email").val()) == "") {
        setErrorMesg(4, true);
        errorcount = errorcount + 1;
    } else {
        setErrorMesg(4, false);
    }
        
    /**/
    if(errorcount > 0) {
        return false;
    } else {
        return true;
    }
    
}

function doReset() {
	$("#linkform").clearForm();
	$("td.error").removeClass("error");
	return false;
}

function resetContent() {
    doReset();
    
    $("#linkform:visible").fadeOut("fast");
    
    $("#main a:first").parent().fadeIn("fast");
    $("#formresponse").fadeOut("fast").html("");

}

function responseLinkForm(responseText, statusText) {
    var data = responseText;
    if(statusText == 'success' && data.result == 'ok') {
        $("#linkform").fadeOut("fast");
        var html = "" + unescape(data.html);
        $("#formresponse").html(html).fadeIn("fast", function(){
            setTimeout("resetContent()", 1000 * 10);
        });
    }
}

$(document).ready(function(){

    /* Link form */
    
    var formoptions = {
    	/*target: '#formoutput',*/
    	beforeSubmit: validateLinkForm,
    	success: responseLinkForm,
    	url: '/INC/links.json.inc.php',
    	dataType: 'json',
    	clearForm: true
    }
    
    $("#linkform").submit(function(){
        $(this).ajaxSubmit(formoptions); 
        return false;
    });
    
    $("#reset").click(function(){
        resetContent();
        return false;
    });
    
    $("#main a:first").bind("click", function(e){
    
        $(this).parent().fadeOut("fast", function(){
            $("#linkform").fadeIn("fast");
        });
        
        e.preventDefault();
        
    });
    
});