function Abrir_ventana (pagina) {
var opciones="toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=yes, width=508, height=365, top=85, left=140";
window.open(pagina,"",opciones);
}

function oculta(id1, id2) {
  var obj1 = findObj ( id1 );
  var obj2 = findObj ( id2 );
  if (obj1)
    obj1.style.display = 'none';
  if (obj1)
    obj2.style.display = 'block';
}

function cerrar(){
  var obj1 = document.getElementById('mensaje_enviado');
	
	if(obj1.style.display == 'block'){
	  obj1.style.display = 'none';
	}
}

function leermas(id1, id2, id3) {
  var obj1 = findObj ( id1 );
  var obj2 = findObj ( id2 );
	var obj3 = findObj ( id3 );
  if (obj1)
    obj1.style.height = 'auto';
		obj1.style.background = 'gray';
  if (obj1)
    obj2.style.height = 'auto';
	if (obj3)
	  obj3.style.display = 'block';
}

function leermas(id){
 var obj1 = findObj(id);
 if(obj1){
   obj1.style.display='none';
 }
}


function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_validateForm() { //v4.1 modify by caridy
  if (!isNaN(TMS_CURRENT_LANG) || (TMS_CURRENT_LANG == 'eng')) {
    var VALIDATE_EMAIL 			= " must contain an e-mail address.\n";
	var VALIDATE_NUMBER 		= " must contain a number.\n";
	var VALIDATE_QUOTED_NUMBER 	= " must contain a quoted number";
	var VALIDATE_REQUIRED_FIELD = " is required.\n";
	var VALIDATE_ERROR_OCCURRED = "The following error(s) occurred:\n";
  } else { // spanish por defecto
    var VALIDATE_EMAIL 			= " debe contener una direccin de E-mail.\n";
	var VALIDATE_NUMBER 		= " debe contener un nmero.\n";
	var VALIDATE_QUOTED_NUMBER 	= " debe contener un nmero entre ";
	var VALIDATE_REQUIRED_FIELD = " es requerido.\n";
	var VALIDATE_ERROR_OCCURRED = "Han ocurrido los siguientes errores:\n";
  }
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+VALIDATE_EMAIL;
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+VALIDATE_NUMBER;
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+VALIDATE_QUOTED_NUMBER+min+'..'+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+VALIDATE_REQUIRED_FIELD; }
  } if (errors) alert(VALIDATE_ERROR_OCCURRED+errors);
  document.MM_returnValue = (errors == '');
}


function check_email( correo ){
  aux = new String(correo);
  var aux1=aux.indexOf("@",1);
  if (aux1 == -1)
    return true;
  if (aux.indexOf(".",aux1) == -1)
    return true;
  return false; // check passed
}
function check_passport( pass, confirmpass )
{
  if(pass != confirmpass)
    return false;
  else
    return true;
}



function validate_field() { //v4.1 by caridy (sources: validateForm from macromedia MX 2004)
  var i,p,q,nm,test,num,min,max,errors='',args=validate_field.arguments;
  for (i=0; i<(args.length-2); i+=4) {
    test=args[i+2];
    msg=args[i+3];
	val=MM_findObj(args[i]);
    if (val) {
	  nm=val.name;
	  if ((val=val.value)!="") {
        if (test.indexOf('isEmail')!=-1) {
		  p=val.indexOf('@');
          if (p<1 || p==(val.length-1))
		    errors+='- '+msg+'\n';
        }
		else if (test!='R') {
		  num = parseFloat(val);
          if (isNaN(val))
		    errors+='- '+msg+'\n';
          if (test.indexOf('inRange') != -1) {
		    p=test.indexOf(':');
            min=test.substring(8,p); max=test.substring(p+1);
            if (num<min || max<num)
		      errors+='- '+msg+'\n';
          }
		}
	  }
	  else if (test.charAt(0) == 'R')
	    errors+='- '+msg+'\n';
	}
  }
  document.MM_returnValue = (errors == '');
  return errors;
}



