// JavaScript Document
function nuevoAjax()
{
	/* Crea el objeto AJAX. Esta funcion es generica para cualquier utilidad de este tipo, por
	lo que se puede copiar tal como esta aqui */
	var xmlhttp=false;
	try
	{
		// Creacion del objeto AJAX para navegadores no IE
		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			// Creacion del objet AJAX para IE
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(E)
		{
			if (!xmlhttp && typeof XMLHttpRequest!='undefined') xmlhttp=new XMLHttpRequest();
		}
	}
	return xmlhttp;
}

String.prototype.trim = function()
{
  return this.replace(/^\s+/,'').replace(/\s+$/,'');
}

function parseScript(_source) {
	var source = _source;
	var scripts = new Array();
	
	// Strip out tags
	while(source.indexOf("<script") > -1 || source.indexOf("</script") > -1) {
		var s = source.indexOf("<script");
		var s_e = source.indexOf(">", s);
		var e = source.indexOf("</script", s);
		var e_e = source.indexOf(">", e);
		
		// Add to scripts array
		scripts.push(source.substring(s_e+1, e));
		// Strip from source
		source = source.substring(0, s) + source.substring(e_e+1);
	}
	
	// Loop through every script collected and eval it
	for(var i=0; i<scripts.length; i++) {
		try {
			//alert(scripts[i]);
			eval(scripts[i]);
		}
		catch(ex) {
			// do what you want here when a script fails
			alert("Error: "+scripts[i]);
		}
	}
	
	// Return the cleaned source
	return source;
}

var loadCSS = function(file) {
	var link = document.createElement('link');
	link.href = file;
	link.rel = 'stylesheet';
	link.type = 'text/css';
	document.getElementsByTagName('head')[0].appendChild(link);
};

var loadJS = function(file) {
	var script = document.createElement('script');
	script.src = file;
	script.type = 'text/javascript';
	document.getElementsByTagName('head')[0].appendChild(script);
};

function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}




function ver_video(op)
{
	if(op == -1)
		var marca = document.getElementById('marca').value;
	else
		var marca = op;
		
	var divResultado = document.getElementById('images');
	var ajax = nuevoAjax();
	ajax.open("POST", "index4.php",true);
	ajax.onreadystatechange=function()
	{
		if (ajax.readyState==4)
		{
		
			 conf_reflection_p=0.2;
			 conf_focus=4;
			 conf_slider_width=14;
			 conf_images_cursor='pointer';
			 conf_slider_cursor='e-resize';
			 conf_imageflow='imageflow';
			 conf_loading='loading';
			 conf_images='images';
			 conf_captions='captions';
			 conf_scrollbar='scrollbar';
			 conf_slider='slider';
			 conf_slideshow='slideshow';
			 conf_youtube='youtubepopup';
			 caption_id=0;
			 new_caption_id=0;
			 current=0;
			 target=0;
			 mem_target=0;
			 timer=0;
			 array_images=[];
			 new_slider_pos=0;
			 dragging=false;
			 dragobject=null;
			 dragx=0;
			 posx=0;
			 new_posx=0;
			 xstep=150;
			 slide=null;
			 sizeAlgo=2;
			 glidetopicture=0;
			 slideshowtime=3000;
			 slideshowbutton = true;
			 slideshowauto=false;
			 videowidth='429';
			 videoheight='265';
			 videotop  = '-218px';
			 videoleft = '-128px';
			 output="video";
			 
			 //alert(ajax.responseText);
			 divResultado.innerHTML = ajax.responseText;
			 if(ajax.responseText.trim() != "")
			 {
				 document.getElementById('slideshow').style.display = 'block';
				 refresh(true);
				 /*slideshow(1);
				 stopslideshow();*/
				 handle(1);			
				 //loadCSS();
			 }
			 else
			 {
				 document.getElementById('captions').innerHTML = "Lo sentimos, no hay resultados.";
				 document.getElementById('slideshow').style.display = 'none';
			 }
		}
	}

	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	if(marca == -1)
		ajax.send(null);
	else
		ajax.send('marca=' + marca);
}

function muestra_info(id_video)
{
	var divResultado = document.getElementById('txtuseccionvideo');
	
	if(IsNumeric(id_video))
	{
		var ajax = nuevoAjax();
		ajax.open("POST", "txtuseccionvideo.php",true);
		ajax.onreadystatechange=function()
		{
			if (ajax.readyState==4)
			{
				divResultado.innerHTML = ajax.responseText;
			}
		}
		ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		ajax.send('id_video=' + id_video);	
	}
	else
	{
		divResultado.innerHTML = "";
	}
}

