var img_actual = 0;

function conseguirContenido(cargando, url, valores, elemento) {
    try {
     req = new XMLHttpRequest(); /* e.g. Firefox */
     } catch(e) {
       try {
       req = new ActiveXObject("Msxml2.XMLHTTP");  /* some versions IE */
       } catch (e) {
         try {
         req = new ActiveXObject("Microsoft.XMLHTTP");  /* some versions IE */
         } catch (E) {
          req = false;
         }
       }
     }
     
	 if (cargando.length > 0)
        document.getElementById(cargando).style.display="inline";
        
     errorTxt = "Ocurrio un error";

     req.open("POST",url,true);
     req.onreadystatechange = function() {respuestaContenido(elemento, errorTxt, cargando);};
     req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
     req.send(valores);
}


function respuestaContenido(elemento, errorTxt, cargando) {
   var output = '';
   var codigo_pre = new Array();
   var codigo = new Array();

   if(req.readyState == 4) {
   	  if(req.status == 200) {
         output = req.responseText;
         if (cargando.length > 0)
            document.getElementById(cargando).style.display="none";
		 document.getElementById(elemento).innerHTML = output;
		 try {
			 codigo_pre = req.responseText.split('<!-- JSX');
		 	 if(typeof(codigo_pre[1])!='undefined') codigo = codigo_pre[1].split('-->');
			 if(typeof(codigo[0])!='undefined') eval(codigo[0]);
		 } catch (e) { }
      } else { document.getElementById(elemento).innerHTML = errorTxt+"\n"+output; }
   }
}

function sw_bx_fl(v) {
    
    if (v==1) {
        document.getElementById('bx_1').style.display='block';
        document.getElementById('bx_2').style.display='none';
    } else {
        document.getElementById('bx_2').style.display='block';
        document.getElementById('bx_1').style.display='none';
    }
}

function cargar_flash() {
    
    var url = document.getElementById('flash_url').value;
    var cat = document.getElementById('categoria').value;
    var ncat = document.getElementById('ncat1').value;
    
    if (cat == 'NULL') {
        alert('Debe de seleccionar una categoria');
    } else if (cat == 'nueva') {
        if (ncat.length > 0) {
            conseguirContenido('loading_flash', 'modules/news/ajax.php', 'op=load&url='+url, 'callback_flash');
        } else {
            alert('Indique la categoria nueva');
        }
    } else {
        conseguirContenido('loading_flash', 'modules/news/ajax.php', 'op=load&url='+url, 'callback_flash');
    }
}

function flash_img_sw(m) {
    
    var ar_imgs = document.getElementById('imgs').value.split('@');
    var img_siguiente = (m) ? img_actual + 1 : img_actual - 1;
    
    if (img_siguiente == (ar_imgs.length-1)) {    
        img_siguiente = 0;
    } else if (img_siguiente<0) {
        img_siguiente = (ar_imgs.length-2);
    }
    
    document.getElementById('img').src = ar_imgs[img_siguiente];    
    img_actual = img_siguiente;
}

function select_cats(v, i) {
    if (v == 'nueva') {
        document.getElementById('ncat'+i).style.display='inline';
    } else {
        document.getElementById('ncat'+i).style.display='none';
    }
}

function guardar_flahs_new() {
    var titulo = document.getElementById('titulo').value;
    var descripcion = document.getElementById('descripcion').value;
    var img = document.getElementById('img').src;
    var url = document.getElementById('url').value;
    var charset = document.getElementById('charset').value;
    var sin_img = document.getElementById('sin_img').checked;
    var categoria = document.getElementById('categoria').value;

    if (categoria=='nueva') {
        categoria = document.getElementById('ncat1').value;
    }
    
    if (categoria == 'NULL' || categoria.length < 1) {
        alert('Debe indicar una categoria');
    } else {
        conseguirContenido('loading_flash', 'modules/news/ajax.php', 'op=save&sin_img='+sin_img+'&charset='+charset+'&url='+url+'&titulo='+titulo+'&descripcion='+descripcion+'&img='+img+'&categoria='+categoria, 'callback_flash');
    }
}

function mark_char(value) {
    document.getElementById('charset').value = value;
}
