// JavaScript Document

google.load("jquery", "1.3.2");


//API AJAX
var TimeOutError;
function AJAX(method, url, data, div, title){
	var m='POST';
    var inMethod=method.toUpperCase();
    if(method){
    	if(inMethod=='POST')
        	m='POST';
        else if(inMethod=='GET')
        	m='GET';
    }
    dataSend='';
    //ShowStatus('LoadQuery',true, title);
    if(data){dataSend=data;}
    $.ajax({
            url: url,
            type: m,
            cache: false,
            data: dataSend,
            async:true,
            timeout: 10000,
            dataType: 'html',
			beforeSend: function(objeto){
            	//ShowStatus('LoadQuery',true, title);
	        },
            success: function(rx){
                if(!(isUndefined(document.getElementById(div).type))){
                    var type = document.getElementById(div).type.toLowerCase();
                    if(type=='textarea'||type=='text'||type=='select-one')
                        {document.getElementById(div).value=rx;
                    }
                }
                else {
                    if(document.getElementById(div)){
                        var replace=document.getElementById(div);
                        replace.innerHTML=rx;
                
                    }
                    if(document.getElementById('CallBackJava')) {
                        var divID = document.getElementById(div)
                        var x = divID.getElementsByTagName('script');
                        var agt=navigator.userAgent.toLowerCase();
                        
                        for(var i=0;i<x.length;i++){
                            if(x[i].text!=''){
                                var code = (x[i].text); 
                                if (!!(window.attachEvent&&!window.opera)){
                                	window.execScript(code);}
                                else{
                                	if(agt.indexOf("safari") != -1){
                                    	window.setTimeout(code,0);}
                                    else{
		                                window.eval(code);
                                    }
                                }
                            }
                        }
                    }
                }
                if(document.getElementById(div)){
                    document.getElementById(div).style.display='block';
                    document.getElementById(div).style.visibility='visible';
                }
                ShowStatus('LoadQuery',false);
            },
            error: function(objeto, quepaso, otroobj){
            	//ShowStatus('LoadQuery',false);
            	ShowError('Error de comunicación','CODIGO: '+quepaso + '                 ');
        	}
    });
}

function ShowError(title,err, w){
	var width  = 'auto';
	if(w) { var width  = w} else { var width  = 'auto';}
    if(TimeOutError){clearTimeout(TimeOutError);}
    if($.browser.msie){
    	if(w){width = w}
        else{width  = 400}
    }

    $(function() {
        $("#dialog").html(err)
        $("#dialog").dialog({ 
        	closeOnEscape: true,
            resizable: true,
            title: title, 
            modal: true,
            width: width,
            height: 'auto',
            close: function(event, ui) { $("#dialog").dialog("destroy")}
        });
    });    
}

