$(document).ready(function(){
	
	$(':file').livequery(function(){
		$(this).change(function(){
			uploadFile(this, $('.form_ajax').attr('action'));
		});
	});
	
	
	$('.ajax_form_fdel').livequery(function(){
		$(this).click(function(){
			var hidden= '<input type="file" id="'+$(this).prev('input').attr('id')+'" name="'+$(this).prev('input').attr('name')+'" accept="'+$(this).prev('input').attr('accept')+'"/>';
			$(this).parent().html(hidden);
			$(hidden).show();
		});
	});
	
	
	var form= $('.form_ajax').html();
	
	//uploadForm
	$('.form_ajax').livequery(function(){
		$(this).submit(function(){

			var obj= this;
			var status=1;
			var a_url= $(this).attr('action');
			var text= new Array;
		
					
			$(':text', this).each(function() {
				if (!$(this).attr('value') && $(this).attr('required'))
				{
					$(this).parent().prev('td').css('color', '#f00');
					status=0;
				}
				else
          		text.push(getPrev(this)+'_'+encodeURIComponent(this.name)+'='+encodeURIComponent(this.value));
          	});
          	
          	
          	
          	$(':file', this).each(function() {
				if (!$(this).attr('value') && $(this).attr('required'))
				{
					$(this).parent().prev('td').css('color', '#f00');
					status=0;
				}
				else
          		text.push('_'+encodeURIComponent(this.name)+'='+encodeURIComponent(this.value));
          	});
          	
          	$(':hidden', this).each(function() {
				if (!$(this).attr('value') && $(this).attr('required'))
				{
					$(this).parent().prev('td').css('color', '#f00');
					status=0;
				}
				else
				if ($(this).attr('name'))
          		text.push('_'+encodeURIComponent(this.name)+'='+encodeURIComponent(this.value));
          	});
          	
          	
          	          	
          	$('textarea', this).each(function() {
          		if (!$(this).attr('value') && $(this).attr('required'))
				{
					$(this).parent().prev('td').css('color', '#f00');
					status=0;
				}
				else
          		text.push(getPrev(this)+'_'+encodeURIComponent(this.name)+'='+encodeURIComponent(this.value));
          	});
          	
          	
          	
          	$('radio :checked', this).each(function() {
          		text.push(getPrev(this)+'_'+encodeURIComponent(this.name)+'='+encodeURIComponent(this.value));
          	});
          	
          	
          	
          	$(':checkbox', this).each(function() {
          		if (!$(this).attr('checked') && $(this).attr('required'))
				{
					$(this).parent().prev('td').css('color', '#f00');
					status=0;
				}
          		text.push(getPrev(this)+'_'+encodeURIComponent(this.name)+'='+encodeURIComponent(this.value));
          	});
          	
          	
          	
           	$('select :selected', this).each(function() {
          		text.push(getPrev(this)+'_'+encodeURIComponent($(this).parent().attr('name'))+'='+encodeURIComponent(this.value)); 
          	});
          	
          	if (!status) return false;
          	if (!FormValidator(this)) return false;
          	
			text.push('crcIdAdv='+encodeURIComponent(form));
	          	        	
	        $('table',obj).hide();
	        $(obj).prepend('<div class="ajax_form_send_loader"></div>');
	          	        	
			$.ajax({
					type: 'post',
					url: a_url,
				    dataType: 'json',
				    data: text.join('&'),
				    cache: false,
				    timeout: 9000,
				    error: function(t)
				    {	
						$(obj).html(form);
				    },
				    success: function(t)
				    {	
				    	if (!t._success)
				    	{	
				    		$('.ajax_form_send_loader').hide();
				    		$('table',obj).show();
				    		if ($(':image',obj).attr('src'))
				    		{
				    			var rand = Math.floor(Math.random()*1000);
				    			$(':image',obj).attr('src', $(':image',obj).attr('src')+'rand/'+rand)
				    		}
					    	$.each(t, function(i, item){
					    		$('[name='+item+']', obj).parent().prev('td').css('color', '#f00');
					    	});	
				    	}	
				    	else				    	
				    	{
				    		$('.ajax_form_send_loader').hide();
				    		$(obj).html(form);
				    	   	$(obj).parent().prepend('<div class="form-send-success">'+t._success+'</div>');
				    	}
				    }
			});

			return false;
		});
	});
	
});

function FormValidator(form)
{
	var status=1;
	$('.mail', form).each(function(){
		if ($(this).attr('value'))
		{
			var filter=/^[\w0-9\.\-\_]{1,}[@][\w\-ąęśżźćńół]{1,}([.]([\w\-ąęśżźćńół0-9]{1,})){1,5}$/;
			if (!filter.test($(this).attr('value')))
			{
				status=false;
				$(this).parent().prev('td').css('color', '#f00');
			}
		}
	});
	
	$('.digit', form).each(function(){
		if ($(this).attr('value'))
		{
			var filter=/^[0-9]{1,}.$/;
			if (!filter.test($(this).attr('value')))
			{
				status=false;
				$(this).parent().prev('td').css('color', '#f00');
			}
		}
	});
	
	return status;
}

function resetForm(form) 
{
  // iterate over all of the inputs for the form
  // element that was passed in
  $(':input', form).each(function() {
	var type = this.type;
	var tag = this.tagName.toLowerCase(); // normalize case
	// it's ok to reset the value attr of text inputs,
	// password inputs, and textareas
	if (type == 'text' || type == 'password' || tag == 'textarea')
	  this.value = "";
	// checkboxes and radios need to have their checked state cleared
	// but should *not* have their 'value' changed
	else if (type == 'checkbox' || type == 'radio')
	  this.checked = false;
	// select elements need to have their 'selectedIndex' property set to -1
	// (this works for both single and multiple select elements)
	else if (tag == 'select')
	  this.selectedIndex = -1;
  });
};

function getPrev(e)
{
	var e_class= $(e).attr('class');
	if (e_class=='form' | !e_class)
	{
		return '';
	}
	else
	{
		var e_class= e_class.split(' ');
		return e_class[1];
	}
}

function uploadFile(file_upload, a_url)
{
		var file_id= file_upload.id;
		var hidden= '<input type="hidden" value="" id="'+$(file_upload).attr('id')+'" name="'+$(file_upload).attr('name')+'" accept="'+$(file_upload).attr('accept')+'"/>';
		if ($(file_upload).attr('accept'))
		a_url= a_url+'?accept='+$(file_upload).attr('accept');
		
		$.ajaxFileUpload
        (
            {
                url:a_url,
                dataType: 'script',
                secureuri:false,
                fileElementId: file_id,
                success: function (data, status)
                {
                	if (data)
                	{
	                	var parent= $('#'+file_id).parent();
	                	$(parent).html(hidden);
	                	$(parent).append(data);
                	}
	                $('#'+file_id).attr('value', $(parent).text());   
                	           	
                },
                error: function (data, status, e)
                {
                	$('#'+file_id).attr('value', '');
                	$('#'+file_id).show();
                	alert(e);
                }
            }
        )
    
}