/******************************************************************************************************************/
/*     ver imagenes antes del Upload         */
/******************************************************************************************************************/

			function actuar(peso, anchura, altura)	{
				this.peso.value = peso;
				this.ancho.value = anchura;
				this.alto.value = altura;
			}

			function ini()	{
				document.forms.ArticuloFormCreate.actualizar = actuar;
				
				window.frames.ver.location.href = "previsor.php";
				
				document.forms.ArticuloFormCreate.actualizar(0, 0, 0);

			}

			function validar(f)	{
				enviar = /\.(gif|jpg|png)$/i.test(f.archivo.value);
				if (!enviar)	alert("seleccione imagen");
				return enviar;
			}

			function limpiar()	{
				document.forms.ArticuloFormCreate.actualizar(0, 0, 0);
				f = document.getElementById("_archivo0");
				nuevoFile = document.createElement("input");
				nuevoFile.id = f.id;
				nuevoFile.type = "file";
				nuevoFile.name = "_archivo0";
				nuevoFile.value = "";
				nuevoFile.onchange = f.onchange;
				nodoPadre = f.parentNode;
				nodoSiguiente = f.nextSibling;
				nodoPadre.removeChild(f);
				(nodoSiguiente == null) ? nodoPadre.appendChild(nuevoFile):
					nodoPadre.insertBefore(nuevoFile, nodoSiguiente);
			}

			function checkear(f,indx)	{
				function no_prever() {
					alert("El fichero seleccionado no es válido...");
					limpiar();
				}
				function prever() {	 
					actionActual = f.form.action;
					targetActual = f.form.target;
					f.form.action = "previsor.php?index="+indx;
					f.form.target = "ver"+indx;
					f.form.submit();
					f.form.action = actionActual;
					f.form.target = targetActual;
					/*Se regresan los valores iniciales*/
					f.form.action = "index.php?page=altaArticulo";
					f.form.target = "";
				}
				
				(/\.(gif|jpg|png)$/i.test(f.value)) ? prever() : no_prever();
			}

			function datosImagen(peso, ancho, alto, error)	{
				function mostrar_error()	{
					enviar = false;					
					mensaje = "Ha habido un error (error nº " + error + "):";
					if (error % 2 == 1) // tipo incorrecto
						mensaje += "\nel fichero no es válido";
					error = parseInt(error / 2);
					if (error % 2 == 1) // excede en peso
						mensaje += "\nla imagen pesa demasiado (" + peso + ").";
					error = parseInt(error / 2);
					if (error % 2 == 1) // excede en anchura
						mensaje += "\nla imagen excede en anchura (" + ancho + ").";
					error = parseInt(error / 2);
					if (error % 2 == 1) // excede en altura
						mensaje += "\nla imagen excede en altura (" + alto + ").";
					error = parseInt(error / 2);
					alert (mensaje);
					limpiar();
				}
				if (error == 0)
					document.forms.ArticuloFormCreate.actualizar(peso, ancho, alto);
				else
					mostrar_error();
			}
/******************************************************************************************************************/
/*     solo numeros para un input         */
/******************************************************************************************************************/

var nums = "0123456789";
function SoloNum(elemento) {
  nume = document.getElementById(elemento);
	cadena = nume.value;
	numCars = cadena.length;
	ultimo = cadena.charAt(numCars-1);
	if (nums.indexOf(ultimo) < 0) {
    nume.value = cadena.substr(0,numCars-1);
  }
}
/******************************************************************************************************************/
/*     solo ciertas extenciones         */
/******************************************************************************************************************/
extArray = new Array(".jpg", ".bmp", ".gif", ".png", ".jpeg");
function limitar_archivo(form, file0, file1, file2, file3, file4) {
  permitir_archivo = false;
	if (!file0) return;
	if (!file1) return;
	if (!file2) return;
	if (!file3) return;
	if (!file4) return;
	while (file.indexOf("\\") != -1)
	file = file.slice(file.indexOf("\\") + 1);
	ext = file.slice(file.indexOf(".")).toLowerCase();
	for (var i = 0; i < extArray.length; i++) {
	if (extArray[i] == ext) { permitir_archivo = true; break; }
	}
 if (permitir_archivo){

 }
 else{
   alert("Solo archivos con extensión: "
   + (extArray.join(" ")) + "\nPor favor, seleccione otro tipo de archivo"
   + "\ne intentelo nuevamente.");
	 document.MM_returnValue = false;     
 }
}

// menus dinamicos...
/******************************************************************************************************************/
/*     Estados y Ciudades         */
/******************************************************************************************************************/
function init_states_list2( obj_id ) {

   var obj = findObj ( obj_id );

   if (obj && obj.options) {

     reset_list ( obj ); // eliminando todos los elementos...

     var items = estados['states'];

	 insert_into_list (obj, '', 'Todos los Estados');

     for(var n=0; n < items.length; n++)

	   insert_into_list (obj, items[n].s, items[n].s);

	 return true;

   } 

   else

     return false;   

}





