var soporte_online = 0;
var soporte_activo = 0;
var sid;
var phpo_nv;

var url_dexp = url_dirajax.split('/');
if(url_dexp[2] != self.location.host)
	url_dirajax = url_dirajax.replace(/^http:\/\/[^\/]+\//i, "http://"+self.location.host+"/")

//div online
var dvht = '<div id="online" style="display: none;">'+texto_online+'</div>';

//div chat window
var dvch = '<div id="div_chat"><table width="506" border="0" cellpadding="0" cellspacing="0"><tr>';
dvch += '<td><img src="'+url_destino+'img/up1.gif" width="382" height="39" /><a href="javascript:;" onclick="cerrar_soporte()"><img src="'+url_destino+'img/up2.gif" width="124" height="39" border="0" /></a></td>';
dvch += '</tr><tr><td><iframe src="javascript:\'\'" name="soporte" id="soporte" scrolling="no" marginheight="0" marginwidth="0" frameborder="0" height="280" width="506"></iframe>';
dvch += '</tr></tr></table></div>';

//css chat window fixed position
var dcss = '<style type="text/css">#div_chat { display: none; position: absolute; z-index:10; left: 254px; top: 50px; width: 506px;}';
dcss += 'body > div#div_chat { position: fixed;}</style>';
dcss += '<!--[if gte IE 5.5]><![if lt IE 7]><style type="text/css">div#div_chat { display: none; bottom: auto; left: 254px; width: 506px; height:355px; top: expression( ( 0 - div_chat.offsetHeight + ( document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight ) + ( ignoreMe = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ) ) + "px" ); }</style><![endif]><![endif]-->';

function escribir_div() {
	document.getElementById('div_online').innerHTML = dvht;
	if(!nueva_ventana)
		document.write(dcss + dvch);
}

escribir_div();


function crearSidC() {
	sfecha = new Date();
	sid = sfecha.getTime()+''+Math.floor(Math.random()*(999-100+1)+100);
	document.cookie = "online_sid="+sid+"; path=/";
}

function readCookie() {
	var ca = document.cookie.split(';');
	var nameEQ = "online_sid=";
	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 abrir_soporte(usuario,email,estudio) {
	if(nueva_ventana) en_ventana(usuario,email,estudio);
	else abrir_inline();
	soporte_activo = 1;
}
function abrir_inline() {
	document.getElementById('div_chat').style.display='block';
	frames['soporte'].location.href=url_destino+'index.php?sid='+sid;
}
var phpoN;

function en_ventana(usuario,email,estudio) {
	phpoN=window.open('','Online','height=320,width=506,left=100,top=100,resizable=no,scrollbars=no,toolbar=no,status=no');
	if(!phpoN) {
		alert("Por favor, desactive el bloqueador de Pop-Up\nPlease, allow pop-up windows for this domain");
		return;
	}
	var w_phpoN = phpoN.document;
	w_phpoN.write('<html><head><style>body {margin: 0; padding: 0}</style><script language="javascript">function cerrar_soporte() {opener.cerrar_soporte();}</script></head><body onunload="cerrar_soporte()">'+dvch+'</body></html>');
	w_phpoN.close();
	phpoN.frames['soporte'].location.href=url_destino+'index.php?sid='+sid+'&usuario='+usuario+'&email='+email+'&estudio='+estudio;
}
function cerrar_soporte() {
 	if(nueva_ventana) {
		phpoN.frames['soporte'].location.href=url_destino+'index.php?modo=cerrar&sid='+sid;
		phpoN.close();
	} else {
		frames['soporte'].location.href=url_destino+'index.php?modo=cerrar&sid='+sid;
		document.getElementById('div_chat').style.display='none';
	}
	soporte_activo = 0;
}
function mostrar_online() {
	escribir_div();
	document.getElementById('online').style.display='block';
	soporte_online=1;
}
function ocultar_online() {
	//document.getElementById('online').style.display='none';
	document.getElementById('div_online').innerHTML = "Soporte Desconectado";
	soporte_online=0;
}


if(!readCookie()) {
    crearSidC();
	phpo_nv=escape(document.referrer)?escape(document.referrer):top.location.href;
} else {
	sid = readCookie();
}

function actualizar() {
	ndir = url_destino+'index.php?modo=actualizar&sid='+sid+'&soporte_online='+soporte_online+'&soporte_activo='+soporte_activo+'&cross_ajax='+url_dirajax;
	ndir += phpo_nv?'&phpo_nv='+phpo_nv:'';
	callToServer(ndir);
	phpo_nv = false;
	if (soporte_online==0)
	{
		document.getElementById('div_online').innerHTML = "Soporte Desconectado";	
	}
}

//creamos el iframe de comunicacion | evitamos los reload/back/forward
var IFrameObj; 
function callToServer(URL) {
  if (!document.createElement) {return true};
  var IFrameDoc;
  if (!IFrameObj && document.createElement) {
   try {
      var tempIFrame=document.createElement('iframe');
      tempIFrame.setAttribute('id','i_online');
      tempIFrame.style.border='0px';
      tempIFrame.style.width='0px';
      tempIFrame.style.height='0px';
      IFrameObj = document.body.appendChild(tempIFrame);
      
      if (document.frames) {
        IFrameObj = document.frames['i_online'];
      }
    } catch(exception) {
      iframeHTML='\<iframe id="i_online" style="';
      iframeHTML+='border:0px;';
      iframeHTML+='width:0px;';
      iframeHTML+='height:0px;';
      iframeHTML+='"><\/iframe>';
      document.body.innerHTML+=iframeHTML;
      IFrameObj = new Object();
      IFrameObj.document = new Object();
      IFrameObj.document.location = new Object();
      IFrameObj.document.location.iframe = document.getElementById('i_online');
      IFrameObj.document.location.replace = function(location) {
        this.iframe.src = location;
      }
    }
  }
  
  if (navigator.userAgent.indexOf('Gecko') !=-1 && !IFrameObj.contentDocument) {
    setTimeout('callToServer()',10);
    return false;
  }

  if (IFrameObj.contentDocument) {
    IFrameDoc = IFrameObj.contentDocument;
  } else if (IFrameObj.contentWindow) {
    IFrameDoc = IFrameObj.contentWindow.document;
  } else if (IFrameObj.document) {
    IFrameDoc = IFrameObj.document;
  } else {
    return true;
  }

  IFrameDoc.location.replace(URL);
  return false;
}

actualizar();

