

var tablica= new Array();
var tablicaName= new Array();

	
var offerPrice=0;


$(document).ready(function(){

	
	var offerPriceRound=Math.round(offerPrice*100)/100 
	
			if (offerPriceRound==0){
				$('.buttonBlue2').addClass('buttonBlue2Disabled');
				$('.buttonBlue2').attr('disabled','disabled');
				$('.buttonBlue2').attr('title','Wybierz minimum jeden plik');
			}
			
			if (offerPriceRound>0){
				$('.buttonBlue2').removeClass('buttonBlue2Disabled');
				$('.buttonBlue2').attr('disabled','');
				$('.buttonBlue2').attr('title','');

			}
			
	
$('table th input.checkIN').click(
	function(){
		
		if ($(this).attr('checked')) {
			$(this).attr('checked', true);
			$(this).parent('th').parent('tr').parent('tbody').children('tr').each(function(){
					
				if ($(this).children('td.checkMe').children('input').attr('checked') == false) {
					offerPrice += offerActualPrice;
					var offerPriceRound = Math.round(offerPrice * 100) / 100
					if (offerPriceRound > 0) {
						$('.buttonBlue2').removeClass('buttonBlue2Disabled');
						$('.buttonBlue2').attr('disabled', '');
						$('.buttonBlue2').attr('title', '');
						
					}
					
					$('.checkedFilesPrice').html(offerPriceRound.toFixed(2));
					val=$(this).children('td.checkMe').children('input').val();
					tablica.push(val);
					
					valName=$(this).children('td.checkMe').children('input').attr('title');			
					tablicaName.push(valName);	
				
				}
					
					$(this).children('td.checkMe').children('input').attr('checked', true);
			
			}) //each
		}//checked, true
		
		if (!$(this).attr('checked')) {
			$(this).attr('checked', false);
			$(this).parent('th').parent('tr').parent('tbody').children('tr').each(function(){
				
				
				if ($(this).children('td.checkMe').children('input').attr('checked') == true) {
					offerPrice = offerPrice - offerActualPrice;
					var offerPriceRound = Math.round(offerPrice * 100) / 100
					
					$('.checkedFilesPrice').html(offerPriceRound.toFixed(2));

					if (offerPriceRound == 0) {
						$('.buttonBlue2').addClass('buttonBlue2Disabled');
						$('.buttonBlue2').attr('disabled', 'disabled');
						$('.buttonBlue2').attr('title', 'Wybierz minimum jeden plik');
					}

					val=$(this).children('td.checkMe').children('input').val();
					for (key in tablica)
					{ 
						if (tablica[key]==val)
							tablica.splice(key,1);
					}
					
					valName=$(this).children('td.checkMe').children('input').attr('title');
					for (key2 in tablicaName)
					{
						if (tablicaName[key2]==valName)
							tablicaName.splice(key2,1);
					}
					
				}
				
				$(this).children('td.checkMe').children('input').attr('checked', false);
				
				
			}) //each
		}// checked, false
		
	});

var offerActualPrice=parseFloat($('.offerDataPrice').attr('value'));
//alert (offerActualPrice);

	$('.itemSubject').click(function(){
		
		if ($(this).attr('checked')==true)
		{
			
			val=$(this).val();
			tablica.push(val);
			
			valName=$(this).attr('title');			
			tablicaName.push(valName);
			offerPrice+=offerActualPrice;
			
			var offerPriceRound=Math.round(offerPrice*100)/100 
			
			if (offerPriceRound>0){
				$('.buttonBlue2').removeClass('buttonBlue2Disabled');
				$('.buttonBlue2').attr('disabled','');
				$('.buttonBlue2').attr('title','');

			}
			
			$('.checkedFilesPrice').html(offerPriceRound.toFixed(2));
			found = true;
			$('#offerFiles').find('tr td .itemSubject').each(function() {
				if ($(this).attr('checked') == false) {
					found = false;
				}
			});
			if (found) {
				$('table th input.checkIN').attr('checked', true);
			}
			//tablica[val]=val;
		}
		else
		{
			val=$(this).val();
			
			offerPrice=offerPrice-offerActualPrice;
			var offerPriceRound=Math.round(offerPrice*100)/100 
			
			$('.checkedFilesPrice').html(offerPriceRound.toFixed(2));

			if (offerPriceRound==0){
				$('.buttonBlue2').addClass('buttonBlue2Disabled');
				$('.buttonBlue2').attr('disabled','disabled');
				$('.buttonBlue2').attr('title','Wybierz minimum jeden plik');
			}
			
			for (key in tablica)
			{
				if (tablica[key]==val)
					tablica.splice(key,1);
			}
			
			valName=$(this).attr('title');
			for (key2 in tablicaName)
			{
				if (tablicaName[key2]==valName)
					tablicaName.splice(key2,1);
			}
			
			found = false;
			$('#offerFiles').find('tr td .itemSubject').each(function() {
				if ($(this).attr('checked') == false) {
					found = true;
				}
			});
			if (found) {
				$('table th input.checkIN').attr('checked', false);
			}
		}
	});
});

function insertDataOffers(){
	$('.offerDataSet').val(tablica);
}

