var tagline = 1;
var aTag = new Array(1,1);
var iFileFieldCounter = 5;

function addFileField(){
	if(iFileFieldCounter < 200){
		if(document.getElementById( 'FileFieldContener'+(iFileFieldCounter-5) ) ){
			var code = '<div class="form_line"><label for="zdjecie' + (iFileFieldCounter) + '" id="zdjecie' + (iFileFieldCounter) + '_label" class="default_label">Zdjęcie ' + (iFileFieldCounter+1) + ':</label> <input type="file" id="zdjecie' + (iFileFieldCounter) + '" name="plik_foto' + (iFileFieldCounter) + '" class="file_wide" size="60" /></div>		<div id="FileFieldContener'+(iFileFieldCounter-4)+'"></div>';
		document.getElementById( 'FileFieldContener'+(iFileFieldCounter-5) ).innerHTML =  document.getElementById( 'FileFieldContener'+(iFileFieldCounter-5) ).innerHTML + code;
			iFileFieldCounter++;
		}
	}else infoBox('Osiągnięto limit', 'Nie możesz dodać więcej niż 200 zdjęć za jednym razem.');
	return false;
}


function checkTags(ext){
	if(ext == null) ext = '';
	var i = 0;
	while(document.getElementById('uTagsID'+ext+i)){
		if(document.getElementById('uTagsID'+ext+i).checked == true) return true
		i++		
	}
	i = 0;
	while(document.getElementById('sTagsNew'+ext+i)){
		if(document.getElementById('sTagsNew'+ext+i).value != '') return true
		i++		
	}
	return false;
}

function addTagsRow( id , nr)
{
	if(nr==''){ 
		var idx = 0;
		var max = tagline;
		var ext = '';
		var a2 = '';
	}
	else{
		var k = nr.split("_");
		if(!aTag[k[0]]) aTag[k[0]] = 1;
		

		var max = aTag[k[0]];
		var ext = 'A';
		var a2 = '[]';
	}		
	var code = '';
	var tagnr = 0;
	for( var il = 0; il < max; il++ )
	{ 
		code = code + '<div class="form_line"><p class="line_description"></p>';

		for( var i = 0; i < 5; i++ )
		{
			realid = tagnr+15;
			if( document.getElementById( 'sTagsNew'+ext+nr+realid ) )
				code = code + '<input type="text" name="sTagsNew'+ext+nr+'[]'+a2+'" class="standard_input" id="sTagsNew'+ext+nr+realid+'" maxlength="20" value="' +document.getElementById( 'sTagsNew'+ext+nr+realid ).value+ '"/>';
			else
				code = code + '<input type="text" name="sTagsNew'+ext+nr+'[]'+a2+'" class="standard_input" id="sTagsNew'+ext+nr+realid+'" maxlength="20"/>';
			
			tagnr++;
		}
		
		code = code + '</div>';
	}

	if(nr=='') tagline++;
	else aTag[k[0]]++;
	
//	alert(code);
	document.getElementById( id ).innerHTML = code;
	
	return false;
}


function checkTagsField(type){
	return true
}


function issetName(id,texttype){
	switch(texttype){
		case 0:
		title = 'Galeria';
		text = 'Galeria musi zawierać nazwę i mieć przypisany przynajmniej jeden tag';
		break;
		case 1:
		title = 'Zdjęcie';
		text = 'Zdjecia musza zawierać nazwę i mieć przypisany przynajmniej jeden tag';
		break;
		case 2:
		title = 'Blog';
		text = 'Wpis musi zawierać nazwę';
		break;
	}

	
	if(document.getElementById(id).value == ''){
		infoBox(title, text);
		return false;
	}else return true;
		
}


function issetTag(id,texttype){
	switch(texttype){
		case 0:
		title = 'Galeria';
		text = 'Galeria musi zawierać nazwę i mieć przypisany przynajmniej jeden tag';
		break;
		case 1:
		title = 'Zdjęcie';
		text = 'Zdjecia musza zawierać nazwę i mieć przypisany przynajmniej jeden tag';
		break;
		case 2:
		title = 'Blog';
		text = 'Wpis musi zawierać nazwę';
		break;
	}

	
	if(!checkTags(id)){
		infoBox(title, text);
		return false;
	}else return true;
		
}


function checkMForm(){
	var result = true;
	var num = document.getElementById('fotoNUM').value;
	
	result = issetName('name',0);
	if(result) result = issetTag(null,0);
	if(result){
		var i = 0;
		while(i<num){
			result = issetName('name'+i,1);
			if(result) result = issetTag(i+'_',0);
			if(!result) break;
		i++;	
		}

	}
	return result;
}

function checkGalleryForm(){
	result = issetName('name',0);
	if(result) result = issetTag(null,0);
	return result;	
}


function UserTags(action, id){
	if(action == 'show'){
		document.getElementById('TagList'+id).style.height = 'auto'; 
		document.getElementById('TagList'+id).style.visibility = 'visible'; 
		document.getElementById('UserTagsLink'+id).innerHTML = '<a href="" onclick="return UserTags(\'hide\',\''+id+'\');">Ukryj moje tagi:</a>';
	}else{
		document.getElementById('TagList'+id).style.height = '20px'; 
		document.getElementById('TagList'+id).style.visibility = 'hidden'; 
		document.getElementById('UserTagsLink'+id).innerHTML = '<a href="" onclick="return UserTags(\'show\',\''+id+'\');">Pokaż moje tagi:</a>';		
	}	
	return false;
}

