function addExtraToolbar(action, javascript, actionJavascript, image, alt, caption, position) {
    var divToolbar = document.getElementById('divToolbar');

    var newToolbar = '<div class=\'item_fer\'><a ';
    if ( javascript ) {
        newToolbar = newToolbar + 'onclick="'+actionJavascript+'"';
    }

    newToolbar = newToolbar + 'href="'+ action +'">';
    newToolbar = newToolbar + '<img src="'+ image +'" alt="'+ alt +'" />';
    newToolbar = newToolbar + '<br>' + caption + '</div></a>';

    if ( position == 'before' ) {
        divToolbar.innerHTML = divToolbar.innerHTML + newToolbar;
    } else {
        divToolbar.innerHTML = newToolbar + divToolbar.innerHTML;
    }
}

function submitForm() {
    document.forms[0].submit();
}

function doDelete(linkElement,idField) {
    if (confirm('Confirma a exclusao?')) {
        var id = document.getElementsByName(idField);
        var f = document.createElement('form');

        document.body.appendChild(f);

        f.method = 'POST';
        f.action = linkElement.href+'/'+idField+'/'+id[0].value;
        f.submit();
    }

    return false;
}

function doDeleteCustomMessage(linkElement,idField,message) {
    if(message.length <= 0){
        message = 'Confirma a exclusao?';
    }

    if (confirm(message)) {
        var id = document.getElementsByName(idField);
        var f = document.createElement('form');

        document.body.appendChild(f);

        f.method = 'POST';
        f.action = linkElement.href+'/'+idField+'/'+id[0].value;
        f.submit();
    }

    return false;
}

function doAction( linkElement, idField ) {
    var id = document.getElementsByName( idField );
    var f = document.createElement( 'form' );

    document.body.appendChild( f );

    f.method = 'POST';
    f.action = linkElement.href+'/'+idField+'/'+id[0].value;
    f.submit();

    return false;
}

function doDeleteGrid(linkElement,idField) {
    var grid = jQuery( '#obj_gridbox' );
    var rows = grid.flexReturnSelected();

    if ( rows === '' ) {
        window.alert('Selecione um registro para exclusao');

        return false;
    }

    if (confirm('Confirma a exclusao?')) {
        if ( rows === '' ) {
            return false;
        }

        var id = grid.cells( rows, 0 );	
        var f = document.createElement('form');

        document.body.appendChild(f);

        f.method = 'POST';
        f.action = linkElement.href + '/' + idField + '/' + id;
        f.submit();
    }
	
    return false;
}

function doDeleteAjax(linkElement,idField,gridbox) {
    if( gridbox ){
        var grid = jQuery( '#' + gridbox );
    } else {
        var grid = jQuery( '#obj_gridbox' );
    }
	
    var rows = grid.flexReturnSelected();
	
    if( rows == '' ){
        window.alert('Selecione um registro para exclusao');

        return false;
    }
    if (confirm('Confirma a exclusao?')) {
        if ( rows === '' ) {
            return false;
        }

        var id = grid.cells( rows, 0 );

        var handlerFunc = function(t) {
            var responseText = t.responseText;

            responseText = responseText.split('|');

            if ( responseText.length > 0 && responseText[0] == 'error' ) {
                alert('Nao foi possivel excluir o registro\n\nErro:\n' + responseText[1]);
            } else {
                grid.flexReload();
            }
        };
			
        var errFunc = function(t) {
            alert('Nao foi possivel excluir o registro\n\nErro:\n' + t.responseText);
        };

        var url_ajax = linkElement.href + '/' + idField + '/' + id + '&ajax=1';

        new Ajax.Request(url_ajax, {
            asynchronous:true,
            evalScripts:false,
            onSuccess:handlerFunc,
            onFailure:errFunc
        });
    }
	
    return false;
}

function doEdit(linkElement,idField, gridbox, newTab, modulo, idCell) {
    if(typeof idCell == 'undefined') {
        idCell = 0;
    }

    if( gridbox ){
        var grid = jQuery( '#' + gridbox );
    } else {
        var grid = jQuery( '#obj_gridbox' );
    }
	
    var rows = grid.flexReturnSelected();
	
    if( rows == '' ){
        alert('Selecione um registro!');
        return false;
    }

    if( linkElement.href == undefined ) {
        var link = linkElement;
    } else {
        var link = linkElement.href;
    }

    var id = grid.cells( rows, idCell );
    var url = link + '/' + idField + '/'+ id;
	
    //verifica se o modulo é diferente do modulo corrente
    var er = new RegExp("\/"+ modulo +"\/");
    if(modulo != '' && modulo != undefined){
        if(!er.test(location.href)){
            url = location.href.substring(0,location.href.indexOf(".php") + 4);
            url+= "/" + modulo + '/' + link + '/' + idField + '/'+ id;
        }
    }

    if( newTab ) {
        window.open( url, '_blank' );
    } else {
        var f = document.createElement('form');

        document.body.appendChild(f);

        f.method = 'POST';
        f.action = url;
        f.submit();
    }
}

function doEditForm(linkElement,idField) {
    var id = document.getElementsByName(idField);
    var f = document.createElement('form');

    document.body.appendChild(f);

    f.method = 'POST';
    f.action = linkElement.href+'/'+idField+'/'+id[0].value;
    f.submit();
}

function doSubmitFormAjax(urlFrom,urlTo,form,func) {
    new Ajax.Request(urlFrom,{
        asynchronous:true,
        evalScripts:false,
        onComplete:function(request, json){
            updateJSON(request,json,urlTo,form,func);
        },
        parameters:Form.serialize(document.forms[form])
        });

    return false;
}