function filesList(page,offerId){
	
var offerActualPrice=parseFloat($('.offerDataPrice').attr('value'));
	
	var back_link = jQuery('#back_link').val();
	limit = jQuery('#limit_p').val();
	file_name = jQuery('#file_name').val();
	file_add_date_from = jQuery('#file_add_date_from').val();
	file_add_date_to = jQuery('#file_add_date_to').val();
	file_size_from = jQuery('#file_size_from').val();
	file_size_to = jQuery('#file_size_to').val();
	file_size_type = jQuery('#file_size_type').val();
	
	s_tablica = tablica.join(',');
	$.ajax({
		type: 'POST',
		url: url_variable + 'moje-biteo/oferta/wyszukiwarka-plikow/'+'?page='+page+'&offerId='+offerId,
		dataType: 'json',
		data: {
	   	tablica: s_tablica,
		file_name:file_name,
		file_add_date_from:file_add_date_from,
		file_add_date_to:file_add_date_to,
		file_size_from:file_size_from,
		file_size_to:file_size_to,
		file_size_type:file_size_type
	  	},
		success: function(data){

			jQuery('#ajaxIconsList').html(data[0]);
			jQuery('#found_records').html(data[1]);
			$('.itemSubject').change(function(){
			
			if ($(this).attr('checked')==true)
			{
				
				val=$(this).val();
				tablica.push(val);
				
				valName=$(this).attr('title');
				tablicaName.push(valName);
				
				offerPrice+=offerActualPrice;
				var offerPriceRound=Math.round(offerPrice*100)/100 
				
			if (offerPriceRound>0){
				$('.buttonBlue2').removeClass('buttonBlue2Disabled');
				$('.buttonBlue2').attr('disabled','');
				$('.buttonBlue2').attr('title','');

			}
				
				$('.checkedFilesPrice').html(offerPriceRound.toFixed(2));
				
				//alert (tablica);
				//tablica[val]=val;
			}
			else
			{
				val=$(this).val();
				
				offerPrice=offerPrice-offerActualPrice;
				var offerPriceRound=Math.round(offerPrice*100)/100 
				
				if (offerPriceRound==0){
					$('.buttonBlue2').addClass('buttonBlue2Disabled');
					$('.buttonBlue2').attr('disabled','disabled');
					$('.buttonBlue2').attr('title','Wybierz minimum jeden plik');
				}
				
				$('.checkedFilesPrice').html(offerPriceRound.toFixed(2));
			
				for (key in tablica)
				{
					if (tablica[key]==val)
						tablica.splice(key,1);
				}
				
				valName=$(this).attr('title');
				for (key2 in tablicaName)
				{
					if (tablicaName[key2]==valName)
						tablicaName.splice(key2,1);
				}
			
			}
			});	   
			
	$('table th input.checkIN').click(
	function(){
		
		if ($(this).attr('checked')) {
			$(this).attr('checked', true);
			$(this).parent('th').parent('tr').parent('tbody').children('tr').each(function(){
					
				if ($(this).children('td.checkMe').children('input').attr('checked') == false) {
					offerPrice += offerActualPrice;
					var offerPriceRound = Math.round(offerPrice * 100) / 100
					if (offerPriceRound > 0) {
						$('.buttonBlue2').removeClass('buttonBlue2Disabled');
						$('.buttonBlue2').attr('disabled', '');
						$('.buttonBlue2').attr('title', '');
						
					}
					
					val=$(this).children('td.checkMe').children('input').val();
					tablica.push(val);
					
					valName=$(this).children('td.checkMe').children('input').attr('title');			
					tablicaName.push(valName);
					
					$('.checkedFilesPrice').html(offerPriceRound.toFixed(2));
				}
					
					$(this).children('td.checkMe').children('input').attr('checked', true);
			
			}) //each	
		} //input.checked true
		
		if (!$(this).attr('checked')) {
			$(this).attr('checked', false);
			$(this).parent('th').parent('tr').parent('tbody').children('tr').each(function(){
				
				
				if ($(this).children('td.checkMe').children('input').attr('checked') == true) {
					offerPrice = offerPrice - offerActualPrice;
					var offerPriceRound = Math.round(offerPrice * 100) / 100
					
					$('.checkedFilesPrice').html(offerPriceRound.toFixed(2));
					if (offerPriceRound == 0) {
						$('.buttonBlue2').addClass('buttonBlue2Disabled');
						$('.buttonBlue2').attr('disabled', 'disabled');
						$('.buttonBlue2').attr('title', 'Wybierz minimum jeden plik');
					}
				
					val=$(this).val();
					for (key in tablica)
					{
						if (tablica[key]==val)
							tablica.splice(key,1);
					}
					
					valName=$(this).attr('title');
					for (key2 in tablicaName)
					{
						if (tablicaName[key2]==valName)
							tablicaName.splice(key2,1);
					}
				}
				
				$(this).children('td.checkMe').children('input').attr('checked', false);

			}) //each
		}//input.checked false
		
	});
			
			//	hiddenDesciptionOfferDetail
	$('.infoOfferDetail').click(function(){
		
		if ($(this).next('div').css('display') == 'none') {
			$('.infoOfferDetail').html('&raquo; ukryj szczegóły');
			$(this).next('div').fadeIn('fast');
		}
		else {
			$('.infoOfferDetail').html('&raquo; pokaż szczegóły');
			$(this).next('div').hide();
		}
	}); 
		}
		
 	});
}


function getChecked(list){	
	tablica=list.split(',');
//	alert(tablica);
}
