function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}

function getCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; 
  }
  return null;
}

function setCookie (name, value) {
  var argv = setCookie.arguments;
  var argc = setCookie.arguments.length;
  var expires = (argc > 2) ? argv[2] : null;
  var path = (argc > 3) ? argv[3] : null;
  var domain = (argc > 4) ? argv[4] : null;
  var secure = (argc > 5) ? argv[5] : false;
   
  document.cookie = name + "=" + escape (value) +
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((secure == true) ? "; secure" : "");
}

/*Modificacion de cookies*/
function actualizaDatos (domain, domain2) {
	var valor_actual = getCookie("COL");
	if (valor_actual != null) {
		var valor_nuevo = valor_actual.substring(0,14) + v_segmento + v_idioma + valor_actual.substring(20,valor_actual.length);
		var today = new Date();
		var ms = today.getMilliseconds();
		var days = new Date(today.getTime()+365*24*60*60*1000+ms);
		setCookie("COL", valor_nuevo, days, '/', domain);
		if (domain2 != null) setCookie("COL", valor_nuevo, days, '/', domain2);
	} else {
		createCookie(domain);
		if (domain2 != null) createCookie(domain2);
	}
}
/*Modificacion de cookies*/

function createCookie(domain) {
	var now = new Date();
	var ms = now.getMilliseconds();
	var milisegundos = (now.getHours()*360000)+(now.getMinutes()*60000)+(now.getSeconds()*1000)+(ms);
	var longitudm = new String(milisegundos).length;
	while (longitudm < 8) {
		milisegundos = "0" + milisegundos;
		longitudm++;
	}
	var anno = "" + now.getFullYear();
	anno = anno.substr(2,2);
	var mes = (now.getMonth() + 1 < 10 ? "0"+(now.getMonth()+1): now.getMonth()+1);
	var dia = (now.getDate() < 10 ? "0"+now.getDate(): now.getDate());
	var fechacreacion = "" +anno+mes+dia+milisegundos;
	var fechaUE = "" + now.getFullYear()+mes+dia;
	var valor_cookie = fechacreacion+"AHOGes0000"+fechaUE+"77777";
	var days = new Date(now.getTime()+365*24*60*60*1000+ms);
	setCookie("COL",valor_cookie,days,'/', domain);
}

function deleteCookie (name) {
	var exp = new Date();
	exp.setTime (exp.getTime() - 1);
	// This cookie is history
	var cval = getCookie (name);
	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

function controlaDatos() {
	cookie=getCookie("COL");
	if (cookie != null) {
		if (v_segmento == "" || v_segmento == null) v_segmento=cookie.substr(14,4);
		if (v_idioma == "" || v_idioma == null) v_idioma=cookie.substr(18,2);
	} else {
		if (v_segmento == "" || v_segmento == null || (arrayNavegacion[v_segmento] == null)) v_segmento="AHOG";
		if (v_idioma == "") v_idioma="es";
	}
	if (arrayNavegacion[v_segmento] != "A") v_segmento=arrayTraducciones[v_segmento];
	if (v_segmento == null) v_segmento = "AHOG";
	var ok = "N";
	for (var i=0; i < arrayIdiomas.length; i ++)
	{
		if (arrayIdiomas[i]==v_idioma) ok = "S";
	}
	if (ok == "N") v_idioma = "es";
}
function getParametro (nombre_param, ventana) {
	if (nombre_param == null)
		return null;
	if (ventana == null)
		ventana = window;

	var valor_param = getArrayParametros(ventana)[nombre_param];

	return valor_param;
}

function getArrayParametros (ventana) {
	if (ventana == null)
		ventana = window;

	var params = getParametros (ventana);

	var arr_params = new Array();

	var idx = params.indexOf('?');
	if (idx != -1) {
		var pares = params.substring(1).split('&');
		for (var i=0; i<pares.length; i++) {
			var nombre_valor_param = pares[i].split('=');
			var nombre_param	= nombre_valor_param[0];
			var valor_param		= unescape(nombre_valor_param[1]);

			arr_params[nombre_param] = valor_param;
		}
	}  

	return arr_params;
}
function getParametros (ventana) {
	if (ventana == null)
		ventana = window;

	return ventana.location.search;
}
