function loadJS(url) {
	  var remoteScript=document.createElement('script');
	  remoteScript.id = 'rs';
	  remoteScript.setAttribute('type','text/javascript');
	  remoteScript.setAttribute('charset', 'iso-8859-1');
	  //remoteScript.setAttribute('src',url);
	  remoteScript.src=url;
	  //var hd=document.getElementsByTagName('head')[0];
	  var hd=document.getElementsByTagName('body')[0];
	  // Gotcha: set attribute and src BEFORE appending, or Safari won't work
	  hd.appendChild(remoteScript);
	  return 0;
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function postForo(){
	if(readCookie('token')){
		document.location.href='http://foros2.esmas.com/post.php';	
		return false;
	}else{
		document.location.href='http://mipagina.esmas.com/login.php?ReturnUrl=http://foros2.esmas.com/post.php'
		return false;
	}
}

function addTV3(video_url){
	if(readCookie('token')){
		loadJS('http://mipagina.esmas.com/fav_TV3.php?type=video&token='+unescape(readCookie('token'))+'&url='+video_url);
		//loadJS('http://mipagina.esmas.com/fav_TV3.php?type=video&url='+video_url);
	}else{
		loadJS('http://mipagina.esmas.com/fav_TV3.php?type=video&url='+video_url);
	}
}

function insertVote(vote_type, page_to_vote, page_type, vote_value, thanks_id, thanks_text, thanks_color){ //en un onclick
	
	url=document.location.href;
	
	//validar agregar a favoritos
	if(url.search(/fanexito=/)!=-1){
		alert('Ya te has unido como un FAN. Gracias!');
		last_char=url.search(/\?/);
		url=url.substring(0,last_char);
		window.location=url;
	}else if(url.search(/vparam=/)!=-1){
		if(url.search(/=si/)!=-1){
			alert('Se agrego con éxito este video a tus favoritos.');
		}else{
			alert('El video ya ha sido agregado a tus  favoritos.');
		}
		last_char=url.search(/\?/);
		url=url.substring(0,last_char);
		window.location=url;	
	}
			
	if (typeof page_to_vote == "undefined"){
		
		last_char=url.search(/\#/);
		if(last_char!=-1){//no se incluyen las anclas
			url=url.substring(0,last_char);	
		}
		last_char=url.search(/\?/);
		if(last_char!=-1){//no se incluyen los parametros ?
			url=url.substring(0,last_char);	
		}
		var page_to_vote=url;
	}
	
	if (typeof page_type == "undefined"){
		if (typeof PAGE_TYPE == "undefined"){
			var page_type='micro';	
		}else{
			var page_type=PAGE_TYPE;
		}
	}
			
	if (typeof vote_type == "undefined"){
		var code=1;
	}else if (vote_type=="negative" ){
		var code=0;
	}else if (vote_type=="stars" ){
		var stars=vote_value;
	}else if (vote_type=="view"){
		loadJS('http://origin-comentarios.esmas.com/ranking/insertViewsLine.php?page_type='+page_type+'&url='+page_to_vote+'&url_r='+document.referrer);
		return false;
	}else{
		var code=1;
	}
	
	if (typeof thanks_id != "undefined"){
	
		if (typeof thanks_text == "undefined"){
			var thanks_text="&iexcl;Gracias por votar!";
		}
		
		if (typeof thanks_color == "undefined"){
			var thanks_color="red";
		}
				
		//menasaje de gracias
		document.getElementById(thanks_id).innerHTML=thanks_text;
		document.getElementById(thanks_id).style.fontSize='9px';
		document.getElementById(thanks_id).style.color=thanks_color;
		document.getElementById(thanks_id).style.height='20px';

	}
	
	if (typeof code != "undefined"){
		loadJS('http://origin-comentarios.esmas.com/ranking/insertViewsLine.php?code='+code+'&page_type='+page_type+'&url='+page_to_vote+'&url_r='+document.referrer);
	}else if (stars<6 && stars>0){
		loadJS('http://origin-comentarios.esmas.com/ranking/insertViewsLine.php?stars='+ stars+'&page_type='+page_type+'&url='+page_to_vote+'&url_r='+document.referrer);
	}

}