function muestra_desc(id_video)
{
	var divResultado = document.getElementById('comentarios');
	var divResultado2 = document.getElementById('escribircomentarios');
	var ajax = nuevoAjax();
	var ajax2 = nuevoAjax();
	if(id_video == -1)
		ajax.open("POST", "comentarios_default.php",true);
	else
		ajax.open("POST", "comentarios.php",true);
	ajax2.open("POST", "escribir_comentarios.php",true);
	ajax.onreadystatechange=function()
	{
		if (ajax.readyState==4)
		{
			divResultado.innerHTML = ajax.responseText;
			settings = {
			  tl: { radius: 10 },
			  tr: { radius: 10 },
			  bl: { radius: 10 },
			  br: { radius: 10 },
			  antiAlias: true,
			  autoPad: true
		  	}
		 	var myBoxObject = new curvyCorners(settings, "rounded");
		  	myBoxObject.applyCornersToAll();
		}
	}
	
	ajax2.onreadystatechange=function()
	{
		if (ajax2.readyState==4)
		{
			if(id_video == -1)
				divResultado2.innerHTML = "";
			else
				divResultado2.innerHTML = ajax2.responseText;
		}
	}
	
	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	ajax.send('id_video=' + id_video);	
	ajax2.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	ajax2.send(null);	
}

function guarda_desc()
{
	var divResultado = document.getElementById('comentarios');
	var ajax = nuevoAjax();
	
	var id_video = document.getElementById('id_video_com').value;
	var comentario = document.getElementById('textocoment').value;
	var nombre = document.getElementById('tunombre').value;
	
	document.getElementById('textocoment').value='';
	//alert("id = "+id_video+"   com = "+comentario+"    nombre = "+nombre); return false;
	
	if(comentario != "")
	{	
		ajax.open("POST", "comentarios.php",true);
		ajax.onreadystatechange=function()
		{
			if (ajax.readyState==4)
			{
				divResultado.innerHTML = ajax.responseText;
				settings = {
				  tl: { radius: 10 },
				  tr: { radius: 10 },
				  bl: { radius: 10 },
				  br: { radius: 10 },
				  antiAlias: true,
				  autoPad: true
				}
				var myBoxObject = new curvyCorners(settings, "rounded");
				myBoxObject.applyCornersToAll();
			}
		}
		ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		ajax.send('guardar=1' + '&id_video=' + id_video + '&comentario=' + comentario + '&nombre=' + nombre);
	}
}

function busca_video()
{
	var buscado = document.getElementById('buscado').value;
	var divResultado = document.getElementById('images');
	var ajax = nuevoAjax();
	ajax.open("POST", "index5.php",true);
	ajax.onreadystatechange=function()
	{
		if (ajax.readyState==4)
		{
		
			 conf_reflection_p=0.2;
			 conf_focus=4;
			 conf_slider_width=14;
			 conf_images_cursor='pointer';
			 conf_slider_cursor='e-resize';
			 conf_imageflow='imageflow';
			 conf_loading='loading';
			 conf_images='images';
			 conf_captions='captions';
			 conf_scrollbar='scrollbar';
			 conf_slider='slider';
			 conf_slideshow='slideshow';
			 conf_youtube='youtubepopup';
			 caption_id=0;
			 new_caption_id=0;
			 current=0;
			 target=0;
			 mem_target=0;
			 timer=0;
			 array_images=[];
			 new_slider_pos=0;
			 dragging=false;
			 dragobject=null;
			 dragx=0;
			 posx=0;
			 new_posx=0;
			 xstep=150;
			 slide=null;
			 sizeAlgo=2;
			 glidetopicture=0;
			 slideshowtime=3000;
			 slideshowbutton = true;
			 slideshowauto=false;
			 videowidth='429';
			 videoheight='265';
			 videotop  = '-218px';
			 videoleft = '-128px';
			 output="video";
			 
			 divResultado.innerHTML = ajax.responseText;
			 if(ajax.responseText.trim() != "")
			 {
				document.getElementById('slideshow').style.display = 'block';
				refresh(true);
				/*slideshow(1);
				stopslideshow();*/
				handle(1);
				//loadCSS();
			 }
			 else
			 {
				 document.getElementById('captions').innerHTML = "Lo sentimos, no hay resultados.";
				 document.getElementById('slideshow').style.display = 'none';
			 }
		}
	}

	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	ajax.send('marca=' + buscado);
}

function envia_form()
{
	var divResultado = document.getElementById("contacto_div");
	var nombre_cont = document.getElementById("nombre_cont").value;
	var correo_cont = document.getElementById("correo_cont").value;
	
	var ajax = nuevoAjax();
	ajax.open("POST", "formulario.php",true);
	ajax.onreadystatechange=function()
	{
		if (ajax.readyState==4)
		{
			divResultado.innerHTML = ajax.responseText;
		}
	}
	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	ajax.send('nombre_cont=' + nombre_cont + '&correo_cont=' + correo_cont);	
}
