//Javascript

function opf(url_imagen,titulo,ancho,alto){

 	var img = new Image();

 	img.src = url_imagen;
	win=window.open('','','width='+ancho+',height='+alto+',scrollbars=no,resizable=1,toolbar=0');
	win.document.write ('<html>\n');
	win.document.write (' <head>\n');
	win.document.write ('  <title>'+titulo+'</title>\n');
	win.document.write (' </head>\n');
	win.document.write (' <body onclick="return window.close();" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">\n');
	win.document.write ('  <img src="' + url_imagen + '" />\n');
	win.document.write (' </body>\n');
	win.document.write ('</html>\n');
	
}

function quinNavegador(){
var navegador = navigator.appName
if (navegador == "Microsoft Internet Explorer")
	return "explorer";
else
	return "otro"
}

function IsNumeric(strString)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }

function isFloat(num)
{

var a = parseInt(num);
var b = parseFloat(num)

return (a==b);

}

function nuevoAjax(){
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
         	// set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
         }
      } 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) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
	  return http_request;
} 


function updatefoto(direccio){

	// aqui creamos una instancia del objeto ajax 
	var contenedor = document.getElementById('mainfoto'); 
	ajax = nuevoAjax(); 

	ajax.open("GET", "files/updatefotosnovetats.php?direccio=" + direccio,true);
		
	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	ajax.onreadystatechange = function() { 
		if (ajax.readyState==4) {
			contenedor.innerHTML = ajax.responseText;
		}
	}
	ajax.send(null);

}

function cargar_imatge(ruta){

	var contenedor = document.getElementById('foto'); 

	contenedor.innerHTML = "<img src='maketumb.php?foto="+ruta+"&maxalto=200&maxancho=247'>";

}

function novafoto(ruta){

	var contenedor = document.getElementById('mainfoto');

	contenedor.innerHTML = "<img src='maketumb.php?foto="+ruta+"&maxalto=220&maxancho=250'>";

	contenedor = document.getElementById('actual');
	alert(actual.innerHTML);
	contenedor.innerHTML = id;
}

function validarCompra(form,idioma){

	switch(idioma){
	
		case "esp": nom = "Nombre vacio";
					cognom1 = "Primer apellido vacio";
					cognom2 = "Segundo apellido vacio";
					direccio = "Direccion vacia";
					numcasa  = "Numero de casa vacio";
					poble    = "Poblacion vacia";
					cp 		 = "Codigo postal vacio";
					email = "Rellene correctamente el e-mail de contacto";
					telf  = "Telefono vacio";
					provincia = "Provincia vacia";
					break;	
	}

	if(form.nom.value.length==0){ alert(nom);form.nom.focus();return false;}
	if(form.cognom1.value.length==0){ alert(cognom1);form.cognom1.focus();return false;}
	if(form.cognom2.value.length==0){ alert(cognom2);form.cognom2.focus();return false;}
	if(form.direccio.value.length==0){ alert(direccio);form.direccio.focus();return false;}
	if(form.numero.value.length==0){ alert(numcasa);form.numero.focus();return false;}
	if(form.poblacio.value.length==0){ alert(poble);form.poblacio.focus();return false;}
	if(form.cp.value.length==0){ alert(cp);form.cp.focus();return false;}
	if(form.email.value.length==0 || mail(form.email.value)==false){ alert(email);form.email.focus();return false;}
	if(form.telf.value.length==0){ alert(telf);form.telf.focus();return false;}
	if(form.provincia.value.length==0){ alert(provincia);form.provincia.focus();return false;}

}

function mail(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 trim(cadena){
 var str = cadena;
 str = str.replace(/^\s*|\s*$/g,"");
 return str;
}
function validar(form){
	
	if(form.nom.value.length==0){ alert("Falta rellenar el campo nombre");form.nom.focus();return false;}
	if( (form.mail.value.length==0 || mail(form.mail.value)==false) && (form.telf.value.length==0 || isNaN(trim(form.telf.value))) ){ 
		alert("Rellene correctamente un campo de contacto correctamente");
		//form.mail.focus();
		return false;
	}
	if(form.missatge.value.length==0){ alert("Falta rellenar el mensaje");form.missatge.focus();return false;}
	return true;
}


//AJAX
function objetus(file) {
    xmlhttp=false;
    this.AjaxFailedAlert = "Su navegador no soporta las funciónalidades de este sitio y podria experimentarlo de forma diferente a la que fue pensada. Por favor habilite javascript en su navegador para verlo normalmente.\n";
    this.requestFile = file;
    this.encodeURIString = true;
    this.execute = false;
    if (window.XMLHttpRequest) { 
        this.xmlhttp = new XMLHttpRequest();
        if (this.xmlhttp.overrideMimeType) {
            this.xmlhttp.overrideMimeType('text/xml');
        }
    } 
    else if (window.ActiveXObject) { // IE
        try {
            this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        }catch (e) {
            try {
                this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
                this.xmlhttp = null;
            }
        }
        if (!this.xmlhttp && typeof XMLHttpRequest!='undefined') {
            this.xmlhttp = new XMLHttpRequest();
            if (!this.xmlhttp){
                this.failed = true; 
            }
        } 
    }
    return this.xmlhttp ;
}

function recibeid(_pagina,valorget,valorpost,capa){ 
    ajax=objetus(_pagina);
    if(valorpost!=""){
        ajax.open("POST", _pagina+"?"+valorget,true);
    } else {
        ajax.open("GET", _pagina+"?"+valorget,true);
    }
    ajax.onreadystatechange=function() {
        if (ajax.readyState==1){
            document.getElementById(capa).innerHTML = "<img src='../ims/spinner.gif' align='center' class='contingut'>";
        }
        if (ajax.readyState==4) {
            if(ajax.status==200)
            {document.getElementById(capa).innerHTML = ajax.responseText;}
            else if(ajax.status==404)
            {
                capa.innerHTML = "La direccion no existe";
            }
            else
            {
                capa.innerHTML = "Error: ".ajax.status;
            }
        }
    }
    if(valorpost!=""){
        ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        ajax.send(valorpost);
    } else {
        ajax.send(null);
    }
}

