/* Javascript document */
function changesrc(element, src) {
	document.getElementById(element).src="images/"+src;
}

function borrar(target) {
	document.getElementById("mb_title").innerHTML="Confirmar borrado";
	document.getElementById("mb_text").innerHTML="Seguro que quiere eliminar este registro ?";
	document.getElementById("mb_icon").src="images/trash.png";
	document.getElementById("mb_yes").onclick = function onclick() {
		window.location=target;
	}
	document.getElementById("disabler").style.display="block";
	document.getElementById("mb_confirm").style.display="block";
}

function checkframes() {
	// Checks if frames exists, if not, its forbidden to execute a php-script by itsself
	if(!top.treeframe || !top.basefrm) {
		top.location = 'denegado.php';
	}
}

function toggle_visible(id) {
	$('#' + id).slideToggle('fast', function() {
		// Animation complete.
	});
}

function wopen(href, query, width, height) {
	width = width || 400; 
	height = height || 400;
	query = query || '';
	window.open(href+'?q='+query, 'ventana', 'width='+width+', height='+height+', top=50, left=50, status=no, toolbar=no, location=no, menubar=no, directories=no, resizable=no, scrollbars=auto');
}

function redondear(numero, decimales) {
	var valor = Math.round(numero * Math.pow(10, decimales)) / Math.pow(10, decimales); 
	var cadena = valor.toString();
	if (cadena.indexOf('.') == -1) cadena += '.'; 
	while (cadena.length < cadena.indexOf('.') + (decimales + 1)) cadena += '0';
	return cadena;
}

function markall() {
	$(":checkbox").each(function(){
		if (this.checked==1) {
			this.checked=0; }
		else {
			this.checked=1; }
	});
	return any_checkbox();
}

function any_checkbox() {
	r = true;
	if (document.getElementById("special")!=null) {
		$(":checkbox").each(function(){
			if (this.checked==1) {
				r = false;
			}
		});
	}
	return r;
}

function editschedule(id, date, dtime, text, alarm, atime, color, terminado) {
	partes = dtime.split(":");
	hora=partes[0];
	minu=partes[1];
	partes2 = atime.split(":");
	hora2=partes2[0];
	minu2=partes2[1];
	if (id==0) {
		document.getElementById("btndel").disabled=true;
		color='#FFFFCC'; }
	else {
		document.getElementById("btndel").disabled=false; }
	document.getElementById("agenda_id").value=id;
	document.getElementById("agenda_fecha").value=date;
	document.getElementById("agenda_fhora").value=hora;
	document.getElementById("agenda_fminu").value=minu;
	document.getElementById("agenda_texto").value=text;
	document.getElementById("agenda_texto").style.backgroundColor=color;
	document.getElementById("agenda_aviso").value=alarm;
	document.getElementById("agenda_ahora").value=hora2;
	document.getElementById("agenda_aminu").value=minu2;
	document.getElementById("agenda_color").value=color;
	document.getElementById("terminado_no").checked=true;
	if (terminado==1) document.getElementById("terminado_si").checked=true;	
	document.getElementById("disabler").style.display="block";
	document.getElementById("editschedule").style.display="block";
}

function delschedule(date, view) {
	id = document.getElementById("agenda_id").value;
	window.location='agenda.php?action=delsched&id='+id+'&date='+date+'&view='+view;
}
	
function help(msg) {
	document.getElementById("helptext").innerHTML=msg;
	document.getElementById("disabler").style.display="block";
	document.getElementById("mb_help").style.display="block";
}

function parse_checkbox(el) {
	document.getElementsByName(el.id)[0].value=((el.checked) ? 1 : 0);
}

function handleResponse(data) {
	avisos = data.getElementsByTagName("avisos");
	document.getElementById("noticetext").innerText=avisos[0].firstChild.firstChild.text;
	document.getElementById("disabler").style.display="block";
	document.getElementById("mb_notice").style.display="block";
}

function cargar_modelos(id_marca) {
	$('#modelo').find('option').remove().end().append('<option value="">Seleccionar</option>').val('');
	$.ajax({
		type: "POST",
		data: "id="+id_marca,
		url: "ajax_modelos.php",
		success: function(data) {
			handleResponse(data);
		}
	});
}

function handleResponse(data) {
	modelos = data.getElementsByTagName("model");
	for (i=0; i<modelos.length; i++) {
		$("#modelo").append('<option value="' + modelos[i].childNodes(0).firstChild.text + '">' + modelos[i].childNodes(1).firstChild.text + '</option>');
	}
}

