var gJanEscVir    = "";  
var gLeft         = parseInt(window.pageXOffset) + "px";
var gTop          = parseInt(window.pageYOffset) + "px";
//var gFea          = "top=" + gTop + ",left=" + gLeft + ",width=800px,scrollbars=yes,status=no,resizable=yes";
var gFea          = "top=" + gTop + ",left=" + gLeft;
  
var timerID       = null;
var timerRunning  = false;
var id            = 0;
var pause         = 0;
var position      = 0;
gIe               = false;
  
function inicio()
{
  startclock();
  //LoadFeed(0);
  LoadLinks();
}
function LoadLinks()
{
  var p = {}, lEnd="";
  lEnd = gVic_BASEPATH + "/links/getLinks/";         /* para a locaweb */
  //alert ("desviando para " + lEnd);
  $.post(lEnd,  
         p,
         retornoLinks
        );   
}
function retornoLinks(pRet)
{
  ret = unescape(pRet);
  //alert ("retornoLinks - str = " + ret);
  var aux = ret.split("^");
  if (aux[0] == undefined || aux[0] == "")
  {
    return;
  }  
  var LNKOutput = window.document.getElementById("divLinksSugeridos");
  LNKOutput.innerHTML += aux[1];
}
function stopclock()
{
  if (timerRunning);
  {
    clearTimeout(timerID);
    timerRunning=false;
  }  
}
function showtime()
{
  var now = new Date();
  var hours = now.getHours();
  var minutes = now.getMinutes();
  var seconds = now.getSeconds();
  var timeValue = "" + hours;
  timeValue += ((minutes < 10) ? ":0" : ":") + minutes;
  timeValue += ((seconds < 10) ? ":0" : ":") + seconds;
  window.document.getElementById("txtRelogio").value = timeValue;
    
  timerID = setTimeout("showtime()", 1000);
  timerRunning = true;
}  
function startclock()
{
  stopclock();
  showtime();
}

function doLogin()
{
  var lEnd = "", p = {};
  
  lEnd = gVic_BASEPATH + "/acessos/validaAcesso/";  

  if (!validaLogin())
  {
    return;
  }

  p["idLogin"]    = escape(window.document.getElementById("idLogin").value);
  p["senhaLogin"] = escape(window.document.getElementById("senhaLogin").value);
  //alert ("doLogin - Desviando para\n-lEnd = " + lEnd + "\n-p[idLogin] = " + p["idLogin"] + "\n-p[senhaLogin] = " + p["senhaLogin"]);  
  $.post(lEnd,
         p, 
         function(str)
         {
           str = unescape(str);
           //alert ("doLogin-retAjax\nstr = " + str);
           var aux = str.split("^");
           switch (aux[0])
           {
             case "OK":
               window.document.getElementById("vic_divMsgLogin").style.color      = "black";    
               window.document.getElementById("vic_divMsgLogin").style.fontweight = "normal";
               window.document.getElementById("vic_divMsgLogin").innerHTML        = "";                                       
               window.document.getElementById("senhaLogin").value                 = "";
               gJanEscVir = window.open("/system/application/html/escola.html", "janEscVir", gFea);               
               break;
             case "KO":
               //alert ("KO\n" + str);	 
               window.document.getElementById("vic_divMsgLogin").style.color      = "red";
               window.document.getElementById("vic_divMsgLogin").style.fontweight = "bold";
               window.document.getElementById("vic_divMsgLogin").innerHTML        = aux[1];             
               break;   
             case "W":
               //alert ("W\n" + str);            	 
               window.document.getElementById("vic_divMsgLogin").style.color      = "red";
               window.document.getElementById("vic_divMsgLogin").innerHTML        = aux[1];             
               break;   
             default:
               alert("default\n" + str);
               break;
           }    
         }
        );
}

function validaLogin()
{
  var lMsg = "";
  if (vic_vazio(window.document.getElementById("idLogin").value))
  {
    lMsg += "Id obrigatório";    
  }
  if (vic_vazio(window.document.getElementById("senhaLogin").value))
  {
    if (lMsg == "")
    {
      lMsg += "Senha obrigatória";
    }
    else
    {
      lMsg += "<br />Senha obrigatória";
    }
  }
  if (lMsg == "")
  {
    if (!vic_valLoginName(window.document.getElementById("idLogin").value))
    {
      lMsg += "<br />-Id inválido";    	
    }
    if (!vic_valSenha(window.document.getElementById("senhaLogin").value))
    {
      if (lMsg == "")
      {
        lMsg += "Senha deve conter apenas letras e números";    	  
      }
      else
      {
        lMsg += "<br />Senha deve conter apenas letras e números";
      }
    }
  }
          
  if (lMsg == "")
  {
    return true;
  }
  else
  {
    window.document.getElementById("vic_divMsgLogin").style.color      = "red";
    window.document.getElementById("vic_divMsgLogin").style.fontweight = "bold";
    window.document.getElementById("vic_divMsgLogin").innerHTML        = lMsg;    
    return false;
  }  
}
function vic_valLoginName(pLoginName)
{
  for (var i=0;i<pLoginName.length;i++)
  {
    if (!vic_isCha(pLoginName.substr(i, 1)))
    {
      if (pLoginName.substr(i, 1) != ".")
      {
        return false;
      }
    }
  }
  return true;
}
function vic_valSenha(pSenha)
{
	
  for (var i=0;i<pSenha.length;i++)
  {
    if (!vic_isCha(pSenha.substr(i, 1)))
    {
      if (!vic_isNum(pSenha.substr(i, 1)))
      {
        return false;
      }
    }
  }
  return true;
}