function init_states_list( obj_id ) {

   var obj = findObj ( obj_id );

   if (obj && obj.options) {

     reset_list ( obj ); // eliminando todos los elementos...

     var items = estados['states'];

	 insert_into_list (obj, '', 'Seleccionar una Opción');

     for(var n=0; n < items.length; n++)

	   insert_into_list (obj, items[n].s, items[n].s);

	 return true;

   } 

   else

     return false;   

}

function init_cities_list( obj_id, state_id ) {

   var obj = findObj ( obj_id );

   var sta = findObj ( state_id );

   var indx = prepare_indx ( sta.value );

   if (sta && sta.options && sta.value && obj && obj.options && (ciudades[indx])) {

     reset_list ( obj ); // eliminando todos los elementos...

     var items = ciudades[indx];

	 insert_into_list (obj, '', '');

     for(var n=0; n < items.length; n++)

	   insert_into_list (obj, items[n].c, items[n].c);

	 return true;

   } 

   else

     return false;   

}
/*
function prepare_indx ( i ) {

   var indx = new String ( i );

   while (indx.indexOf ( '/' ) > -1)

     indx = indx.replace ( '/', ' ' );

   while (indx.indexOf ( ' ' ) > -1)

     indx = indx.replace ( ' ', '_' );

   return indx;

}
*/
/*********************************************************************************************/
function prepare_indx ( i ) {
     var indx = new String ( i );

     while (indx.indexOf ( '/' ) > -1)
          indx = indx.replace ( '/', ' ' );

     while (indx.indexOf ( ' ' ) > -1)
          indx = indx.replace ( ' ', '_' );
					
		 while (indx.indexOf ( '-' ) > -1)
          indx = indx.replace ( '-', '_' );
					
		 while (indx.indexOf ( '(' ) > -1)
          indx = indx.replace ( '(', '_' );
					
		 while (indx.indexOf ( ')' ) > -1)
          indx = indx.replace ( ')', '_' );
		
		 while (indx.indexOf ( 'ñ' ) > -1)
          indx = indx.replace ( 'ñ', 'n' );
		
		 while (indx.indexOf ( 'Ñ' ) > -1)
          indx = indx.replace ( 'Ñ', 'N' );
		
		 while (indx.indexOf ( 'á' ) > -1)
          indx = indx.replace ( 'á', 'a' );
					
		 while (indx.indexOf ( 'é' ) > -1)
          indx = indx.replace ( 'é', 'e' );
					
		 while (indx.indexOf ( 'í' ) > -1)
          indx = indx.replace ( 'í', 'i' );
					
		 while (indx.indexOf ( 'ó' ) > -1)
          indx = indx.replace ( 'ó', 'o' );
					
		 while (indx.indexOf ( 'ú' ) > -1)
          indx = indx.replace ( 'ú', 'u' );
					
		 while (indx.indexOf ( 'Á' ) > -1)
          indx = indx.replace ( 'Á', 'A' );
					
		 while (indx.indexOf ( 'É' ) > -1)
          indx = indx.replace ( 'É', 'E' );
					
		 while (indx.indexOf ( 'Í' ) > -1)
          indx = indx.replace ( 'Í', 'I' );
					
		 while (indx.indexOf ( 'Ó' ) > -1)
          indx = indx.replace ( 'Ó', 'O' );
					
		 while (indx.indexOf ( 'Ú' ) > -1)
          indx = indx.replace ( 'Ú', 'U' );

     return indx;
}
/*********************************************************************************************/

function findObj(theObj, theDoc)
{
  var p, i, foundObj;
	//alert(theObj);
  if(!theDoc) theDoc = document;
  if( (p = theObj.indexOf("?")) > 0 && parent.frames.length)
  {
    theDoc = parent.frames[theObj.substring(p+1)].document;
    theObj = theObj.substring(0,p);
  }
  if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj];
  for (i=0; !foundObj && i < theDoc.forms.length; i++)
    foundObj = theDoc.forms[i][theObj];
  for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++)
    foundObj = findObj(theObj,theDoc.layers[i].document);
  if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);

  return foundObj;
}
