// JavaScript Document

function chequeaNuevoItem(capa,valor) {
	if (!valor=="")	{
		cambiarVisibilidad(capa,'none');
	}
	else {
		cambiarVisibilidad(capa,'inline');
	}
}

function cambiarVisibilidad(id,modo) {
	//Detectamos el tipo de navegador
	if (navigator.userAgent.indexOf("Opera")!=-1 && document.getElementById) type="OP";
	if (document.all) type="IE";
	if (document.layers) type="NN";
	if (!document.all && document.getElementById) type="MO";
	
	if (type=="IE")	{
		eval("document.all." + id + ".style.display='" + modo + "'");
	} else if (type=="NN") {
		eval("document." + id + ".display='" + modo + "'");
	} else if (type=="MO" || type=="OP") {
		eval("document.getElementById('" + id + "').style.display='" + modo + "'");
	} 
}

function chequeaPalabra(elemento) {
	var cuadro = document.getElementById(elemento);
	cuadro.value = cuadro.value.toUpperCase();
	for (i=0;i<cuadro.value.length;i++) {
		if (!((cuadro.value.charCodeAt(i)>=48 && cuadro.value.charCodeAt(i)<=57) || (cuadro.value.charCodeAt(i)>=65 && cuadro.value.charCodeAt(i)<=90))) {
			cuadro.value = cuadro.value.substr(0,i);
		}
	}
	return true;
}

function exportarFormato(frm) {
	exito = true;
	formato = document.getElementById('export');
	if (formato.options[0].selected) {
		alert('Please select a format to export.');
		exito = false;
	}
	if (exito) {
		document.location=formato.options[formato.options.selectedIndex].value;
	}
	return exito;
}


function chequeaAccion(frm) {
		exito = true;
		if (document.forms[frm].accion.options[0].selected) {
			alert('Please select an action.');
			exito = false;
		}
		if (exito) {
			document.forms[frm].submit();
		}
		return exito;
}

function chequeaFormularioItem() {
	exito = true
	if((window.parent.document.forms['frmItem'].title_.options[0].selected) && (window.parent.document.forms['frmItem'].mytitle.value=="")) {
		alert("Please fill in the title field");
		exito = false;
	}
	else if((window.parent.document.forms['frmItem'].label_.options[0].selected) && (window.parent.document.forms['frmItem'].mylabel.value=="")) {
		alert("Please fill in the record company field");
		exito = false;
	}
	else if((window.parent.document.forms['frmItem'].format_.options[0].selected) && (window.parent.document.forms['frmItem'].myformat.value=="")) {
		alert("Please fill in the format field");
		exito = false;
	}
	else if(window.parent.document.forms['frmItem'].country_.options[0].selected) {
		alert("Please fill in the country field");
		exito = false;
	}
	else if(window.parent.document.forms['frmItem'].catnumber_.value=="") {
		alert("Please fill in the catalogue number field");
		exito = false;
	}
	else if(window.parent.document.forms['frmItem'].year_.options[0].selected) {
		alert("Please fill in the year field");
		exito = false;
	}
	/*else if(window.parent.document.forms['frmItem'].matrixinfo_.value=="") {
		alert("Please fill in the matrix info field");
		exito = false;
	}*/
	else if(!window.parent.document.forms['frmItem'].own_[0].checked && !window.parent.document.forms['frmItem'].own_[1].checked) {
		alert("Please check if you have this item");
		exito = false;
	}
	if (exito) {
		if (confirm('KEEP IN MIND:\n\nPlease, always CHECK TWICE information written on each field to prevent:\n\n(1) misleading info.\n(2) doubled items.\n\nIf you cannot prove 100% any given info (real release date, record label...), please state it with a comment at EXTENDED INFO field.')) {
			window.parent.document.forms['frmItem'].submit();
		}
	}
	return exito;
}

function chequeaSubirFotoMOWD() {
	exito = true
	if(document.forms['frmPhoto'].archivo.value=="") {
		alert("Please choose a photo");
		exito = false;
	}
	else if (document.forms['frmPhoto'].archivo.value.toLowerCase().substr(document.forms['frmPhoto'].archivo.value.length-4,4)!='.jpg') {
		alert("Only JPG files are allowed");
		exito = false;
	}
	else if(document.forms['frmPhoto'].posicion.options[0].selected) {
		alert("Please select a category");
		exito = false;
	}
	if (exito) {
		document.forms['frmPhoto'].submit();
		cambiarVisibilidad('photoOptions','none');
		cambiarVisibilidad('micargador','inline');
		document.getElementById('archivo').disabled= true;
	}
	return exito;
}

function mostrarBorradoFoto(idfoto) {
	idfoto = document.getElementById(idfoto);
	for (i=1;i<idfoto.length;i++ )	{
		photo = idfoto.options[i].value;
		photo = document.getElementById(photo);
		photo.className = ('fotoItem');
	}
	photo = idfoto.options[idfoto.options.selectedIndex].value;
	photo = document.getElementById(photo);
	photo.className = ('fotoItemBorrar');
	document.location = "#" + idfoto.options[idfoto.options.selectedIndex].value;
	return true;
}

