function popup(ref,name,w,h,scroll) {
	var URLtoOpen = ref;
	var windowName = name;
	var popW = w, popH = h;
	var scrollB = 'no';
	if(scroll==1){scrollB='yes'};
	w = screen.availWidth;
	h = screen.availHeight;
	var leftPos = (w-popW)/2, topPos = (h-popH)/2;
	window.open(URLtoOpen, windowName,'width=' + popW + ',height=' + popH + ',scrollbars=' + scrollB + ',screenx=' +leftPos +',screeny=' +topPos +',top=' +topPos +',left=' +leftPos, +'location=no, toolbar=no, directories=no, menubar=no, resizable=no, fullscreen=0, status=no, channelmode=0');
}

function verify_edit() {
	if(document.form_edit.t1_codigo.value=="") {document.getElementById("IDt1_codigo").style.background="red"; document.form_edit.t1_codigo.focus(); return;}
	if(document.form_edit.t1_titulo.value=="") {document.getElementById("IDt1_titulo").style.background="red";document.form_edit.t1_titulo.focus(); return;}
	replace_char(document.form_edit.t1_titulo);
	replace_num(document.form_edit.t1_pvp);
	replace_num(document.form_edit.t1_precio1);
	replace_num(document.form_edit.t1_precio2);
	replace_num(document.form_edit.t1_precio_coste);
	if(!isNaN(document.form_edit.t1_precio_coste.value)&&document.form_edit.t1_precio_coste.value.length>0) {
		document.form_edit.t1_precio_coste.value=parseFloat(document.form_edit.t1_precio_coste.value)+(document.form_edit.t1_precio_coste.value*0.16);
		document.form_edit.t1_precio_coste.value=Math.round(parseFloat(document.form_edit.t1_precio_coste.value)*100)/100;
	}
	replace_num(document.form_edit.t1_peso);
	if(document.form_edit.t1_pvp.value==""||isNaN(document.form_edit.t1_pvp.value)){document.form_edit.t1_pvp.value=0;}
	if(document.form_edit.t1_precio1.value==""||isNaN(document.form_edit.t1_precio1.value)){document.form_edit.t1_precio1.value=0;}
	if(document.form_edit.t1_precio2.value==""||isNaN(document.form_edit.t1_precio2.value)){document.form_edit.t1_precio2.value=0;}
	if(document.form_edit.t1_precio_coste.value==""||isNaN(document.form_edit.t1_precio_coste.value)){document.form_edit.t1_precio_coste.value=0;}
	if(document.form_edit.t1_peso.value==""||isNaN(document.form_edit.t1_peso.value)){document.form_edit.t1_peso.value=200;}
	document.form_edit.t1_peso.value =  (String)(parseInt(document.form_edit.t1_peso.value));
	if(document.form_edit.t1_num_pictures.value=='') {document.form_edit.t1_num_pictures.value=0;}
	
	document.form_edit.submit();
}

function replace_intro(field) {
	var salida = field.value;
	salida=salida.replace(/\r\n/g, '<br>');
	salida=salida.replace(/\r/g, '<br>');
	salida=salida.replace(/\n/g, "<br>");
	salida=salida.replace(/  /g, ' &nbsp;');
	return salida;
}

function replace_num(field) {
	var salida = field.value;
	salida=salida.replace(/,/g, ".");
	field.value = salida;
}

function replace_char2(field) {
  	var salida = field.value;
    salida=salida.replace(/  /g, ' &nbsp;');
	salida=salida.replace(/\r\n/g, '<br>');
	salida=salida.replace(/\r/g, '<br>');
	salida=salida.replace(/\n/g, '<br>');
    field.value = salida;
}

