var $ = jQuery.noConflict();
var tableName;
var contentId;

function addComment()
{
	var u = '/comments/index/addcomment/'; 
	tableName = $('#tableName').text();
	contentId = $('#contentId').text();
	
	$.ajax({
		type: 'get',
		url: u,
		dataType: 'html',
		error: function(){
			
		},
	    success: function(p){
		  	$('#addComment').html(p);
		}
	});
	$('#captcha_val').livequery(function (){
		$(this).val('');
	});
	$('#commentForm #contentId').livequery(function (){
		$(this).val(contentId);
	});
	$('#commentForm #tableName').livequery(function (){
		$(this).val(tableName);
	});
	 
	$('#commentForm').livequery(function (){
		
		var options = { 
        
		//target:        '.comments',   // target element(s) to be updated with server response 
        //beforeSubmit:  showRequest,  // pre-submit callback 
        data: 'contentId='+contentId+'&dataBase='+tableName,
        beforeSubmit: validate,
        dataType:  'html',
       	success: commentSuccess,  // post-submit callback 
       	resetForm: true
       	
 
        // other available options: 
        //url:       url         // override for form's 'action' attribute 
        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
        //clearForm: true        // clear all form fields after successful submit 
        //resetForm: true        // reset the form after successful submit 
 
        // $.ajax options can be used here too, for example: 
        //timeout:   3000 
    }; 
		$(this).ajaxForm(options);
	});

}
function validate(formData, jqForm, options)
{
	

	var form = jqForm[0]; 
	var emailreg = /^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9\-]+)*\.(([0-9]{1,3})|([a-zA-Z]{2,3})|(aero|coop|info|museum|name))/i;
	var urlreg = /^http:\/\/([a-z0-9]+\.)+[a-z]{2,10}$/i;
	var authorreg = /^[ _a-zA-Z0-9\-ąśżź]+[ _a-zA-Z0-9\-ąśżź]{1,30}$/i;
	var i = true;
	$(".inf").html("");
	$("form input").css({ backgroundColor:"#fff"});
    if (!form.author.value)
    {
    	$("form #author").css({ backgroundColor:"#FFF4E8"}).after('<div class="inf"> Musisz wypełnić pole</div>');
    	i = false
    }
    if (!form.author.value.match(authorreg))
    {
    	$("form #author").css({ backgroundColor:"#FFF4E8"}).after('<div class="inf"> Użyto niedozwolonych znaków</div>');
    	i = false
    }
    if (!form.email.value) 
    { 
        $("form #email").css({ backgroundColor:"#FFF4E8"}).after('<div class="inf"> Musisz wypełnić pole</div>');
        i = false;
    }
    else
    {
	    if (!form.email.value.match(emailreg))
	    {
	    	$("form #email").css({ backgroundColor:"#FFF4E8"}).after('<div class="inf"> Został wpisany niepoprawny adres email<div>');
	    	i = false;
	    }
	}
    if (form.url.value)
    { 
	    if (!form.url.value.match(urlreg))
	    {
	    	$("form #url").css({ backgroundColor:"#FFF4E8"}).after('<div class="inf">Niepoprawny adres www</div>');
	    	i = false
	    }
    }
    if (!form.comment_content.value) 
    { 
        $("form #comment_content").css({ backgroundColor:"#FFF4E8"}).before('<div class="inf"> Musisz wypełnić pole</div>');
        i = false;
    } 
    
    if(!i)
    {
    	return false;
    }
}

function commentSuccess(data, status)
{
	$("#addComment").html(data);
	

	$('#captcha_val').livequery(function (){
		$(this).val('');
	});
	$('.cms_form input#contentId').livequery(function (){
		$(this).attr('value', contentId);
	});
	$('.cms_form input#tableName').livequery(function (){
		$(this).attr('value', tableName);
	});
	
	var act = '/comments/index/displaycomments'; 
	$.ajax({
		type: 'post',
		url: act,
		data: 'contentId='+contentId+'&dataBase='+tableName,
		dataType: 'html',
		error: function(){
			alert('Error loading');
		},
	    success: function(t){
	    	
		  	$('div.comments').html(t);
		}
	});
	
	

	
}
$(document).ready(addComment);
