function Loader_show(id) {
	jQuery('#'+id).hide();
	jQuery('#'+id+'_loader').show();
}

function Loader_hide(id) {
	jQuery('#'+id+'_loader').hide();
	jQuery('#'+id).show();
	//alert('dd');
}

function sendAjaxForm(form, id, type) {
	if (id == undefined || id == '') id = 'contentFrame';
	
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

	var inputs = jQuery('input[type=hidden], input[type=text], input[type=password], input[type=checkbox], select, textarea', form);
	var vars = {};
	for (var i=0 ; i<inputs.length ; i++) {
		vars[inputs.eq(i).attr('name')] = inputs.eq(i).val();
		
		if (inputs.eq(i).attr('name') == 'user[polityka_prywatnosci]' && !inputs.eq(i).attr('checked')) {
			alert('Rejestracja bez zgody na przetwarzanie danych osobowych nie jest możliwa');
			return false;
		}
		else if (inputs.eq(i).val() == '' || (inputs.eq(i).attr('type') == 'checkbox' && inputs.eq(i).attr('checked') != true)) {
			alert('Proszę wypełnić poprawnie wszytkie pola.');
			return false;
		}
		else if (inputs.eq(i).attr('name') == 'user[imie]' && inputs.eq(i).val() == 'Imię') {
			alert('Proszę wypełnić poprawnie wszytkie pola..');
			return false;
		}
		else if (inputs.eq(i).attr('name') == 'user[nazwisko]' && inputs.eq(i).val() == 'Nazwisko') {
			alert('Proszę wypełnić poprawnie wszytkie pola...');
			return false;
		}
		
		else if (inputs.eq(i).attr('name').indexOf('mail') != -1 && !reg.test(inputs.eq(i).val())) {
			alert('Proszę wypełnić poprawnie wszytkie pola....');
			return false;
		}
	}
	//alert(vars[]);
	//return false;
	Loader_show(id);	
	$.post('ajax.php'+jQuery(form).attr('action'), vars, function(data) {
		Loader_hide(id);
		//alert('do '+id)
		jQuery('#'+id).html(data);
		//Loader_hide();
		//return false;
	});
	
	return false;
}

function loadFrame(address, id) {
	if (id == undefined || id == '') id = 'contentFrame';
	
	$('#fixedLoader').hide();
	Loader_show(id);	
	jQuery('#'+id).load('ajax.php'+address, {}, function() {
		Loader_hide(id);
		//alert('laduje do '+id);
		//jQuery('#frame').html(data);
		//Loader_hide();
		//return false;
	});	
}

function loadFileList(id) {
	return; //OFF
	loadFrame('?act=files&id='+id, 'fileList');
}

function categoryEdit(id, ip) {
	loadFrame('?act=category&id='+id+'&ip='+ip, 'fileList');
}

function projectEdit(id) {
	loadFrame('?act=materialsForm&id='+id);
}

function projectDelete(id) {
	loadFrame('?act=materialsForm&del='+id);
}

function fileEdit(id) {
	loadFrame('?act=FTPFiles&id='+id);
}

function mailingPreview(id) {
	var obj=window.open('ajax.php?act=mailingPreview&id='+id, 'podgląd', 'width=700,height=700, status=no, resizable=yes, top=20, left=20, dependent=yes, alwaysRaised=yes, scrollbars=yes');
	obj.focus();
	//loadFrame('?act=mailingPreview&id='+id);
}

function categoryMove(id, ip, direction) {
	$.post('ajax.php'+'?act=category&id='+id+'&move='+direction, {}, function(data) {
		if (parseInt(data) == 2) loadFrame('?act=categories&ip='+ip+'&only=1', 'categoryMenu'); 
	});
}

function fileAdd(select, idC, ip) {
	$.post('ajax.php'+'?act=category&addFile='+jQuery('#'+select).val()+'&idC='+idC, {}, function(data) {
		if (parseInt(data) > 0) {
			loadFrame('?act=category&id='+idC+'&ip='+ip, 'fileList');
			loadFrame('?act=categories&ip='+ip+'&only=1', 'categoryMenu');
		}
	});
}

function userDelete(id) {
	$.post('ajax.php'+'?act=deleteUser&id='+id, {}, function(data) {
		
      loadFrame('?act=users')		

	});
}

function subscriberDelete(id) {
	$.post('ajax.php'+'?act=deleteSubscriber&id='+id, {}, function(data) {
		
      loadFrame('?act=subscribers')		

	});
}

function userToggleForm(id) {
	$.post('ajax.php'+'?act=toggleFormUser&id='+id, {}, function(data) {
		
      loadFrame('?act=users')		

	});
}

function userMailToggleForm(id) {
	$.post('ajax.php'+'?act=toggleMailFormUser&id='+id, {}, function(data) {
		
      loadFrame('?act=subscribers')		

	});
}

