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 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';
 }
}
/******************************************************************************************************************/
/*     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;
}
