// JavaScript Document

// Navegador
var ie = document.all ? true : false;
var ns = document.layers ? true : false;

if (!ie) document.captureEvents(Event.MOUSEMOVE)	// capturamos los eventos de raton
document.onmousemove=raton_xy;

var raton_x = 0
var raton_y = 0

function raton_xy(e) {
	if(ie){
		raton_x = event.clientX + document.body.scrollLeft; 
		raton_y = event.clientY + document.body.scrollTop; 
	}else{ 
		raton_x = e.pageX; 
		raton_y = e.pageY;
	}

	if (raton_x < 0){raton_x = 0;}
	if (raton_y < 0){raton_y = 0;}  
	
	return true;
}
//·································································································
// Popup
function abre_pop(cual, nombre, ancho, alto, ajustable, scroll) {
	var ventana;
	if (ie) {
		window.open(cual, nombre, 'width='+ancho+', height='+alto+', resizable='+ajustable+', scrollbars='+scroll+', left=0, top=0, location=no, toolbar=no, directories=no,  menubar=no, status=no, copyhistory=no');
	} else if (ns) {
		window.open(cual, nombre, 'width='+ancho+', height='+alto+', resizable='+ajustable+', scrollbars='+scroll+', left=0, top=0, location=no, toolbar=no, directories=no, menubar=no, status=no, copyhistory=no');		
	} else {
		window.open(cual, nombre, 'width='+ancho+', height='+alto+', resizable='+ajustable+', scrollbars='+scroll+', left=0, top=0, location=no, toolbar=no, directories=no, menubar=no, status=no, copyhistory=no');		
	}
}
//·································································································
function ver_paginas(tipo){	
	switch(tipo){
	case 0:
		document.getElementById("pag_capa").style.display="none";
		break;
	case 1:
		document.getElementById("pag_capa").style.display="block";	
		document.getElementById("pag_capa").style.left=raton_x-10;
		document.getElementById("pag_capa").style.top=raton_y-50;
		break;
	}
}
//·································································································
function valida(frm){
	var i, campo, valor, seguir, seleccionado;
	seguir=true;
	chk_sel=-1;
	
	for (i=0; i<frm.elements.length; i++) {
		campo=frm.elements[i].name.split("|");
		valor=frm.elements[i].value;
		if(campo[0]=="chk") {
			switch (campo[1]) {
			case "str":
				if (campo[2]=="true" && valor=="" && seguir==true){
					alert("O campo '"+campo[3]+"' non pode estar vacio");
					frm.elements[i].focus();
					seguir=false;
				}
				break;
			case "date":
				if (((campo[2]=="false" && valor!="" && valida_fecha(valor)!="true") || (campo[2]=="true" && (valor=="" || valida_fecha(valor)!="true")))&& seguir==true) {
					alert("O campo '"+campo[3]+"' debe ser de tipo data: DD/MM/AAAA");
					frm.elements[i].focus();
					seguir=false;
				}
				break;
			case "eml":
				if(((valor.indexOf("@")==-1 && valor!="") || (campo[2]=="true" && valor=="")) && seguir==true){
					alert("O campo '"+campo[3]+"' non e correcto");
					frm.elements[i].focus();
					seguir=false;
				}
				break;
			case "int":
				if(((isNaN(valor)==true && valor!="") || (campo[2]=="true" && valor=="")) && seguir==true){
					alert("O campo '"+campo[3]+"' non pode estar vacio e debe ser un número");
					frm.elements[i].focus();
					seguir=false;
				}
				break;
			case "sel":
				if (campo[2]=="true"){
					if(chk_sel==-1){chk_sel=0;}
					if(frm.elements[i].checked==true){
						chk_sel+=1;
					}
				}
				break;
			}
		}
	}
	
	if(chk_sel==0 && seguir==true){
		alert("Debe seleccionar polo menos un artigo para o seu pedido");
		seguir=false;
	}
	
	return seguir;
}
//·································································································
function valida_fecha(dato) {
	var fecha_split;
	var i;
	var aux;
	var tdia, tmes, tano;
	
	aux="true";
	fecha_split = dato.split('/');
	tdia = parseInt(fecha_split[0],10);
	tmes = parseInt(fecha_split[1],10);
	tano = parseInt(fecha_split[2],10);
	// Comprobamos que sean 3 elementos
	if(fecha_split.length != 3){
		aux="Debe tener DD/MM/AAAA";
	}else{
		// Comprobamos que no sean ceros
		for (i=0; i<fecha_split.length; i++) {
			if (parseInt(fecha_split[i],10) == 0) aux="No existe la fecha: 0";
		}
		// Comprobamos que el AÑO tenga 4 dígitos
		if(fecha_split[2].length != 4) aux="El año debe tener 4 dígitos: AAAA";
		// Comprobamos que sea dia/mes/año: 28/02/1997
		var f = new Date(tano,tmes-1,tdia);
		if(f.getDate() != tdia) aux="El día no es correcto";
		if(f.getMonth() != (tmes-1)) aux="El mes no es correcto";
		if(f.getFullYear() != tano) aux="El año no es correcto";
	}
	return aux;
}
//·································································································
var scroll;
var activo;
activo=false;
function sube(){
	obj_contenedor=document.getElementById("mascara");
	obj_contenido=document.getElementById("contenido");
	if (parseInt(obj_contenido.style.top)+obj_contenido.offsetHeight>parseInt(obj_contenedor.offsetHeight)){
		obj_contenido.style.top=parseInt(obj_contenido.style.top)-5;
		scroll=setTimeout("sube()",50);
		activo=true;
	}
}
//·································································································
function baja(){
	obj_contenido=document.getElementById("contenido");
	if (parseInt(obj_contenido.style.top)<=0){
		obj_contenido.style.top=parseInt(obj_contenido.style.top)+5;
		scroll=setTimeout("baja()",50);
		activo=true;
	}
}
//·································································································
function para(){
	if(activo==true){
		clearTimeout(scroll);
	}
}

//·································································································

function calcula_importe(){
	var cantidad_1 = parseInt($("cantidad_1").value, 10);
	var precio_1 = parseInt($("precio_1").value, 10);
	var cantidad_2 = parseInt($("cantidad_2").value, 10);
	var precio_2 = parseInt($("precio_2").value, 10);

	$("importe").value = (cantidad_1 * precio_1) + (cantidad_2 * precio_2);
}

//·································································································

function validaCantidad(){
	if($("importe").value == ""){
		alert("O pedido está baleiro");
		return false;
	}else{
		return true;
	}
}
