var busy = false;
var resAjx = '';//resAjx.data1, resAjx.data2 ... es un objeto

//pude ser al devolver datos en ajax o bien al tratar de conectarse al servicio
var ajax_error = 'se produjo un error durante el proceso';
var ajax_busy = 'Error, no se ha a\u00f1adido al carrito.\nPor favor, presione la tecla F5 y vuelvalo a a\u00f1adirlo.\nNo se perder\u00e1n los dem\u00e1s productos a\u00f1adidos en su pedido.';

var ajx = {
    'http' : 0,
    'fncLoad' : '',
    'fncError' : '',
    'bind' : function(data){
        var info = eval(data);//url, load, error, mimetype, formNode
        var browser = navigator.appName;
        if(browser == "Microsoft Internet Explorer"){
            this.http = new ActiveXObject("Microsoft.XMLHTTP");
        }else{
            this.http = new XMLHttpRequest();
        }
        var url = info.url;
        if(info.formNode){
            url += this.getForm(info.formNode);
        }
        //prompt('var',url);
        this.fncLoad = info.load;
        this.fncError = info.error;
        this.http.open('get', url);
        this.http.onreadystatechange = this.execute;
        this.http.send(null);
    },
    'execute' : function(){
        if(ajx.http.readyState == 4){//si esta todo ok
            if (ajx.http.status == 200) {
                ajx.fncLoad(ajx.http.responseText);
            } else {
                ajx.fncError();
            }
        }
    },
	'send' : function(url,fnc){
		if(busy){
			this.msgBusy();
			return;
		}
		busy = true;
		if(this.send.arguments.length==2){
			conector = (url.indexOf('=')==-1)?'?':'&';
			url += conector+'function='+fnc;
		}
		//prompt('ver',url);
		this.bind({
			url: url,
			load: this.process,
			error: this.msgError,
			mimetype: "text/json"
		});
	},
	'sendForm' : function(url,fnc,form,htaccess){
		if(busy){
			this.msgBusy();
			return;
		}
		busy = true;
		if(htaccess == true){
      conector = '&';
    }else{
      conector = (url.indexOf('=')==-1)?'?':'&';
    }
		url += conector+'function='+fnc;
		//prompt('ver',url);
		this.bind({
			url: url,
			load: this.process,
			error: this.msgError,
			mimetype: "text/json",
			formNode: form
		});
	},
	'sendContent' : function(url,content,form){
		conector = (url.indexOf('=')==-1)?'?':'&';
		url += conector+'content='+content;
		if(this.sendContent.arguments.length==3){
			this.sendForm(url,'ajx.processContent',form);
		}else{
			this.send(url,'ajx.processContent');
		}
	},
    'sendRequest' : function(className,form,nfunction){
        if(!nfunction)nfunction = 'requestForm';
        //var url = 'response/obj='+className+'Ajx&nfunction='+nfunction;
        var url = 'response/obj='+className+'&nfunction='+nfunction;
        this.sendWin(url,form);
    },
    'sendWin' : function(url,form){
        //abre div pop up bloqueante con id=winPopUp
        wdw.open('winPopUp',300,400);
        this.sendContent(url,'winPopUp',form);
    },
	'process' : function(data){
        eval('rta = '+data);
        if(rta.content){
			resAjx = rta.data;
			eval("ajx.processContent('"+rta.content+"');");
			busy = false;
		}else{
			resAjx = rta.data;
			eval('busy = '+rta.func+'();');
		}
	},
	'processContent' : function(content){
        document.getElementById(content).innerHTML = resAjx;
    },
	'msgError' : function(){
		alert(ajax_error);
	},
	'msgBusy' : function(){
		alert(ajax_busy);
	},
	'msg' : function(){
        alert(this.http.readyState);
		return false;
	},
    'getForm' : function (nform){
        //var els = document.forms.insert.elements;
        var els = eval('document.forms.'+nform+'.elements');
        var len = els.length;
        var url = "";
        for (var i=0; i<len; i++) {
            var el = els[i];
            if (!el.disabled) {
                switch(el.type) {
                    case 'text': case 'password': case 'hidden': case 'textarea': 
                        url +='&v['+el.name+']='+el.value;
                        break;
                    case 'select-one':
                        if (el.selectedIndex>=0) {
                        url +='&v['+el.name+']='+el.options[el.selectedIndex].value;
                        }
                        break;
                    case 'select-multiple':
                        for (var j=0; j<el.options.length; j++) {
                            if (el.options[j].selected) {
                              url +='&v['+el.name+']='+el.options[j].value;
                            }
                        }
                        break;
                    case 'checkbox': case 'radio':
                        if (el.checked) {
                        url +='&v['+el.name+']='+el.value;
                        }
                        break;
                }
            }
        }
        return url;
    }
}
