function http_request() {
    try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
            xmlhttp = false;
        }
    }
    if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
        xmlhttp = new XMLHttpRequest();
    }
    return xmlhttp
}
function str_replace(search, replace, subject) { 
    var f = search, r = replace, s = subject;
    var ra = r instanceof Array, sa = s instanceof Array, f = [].concat(f), r = [].concat(r), i = (s = [].concat(s)).length;
 
    while (j = 0, i--) {
        if (s[i]) {
            while (s[i] = s[i].split(f[j]).join(ra ? r[j] || "" : r[0]), ++j in f){};
        }
    };
 
    return sa ? s : s[0];
}

function strlen (string) {
    return (string+'').length;
}

function http_request2() {
    try {
        xmlhttp2 = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            xmlhttp2 = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
            xmlhttp2 = false;
        }
    }
    if (!xmlhttp2 && typeof XMLHttpRequest!='undefined') {
        xmlhttp2 = new XMLHttpRequest();
    }
    return xmlhttp2
}

function http_request3() {
    try {
        xmlhttp3 = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            xmlhttp3 = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
            xmlhttp3 = false;
        }
    }
    if (!xmlhttp3 && typeof XMLHttpRequest!='undefined') {
        xmlhttp3 = new XMLHttpRequest();
    }
    return xmlhttp3
}
var time_init;
var time_finish;
var script_id;
var script_old_id;

function do_child_src(URL) {

   if (script_id == null || script_id == undefined) {
    script_id = 0;
  }

  if (((new Date()).getTime()-script_id)<300) return;

  script_id = (new Date()).getTime();
  
  var script = document.createElement('script');

  script.type = 'text/javascript';
  script.src = "http://www.nexobid.com/"+URL;
  script.id = script_id;

  // remove old script-node (if there is one..)
  el = document.getElementById(script_old_id);
  if (el) {
  	//alert('borramos el script antiguo...');
    document.getElementsByTagName('head')[0].removeChild( el );
  }
  script_old_id = script_id;
  // set new script node
  document.getElementsByTagName('head')[0].appendChild(script);
  
}

function comprobarURLMulti(bids_id)
{

	time_init = new Date();
    do_child_src("online.php?op=mtime&bids_id=" + bids_id);
    return
}

function formatea_response(response,bids_id){
		buffer = response;
	   buffer = buffer.split(";"); 
                if(document.getElementById("user_bids_layer")!=null){
                   	document.getElementById("user_bids_layer").innerHTML = buffer[1];
                }

                
       // SI SON MAS DE LAS 23H y menos de las 11H LE MUESTRO EL AVISO
       ahora = new Date();
       if(ahora.getHours()>21){
       	if(ahora.getMinutes()>55){
       		document.getElementById("aviso_23h").style.display='block';
       	}
       }
       
       if(ahora.getHours()<11){     	
       		document.getElementById("aviso_23h").style.display='block';
       }
       
       // FINAL 
                buffer = buffer[0].split(",");
				
				var x;
				
                for (x in buffer){
                	
                	if(buffer[x]!=''){
                		
                		trozo = buffer[x].split("|");
                		
                		if(document.getElementById("amount_layer_"+trozo[3]).innerHTML != trozo[1] && document.getElementById("history_layer_"+trozo[3]) != null){
                			update_history(trozo[3]);
                			
                			
                		}
                		
                		if(trozo[0]=='0'){
                			document.getElementById("time_layer_"+trozo[3]).innerHTML = 'Vendido';
                			bids_id = str_replace(","+ trozo[3] +",",",",bids_id);
                			if(bids_id==trozo[3] +","){
                				bids_id = '';
                			}
                			
                			if(document.getElementById("boton_pujar_"+trozo[3]) != null){
                				document.getElementById("boton_pujar_"+trozo[3]).innerHTML = '<img src="images/sub_fin.jpg" alt="Subasta Finalizada" border=0/>';
                				setTimeout("window.location.reload()",3000);
                			}
                			
                		} else {
                			document.getElementById("time_layer_"+trozo[3]).innerHTML = format_time(trozo[0]);
                			if(trozo[0]<20){
                				document.getElementById("time_layer_"+trozo[3]).className='tiempo_box_final';
                				
                				if(document.getElementById("cont_puja_"+trozo[3]) != null){
                				document.getElementById("cont_puja_"+trozo[3]).style.borderColor='#CC0000';
                				}
                				
                			} else {
                				document.getElementById("time_layer_"+trozo[3]).className='tiempo_box';
                				
	                			if(document.getElementById("cont_puja_"+trozo[3]) != null){
	                				document.getElementById("cont_puja_"+trozo[3]).style.borderColor='#89C3D9';
	                				}
                				
                			}
                		}
                		
                		// AQUI SE CAMBIA EL COLOR DEL FONDO AL RECIBIR UNA PUJA NUEVA DE UN USUARIO
                		if(document.getElementById("nickname_layer_"+trozo[3]).innerHTML!=''){
                			
                			if(document.getElementById("nickname_layer_"+trozo[3]).innerHTML != trozo[2]){
                				pon_fondo("amount_layer_"+trozo[3]);
                			}
                			
                		}
                		// FIN CAMBIO PUJA NUEVA
                		
                		document.getElementById("amount_layer_"+trozo[3]).innerHTML = trozo[1]+'&euro;';
                		document.getElementById("nickname_layer_"+trozo[3]).innerHTML = trozo[2];
                		
                		
                		
                		
                		
                	}
                }
                time_finish = new Date();
    var total_tiempo_tardado = parseFloat(time_finish.getTime()) - parseFloat(time_init.getTime());
    
    if(total_tiempo_tardado < 250){
    	var tiempo_refresh = 1000 - total_tiempo_tardado;
    } else {
    	var tiempo_refresh = 1000;
    }
    //alert("Tardado: " + total_tiempo_tardado +"\n Ajustamos a: " +tiempo_refresh);
    
    if(strlen(bids_id)>1){
    	
    setTimeout("comprobarURLMulti('"+bids_id+"')",tiempo_refresh);
    } else {
    	//alert('no continuamos...');
    }
    
}

