﻿function $obj(id) {
    return document.getElementById(id);
}

Array.prototype.contains = function(element) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == element) {
            return true;
        }
    }
    return false;
}

Array.prototype.posicao = function(element) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == element) {
            return i;
        }
    }
    return false;
}

var PAGINA = 0;
var TPAGINA = 0;
function pgAnterior() {
    if (PAGINA > 0) {
        PAGINA--;
        PageMethods.pgAnterior(PAGINA, OnSucceeded, OnFailed);
    }
}

function pgProxima() {
    if (PAGINA < TPAGINA - 1) {
        PAGINA++;
        PageMethods.pgProxima(PAGINA, OnSucceeded, OnFailed);
    }
}

function TFotos() {
    PageMethods.TFotos(OnSucceeded, OnFailed);
}

function enviaRecado() {
    if ($obj('txtRecado').value != '') {
        PageMethods.enviaRecado($obj('txtRecado').value, OnSucceeded, OnFailed);
    } else {
        alert('Você não escreveu nenhum recado');
    }
}

function OnSucceeded(result, userContext, methodName) {
    if (methodName == "pgAnterior" || methodName == "pgProxima") {
        var item;
        if (result.length > 0) {
            $obj('recados').innerHTML = '';
        }
        for (cc = 0; cc < result.length; cc++) {
            item = '<div class="comentario_mural">' +
                '<p>' + result[cc].Nome + ' diz:</p>' +
                '<span>' + result[cc].Recado + '</span>' +
                '</div>';
            $obj('recados').innerHTML += item;
        }
    }
    if (methodName == "enviaRecado") {
        $obj('txtRecado').value = '';
        alert('Recado enviado com sucesso. Antes de ser liberado será analisado por nossa equipe.');
    }
}

function OnFailed(error, userContext, methodName) {
    if (methodName == "pgAnterior" || methodName == "pgProxima" ||
        methodName == "enviaRecado") {
        alert(error.get_message());
    }
}

if (typeof (Sys) !== "undefined") Sys.Application.notifyScriptLoaded();

function addEvent(obj, evType, fn) {
    if (obj.addEventListener) {
        obj.addEventListener(evType, fn, false);
    } else if (obj.attachEvent) {
        obj.attachEvent("on" + evType, fn);
    } else {
        window.onload = fn;
    }
}
//addEvent(window, 'load', INI);
//addEvent(window, 'load', CarregaINI);
//window.onload = function() { INI(); CarregaINI(); };

function CarregaINI() {
    PAGINA--;
    pgProxima();
}

function pageLoad() {
    INI();
    CarregaINI();
    try {
        CIDADE();
    } catch (e) { }
}

function limitar(obj){
    var tecla = window.event.keyCode;
    var res = new RegExp("","g");  
    var x = obj.value.replace(res,"").length;
    if((x >= 500) && ((tecla > 33 && tecla < 255) || (tecla > 95 && tecla < 106)) && (tecla != 13)){
      if(obj.erro){
        alert(obj.erro);
      }
      window.event.returnValue=false;      
    }
    // tratamento para o texto colado
    if((obj.value.length >= 500) && (window.event.type == 'paste')){
      var texto = obj.value;
      obj.value = "";
      obj.value = texto.slice(0, obj.maxLength - 1);
    } 
  }  