function confirmaBorradoFoto(id) {
	id = document.forms['frmDeletePhoto'].idphotodeleted.options[document.forms['frmDeletePhoto'].idphotodeleted.options.selectedIndex].value;
	exito = true;
	if(document.forms['frmDeletePhoto'].idphotodeleted.options[0].selected) {
		alert("Please select a photo to delete");
		exito = false;
	}
	if (exito) {
		if (confirm('THE SELECTED PHOTO WILL BE DELETED, ARE YOU SURE?') ) {
			document.forms['frmDeletePhoto'].submit();
		}
	}
	return exito;
}

function chequeaEdicionItem() {
	exito = true
	if ((document.forms['frmItem'].title_.options[0].selected) && (document.forms['frmItem'].mytitle.value=="")) {
		alert("Please fill in the title field");
		exito = false;
	}
	else if((document.forms['frmItem'].label_.options[0].selected) && (document.forms['frmItem'].mylabel.value=="")) {
		alert("Please fill in the record company field");
		exito = false;
	}
	else if(document.forms['frmItem'].format_.options[0].selected) {
		alert("Please fill in the format field");
		exito = false;
	}
	else if(document.forms['frmItem'].country_.options[0].selected) {
		alert("Please fill in the country field");
		exito = false;
	}
	else if(document.forms['frmItem'].catnumber_.value=="") {
		alert("Please fill in the catalogue number field");
		exito = false;
	}
	else if(document.forms['frmItem'].year_.options[0].selected) {
		alert("Please fill in the year field");
		exito = false;
	}
	if (exito) {
		document.forms['frmItem'].submit();
	}
	return exito;
}

function enviarCadena() {
	marco = document.getElementById('busquedaSimultanea');
	title = document.getElementById('title_');
	label = document.getElementById('label_');
	format = document.getElementById('format_');
	country = document.getElementById('country_');
	catnumber = document.getElementById('catnumber_');
	anio = document.getElementById('year_');
	matrixinfo = document.getElementById('matrixinfo_');
	comments = document.getElementById('comments_');

	mytitle = document.getElementById('mytitle');
	mylabel = document.getElementById('mylabel');
	myformat = document.getElementById('myformat');

	f_title = title.options[title.options.selectedIndex].value;
	f_label = label.options[label.options.selectedIndex].value;
	f_format = format.options[format.options.selectedIndex].value;
	f_country = country.options[country.options.selectedIndex].value;

	if (mytitle.value!="") {
		f_title = mytitle.value;
	}
	if (mylabel.value!="") {
		f_label = mylabel.value;
	}
	if (myformat.value!="") {
		f_format = myformat.value;
	}
	
	cadena = 'search.asp?title=' + encodeURIComponent(f_title) + '&label=' + encodeURIComponent(f_label) + '&format=' + encodeURIComponent(f_format) + '&country=' + f_country + '&catnumber=' + catnumber.value + '&year=' + anio.options[anio.options.selectedIndex].value + '&matrixinfo=' + matrixinfo.value + '&comments=' + encodeURIComponent(comments.value);

	marco.src=cadena;
	return true;
}

function aplicaFiltro() {
	title = document.getElementById('title');
	label = document.getElementById('label');
	format = document.getElementById('format');
	country = document.getElementById('country');
	anio = document.getElementById('anio');
	idusr = document.getElementById('idusr');
	order = document.getElementById('order');
	catnumber = document.getElementById('catnumber');
	action = document.getElementById('action');
	page = document.getElementById('page');
	f = document.getElementById('f');
	paginador = document.getElementById('paginador');
	
	destino = '?title=' + title.options[title.options.selectedIndex].value + '&label=' + label.options[label.options.selectedIndex].value + '&format=' + format.options[format.options.selectedIndex].value + '&country=' + country.options[country.options.selectedIndex].value + '&anio=' + anio.options[anio.options.selectedIndex].value + '&idusr=' + idusr.value + '&catnumber=' + catnumber.value + '&order=' + order.value + '&action=' + action.value + "&el=1&page=" + page.value + "&f=" + f.value + "&np=" + paginador.options[paginador.options.selectedIndex].value;
	location.href = destino;
	return true;
}

function refrescaPadre() {
	if (parent.location.href.indexOf('?')>0){
		parent.location = parent.location.href.substr(0,parent.location.href.indexOf('?'));
	}
	else {
		parent.location = parent.location;
	}
	return true;
}

function borrarItem(id,info) {
	if (confirm('THIS ITEM WILL BE COMPLETELY REMOVED FROM THE DATA BASE, ARE YOU SURE?' + info) ) {
		document.location='sys_delitem.asp?id='+id;
	}
}

function restaurarItem(id,info) {
	if (confirm('Are you sure you want to restore this item?' + info) ) {
		document.location='sys_restoreitem.asp?id='+id;
	}
}


function chequeaTitulo(elemento) {
	var cuadro = document.getElementById(elemento);
	cuadro.value = cuadro.value.toUpperCase();
	for (i=0;i<cuadro.value.length;i++) {
		if (!((cuadro.value.charCodeAt(i)>=48 && cuadro.value.charCodeAt(i)<=57) || (cuadro.value.charCodeAt(i)>=65 && cuadro.value.charCodeAt(i)<=90) || cuadro.value.charCodeAt(i)==32 || cuadro.value.charCodeAt(i)==39 || cuadro.value.charCodeAt(i)==47 || cuadro.value.charCodeAt(i)==34)) {
			cuadro.value = cuadro.value.substr(0,i);
		}
	}
	return true;
}
