$(document).ready(function() {

	function readCookie(name){
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0; i < ca.length; i++){
			var c = ca[i];
			while(c.charAt(0) == ' '){
				c = c.substring(1, c.length);
			}
			if(c.indexOf(nameEQ) == 0){
				return c.substring(nameEQ.length, c.length);
			}
		}
		return null;
	}
	
	function setCookie(cname, cvalue, exdays){
		var d = new Date();
		d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
		var expires = "expires="+d.toUTCString();
		document.cookie = cname + "=" + cvalue + "; " + expires + '; path=/';
	}
	
	
	
	
	$('#otvori_skladisni_artikl').click(function(){
		if($('#ProductNameLong').val() == '' || $('#ProductTaxGroupId').val() == ''){
			alert('Za dodavanje skladišnog artikla obavezno je upisati "Naziv na računu" i odabrati "Poreznu grupu"!');
			return false;
		}
		if(confirm("ŽELITE LI OTVORITI SKLADIŠNI ARTIKL : Naziv s računa (bez 0,03 ili 0.03)?")){
			//if($(this).data('source') == 'edit'){
			//}
			//if($(this).data('source') == 'add'){
			//}
			
			var l_item_max = $("select[id^='ProductItem']").length - 2;
			if(l_item_max >= 0){
				if($('#ProductItem' + l_item_max + 'ArticleId').val() != '' || $('#ProductItem' + l_item_max + 'Amount').val() != ''){
					$('#article-add').click();
				}
			}
			
			skladisni_artikl_add($('#ProductTaxGroupId').val(), $('#ProductNameLong').val(), $(this).data('source'));
		}
		return false;
	});
	
	
	
	function str_ends_with(source, ending){
		var result = false;
		
		var l_ending_len = ending.length;
		var l_source_len = source.length;
		
		if(source.substr(l_source_len - l_ending_len) == ending){
			result = true;
		}
		
		return result;
	}
	
	
	
	function skladisni_artikl_add(tax_group_id, name, source){
		
		name = $.trim(name);
		
		var is_003 = str_ends_with(name, '0.03') || str_ends_with(name, '0,03');
		
		var l_is003 = '';
		if(is_003){
			l_is003 = '&is_003=1';
		}
		
		
		var l_data = '&tax_group_id=' + tax_group_id + '&name=' + name + l_is003;
		
		$.ajax({
			url: '<?=$g_path_sl?>app/webroot/iso/res/aj/skladisni_artikl_add.php',
			type: 'POST',
			data: l_data,
			success: function(response){
				var d = $.parseJSON(response);
				
				//console.log(d);
				
				l_name_2 = name.replace(' 0.03', '').replace(' 0,03', '').replace('0.03', '').replace('0,03', '');
				
				if(d['ok'] == -1){
					alert('Pokušali smo otvoriti skladišnu kartica artikla "' + name + '" i/ili "' + l_name_2 + '", ali ona postoji. Molimo provjerite Vaše podatke.');
				
				}else{
				
					var opt;
					
					var obj = document.getElementById('ProductItem{template}ArticleId');
					opt = document.createElement("option");
					opt.value = d.data['id'];
					opt.text = d.data['name'];
					obj.appendChild(opt);
					
					
					var l_elem_cnt = $("select[id^='ProductItem']").length - $("select[id^='ProductItem{template}']").length - 1;
					//if(source == 'edit'){
						//l_elem_cnt++;
					//}
					
					var obj = document.getElementById('ProductItem' + l_elem_cnt + 'ArticleId');
					opt = document.createElement("option");
					opt.value = d.data['id'];
					opt.text = d.data['name'];
					obj.appendChild(opt);
					
					
					$('#ProductItem' + l_elem_cnt + 'ArticleId').val(d.data['id']);
					if(is_003){
						$('#ProductItem' + l_elem_cnt + 'Amount').val('0.03');
					}else{
						$('#ProductItem' + l_elem_cnt + 'Amount').val('1');
					}
					
					alert('Otvoren je skladišni artikl "' + l_name_2 + '" i isti je dodan u sastojke ovog normativa');
				
				}
				
			},
			error: function(d){
				//alert('not ok');
				//alert(d);
			}
		});
		
	}
	
	
	
	
	

	
	$('.normativ-kopiraj').click(function(e){
		
		if($(this).hasClass('no-copy')){
			return false;
		}
		
		var l_copy_cookie = '';
		$('#normativ-articles-list tr td select').each(function(index, element){
			if($(this).attr('id').indexOf('ProductItem') == 0){
				if($(this).val().length > 0){
					l_copy_cookie += $(this).val() + ':';
					$l_val = $(this).closest('tr').find('td input');
					if($l_val.attr('id').indexOf('ProductItem') == 0){
						l_copy_cookie += $l_val.val() + '#';
					}
				}
			}
		});
		
		if(l_copy_cookie){
			setCookie('normativ_copy', l_copy_cookie);
			
			var la_tmp = l_copy_cookie.split('#');
			$('.normativ-kopiraj').html('Kopiraj (' + (la_tmp.length - 1) + ')');
			
		}
		
		return false;
	});
	
	$('.x-sign').click(function(e){
		$('.normativ-zalijepi-layer-data .in').html();
		$('.normativ-zalijepi-layer').hide();
		return false;
	});
	
	
	$('#paste-do').click(function(e){
		
		
		var l_cnt;
		
<?
		// adding
		if($l_edit == 0){
		}
		// editing
		if($l_edit == 1){
		}
?>
		
		$('.normativ-zalijepi-layer-data .in input.normativ-copy-input').each(function(index, element){
			if($(this).is(':checked')){
				
				l_cnt = $("select[id^='ProductItem']").length - 2;
				if(l_cnt >= 0){
					if($('#ProductItem' + l_cnt + 'ArticleId').val() != '' || $('#ProductItem' + l_cnt + 'Amount').val() != ''){
						$('#article-add').click();
						l_cnt++;
					}
				}
				
				$('#ProductItem' + l_cnt + 'ArticleId').val($(this).data('id'));
				$('#ProductItem' + l_cnt + 'Amount').val($(this).data('value'));
				
			}
		});
		
		if(l_cnt > 0){
			$('.normativ-kopiraj.no-copy').removeClass('no-copy');
		}
		
		//$('.normativ-zalijepi.has-data').removeClass('has-data');
		$('.normativ-zalijepi-layer-data .in').html();
		$('.normativ-zalijepi-layer').hide();
		return false;
	});
	
	$('.article-remove').live('click', function(e) {
		$('.normativ-zalijepi.has-data').removeClass('has-data');
		$('#otvori_skladisni_artikl').attr('disabled', 'disabled');
		$('#otvori_skladisni_artikl').attr('style', 'opacity:0.4; cursor:default;');
		return false;
	});
	
	$('.normativ-zalijepi').click(function(e){
		if(g_normativ_copy.length == 0 || !$(this).hasClass('has-data')){
			return false;
		}
		
		$('.normativ-zalijepi-layer').show();
		
		$.ajax({
			url: '<?=$g_path_sl?>app/webroot/iso/res/aj/normativ_cookie.php',
			type: 'POST',
			data: '&data=' + g_normativ_copy,
			success: function(response){
				var d = $.parseJSON(response);
				//console.log(d);
				
				if(d['ok'] == 1){
					
					var l_html = '<table>';
					l_html += '<tr>';
					l_html += '<td><strong>Artikl</strong></td>';
					l_html += '<td><strong>Količina</strong></td>';
					l_html += '<td><strong>Zalijepi</strong></td>';
					l_html += '</tr>';
					
					var la_tmp = g_normativ_copy.split('#');
					for(var i = 0; i < la_tmp.length - 1; i++){
						var la2_tmp = la_tmp[i].split(':');
						
						l_html += '<tr>';
						l_html += '<td>' + d['data'][i] + '</td>';
						l_html += '<td>' + la2_tmp[1] + '</td>';
						l_html += '<td><input type="checkbox" class="normativ-copy-input" data-id="' + la2_tmp[0] + '" data-value="' + la2_tmp[1] + '" value="1" checked></td>';
						l_html += '</tr>';
					}
					
					l_html += '</table>';
					
					$('.normativ-zalijepi-layer-data .in').html(l_html);
				}
				
				
			},
			error: function(d){
				//alert('not ok');
				alert(d);
			}
		});
		
		
		
		return false;
	});
	
	
	
	var g_normativ_copy = readCookie('normativ_copy');
	if(g_normativ_copy == null){
		g_normativ_copy = '';
	}
	//console.log(g_normativ_copy);
	
	if(g_normativ_copy.length > 0){
		var la_tmp = g_normativ_copy.split('#');
		$('.normativ-kopiraj').html('Kopiraj (' + (la_tmp.length - 1) + ')');
	}
	
	//if(g_normativ_copy.length > 0 && !$('#ProductItem0ArticleId').val()){
	if(g_normativ_copy.length > 0){
		$('.normativ-zalijepi').addClass('has-data');
	}
	
});
