function checkEmail(Texto) 
{      
  return (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(Texto));      
}

function checkDate(Texto) {
	return (/^(0[1-9]|[12][0-9]|3[01])(\/|-)(0[1-9]|1[012])(\/|-)(\d{4})$/.test(Texto));
}

function ignoreSpaces(string) 
{
	var temp = "";
	string = '' + string;
	splitstring = string.split(" ");
	for(i = 0; i < splitstring.length; i++)
		temp += splitstring[i];
	return temp;
}

function checkCampoTexto(Texto)
{
	return (ignoreSpaces(Texto)!="");
}

function checkMinimiumTags(string, num)
{
	string_separated_commas = string.split(',');
	total = string_separated_commas.length;
	
	if(total < num)
	{
		return 0;
	}
	else
	{
		return 1;
	}
}

function getTextFromFlash(str) {
	$('filename_webcam').value = str;
}

function isset(variable_name) {
	
    try {
         if (typeof(eval(variable_name)) != 'undefined')
         if (eval(variable_name) != null)
         return true;
     } catch(e) { }
     
    return false;
}

function pagination_action(e)
{
	var page = this.activate_info;
	//var page = this.activate_info - 1;
	//var page_special = this.activate_info;
	var content_id = $('content_id').value;
	
			//var url = '/comentario/ver/' + content_id + '/' + page + '/' + page_special + '/';
			var url = '/comentario/ver/' + content_id + '/' + page + '/';
			//$('capa-comentarios').className = "hidden";
			var pars = {};
			var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars,
				onSuccess: function(transport) { 
					if(transport.responseText)
					{
						//alert(transport.responseText);	
						$('capa-comentarios').innerHTML = transport.responseText;
						load_comment_events();
					}
			}});
	Event.stop(e);
}

function pagination_action_bis(e)
{	
	var page = this.activate_info;
	var content_id = $('content_id').value;
	
	var url = '/comentario/gallery_page/' + content_id + '/' + page + '/';
	var pars = {};
	var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars,
		onSuccess: function(transport) { 
			if(transport.responseText)
			{
				//alert(transport.responseText);
				$('capa-com-gal').innerHTML = transport.responseText;
				load_comment_events();
			}
		}});
	Event.stop(e);
}

function menucm_action(e)
{
	var menu_list = document.getElementsByClassName("menucm");	
	var capa_list = document.getElementsByClassName("capacm");	
	for (var i = 0; i < menu_list.length; i++) 
	{
		menu_list[i].className = "menucm";
		capa_list[i].className = "capacm hidden";	
		if(this.activate_info == i)
		{
			menu_list[i].className = "menucm active";		
			capa_list[i].className = "capacm";	
			$('type_attach').value = this.activate_info;
		}	
	}
	Event.stop(e);
}

function load_menucm_events()
{	
	var pagination_list = document.getElementsByClassName("menucm");	
	for (var i = 0; i < pagination_list.length; i++) 
	{
		var activate_obj = { activate_info: i };
		Event.observe(pagination_list[i], 'click', menucm_action.bindAsEventListener(activate_obj));
	}
}

function cbox_action(e)
{
	var cbox_list = document.getElementsByClassName("pickImage");	
	
	for (var i = 0; i < cbox_list.length; i++) 
	{
		if(this.activate_info == i)
		{
			cbox_list[i].checked = "true";
			$('type_gallery').value = cbox_list[i].value;
		}
		else
		{
			cbox_list[i].checked = "";
		}
	}
}

