var sCurrentUrl = window.location.href;

var bEnvironmentFlag = sCurrentUrl.indexOf(".jif.es");

var sRedyaValidationHost = (bEnvironmentFlag == -1 ? 'validacion.ya.com' : 'desarrollo.red2.ya.jif.es'); 

/* var sDomain = window.location.host.substr(window.location.host.lastIndexOf(".", window.location.host.lastIndexOf(".")-1)); */

var sDomain = GetDomain();

var sUsrNick = null;

function Connect()

{
  var f = document.forms["identification"];

  f.action = "http://" + sRedyaValidationHost + "/SMain?M=login20";

  var currLocation = window.location.href;
  var urlReturn;
  if (currLocation.indexOf("$M=error") > -1){
    urlReturn = "http://foros.ya.com";
  } else {
    urlReturn = currLocation;
  }
  f.URL_VOLVER.value = urlReturn;

  f.SITE.value = GetSite();

  f.submit();

  return false;

}

function GetSite()

{

  var s = window.location.href;

  var pos1, pos2;

  

  pos1 = s.indexOf("//");

  if (pos1!=-1) {

    pos2 = s.indexOf("/", pos1+2);

    if (pos2!=-1) {

      s = s.substring(pos1+2, pos2);

      pos1 = s.indexOf(".");

      if (pos1!=-1) {

        pos2 = s.indexOf(".", pos1+1);

        if (pos2!=-1) {

          return s.substring(pos1+1, pos2);

        }

      }

    }

  }

  return "ya";

}

var oCookieNames = new Array("REDYA", "FOROS_YA", "SPACE20", "SPACE20MAIL", "CUS", "REDYAPT");

function DisConnect()

{

  var f = document.forms["identification"];

  f.action = "http://" + sRedyaValidationHost + "/SDisconnectWindowNoPopup";

  f.URL_VOLVER.value = window.location.href;

  f.SITE.value = GetSite();

  f.submit();

  return false;

  /*   

  var expires = new Date(1971, 6, 24);  

  var c;  

  for(var i=0; i<oCookieNames.length; i++) {

    c = "";

    c += oCookieNames[i] + "=" + "";

    c += "; expires=" + expires.toGMTString();

    c += "; path=" + "/";

    c += "; domain=" + sDomain;

    if (window.location.href.indexOf("$TETE")!=-1) {

      alert(c);

    }

    document.cookie = c;

  }

  window.location.reload(true);  

  return false;

  */  

}

function CheckIdentification(isPt)

{

  var cookies = unescape(document.cookie);

  var pos1 = cookies.indexOf((isPt ? "REDYAPT=" : "REDYA="));

  if (pos1==-1) return false;

  var pos2 = cookies.indexOf(";", pos1);

  if (pos2==-1) pos2 = cookies.length;

  var cookie = unescape(cookies.substring(pos1+6, pos2));

  

  pos1 = cookie.indexOf("ALINOMBRE=");

  if (pos1==-1) return false;

  pos2 = cookie.indexOf("|", pos1);

  if (pos2==-1) return false;

  

  sUsrNick = cookie.substring(pos1+10, pos2);

  

}

function ShowIdentification()

{

  if (sDomain.indexOf("terravista")!=-1) {

    CheckIdentification(true);

  } else {

    CheckIdentification(false);

  }

  if (sUsrNick==null) {

    if (sDomain.indexOf("terravista")!=-1) {

      document.write("&nbsp;");

    } else {

      document.write('<a href="#" onclick="return Connect();">Conectar</a>');

    }

  } else {

    if (sDomain.indexOf("terravista")!=-1) {

      DisConnect();

    } else {

      document.write(sUsrNick + ' <a href="#" onclick="return DisConnect();">Desconectar</a>');

    }

  }

}

function GetUrlParameter(paramName)

{

  var pos1, pos2;

  var paramValue = null;

  

  pos1 = sCurrentUrl.indexOf(paramName + "=");

  if (pos1!=-1) {

    pos2 = sCurrentUrl.indexOf("$", pos1);

    if (pos2==-1) {

      pos2 = sCurrentUrl.indexOf("&", pos1);

    }

    if (pos2!=-1) {

      paramValue = sCurrentUrl.substring(pos1 + paramName.length + 1, pos2);

    } else {

      paramValue = sCurrentUrl.substr(pos1 + paramName.length + 1);

    }

  }

  return paramValue;

}

var numbers = "0123456789";

function IsNumeric(s)

{

  var length = s.length;

  for(var i=0; i<length; i++) {

    if (numbers.indexOf(s.charAt(i))==-1) return false;

  }

  return true;

}

function UserStyleCookie()

{

  var cus = GetUrlParameter("CUS");

  var f = GetUrlParameter("F");

  if (cus!=null && f!=null) {

    if (IsNumeric(cus) && IsNumeric(f)) {

      var expires = new Date(1000*60*60*24*30*6 + new Date().getTime());

      var c = "";

      c += "CUS" + parseInt(f, 10).toString(34) + "=" + parseInt(cus, 10).toString(34);

      c += "; expires=" + expires.toGMTString();

      c += "; path=" + "/";

      c += "; domain=" + sDomain;

      document.cookie = c;  

    }

  }

}

UserStyleCookie();

function GetDomain()

{

  var sCurrentUrl = window.location.href;

  if (sCurrentUrl.indexOf("finanzas")!=-1) {

    return ".finanzas.com";

  } else if (sCurrentUrl.indexOf("yatv")!=-1) {

    return ".yatv.com";

  } else if (sCurrentUrl.indexOf("terravista")!=-1) {

    return ".terravista.pt";

  } else if (sCurrentUrl.indexOf("inicio")!=-1) {

    return ".inicio.tiendapc.com";

  } else if (sCurrentUrl.indexOf("chueca")!=-1) {

    return ".chueca.com";

  } else if (sCurrentUrl.indexOf("supermotor")!=-1) {

    return ".supermotor.com";

  } else if (sCurrentUrl.indexOf("viajar")!=-1) {

    return ".viajar.com";

  } else if (sCurrentUrl.indexOf("infor")!=-1) {

    return ".ya.com";

  } else {

    return ".ya.com";

  }

}