var valid = new Array();

window.onload = function(){
	for (var i = 0; i < document.forms.length; i++){
		valid[i] = new Validation(document.forms[i]);
	}
};

function updateAutorA(text, li){
	$('authora').value = li.id;
	
	var frm = null;
	if ($('edit_melody')) frm = 'melody';
	else if ($('edit_song')) frm = 'song';
	
	revealBelowMe($('input_authora'), frm);
}

function updateAutorB(text, li){
	$('authorb').value = li.id;
	
	var frm = null;
	if ($('edit_melody')) frm = 'melody';
	else if ($('edit_song')) frm = 'song';
	
	revealBelowMe($('input_authorb'), frm);
}

function updateTranslator(text, li){
	$('translator').value = li.id;
	
	var frm = null;
	if ($('edit_melody')) frm = 'melody';
	else if ($('edit_song')) frm = 'song';
	
	revealBelowMe($('input_translator'), frm);
}

function transferItems(from, to){
	for (var i = 0; i < from.options.length; i++){
		if (from.options[i].selected){
			if (checkItemExists(to, from.options[i].value)){
				to.options[to.options.length] = new Option(from.options[i].text, from.options[i].value);
			}
		}
	}
}

function transferItemsText(from, to){
	for (var i = 0; i < from.options.length; i++){
		if (from.options[i].selected){
			to.value = from.options[i].value.split('-')[1];
			$('input_' + to.id).value = from.options[i].text;
			break;
		}
	}
}



function checkItemExists(elem, item){
	for (var i = 0; i < elem.options.length; i++){
		if (elem.options[i].value == item) return false;
	}
	
	return true;
}

function removeItem(selItem){
	for (var i = 0; i < selItem.options.length; i++){
		if (selItem.options[i].selected){
			selItem.remove(i);
		}
	}
}

function removeItems(selItem){
	selItem.options.length = 0;
	
}

function selectAllOptionsSongs(){
	for (var i = 0; i < $('melody').options.length; i++)
		$('melody').options[i].selected = true;
}

function selectAllOptionsMelody(){
	for (var i = 0; i < $('book').options.length; i++)
		$('book').options[i].selected = true;
}

function uploadSubmit(uploading, name){
	Element.show(name + '-loading'); 
	$(uploading + '-update-btn').disabled = true;
	$(uploading + '-reset-btn').disabled = true;
}

function checkUploadedFile(frame, elem, name){
	var tmp = frame.contentWindow.location.href.split('/');
	if (tmp[tmp.length - 1] != 'blank.htm'){
		Element.hide(elem + '-loading');
		$(name + '-update-btn').disabled = false;
		$(name + '-reset-btn').disabled = false;
		
		var tmp = frame.contentWindow.document.body.innerHTML;
		var respText = tmp.split(':');
		
		if (respText[0] == 'ERROR'){
			alert(respText[1]);
		}
		else /*if (respText[0] == 'OK')*/{
			$(elem + '-file').update(respText[1]);
			$('edit_' + name).elements[elem].value = respText[1];
		}
	}
}

function deleteFile(what, id){
	var params = {
		asynchronous: true, 
		method: 'post', 
		encoding: 'UTF-8', 
		parameters: '&where=' + what + '&id=' + id, 
		onSuccess: function(){
			$(what + '-loading').hide();
			$(what + '-file').innerHTML = "";
			$('edit_melody').elements[what].value = "";
		}
	};
	$(what + '-loading').show();
	new Ajax.Request('content/deleteFile.php', params);
}

function hideBelowMe(el, fName){
/*
	if (fName = 'melody'){
		switch (el.id){
			case 'input_authora':
				Element.hide('input_authorb');
				
				if ($('input_translator'))
					Element.hide('input_translator');
					
				if ($('assmelodies-hider'))
					Element.hide('assmelodies-hider');
					
				if ($('hidefiles'))
					Element.hide('hidefiles');
				
				break;
			case 'input_authorb':
				if ($('input_translator'))
					Element.hide('input_translator');
					
				if ($('assmelodies-hider'))
					Element.hide('assmelodies-hider');	
					
				if ($('hidefiles'))
					Element.hide('hidefiles');
					
				break;
			case 'input_translator':
				if ($('assmelodies-hider'))
					Element.hide('assmelodies-hider');
				
				break;
			default: break;
		}
	}	
	else{
	
	}
*/
}

function revealBelowMe(el, fName){
/*
	if (fName = 'melody'){
		switch (el.id){
			case 'input_authora':
				Element.show('input_authorb');
				
				if ($('input_translator'))
					Element.show('input_translator');
					
				if ($('assmelodies-hider'))
					Element.show('assmelodies-hider');
					
				if ($('hidefiles'))
					Element.show('hidefiles');
				
				break;
			case 'input_authorb':
				if ($('input_translator'))
					Element.show('input_translator');
					
				if ($('assmelodies-hider'))
					Element.show('assmelodies-hider');	
					
				if ($('hidefiles'))
					Element.show('hidefiles');
					
				break;
			case 'input_translator':
				if ($('assmelodies-hider'))
					Element.show('assmelodies-hider');
				
				break;
			default: break;
		}
	}	
	else{
	
	}
*/
}

function printSong(sid){
	var win = window.open('content/printSong.php?id=' + sid, 'Print', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=400,height=600');
}

function printMelody(mid){
	var win = window.open('content/printMelody.php?id=' + mid, 'Print', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=400,height=600');
}

function removeAuthor(elId){
	$(elId).value = "-1";
	$('input_' + elId).value = "";
}