function load_comment_events()
{	
	var pagination_list = document.getElementsByClassName("pagination_link");	
	for (var i = 0; i < pagination_list.length; i++) 
	{
		var activate_obj = { activate_info: pagination_list[i].innerHTML };
		Event.observe(pagination_list[i], 'click', pagination_action.bindAsEventListener(activate_obj));
	}
	
	var pagination_list_bis = document.getElementsByClassName("pagination_link_bis");	
	for (var i = 0; i < pagination_list_bis.length; i++) 
	{
		var activate_obj = { activate_info: pagination_list_bis[i].innerHTML };
		Event.observe(pagination_list_bis[i], 'click', pagination_action_bis.bindAsEventListener(activate_obj));
	}	
	
	var cbox_list = document.getElementsByClassName("pickImage");	
	for (var i = 0; i < cbox_list.length; i++) 
	{
		var activate_obj = { activate_info: i };
		Event.observe(cbox_list[i], 'click', cbox_action.bindAsEventListener(activate_obj));
	}
 
    if($('comment_form'))
	{
	Event.observe($('comment_form'), 'submit', function(e) 
	{
		check_fields = new Array ("comment", "author_name", "email");
		name_fields = new Array ("Comentario", "Autor", "Correo electrónico");	
		
		var error_list = document.getElementsByClassName("hidden");	
		
		if(error_list)
		{
			ok = 1;
			for (var i = 0; i < check_fields.length; i++)
			{
				if(!checkCampoTexto($F(check_fields[i])))
				{
					var error_list = document.getElementsByClassName(check_fields[i]);	
		
					for (var a = 0; a < error_list.length; a++)
					{
						error_list[a].className = check_fields[i] + " error";
						error_list[a].innerHTML = "Campo incorrecto";
					}
					ok = 0;
				}
				else
				{
					var error_list = document.getElementsByClassName(check_fields[i] + " error");	
		
					for (var a = 0; a < error_list.length; a++)
					{
						error_list[a].className = "hidden " + check_fields[i];
					}
				}
				
				if(check_fields[i] = "email")
				{
					if(!checkEmail($F(check_fields[i])))
					{
						var error_list = document.getElementsByClassName(check_fields[i]);	
		
						for (var a = 0; a < error_list.length; a++)
						{
							error_list[a].className = check_fields[i] + " error";
							error_list[a].innerHTML = "Campo no valido";
						}
						ok = 0;
					}
					else
					{
						var error_list = document.getElementsByClassName(check_fields[i] + " error");	
			
						for (var a = 0; a < error_list.length; a++)
						{
							error_list[a].className = "hidden " + check_fields[i];
						}
					}
				}
			}
			
		}
			
		if(ok == 1)
		{	
			$('comment_form').submit();		
		}
		else
		{
			var loading = document.getElementsByClassName("loading");
			loading[0].className = "loading";
			loading[0].innerHTML = "<div class=\"advertencia\">Error al introducir los datos.</div>";
		}

		Event.stop(e);
	});
	}
	
	if($('cFoto')){
	Event.observe($('cFoto'), 'click', function(e) 
		{	
		$('liFoto').className = "active";
		$('liYoutube').className = "";
		$('liWebcam').className = ""
		$('unaFoto').className = "";
		$('unYoutube').className = "hidden";
		$('unaWebcam').className = "hidden";
		$('type_attach').value = 0;
		Event.stop(e);
		});
	}
	
	if($('cYoutube')){
		Event.observe($('cYoutube'), 'click', function(e) 
		{
		$('liFoto').className = "";
		$('liYoutube').className = "active";
		$('liWebcam').className = "";
		$('unaFoto').className = "hidden";
		$('unYoutube').className = "";
		$('unaWebcam').className = "hidden";
		$('type_attach').value = 1;
		Event.stop(e);
		});
	}

	if($('cWebcam')){
		Event.observe($('cWebcam'), 'click', function(e) 
		{	
		$('liFoto').className = "";
		$('liYoutube').className = "";
		$('liWebcam').className = "active";
		$('unaFoto').className = "hidden";
		$('unYoutube').className = "hidden";
		$('unaWebcam').className = "";
		$('type_attach').value = 2;
		Event.stop(e);
		});
	}
	
	if($('subir')){
		Event.observe($('subir'), 'click', function(e) 
		{
		
		//alert("prueba");
		
		if($('urlYoutube').value != "")
		{
			var parte1 = $('urlYoutube').value.split("=");

			if(parte1[1] != "")
			{
				$('mini_cap_youtube').innerHTML = '<object width="130" height="110"><param name="movie" value="'+'http://www.youtube.com/v/'+ parte1[1] +'&rel=1"></param><param name="wmode" value="transparent"></param><embed src="'+'http://www.youtube.com/v/'+ parte1[1] +'&rel=1" type="application/x-shockwave-flash" wmode="transparent" width="130" height="110"></embed></object>';
				var error_id = "urlYoutube";
				$(error_id).className = $(error_id).className;
				var error_list = document.getElementsByClassName(error_id);	
			
				for (var a = 0; a < error_list.length; a++)
				{
					error_list[a].className = error_id + " spanerror hidden";
				}
			}
		}
		
		Event.stop(e);
	});
}
}

Event.observe(window, 'load', function(event) {
    load_menucm_events();
	load_comment_events();	
});

    

