// xmlHttpRequestを通してリモートデータの呼び出しを行う

function datosServidor() {
};
datosServidor.prototype.iniciar = function() {
	try {
		// Mozilla / Safari
		this._xh = new XMLHttpRequest();
	} catch (e) {
		// Explorer
		var _ieModelos = new Array(
		'MSXML2.XMLHTTP.5.0',
		'MSXML2.XMLHTTP.4.0',
		'MSXML2.XMLHTTP.3.0',
		'MSXML2.XMLHTTP',
		'Microsoft.XMLHTTP'
		);
		var success = false;
		for (var i=0;i < _ieModelos.length && !success; i++) {
			try {
				this._xh = new ActiveXObject(_ieModelos[i]);
				success = true;
			} catch (e) {
				// 例外
			}
		}
		if ( !success ) {
			// Implementar manejo de excepciones, mientras alerta.
			return false;
		}
		return true;
	}
}

datosServidor.prototype.ocupado = function() {
	estadoActual = this._xh.readyState;
	return (estadoActual && (estadoActual < 4));
}

datosServidor.prototype.procesa = function() {
	if (this._xh.readyState == 4 && this._xh.status == 200) {
		this.procesado = true;
	}
}

datosServidor.prototype.enviar = function(urlget,datos) {
	if (!this._xh) {
		this.iniciar();
	}
	if (!this.ocupado()) {
		this._xh.open("GET",urlget,false);
		this._xh.send(datos);
		if (this._xh.readyState == 4 && this._xh.status == 200) {
			return this._xh.responseText;
		}
		
	}
	return false;
}


// 高速アクセスのために、URLとDIVを渡します
function _gr(reqseccion,divcont) {
	remotos = new datosServidor;
	nt = remotos.enviar(reqseccion,"");
	document.getElementById(divcont).innerHTML = nt;
}

// 人数算出
function setNumber(votenum,feedid){
	//alert(feedid);
    if(document.getElementById){
        //var obj=document.getElementById("html_v1_"+feedid);
        var numobj = document.getElementById("num_v"+votenum+"_"+feedid);
        numorg = numobj.innerHTML;
        numaft = parseInt(numorg) + 1;
        if(typeof(numobj.innerHTML)!="undefined"){
        	// innerHTML対応ブラウザのみ実行
        	numobj.innerHTML = numaft;
        	
        	var obj=document.getElementById("html_v"+votenum+"_"+feedid);
        	
        	if(typeof(obj.innerHTML)!="undefined"){
        		
        		html = obj.innerHTML;
        		//alert(html);
        		
        		//orgstr = "";
        		repstr = "";
        		html = html.replace(/<A.*?>/g,repstr);
        		html = html.replace(/<a.*?>/g,repstr);
        		//alert(html);
        		
        		orgstr = "src=\"/pub/img/btn_plus_1.gif\"";
        		repstr = "src=\"/pub/img/btn_plus_3.gif\"";
        		html = html.replace(orgstr,repstr);
        		//alert(html);
        		
        		orgstr = "src=\"/pub/img/btn_plus_2.gif\"";
        		repstr = "src=\"/pub/img/btn_plus_3.gif\"";
        		html = html.replace(orgstr,repstr);
        		//alert(html);
        		
        		orgstr = "</A>";
        		repstr = "";
        		html = html.replace(orgstr,repstr);
        		//alert(html);
        		
        		orgstr = "</a>";
        		repstr = "";
        		html = html.replace(orgstr,repstr);
        		//alert(html);
        		
        		obj.innerHTML = html;
        	}
        }
    }
}

// rating算出
function setRating(rating,feedid,ratetotal,ratenum){

	var obj=document.getElementById("html_v0_"+feedid);
	var result = 0;
	
	alert(ratetotal);
	alert(rating);
	
	ratetotal = parseInt(ratetotal) + parseInt(rating);
	alert(ratetotal);
	result = (Math.round(ratetotal/ratenum*10))/10;
	alert(result);
	
	if(typeof(obj.innerHTML)!="undefined"){
		
		html = obj.innerHTML;
		//alert(html);
		
		//orgstr = "";
		repstr = result + "点";
		html = html.replace(/(.*?)点/,repstr);
		//alert(html);
		
		obj.innerHTML = html;
    }
}

//Estas dos son para guardar
var urlBase = "voteupdate.php?";

function rateImg(rating,feedid,ratetotal,ratenum)  {
		var result;
		remotos = new datosServidor;
		
		result = (Math.round((ratetotal + rating)/(ratenum + 1)*10))/10;
		result = result + "点";
		nt = remotos.enviar('voteupdate.php?rating='+rating+'&feedid='+feedid);
		document.getElementById('html_v0_'+feedid).innerHTML = document.getElementById("html_v0_"+feedid).innerHTML.replace(/(.*?)点/,result);
		rating = rating * 12;
		document.getElementById('current-rating-'+feedid).style.width = rating+'px';
		
}

function voteImg(voting,feedid)  {
		remotos = new datosServidor;
		nt = remotos.enviar('voteupdate.php?voting='+voting+'&feedid='+feedid);
		setNumber(voting,feedid);
}

function swImg(iName,str)
{
	document.images[iName].src = str;
}