function subscriberMailToggleForm(id) {
	$.post('ajax.php'+'?act=toggleMailFormSubscriber&id='+id, {}, function(data) {
		
      loadFrame('?act=subscribers')		

	});
}

function fileRemove(id, idC, ip) {
	$.post('ajax.php'+'?act=category&removeFile='+id, {}, function(data) {
		if (parseInt(data) > 0) {
			loadFrame('?act=category&id='+idC+'&ip='+ip, 'fileList');
			loadFrame('?act=categories&ip='+ip+'&only=1', 'categoryMenu');
		}
	});
}

function mailingSend(id) {
    $('#fixedLoader').show();
	$.post('ajax.php'+'?act=sendMailing&id='+id, {}, function(data) {
		if (data != '1') {
			alert('Nie udało się wysłać całego mailingu, można go wznowić z listy mailingów: '+data);
		} else {
			alert('Mailing wysłany.');
		}
	    $('#fixedLoader').hide();
		loadFrame('?act=mailingList');
	});
}

function fileDown(id, idC, ip) {
	$.post('ajax.php'+'?act=category&id='+id+'&moveFile=down', {}, function(data) {
		if (parseInt(data) == 2) {
			loadFrame('?act=category&id='+idC+'&ip='+ip, 'fileList');
			loadFrame('?act=categories&ip='+ip+'&only=1', 'categoryMenu');
		}
	});
}
function fileUp(id, idC, ip) {
	$.post('ajax.php'+'?act=category&id='+id+'&moveFile=up', {}, function(data) {
		if (parseInt(data) == 2) {
			loadFrame('?act=category&id='+idC+'&ip='+ip, 'fileList');
			loadFrame('?act=categories&ip='+ip+'&only=1', 'categoryMenu');
		}
	});
}
function categoryAdd(id, ip, where) {
	loadFrame('?act=category&id='+id+'&add='+where, 'fileList');
	return;
	
	$.post('ajax.php'+'?act=category&id='+id+'&add='+where, {}, function(data) {
		if (parseInt(data) == 2) loadFrame('?act=categories&ip='+ip+'&only=1', 'categoryMenu'); 
	});
}

function categoryDelete(id) {
	if (confirm('Czy chcesz usunąć kategorię?')) {
		$.post('ajax.php'+'?act=category&delete='+id, {}, function(data) {
			//alert(data);
			if (parseInt(data) != 0) loadFrame('?act=categories&ip='+data); 
		});
	}	
}

function toggleBranch(obj, id) {
	jQuery(obj).next('ul').slideToggle(120, function() {
		if (jQuery(obj).next('ul').css('display') == 'none' || jQuery(obj).next('ul').length == 0) {
			//alert('closed');
			jQuery('img.opened', obj).hide();
			jQuery('img.closed', obj).show();
		} else {
			//alert('opened'+jQuery(obj).next('ul').css('display'));
			jQuery('img.closed', obj).hide();
			jQuery('img.opened', obj).show();
			//loadFileList(id);
		}
	});
	

}

function sendMailing(rand) {
	//var form = $('#mailingForm');
    if ($('[name=mailing\\[adresat\\]]').val() == '0' || tinyMCE.get('elm'+rand).getContent() == '' || $('[name=mailing\\[tytul\\]]').val() == '') {
    	alert('Wprowadz poprawnie wszystkie pola');
     } else {
    	 //alert('dfdf');
    	 $('#mailingForm').submit();
    }
}
function showRequest(formData, jqForm, options) { 
    // formData is an array; here we use $.param to convert it to a string to display it 
    // but the form plugin does this for you automatically when it submits the data 
    var queryString = $.param(formData); 
 
    // jqForm is a jQuery object encapsulating the form element.  To access the 
    // DOM element for the form do this: 
    // var formElement = jqForm[0]; 
 
    $('#fixedLoader').show();
    //alert('About to submit: \n\n' + queryString); 
 
    // here we could return false to prevent the form from being submitted; 
    // returning anything other than false will allow the form submit to continue 
    return true; 
} 
 
// post-submit callback 
function showResponse(responseText, statusText, xhr, $form)  { 
    // for normal html responses, the first argument to the success callback 
    // is the XMLHttpRequest object's responseText property 
 
    // if the ajaxSubmit method was passed an Options Object with the dataType 
    // property set to 'xml' then the first argument to the success callback 
    // is the XMLHttpRequest object's responseXML property 
 
    // if the ajaxSubmit method was passed an Options Object with the dataType 
    // property set to 'json' then the first argument to the success callback 
    // is the json data object returned by the server 
	if (responseText != '1') {
		alert('Nie udało się wysłać całego mailingu, można go wznowić z listy mailingów ('+responseText+')');
	} else {
		alert('Mailing wysłany.');
	}
    $('#fixedLoader').hide();
 	loadFrame('?act=mailingList');
} 