function replace_char_special(field) {
  	var salida = field.value;
    salida=salida.replace(/'/g, '&#039;');
    salida=salida.replace(/  /g, ' &nbsp;');
	salida=salida.replace(/\r\n/g, '<br><br>');
	salida=salida.replace(/\r/g, '<br><br>');
	salida=salida.replace(/\n/g, '<br><br>');
	salida=salida.replace(/<br><br><br>/g, '<br><br>');
    field.value = salida;
}

function replace_char(field){
  	var salida = field.value;
    salida=salida.replace(/&/g, '&amp;');
    salida=salida.replace(/"/g, '&quot;');
    salida=salida.replace(/'/g, '&#039;');
    salida=salida.replace(/</g, '&lt;');
    salida=salida.replace(/>/g, '&gt;');
    field.value = salida;
}

function replace_inv(field){
  	var salida = field.value;
    salida=salida.replace(/&#039;/g, '\'');
    field.value = salida;
}

function return_form(e,clase){
	key=(document.all) ? e.keyCode : e.which;
	if (key==13) {
		if(clase=="top") {verify_buscador_top()}
		if(clase=="left") {verify_buscador_left()}
	}
}

function verify_buscador_top() {
	if(document.form_buscador_top.t.value=="") {document.form_buscador_top.t.focus(); return;}
	replace_char(document.form_buscador_top.t);
	
	document.getElementById("personaliza").style.visibility="hidden";
	document.cookie="personaliza=hidden";
	document.form_buscador_top.submit();
}

function verify_buscador_left() {
	if(document.form_buscador_left.t.value=="") {document.form_buscador_left.t.focus(); return;}
	replace_char(document.form_buscador_left.t);
	
	document.getElementById("personaliza").style.visibility="hidden";
	document.cookie="personaliza=hidden";
	document.form_buscador_left.submit();
}


function change_bg(id,color) {
	document.getElementById(id).style.background = color;
}

function show(id,status) {
	if(document.getElementById(id)) {document.getElementById(id).style.visibility = status;}
}

function isNull(val){return(val==null);}


function comprueba_cantidad(numero,seccion,stock){
	if(isNaN(document.form_carrito.car_cantidad[numero].value)){document.form_carrito.car_cantidad[numero].value=1;}
	if(document.form_carrito.car_cantidad[numero].value==''){document.form_carrito.car_cantidad[numero].value=0;}
	if(seccion==4&&document.form_carrito.car_cantidad[numero].value>stock) {document.form_carrito.car_cantidad[numero].value=stock;alert("Stock en Oferta de este producto: " + stock)}
	childUrl("f_carrito.asp" + document.form_carrito.url.value + "&incl=1&car_codigo=" + document.form_carrito.car_codigo[numero].value + "&car_cantidad=" + document.form_carrito.car_cantidad[numero].value);
}

function tecla(oEvento,numero,seccion,stock){
	var iAscii; 
   	if (oEvento.keyCode) 
       	iAscii = oEvento.keyCode; 
   	else if (oEvento.which) 
       	iAscii = oEvento.which; 
   	else 
       	return false; 
   	if (iAscii == 13) comprueba_cantidad(numero,seccion,stock); 
}
	
function childUrl(url) {
	
	// use the dom2 to find 'loadScript' in the head tag
	var head = document.getElementsByTagName('head').item(0);
	var scriptTag = document.getElementById('loadScript');

	// if 'loadScript' already exists - remove it
	if (scriptTag) head.removeChild(scriptTag);

	// create a new element by the name of script
	script = document.createElement('script');

	// set the new scripts properties
	script.src = url
	script.type = 'text/javascript';
	script.id = 'loadScript';

	// move the element script into the head
	head.appendChild(script);
}


function verify_prov() {

	if(document.form_registro.t7_idpais.selectedIndex!=0) {
		document.getElementById('provincia1').style.visibility = "hidden";
		document.getElementById('provincia2').style.visibility = "visible";
	}
	else {
		document.getElementById('provincia2').style.visibility = "hidden";
		document.getElementById('provincia1').style.visibility = "visible";
	}
}


function verify_registro() {

	for (i=1; i<12; i++) {document.getElementById('fr'+i).innerHTML = "";}
	var send = true;

	if(comprueba_email(document.form_registro.t7_usuario.value)==false){document.getElementById('fr1').innerHTML = "* e-mail incorrecto";send=false;}
	if(typeof incl!='undefined') {
		if(send==true&&incl==3){ajaxpage("c_sendmails.asp?op=3","","fr1");}
	}
	
	if(document.form_registro.t7_password.value.length<4){document.getElementById('fr2').innerHTML = "* mínimo 4 caracteres";send=false;}
	if(document.form_registro.password.value==""){document.getElementById('fr3').innerHTML = "* valida la contraseña";send=false;}
	if(document.getElementById('fr3').innerHTML==""&&document.getElementById('fr2').innerHTML==""&&document.form_registro.password.value!=document.form_registro.t7_password.value){document.getElementById('fr2').innerHTML = "* validación incorrecta";document.form_registro.t7_password.value="";document.form_registro.password.value="";send=false;}

	if( document.form_registro.t7_nombre.value==""){document.getElementById('fr4').innerHTML = "* campo requerido";send=false;}
	if(document.form_registro.t7_apellidos.value==""){document.getElementById('fr5').innerHTML = "* campo requerido";send=false;}
	
	if(document.form_registro.t7_idpais.selectedIndex==0) {
		dni = document.form_registro.t7_dni.value;
		if(isNaN(dni.substring(0,1))) {if(cif(dni)==false){document.getElementById('fr6').innerHTML = "* CIF incorrecto";send=false;}}
		else {if(nif(dni)==false){document.getElementById('fr6').innerHTML = "* NIF incorrecto";send=false;}}
	}
	
	if( document.form_registro.t7_provincia.value==""&&document.form_registro.t7_idpais.selectedIndex!=0){document.getElementById('fr7').innerHTML = "* campo requerido";send=false;}
	
	if( document.form_registro.t7_direccion.value==""){document.getElementById('fr8').innerHTML = "* campo requerido";send=false;}
	if( document.form_registro.t7_ciudad.value==""){document.getElementById('fr9').innerHTML = "* campo requerido";send=false;}
	
	if(document.form_registro.t7_idpais.selectedIndex==0) {
		if (isNaN(document.form_registro.t7_cp.value)|| document.form_registro.t7_cp.value.length!=5){document.getElementById('fr10').innerHTML = "* CP incorrecto";send=false;}
	}
	else {
		if (document.form_registro.t7_cp.value==""){document.getElementById('fr10').innerHTML = "* camplo obligatorio";send=false;}
	}
	
	if(document.form_registro.t7_idpais.selectedIndex==0) {
		if (isNaN(document.form_registro.t7_telefono.value)|| document.form_registro.t7_telefono.value.length!=9){document.getElementById('fr11').innerHTML = "* teléfono incorrecto";send=false;}
	}
	else {
		if (document.form_registro.t7_telefono.value==""){document.getElementById('fr11').innerHTML = "* camplo obligatorio";send=false;}
	}
	
	if(document.form_registro.t7_descuento) {
		document.getElementById('fr12').innerHTML = "";
		replace_num(document.form_registro.t7_descuento);
		if(document.form_registro.t7_descuento.value=="") {document.form_registro.t7_descuento.value=0}
		if(isNaN(document.form_registro.t7_descuento.value)) {document.getElementById('fr12').innerHTML = "* descuento inválido";send=false;}
	}

	if(send==true&&document.getElementById("fr1").innerHTML==""){
		document.getElementById('idloading2').style.visibility = "visible";
		replace_char(document.form_registro.t7_password);
		replace_char(document.form_registro.t7_nombre);
		replace_char(document.form_registro.t7_apellidos);
		replace_char(document.form_registro.t7_provincia);
		replace_char(document.form_registro.t7_direccion);
		replace_char(document.form_registro.t7_ciudad);
		document.form_registro.submit();
	}

}


function comprueba_email(texto){

    var mailres = true;            
    var cadena = "abcdefghijklmnñopqrstuvwxyzABCDEFGHIJKLMNÑOPQRSTUVWXYZ1234567890@._-";
    var arroba = texto.indexOf("@",0);

    if ((texto.lastIndexOf("@")) != arroba) arroba = -1;
    
    var punto = texto.lastIndexOf(".");
                
     for (var contador = 0 ; contador < texto.length ; contador++){
        if (cadena.indexOf(texto.substr(contador, 1),0) == -1){
            mailres = false;
            break;
     	}
     }

    if ((arroba > 1) && (arroba + 1 < punto) && (punto + 1 < (texto.length)) && (mailres == true) && (texto.indexOf("..",0) == -1))
     mailres = true;
    else {
     mailres = false;
	}
                
    return mailres;
} 

function cif(cif){
	par = 0
	non = 0
	letras="ABCDEFGHKLMNPQS"
	let=cif.charAt(0)

	if (cif.length!=9) {
		  return false
  	}

	if (letras.indexOf(let.toUpperCase())==-1) {
  		return false
  	}

	for (zz=2;zz<8;zz+=2){par = par+parseInt(cif.charAt(zz))}

	for (zz=1;zz<9;zz+=2){
		nn = 2*parseInt(cif.charAt(zz))
  		if (nn > 9) nn = 1+(nn-10)
  		non = non+nn
	}

	parcial = par + non
	control = (10 - ( parcial % 10))
	if (control==10) control=0

	if (control!=cif.charAt(8)) {
  		return false
	}
	return true
}


function nif(abc) {
	dni=abc.substring(0,abc.length-1)
	let=abc.charAt(abc.length-1)
	if (!isNaN(let)) {
  		return false
	}
	else {
  		cadena="TRWAGMYFPDXBNJZSQVHLCKET"
  		posicion = dni % 23
		letra = cadena.substring(posicion,posicion+1)
	  	if (letra!=let.toUpperCase()) {
    		return false
   		}
 	}
	return true
}
 
 
function textId(id,url) {
	document.getElementById(id).innerHTML = "Usuario: <input type='text' name='" + id + "' size='30'> <img onclick='if(document.form_login."+id+".value!=\"\"){ajaxpage(\"" + url + "\",\"\",\"" + id + "\");}' src='picture_library/validar.gif' title='enviar' border='0' style='cursor: pointer;'>";
}

function ajaxpage(url,form,id){
	var suscripcion = "";
	if(document.form_login){
		if(document.form_login.login_cookie.checked==true){suscripcion=1}
	}
	if(id=="idpass") {url = url + "&usuario=" + document.form_login.idpass.value;}
	if(id=="idvalida") {url = url + "&usuario=" + document.form_login.idvalida.value;}
	if(id=="fr1"){url = url + "&usuario=" + document.form_registro.t7_usuario.value;}
	if(form=="form_login"){url = url + "&usuario=" + document.form_login.login_usu.value + "&password=" + document.form_login.login_pass.value + "&cookie=" + suscripcion;}
	http_request = false;
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
    	http_request = new XMLHttpRequest();
    } 
	else if (window.ActiveXObject) { // IE
   		try {http_request = new ActiveXObject("Msxml2.XMLHTTP");}
		catch (e) {
        	try {http_request = new ActiveXObject("Microsoft.XMLHTTP");}
			catch (e) {}
        }
    }
    if (!http_request) {return false;}
		
	if(id=="fr1"){
		if (window.ActiveXObject) { 
			http_request.open('GET', url, false); // false espera respuesta del servidor IExplorer
			http_request.onreadystatechange = function(){alertContents(form,id)};
			http_request.send(null); // información que se quiere pasar si se utilizar "POST" en lugar de "GET". Ejemplo "name=value&anothername=othervalue&so=on"
		}
		else { 
			http_request.open('GET', url, false); // da error si espera (llamada síncrona) en Mozilla
			// la solución es eliminar onreadystatechange, llamar al send y ejecutar el código
			http_request.send(null);
			document.getElementById(id).innerHTML = http_request.responseText;
			if (http_request.responseText!="") {
				document.form_registro.t7_password.value = "";
				document.form_registro.password.value = "";
			}
		}
	}
	else {
	    http_request.open('GET', url, true); // TRUE no se espera respuesta del servidor
		http_request.onreadystatechange = function(){alertContents(form,id)};
		http_request.send(null); // información que se quiere pasar si se utilizar "POST" en lugar de "GET". Ejemplo "name=value&anothername=othervalue&so=on"
	}
}

function alertContents(form,id){
        if (http_request.readyState == 4) { // 0 (no inicializada) 1 (leyendo) 2 (leido) 3 (interactiva) 4 (completo) 
            if (http_request.status == 200) { // 200 Ok 
				if(id!="") {
					document.getElementById(id).innerHTML = http_request.responseText;
					if (id=="fr1"&&http_request.responseText!="") {
						document.form_registro.t7_password.value = "";
						document.form_registro.password.value = "";
					}
				}
				if(form=="form_login") {
					document.getElementById("idlogin_usu").innerHTML = "";
					document.getElementById("idlogin_pass").innerHTML = "";
					if(http_request.responseText=="usuario incorrecto") {document.getElementById("idlogin_usu").innerHTML = http_request.responseText;}
					if(http_request.responseText=="usuario sin validadar") {document.getElementById("idlogin_usu").innerHTML = http_request.responseText;}
					if(http_request.responseText=="contrase&ntilde;a incorrecta") {document.getElementById("idlogin_pass").innerHTML = http_request.responseText;}
					if(http_request.responseText=="ok") {document.form_login.submit();}
				}
				if(id=="idtucuenta") {document.getElementById("idtucuenta").innerHTML = http_request.responseText;}
			}
		}
}

function moveB(id,y) {
	if (navigator.appName=="Microsoft Internet Explorer") {document.getElementById(id).style.top =  y + document.body.scrollTop;}
	else if(window.innerHeight+document.body.scrollTop<=document.body.scrollHeight) {
			document.getElementById(id).style.top =  y + document.body.scrollTop;
	}
}

function loading() {
	if(document.getElementById("IDloading")){document.getElementById("IDloading").style.visibility="hidden";}
	if(document.getElementById("results")){document.getElementById("results").style.visibility="visible";}
	if(document.getElementById("ficha")){document.getElementById("ficha").style.visibility="visible";}
	if(document.getElementById("paginacion")){document.getElementById("paginacion").style.visibility="visible";}
}

function setCookie(name,value,expires, options) {
	if (options===undefined) { options = {}; }
	if ( expires ) {
		var expires_date = new Date();
		expires_date.setDate(expires_date.getDate() + expires)
	}
	document.cookie = name+'='+escape( value ) +
	( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) +
	( ( options.path ) ? ';path=' + options.path : '' ) +
	( ( options.domain ) ? ';domain=' + options.domain : '' ) +
	( ( options.secure ) ? ';secure' : '' );
}

function getCookie(name){
  var cname = name + "=";               
  var dc = document.cookie;             
  if (dc.length > 0) {              
    begin = dc.indexOf(cname);       
    if (begin != -1) {           
      begin += cname.length;       
      end = dc.indexOf(";", begin);
      if (end == -1) end = dc.length;
        return unescape(dc.substring(begin, end));
    } 
  }
  return null;
}

function setPortNum(numero,valor) {
	childUrl("f_menu.asp?numero="+numero+"&valor="+valor+"&cookie=portNum");
}

function tPortNum(oEvento,numero,valor){
	var iAscii; 
   	if (oEvento.keyCode) 
       	iAscii = oEvento.keyCode; 
   	else if (oEvento.which) 
       	iAscii = oEvento.which; 
   	else 
       	return false; 
   	if (iAscii == 13) setPortNum(numero,valor); 
}

function setPortName(numero,valor) {
	childUrl("f_menu.asp?numero="+numero+"&valor="+valor+"&cookie=portName");
}

function unFocusA() {
    anclas=document.getElementsByTagName("a").length;
    for (i=0;i<anclas;i++)
    document.getElementsByTagName("a").item(i).onfocus=new Function("if(this.blur)this.blur()")
}

function p_edit_control_stock(idpedido) {
	if(!isNaN(document.form_edit.cantidad.value)&&document.form_edit.cantidad.value!=''&&document.form_edit.cantidad.value!=0){
		if(document.form_edit.cantidad.value<0) {
			//salida de stock
			if(window.name=="TB_iframeContent") {window.location = "p_pedidos_relacionados.asp?idpedido="+idpedido+"&codigo="+document.form_edit.t1_codigo.value+"&stock="+document.form_edit.t1_stock.value+"&cantidad="+parseInt(document.form_edit.cantidad.value)}
			else {childUrl("c_pedidos_relacionados.asp?idpedido="+idpedido+"&codigo="+document.form_edit.t1_codigo.value+"&stock="+document.form_edit.t1_stock.value+"&cantidad="+parseInt(document.form_edit.cantidad.value));var_refrescar2=1;init2();}
		}
		else {
			//entrada de stock
			replace_num(document.form_edit.t1_precio_coste);
			if(document.form_edit.t1_precio_coste.value==""||isNaN(document.form_edit.t1_precio_coste.value)){document.form_edit.t1_precio_coste.value=0;}
			childUrl("c_edit.asp" + "?accion=2&t1_codigo=" + document.form_edit.t1_codigo.value + "&t1_stock=" + document.form_edit.cantidad.value + "&t1_precio_coste=" + document.form_edit.t1_precio_coste.value);
		}
	}
	document.form_edit.cantidad.value = "";
}

function delAllCookies() {
	alert(document.cookie);
	if (document.cookie != "") {
		la_cookie = document.cookie.split("; ");
		fecha_fin = new Date;
		fecha_fin.setDate(fecha_fin.getDate()-1);
		for (i=0; i<la_cookie.length; i++) {
			mi_cookie = la_cookie[i].split("=")[0];
			document.cookie = mi_cookie + "=;expires=" + fecha_fin.toUTCString();
		}
	}
	alert(document.cookie);
}

function log_out(confirmation_message) {
	var ht = document.getElementsByTagName("body")[0];
	if (navigator.appName=="Microsoft Internet Explorer") {
		ht.style.filter = "progid:DXImageTransform.Microsoft.BasicImage(grayscale=1)";
		//ht.style.filter = "progid:DXImageTransform.Microsoft.Blur(PixelRadius='3', MakeShadow='false', ShadowOpacity='0.25')";
	}
	if (confirm(confirmation_message)) {
		return true;
	}
	else {
		ht.style.filter = "";
		return false;
	}
}

function show_picture(code,num) {
	document.getElementById('ficha_imagen').innerHTML = '<img src="c_picture.asp?code=' + code + '&num=' + num + '" width=600 style=\"border: 1px solid black\">';
}