var fckeditorload = false;


function checkValue(id_multipart,id_group,comm_title,comm_text, button) 
{ // id1_id2_id_3 ... lub *id_1_ ... jesli id zawiera w nazwie _
	//group  minnum_id1_id2_id_3 ... lub *id_1_ ... jesli id zawiera w nazwie _
	var result = true;
		
	var array_id = id_multipart.split("_");
	var i = 0;

	while(array_id[i]){
		if(array_id[i].substr(0,1) == '*'){
			i++;	
			array_id[i] = array_id[i-1].substr(1,array_id[i-1].length-1)+'_'+array_id[i];
		}
		if(document.getElementById(array_id[i])){
			if(document.getElementById(array_id[i]).value=='')
				result = false;
		}	
		else{ 
			comm_text = 'Obiekt o podanym id nie istnieje.';
			result = false;
			
			}
		i++;
	}
	if(result && id_group != ''){
		
		var garray_id = id_group.split("_");
		i = 1;
		var num = 0;
		while(garray_id[i]){
			if(garray_id[i].substr(0,1) == '*'){
				i++;	
				garray_id[i] = garray_id[i-1].substr(1,garray_id[i-1].length-1)+'_'+garray_id[i];
			}
			if(document.getElementById(garray_id[i])){
				if(document.getElementById(garray_id[i]).value!='')
					num++;
			}
			else{ 
			comm_text = 'Obiekt o podanym id nie istnieje.';
			result = false;
			
			}
		i++;
		}
		
		if(garray_id[0]>num) result = false;
		
	}
	 if(!result){
	 	infoBox(comm_title,comm_text);
	 	if(button!=null) button.disabled = false;
	 	}
	 return result;	

}

function checkValueWithFCK(id_multipart,id_group, fckeditor, comm_title,comm_text,button,form,tags) 
{ // id1_id2_id_3 ... lub *id_1_ ... jesli id zawiera w nazwie _
	//group  minnum_id1_id2_id_3 ...	lub *id_1_ ... jesli id zawiera w nazwie _
	//fckeditor = fckid
	if(button!=null) button.disabled = true;
	var result = false;
	result = checkValue(id_multipart,id_group,comm_title,comm_text,button); 
	if(result){
		if(fckeditorload){
			if(FCKeditorAPI.GetInstance(fckeditor)){
		
				var FCKHTML = FCKeditorAPI.GetInstance(fckeditor).GetHTML() ;
				var FCKLen = FCKHTML.length ;
				if(FCKLen==0) result = false; 
			
			} 
			else{
				result = false;
				comm_text = 'Obiekt FCK nie istnieje.';
			}
		}else {
				result = false; 
				comm_text = 'Zaczekaj na załadowanie edytora.';
			}

		
		if(result == true && tags != null) result = checkTags();
		
		 if(!result){
		 	infoBox(comm_title,comm_text);
		 	if(button!=null) button.disabled = false;
		}
	 }
	 
	 
	 if(result && form != null) form.submit();
	 else{
	 	if(form != null) return false;
    	return result;		 	
	 	}
}



function isChecked(id_multipart) 
{ // id1_id2_id_3 ...
	var array_id = id_multipart.split("_");
	var i = 0;
	var result = false;
	while(array_id[i]){
		if(document.getElementById(array_id[i]).checked==true)
			result = true;
		i++;
	}
	 return result;	

}
 
function checkFormat(id_multipart){
	var array_id = id_multipart.split("_");
	var i = 0;
	var filetype;
	var type;
	var result = true;
	while(array_id[i]){
		filetype = document.getElementById(array_id[i]).value;
		if(filetype){
			filetype = filetype.split(".");
			type = filetype[filetype.length-1];
			type = type.toLowerCase();
			if(type!='jpg' && type!='jpeg' && type!='gif' && type!='png'){
				result = false; 
				document.getElementById(array_id[i]+'_label').style.color = 'red';
			} else document.getElementById(array_id[i]+'_label').style.color = '#888888';
		}
		i++;
	}
	if(!result) infoBox('Dodawanie zdjęć','Próbujesz dodać zdjęcie o niewłaściwym formacie.');
	return result;	
}

function checkAddFotoForm(){
	
	
	var pattern = '';
	var i=0;
	while(document.getElementById('zdjecie'+i)){
		if(i>0) pattern += '_';
		pattern +=  'zdjecie'+i;
		i++;
	}
	


	result1 = checkValue('','1_'+pattern,'Dodawanie zdjęć','Musisz dodać przynajmniej jedno zdjęcie.'); 
	result2 = checkFormat(pattern);
	
	/*
	
	result1 = checkValue('','1_zdjecie0_zdjecie1_zdjecie2_zdjecie3_zdjecie4','Dodawanie zdjęć','Musisz dodać przynajmniej jedno zdjęcie.'); 
	result2 = checkFormat('zdjecie0_zdjecie1_zdjecie2_zdjecie3_zdjecie4');
	*/	
	if(result1 == false || result2 == false)
	return false;
	else{
	document.getElementById('loader').style.visibility = 'visible';	
	return true;
		 }
	
}