/**
 * @author charly
 */
//********************************************************************************  
var fecha = function(){
    var fecha = new Date()
    var mes = new Array('Enero','Febrero','Marzo','Abril','Mayo','Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre');
    document.getElementById("date").value = fecha.getDate()+'-'+mes[fecha.getMonth()]+'-'+fecha.getFullYear();
}

//********************************************************************************  


function TeclaPresionada(e){
    var key = window.event ? e.keyCode : e.which;
    return key;
}
//********************************************************************************
function myDataTableToSpanish(myDataTable){
    myDataTable.set('MSG_EMPTY','Ninguna entrada');
    myDataTable.set('MSG_LOADING','Cargando...');
    myDataTable.set('MSG_ERROR','Error');
}
//********************************************************************************  

function animateRow(elRow){
    var origColor = YAHOO.util.Dom.getStyle(elRow.cells[0], "backgroundColor");
    var pulseColor = "#ff0";

    var rowColorAnim = new YAHOO.util.ColorAnim(elRow.cells, {
        backgroundColor: {
            to: origColor,
            from: pulseColor
        },
        duration: 2
    });
    var onComplete = function(){
        rowColorAnim = null;
        YAHOO.util.Dom.setStyle(elRow.cells, "backgroundColor", "");
    }
    rowColorAnim.onComplete.subscribe(onComplete);
    rowColorAnim.animate();
}
//********************************************************************************
var getValue = function(varname){
    var url = window.location.href;
    var qparts = url.split("?");
    if (qparts.length == 0) {
        return "";
    }
    var query = qparts[1];
    var vars = query.split("&");
    var value = "";
    for (i = 0; i < vars.length; i++) {
        var parts = vars[i].split("=");
        if (parts[0] == varname) {
            value = parts[1];
            break;
        }
    }
    value = unescape(value);
    value.replace(/\+/g, " ");
    return value;
};

//********************************************************************************  
function encode_utf8( s )
{
    return unescape( encodeURIComponent( s ) );
}
//********************************************************************************  
function decode_utf8( s )
{
    return decodeURIComponent( escape( s ) );
}
//********************************************************************************  
var waitTime = function(millis){
    var date = new Date();
    var curDate = null;
    
    do {
        curDate = new Date();
    }
    while (curDate - date < millis);
};

//********************************************************************************  

var one2two = function(elId1, elId2){
    var m1 = document.getElementById(elId1);
    var m2 = document.getElementById(elId2);
    m1len = m1.length;
    for (i = 0; i < m1len; i++) {
        if (m1.options[i].selected == true) {
            m2len = m2.length;
            m2.options[m2len] = new Option(m1.options[i].text, m1.options[i].value);
        }
    }
    for (i = (m1len - 1); i >= 0; i--) {
        if (m1.options[i].selected == true) {
            m1.options[i] = null;
        }
    }
}

//********************************************************************************  

var two2one = function(elId1, elId2){
    var m1 = document.getElementById(elId1);
    var m2 = document.getElementById(elId2);
    ;
    m2len = m2.length;
    for (i = 0; i < m2len; i++) {
        if (m2.options[i].selected == true) {
            m1len = m1.length;
            m1.options[m1len] = new Option(m2.options[i].text);
        }
    }
    for (i = (m2len - 1); i >= 0; i--) {
        if (m2.options[i].selected == true) {
            m2.options[i] = null;
        }
    }
}

//********************************************************************************  

function createCookie(name, value, days){
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else 
        var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

//********************************************************************************  

function readCookie(name){
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') 
            c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) 
            return c.substring(nameEQ.length, c.length);
    }
    return null;
}

//********************************************************************************  

function eraseCookie(name){
    createCookie(name, "", -1);
}



//******************************************************************************** 
function llenarListaCatalogo(arreglo, listaEl, seleccion){
    var campo = document.getElementById(listaEl);
    for(var j=0;j<arreglo.length;j++){
        campo.options[j] = new Option(arreglo[j].label,arreglo[j].value);
        if (seleccion == arreglo[j].value) {
            campo.options[j].selected = true;
        }
    }
}
//******************************************************************************** 
function getNombresArreglo(arreglo){
    var regreso = new Array();
    for (var j = 0; j < arreglo.resultados.length; j++) {
        regreso[j] = arreglo.resultados[j].nombre;
    }
    return regreso;
}

//******************************************************************************** 

function getSeleccionArreglo(arreglo, indice){
    var regreso = new Array();
    for (var j = 0; j < arreglo.resultados.length; j++) {
        if(arreglo.resultados[j].ide == indice){
            regreso = arreglo.resultados[j];
        }
    }
    return regreso;
}

//******************************************************************************** 
function ajax_getDatos(servicioWeb){
    //var httpRequest = GetXmlHttpObject();
    var xmlHttp;
    if (window.XMLHttpRequest){
        xmlHttp=new XMLHttpRequest();
    }else{
        // Internet Explorer 5/6
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlHttp.open('GET', servicioWeb, false);
    xmlHttp.send(null);
    return (JSON.parse(xmlHttp.responseText));
}
//******************************************************************************** 
function ajax_saveDatos(datos,servicioWeb){
    var xmlHttp;
    if (window.XMLHttpRequest){
        xmlHttp=new XMLHttpRequest();
    }else{
        // Internet Explorer 5/6
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlHttp.open('POST', servicioWeb, false);
    xmlHttp.send(datos);
    /*httpRequest.onreadystatechange = function(){
	if (httpRequest.readyState == 1) {
      	}
        else 
            if (httpRequest.readyState == 4) {
                if (httpRequest.status == 200) {
  					return true;	
                }
                else 
                    if (httpRequest.status == 404) {
                        alert("La página no existe");
                    }
                    else {
                        alert(httpRequest.status);
                    }
            }
	}*/
    //return YAHOO.lang.JSON.parse(httpRequest.responseText)
    //return (JSON.parse(httpRequest.responseText));
    return xmlHttp;
}


