/* JavaScript Source Code
 * Copyright(C) 2009 by Origen Bureau
 * Copyright: See COPYING file that comes with this distribution
 * Author: juan.jimenez@origenbureau.com
 */

function menu_hover(obj){
    obj.style.color="#9D6214";
}//function

function menu_out(obj){
    obj.style.color="#FFFFFF";
}//function

function createRequestObject(){
    var rqt;
    if(navigator.appName == "Microsoft Internet Explorer")
        rqt = new ActiveXObject("Microsoft.XMLHTTP");
    else
        rqt = new XMLHttpRequest();
    return rqt;
}//function

function requestPage(url,obj,method,values) {
    if((url==null)||(obj==null)||(method==null)||(url=="")||(method==""))
        return;

    method = method.toUpperCase();
    if(method!="POST" && method!="GET")
        return;

    var http = createRequestObject();
    if(http && http.readyState!=0 && http.readyState!=4){
        return;
    }//if
    http.open(method,url,true);

    http.onreadystatechange = function(){
        if(http.readyState == 4)
            obj.innerHTML = http.responseText;
        else
            obj.innerHTML="<span style='color:#9d6214;'>Loading...</span>";
    }//function
    http.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    http.send(values);
    return;
}//function

function displayContent(url){
    requestPage(url,document.getElementById('data'),'GET');
    requestPage('bannerheader.php',document.getElementById('bannerheader'),'GET');
    requestPage('bannerright.php',document.getElementById('bannerright'),'GET');
}//function

//DOJO FUNCTIONS
function requestDojoPage(urlx, formx) {
    dojo.xhrPost( {
    // The following URL must match that used to test the server.
    //url: "http://server/ajax.txt",
    url: urlx,
    form: formx,
    handleAs: "text",

    timeout: 5000, // Time in milliseconds

    // The LOAD function wills be called on a successful response.
    load: function (data) {
      dijit.byId('content').attr('content',data);
    },

    // The ERROR function will be called in an error case.
    error: function(response, ioArgs) { 
        console.error("HTTP status code: ", ioArgs.xhr.status);
        dojo.byId("content").innerHTML = response;
        return response;
    }
    });
}//function

function gridAction(url,dispatch,idindex){
    var row = dijit.byId('grid').selection.getSelected();
    if(row.length <= 0 && dispatch!='add' && dispatch!='list'){
        alert('Seleccione un registro.');
        return;
    }//if

    var values='dispatch='+dispatch;
    
    if(dispatch == 'delete'){
        if(!confirm('Confirme que desea eliminar los registro(s) seleccionado(s).'))
            return;
        for(var i=0; i<row.length; i++)
            values+='&id='+dijit.byId('grid').model.data[row[i]][idindex];
    }else if(dispatch == 'edit'){
        if(row.length > 1){
            alert('Seleccione solamente un registro a editar.');
            return;
        }//if
        values+='&id='+dijit.byId('grid').model.data[row[0]][idindex];
    }//if
    requestDojoPage(url+'?'+values);
}//function

function submitUserForm(){
    if(dijit.byId('mainForm').validate())
        if(dijit.byId('pass').value == dijit.byId('repass').value)
            requestDojoPage('','mainForm');
        else
            alert("Las contraseñas no son iguales.");
    else
        alert('El formulario contiene datos inválidos.');
}//function

function submitAccountForm(){
    if(dijit.byId('dialogForm').validate())
        if(dijit.byId('newpass').value == dijit.byId('renewpass').value){
            //dijit.byId('dialog1').hide();
            requestDojoPage('','dialogForm');
            return;
        }else
            alert("Las contraseñas no son iguales.");
    else
        alert('El formulario contiene datos inválidos.');

    dijit.byId('dialogForm').reset()
    dijit.byId('oldpass').focus();
}//function

function addTableRow(tbl,args){
    var table = document.getElementById(tbl);
    var row = table.insertRow(table.rows.length);
    row.id=table.rows.length-1;
    var cell;
    for(var i=0; i<args.length; i++){
        cell = row.insertCell(i);
        cell.innerHTML=args[i];
        //cell.align="center";
    }//for
    //row.style.backgroundColor="#DDDDDD";
    dojo.parser.parse(table);
}//function

function deleteTableRow(tbl,row){
    var table = document.getElementById(tbl);
    table.deleteRow(document.getElementById(row).rowIndex);
}//function


function reportColor(selectedColor){
    dojo.byId('colorpick').value=selectedColor;
}