var zapisane = new Array()
var myClubs = new Array()
var clubName = new Array()
var i = 0
var other =""
function showComments(id){
	showhidden('navi')
	showhidden('comments')
	advAJAX.get({
		url: "getcomments.php",
		parameters : {
		  "id" : id
		},
	onSuccess : function(obj) { 
			
			document.getElementById('comments').innerHTML= obj.responseText
			},
	onError : function(obj) { alert("Error: " + obj.status); }
	});
	
//	showhidden('mainForm')
}
function addSelected(){
	showhidden('clubsList')
	showhidden('mainForm')
	checkClub();
	
	}
function checkClub(clubb){
	document.getElementById('txt').innerHTML=clubb
	if (clubb){
		zapisane.push(clubb);
		document.getElementById('selectClub').innerHTML = myClubs.length
		for (j=0; j<zapisane.length; j++){
			getClubNameArr(zapisane, 'selectClub');
			}
		document.getElementById('selectClub').innerHTML = "<a href=\"javascript:edit()\">modify selected club(s)</a>";
		clubb=null
		}
	else{
		if (myClubs.length>0){
			document.getElementById('selectClub').innerHTML = clubName
			document.getElementById('selectClub').innerHTML+= "<input type=\"hidden\" name=\"myClubs\" value=\""+myClubs+"\">"
			document.getElementById('selectClub').innerHTML+= "<br><a href=\"javascript:selectClub()\">modify selected club(s)</a>"
			}
		else if (other!=""){
			document.getElementById('selectClub').innerHTML="<input type=\"text\" name=\"otherPlace\" value=\""+other+"\" class=\"inputField\">"
			}
		else {
			document.getElementById('selectClub').innerHTML="<a href=\"javascript:selectClub()\">select club</a>"
		}
	}
}
function addToArray(clubId,nazwa){
	myClubs.push(clubId)
	clubName.push(nazwa);
	i++	
	}
function deleteClub(club){
	i--;
	(i<1) ? hidde('ok'): show('ok')
	myClubs.splice(club,1);
	clubName.splice(club,1);
	document.getElementById('status').innerHTML=""
	for (j=0; j<myClubs.length; j++){
		document.getElementById('status').innerHTML+= clubName[j]+"-<a href='javascript:deleteClub("+j+")'>delete</a> <br>";
		}	
	}
function getClubName(club, divName){
	advAJAX.get({
		url: "getclubname.php",
		parameters : {
		  "id" : club
		},
	onSuccess : function(obj) { 
			document.getElementById(divName).innerHTML+= obj.responseText+" <a href='javascript:deleteClub("+i+")'>delete</a> <br>";
			addToArray(club,obj.responseText);					
			},
	onError : function(obj) { alert("Error: " + obj.status); }
	});
}
function getClubNameArr(club, divName){
	advAJAX.get({
		url: "getclubname.php",
		parameters : {
		  "id" : club
		},
	onSuccess : function(obj) { 
			addToArray(club,obj.responseText);					
			},
	onError : function(obj) { alert("Error: " + obj.status); }
	});
}
function choose(){		
	show('ok')
	
	getClubName(element('kluby').value,'status')
	}
function selectClub(){
	showhidden('clubsList')
	showhidden('mainForm')
	}
function edit(){
	showhidden('clubsList')
	showhidden('mainForm')
	document.getElementById('status').innerHTML=""
	for (j=0; j<myClubs.length; j++){
		document.getElementById('status').innerHTML+= clubName[j]+" <a href='javascript:deleteClub("+j+")'>delete</a> <br>";
		}	
}
function newClub(){
	showhidden('newClubForm')
	showhidden('clubsList')
}
function addOther(){
	showhidden('clubsList')
	showhidden('otherPlaceForm')
}
function zapisane(){	
	newClub()
	getClubList();
	document.getElementById('dodany').innerHTML="club is added, you can choose the club for list"
}
function addClub(){
	clubName = element('clubName').value
	city = element('city').value
	url = element('url').value
	advAJAX.get({
		url: "addclub.php",
		parameters : {
		  "name" : clubName, "city" : city, "url" : url
		},
		onSuccess : function(obj) { 
			
			
			if (obj.responseText=='zapisane'){
				document.getElementById('dodany').innerHTML="club is added, you can choose the club for list"
			} else {
				document.getElementById('dodany').innerHTML="club <b>already exit</b>, you can choose the club for list"
			}
				getClubList();
				newClub()
		
			},
		onError : function(obj) { alert("Error: " + obj.status); }
	});
}
function addOtherPlace(){
	other = element('otherPlace').value
	showhidden('mainForm')
	showhidden('otherPlaceForm')
	checkClub()
}

function getClubList(){
	advAJAX.get({
		url: "getclublist.php",
		onInitialization : function() {
        	document.getElementById('option').innerHTML="loading club's list"
    	},
		onSuccess : function(obj) { document.getElementById('option').innerHTML=obj.responseText;},
		onError : function(obj) { alert("Error: " + obj.status); }
	});
}

function showhidden(obj){
	obj = document.getElementById(obj);
	obj.style.display == 'none' ? obj.style.display = '' : obj.style.display ='none';
} 
function show(obj){
	obj = document.getElementById(obj);
	obj.style.display = ''
} 
function hidde(obj){
	obj = document.getElementById(obj);
	obj.style.display = 'none'
} 
function element(obj){
	obj = document.getElementById(obj);
	return obj;
} 
function validateForm(f) {
	(element('clubName').value.length>0 && element('city').value.length>0) ? addClub() : alert("enter club name and city")
}
function validateOther(f) {
	(element('otherPlace').value.length>0) ? addOtherPlace() : alert("enter name")
}
function validateMain(f) {
	error = false
	var myfile = f.plik	
	var mysplit = myfile.value.split('.');
	var len = mysplit.length;
	var ext = mysplit[len-1];
	ext=ext.toLowerCase();

	if(ext == 'jpg') {
		document.getElementById('fileError').innerHTML=""		
		}
	else {
		document.getElementById('fileError').innerHTML="only jpg files allowed"
		error = true
	}
	if (f.name.value.length>0) {
		document.getElementById('nameError').innerHTML=""
		}
	else {
		document.getElementById('nameError').innerHTML="enter artwork name"
		error = true
	}
	if (f.data.value.length>0) {
		document.getElementById('dataError').innerHTML=""
		}
	else {
		document.getElementById('dataError').innerHTML="enter year"
		error = true
	}
	if (!error) f.submit()
	return error
}

function validateMainUpdate(f) {
	error = false

	if (f.name.value.length>0) {
		document.getElementById('nameError').innerHTML=""
		}
	else {
		document.getElementById('nameError').innerHTML="enter artwork name"
		error = true
	}
	if (f.data.value.length>0) {
		document.getElementById('dataError').innerHTML=""
		}
	else {
		document.getElementById('dataError').innerHTML="enter year"
		error = true
	}
	if (!error) f.submit()
	return error
}