function update_history(bids_id){
	_objetus2=http_request2()
    _URL_2="online.php?op=history&bids_id=" + bids_id;
    _objetus2.open("GET",_URL_2,true);
    _objetus2.onreadystatechange=function() {
		
    	if(_objetus2.readyState==4) {
    		
    		if(_objetus2.status==200) {
    			
                document.getElementById("history_layer_"+bids_id).innerHTML = _objetus2.responseText;
    		}
    	}
    }
    	_objetus2.send(null);
}


function do_seg(bids_id){
	_objetus3=http_request3()
    _URL_3="online.php?op=seg&bids_id=" + bids_id;
    _objetus3.open("GET",_URL_3,true);
    _objetus3.onreadystatechange=function() {
		
    	if(_objetus3.readyState==4) {
    		
    		if(_objetus3.status==200) {
               document.getElementById('seg_layer_'+bids_id).innerHTML = _objetus3.responseText;
    		}
    	}
    }
    	_objetus3.send(null);
}

function undo_seg(bids_id){
	_objetus3=http_request3()
    _URL_3="online.php?op=undo_seg&bids_id=" + bids_id;
    _objetus3.open("GET",_URL_3,true);
    _objetus3.onreadystatechange=function() {
		
    	if(_objetus3.readyState==4) {
    		
    		if(_objetus3.status==200) {
               document.getElementById('seg_layer_'+bids_id).innerHTML = _objetus3.responseText;
    		}
    	}
    }
    	_objetus3.send(null);
}

function do_bid(bids_id){
	_objetus2=http_request2()
    _URL_2="http://www.nexobid.com/online.php?op=time&op2=pujar&bids_id=" + bids_id;
    _objetus2.open("GET",_URL_2,true);
    _objetus2.onreadystatechange=function() {
		
    	if(_objetus2.readyState==4) {
    		
    		if(_objetus2.status==200) {
    			
                if(_objetus2.responseText=='PP'){
               	alert('Lo sentimos, esta puja esta paralizada hasta las 11:00h AM. Podra pujar en ella a partir de esa hora.');
               }
               if(_objetus2.responseText=='FT'){
               	alert('Lo lamentamos, no puede realizar mas pujas, la subasta ha finalizado.');
               }
               if(_objetus2.responseText=='PR'){
               	alert('Ya eres el maximo pujador de esta subasta, no puedes volver a pujar hasta ser superado.');
               }
               if(_objetus2.responseText=='PR'){
               	alert('No se pueden ganar mas de 2 subastas por semana. Su puja no se contabiliza.');
               }
               if(_objetus2.responseText=='NB'){
               	alert('No dispone de BIDS disponibles. Recargue su cuenta para realizar pujas.');
               		
               		if(confirm("¿Quieres recargar BIDS?")){ 
						location.href="http://nexobid.com/miembros-recarga.html"; 
					}

               }
              
               
    		}
    	}
    }
    	_objetus2.send(null);
}

function format_time(timestamp){
	var horas;
	var minutos;
	var segundos;
	var dt = new Date(timestamp * 1000);
	if(dt.getHours()<10){
		horas = '0'+ (dt.getHours()-1);
	} else {
		horas = (dt.getHours()-1);
	}
	
	if(dt.getDate()>1){
		horas = ((parseFloat(dt.getDate()-1))*24)+(parseFloat(dt.getHours())-1);
	}
	
	if(dt.getMinutes()<10){
		minutos = '0'+ dt.getMinutes();
	} else {
		minutos = dt.getMinutes();
	}
	
	if(dt.getSeconds()<10){
		segundos = '0'+ dt.getSeconds();
	} else {
		segundos = dt.getSeconds();
	}
	
	if(horas>48){
		return "Próximamente";
	} else {
		return horas +":"+ minutos +":"+ segundos;		
	}


}


function pon_fondo(id_capa){
colores = new Array("#9E0000", "#A30D0D", "#A81A1A", "#AD2828", "#B23535", "#B74343", "#BC5050", "#C15D5D", "#C66B6B", "#CB7878", "#D18686", "#D69393", "#DBA1A1", "#E0AEAE", "#E5BBBB", "#EAC9C9", "#EFD6D6", "#F4E4E4", "#F9F1F1", "#FFFFFF");
tiempo=0;
for (var i = 0; i < colores.length; i++){
tiempo=(tiempo+30);
setTimeout("color_fondo('"+ id_capa +"','"+ colores[i] +"')", tiempo);
}
}

function color_fondo(id_capa,color){

document.getElementById(id_capa).style.background = color;

}