function doRedirect(urlFor,idField, load, usesElement ) {
    var grid = jQuery( '#obj_gridbox' );

    if( usesElement ){
        var id = document.getElementsByName( idField )[0].value;
    } else {
        var rows = grid.flexReturnSelected();

        if( rows == '' ){
            alert('Selecione um registro!');
            return false;
        }

        var id = grid.cells( rows, 0 );
    }

    f = document.createElement('form');

    document.body.appendChild(f);

    f.method = 'POST';
    f.action = urlFor+'/'+idField+'/'+id;
    f.submit();
	
}

function getHotspotLoginsReport() {
    var date = new Date();
    var today = date.getFullYear() + "-" + (date.getMonth()+1) + "-" + date.getDate();
    var link = $('link').value;

    if($('hostname')) {
        var hostname = $('hostname').innerHTML;
        if(hostname == '')
            window.alert('Esse hotspot esta sem o hostname. Por favor verifique!');
    } else {
        window.alert('Essa entidade ainda nao somente um prospect!');

        return false;
    }

    var url =  link + 'hotspot/loginsSuccess?NasIdentifier=' + hostname + '&iniDate=' + today + '&endDate=' + today;
    window.location = url;
}

var allowChangeToolbar = true;

function changeTitleToolbar( name ) {
    if ( allowChangeToolbar == true ) {
        document.getElementById('module-label').innerHTML = name;
    }
}

function openHotelUserReport() {
    var id = document.getElementById( 'idHotelUserTransaction' ).value;
    var url = '../../userReport?idHotelUserTransaction=' + id;
    var url_ajax = '../../endUserAccess?idHotelUserTransaction=' + id + '&isAjax=true';
	
    var handlerFunc = function(t) {
        var responseText = t.responseText;
        responseText = responseText.split('|');

        if ( responseText.length > 0 && responseText[0] == 'error' ) {
            alert('Nao foi possivel finalizar a transacao. Tente novamente\n\nErro:\n' + responseText[1]);
        } else {
            window.open( url, "connectedEventUsers", "location=0,status=1,scrollbars=1,width=830,height=640" );
            window.location = '../../../hotel_manager';
        }
        Element.hide('indicator');
    };
			
    var errFunc = function(t) {
        Element.hide('indicator');
        alert('Nao foi possivel excluir o registro\n\nErro:\n' + t.responseText);
    };

    Element.show('indicator');
    new Ajax.Request(url_ajax, {
        asynchronous:true,
        evalScripts:false,
        onSuccess:handlerFunc,
        onFailure:errFunc
    });
}

function doDeleteAjaxButton(link,idField,gridbox) {
    jQuery('div#indicator').first().show();

    if( gridbox ){
        var grid = jQuery( '#' + gridbox );
    } else {
        var grid = jQuery( '#obj_gridbox' );
    }

    var rows = grid.flexReturnSelected();

    if( rows == '' ){
        window.alert('Selecione um registro para exclusao');

        jQuery('div#indicator').first().hide();

        return false;
    }
    if (confirm('Confirma a exclusao?')) {
        if ( rows === '' ) {
            return false;
        }

        var id = grid.cells( rows, 0 );

        var handlerFunc = function(t) {
            jQuery('div#indicator').first().show();

            var responseText = t.responseText;

            responseText = responseText.split('|');
            
            if ( responseText.length > 0 && (responseText[0] == 'error' || responseText[0] == '1' )) {            	
                alert('Nao foi possivel excluir o registro\n\nErro:\n' + responseText[1]);
            } else {
                grid.flexReload();
            }
            jQuery('div#indicator').first().hide();   
        };

        var errFunc = function(t) {
            jQuery('div#indicator').first().hide();
            alert('Nao foi possivel excluir o registro\n\nErro:\n' + t.responseText);
        };

        var url_ajax = link + '/' + idField + '/' + id + '&ajax=1';
        new Ajax.Request(url_ajax, {
            asynchronous:false,
            evalScripts:false,
            onSuccess:handlerFunc,
            onFailure:errFunc
        });
    }

    jQuery('div#indicator').first().hide();

    return false;
}

function submitFormWiredClient() {
	
	jQuery().ajaxStart(function() { jQuery('#indicator').show(); });
    jQuery.post(jQuery('input#pathCheckFields').val()+'?'+ document.forms[0].serialize(),
     {
          
     },
      function(data) {
          if(data != '0'){                
        	  alert(data);
        	  return false;
          }else{
        	  document.forms[0].submit();
          }
      });
     jQuery().ajaxStop(function() { jQuery('#indicator').hide(); });	
    
}

function doChangeStatusAjax(linkElement,idField,gridbox) {
    if( gridbox ){
        var grid = jQuery( '#' + gridbox );
    } else {
        var grid = jQuery( '#obj_gridbox' );
    }
	
    var rows = grid.flexReturnSelected();
	
    if( rows == '' ){
        window.alert('Selecione um registro para modificação');
        return false;
    }
        if ( rows === '' ) {
            return false;
        }

        var id = grid.cells( rows, 0 );

        var handlerFunc = function(t) {
            var responseText = t.responseText;
            responseText = responseText.split('|');
            if ( responseText.length > 0 && responseText[0] == 'error' ) {
                alert('Nao foi possivel modificar o registro\n\nErro:\n' + responseText[1]);
            } else {
                grid.flexReload();
            }
        };
			
        var errFunc = function(t) {
            alert('Nao foi possivel modificar o registro\n\nErro:\n' + t.responseText);
        };

        var url_ajax = linkElement.href + '/' + idField + '/' + id ;

        new Ajax.Request(url_ajax, {
            asynchronous:true,
            evalScripts:false,
            onSuccess:handlerFunc,
            onFailure:errFunc
        });
	
    return false;
}
