﻿function getCookie(str, name) {
    var start = str.indexOf(name + "=");
    var len = start + name.length + 1;
    if ((!start) && (name != str.substring(0, name.length))) return null;
    if (start == -1) return null;
    var end = str.indexOf(";", len);
    if (end == -1) end = str.length;
    return str.substring(len, end);
}

function SomenteNumero(e) 
{
    var tecla = (window.event) ? event.keyCode : e.which;
    if ((tecla > 47 && tecla < 58)) return true;
    else {
        if (tecla == 8 || tecla == 0) return true;
        else return false;
    }    
}

function getCookiePropValue(cookieName, propertyName) {
    str = document.cookie;

    var cookieStr = "";

    while ((cookieStr = getCookie(str, cookieName)) != null) {

        if (cookieStr.match(propertyName + '=') != null)
            break;
        str = str.substring(str.indexOf(cookieName + "=") + cookieName.length + 1, str.length);

    }

    if (cookieStr == null)
        return null;
    value = cookieStr.substring(cookieStr.indexOf(propertyName + '=') + propertyName.length + 1, cookieStr.length);

    if (value.match("&"))
        value = value.substring(0, value.indexOf("&"));

    return value;
}

function decodeTransposition(nick) {
    var result = new String();
    for (var i = 0; i < nick.length; i++) {
        var c = nick.charCodeAt(i);
        (c % 2 == 1) ? c++ : c--;
        result += String.fromCharCode(c);
    }
    return result;
}

function getName() {
    var nick = getCookiePropValue("SyncrohomeP", "Nick", true);
    var name = nick;
    if (name == null || name.length == 0)
        name = getCookiePropValue("SyncrohomeP", "FnUsr", true);
    if (name == null || name.length == 0)
        return "";
    return decodeTransposition(unescape(name));
}

function getCustId() {
    var custId = getCookiePropValue("SyncrohomeS", "CI", false);
    return custId;
}

function isLogged(custId) {
    logged = false;
    for (i = 0; i < custId.length; i++) {
        if (((custId[i] > 'A') && (custId[i] < 'Z')) || ((custId[i] > 'a') && (custId[i] < 'z'))) {
            return true;
        }
    }
    return false;
}

function genWelcomeMessage(nick) {
    hours = new Date().getHours();
    w = "Bom Dia";
    if (hours >= 12)
        w = "Boa Tarde";
    if (hours >= 18)
        w = "Boa Noite";
    message = "";
    if (getName().length == 0)    // nao logado.
        message = w + '. Fa&ccedil;a o login para ver sua <a href="http://www.Syncrohome.com.br/meuSyncrohome/default.aspx" class="welcomeuser"target="_parent"><u>p&aacute;gina personalizada</u></a>';
    else
        message = 'Ol&aacute;' + ', <strong>' + nick + '</strong>' + '. Confira sua <a href="http://www.Syncrohome.com.br/meuSyncrohome/default.aspx" class="welcomeuser"target="_parent"><u>p&aacute;gina personalizada.</u></a> <span class=txtlogout>( <a href="https://www2.Syncrohome.com.br/login.aspx?logout="target="_parent" class=txtlogout><u> N&atilde;o &eacute; voc&ecirc; ?</u></a> )</span>';
    return message;

    //Modificado por Marcelo Mendes 25/03/2008 - Para atender chamado 86652 (Mensagem bugando Layout em 800x600)


}

function getDateStr() {
    var dayArray = new Array("Domingo", "Segunda", "Ter&ccedil;a", "Quarta", "Quinta", "Sexta", "S&aacute;bado");
    d = new Date();
    return dayArray[d.getDay()] + ', ' + ((d.getDate() > 9) ? '' : '0') + d.getDate() + '/' + ((d.getMonth() > 9) ? '' : '0') + (d.getMonth() + 1) + '/' + (d.getFullYear());
}

function welcomeInit() {
    document.getElementById('dateuser').innerHTML = getDateStr();
    document.getElementById('welcomeuser').innerHTML = genWelcomeMessage(getName());
}

