var userLoged = false;

function pop_setLogged() {
	userLoged = true;
}
var isRegisterOnlyFlag = false;

function get_var(varname) {
	return window[varname];
}  

function set_var(obj){
	//console.log(obj);
	for(v in obj) {
		window[v]= obj[v];
	}
}

function set_paxData(obj) {
	paxData = obj;
}

var steps = [];	// [{funcName:args}]

function pop_next() {
	if(steps.length) {
		var next = steps.pop(); //
		for(var funcName in next) {
			var f = eval(funcName);
			f(next[funcName]);
		}
	}
}

function pop_nextLogged() {
	requestJSON({rq:'checkLogin'},(function(responseJSON,responseText) { 
		if(!responseJSON) {
			showPop('pop_auth');
		}else{
			pop_setLogged(); /// marca logueado
			pop_next();
		};})
	,showComError);
}

function showComError(){
	alert('error de coneccion');
}

function showGrayBackground(){
	$("capa_gris").set({
   		'styles': {
	   		display: 'block',
	   		opacity: 0.7}});

	if ((navigator.userAgent.toLowerCase().indexOf('msie 6') != -1) && (navigator.userAgent.toLowerCase().indexOf('msie 7') == -1) ) {  	//only for IE6
		$("capa_gris").setHeight = document.documentElement.clientHeight;
	}
}
function hideGrayBackground(){
	$("capa_gris").set({
   		'styles': {
	   		display: 'none',
	   		opacity: 0}});
}

function submitShowingWaitMsg(isValid,formId) {
	if(isValid) {
		showPop('pop_wait_only');
		$(formId).submit();
	}
}

function showPop(eid,params) {
	
	pop_clear();
	$(eid).set({
	   	'styles': {
	   		'top': 0,
	   		'left': 0,
	   		'width': '100%',
	   		'height': '100%',
	   		'display': 'block',
	   		'overflow': 'auto'}});
	 showGrayBackground();	 
}

function pop_clear(){
	$(document).getElements('div[id^=pop_]').each(function(e) {
		if(e.style.display == 'block') {
			hidePop(e);
		}
	});
}

function hidePop(id) {
	$(id).set({
	   	'styles': {
	   		'display': 'none'}});
	hideGrayBackground();
}


function requestJSON(data,onSuccessFunction,onFailureFunction) {
	var jsonRequest = new Request.JSON({
		url: 'http://' + document.location.hostname + '/xhttprequest/',
		onSuccess: onSuccessFunction,
		onFailure: onFailureFunction,
	}).get(data);
}


function getElementsByClassName(className,rootNode) {
    rootNode = rootNode? rootNode : document.body;
    var results = [];
    walkTheDOM(rootNode, function (node) {
        var a, c = node.className, i;
        if (c) {
            a = c.split(' ');
            for (i = 0; i < a.length; i += 1) {
                if (a[i] === className) {
                    results.push(node);
                    break;
                }
            }
        }
    });
    return results;
}
function walkTheDOM(node, func) {
    func(node);
    node = node.firstChild;
    while (node) {
        walkTheDOM(node, func);
        node = node.nextSibling;
    }
}
