var queObj = new function() {
	this.ajax = new Ajax();
	this.activeQ = null;
	this.activeA = null;
	this.tim = null;
	this.timeoutTime = 100;
	
	this.getAdvertisers = function(q_id, a_id, cat_id) {
		queObj.activeA = a_id;
		queObj.activeQ = q_id;
		this.ajax.setMimetype="text/html";
		this.ajax.responseFormat="text";
		this.ajax.doGet("/includes/questionnaire/src/index.php?xaction=get+advertisers"+
			"&cat_id="+cat_id+
			"&q_id="+q_id,
			queObj.finishAdvertisers, "text"); 
	}; 
	
	this.finishAdvertisers = function(ajax_text) {
		var container_name = "adv_"+queObj.activeQ;
		if(ajax_text.length > 0) {
			var c = document.getElementById(container_name);
			c.innerHTML=ajax_text;
			
			queObj.fade(container_name, "in");
		}
	};
	
	this.fade = function(cont_id, direction) {
		var c = document.getElementById(cont_id);
		
		var currentLevel = c.className.substr(14, 1);
		
		var done = true;
		if(direction == "in" && currentLevel < 9) {
			currentLevel++;
			done = false;
		} else if(direction == "out" && currentLevel > 0) {
			currentLevel--;
			done = false;	
		}
		
		if(!done) {
			c.className="adv_container_"+currentLevel;
			queObj.tim = setTimeout("queObj.fade('"+cont_id+"', '"+direction+"')", queObj.timeoutTime);
		}
	};
	
	this.hideAdvertisers = function(q_id) {
		var cont_id = "adv_"+q_id;
		queObj.fade(cont_id, "out");
		return false;
	};
	
	this.toggleAdvertiser = function(adv_id, bool) {
		this.ajax.setMimetype="text/html";
		this.ajax.responseFormat="text";
		this.ajax.doGet("/includes/questionnaire/src/index.php?xaction=toggle+advertiser"+
			"&bool="+bool+
			"&adv_id="+adv_id,
			queObj.doNothing, "text"); 
		
	};
	
	this.doNothing = function(ajax_text) {
	
	};
	
	this.browseAdv = function(adv_id) {
		this.ajax.setMimetype="text/html";
		this.ajax.responseFormat="text";
		this.ajax.doGet("/includes/questionnaire/src/index.php?xaction=get+advertiser"+
			"&adv_id="+adv_id,
			queObj.finishAdvertiser, "text"); 
		return false;
	};
	
	this.finishAdvertiser = function(ajax_html) {
		var c = document.getElementById("adv_browse");
		c.innerHTML = ajax_html;
		
		queObj.positionBoxes();
		
		c.style.display = "block";
		var c = document.getElementById("adv_backdrop");
		c.style.display = "block";
		var c = document.getElementById("adv_browse_box");
		c.style.display = "block";
		
		window.onscroll = queObj.positionBoxes;
	};
	
	this.hideAdvertiser = function() {
		var c = document.getElementById("adv_browse");
		c.style.display = "none";
		var c = document.getElementById("adv_browse_box");
		c.style.display = "none";
		var c = document.getElementById("adv_backdrop");
		c.style.display = "none";
		
		window.onscroll = null;
		
		return false;
	};
	
	this.positionBoxes = function() {
		var scrollInfo = {
			"vert":Geometry.getVerticalScroll(),
			"horiz":Geometry.getHorizontalScroll(),
			"width":Geometry.getViewportWidth(),
			"height":Geometry.getViewportHeight()
		  };
	  
		var c = document.getElementById("adv_backdrop");
		c.style.top = scrollInfo["vert"]+"px";
		c.style.left = scrollInfo["horiz"]+"px";
		c.style.width = (scrollInfo["width"]-17)+"px";
		c.style.height = scrollInfo["height"]+"px";
		
		var c = document.getElementById("adv_browse_box");
		c.style.top = (Math.floor((scrollInfo["height"]-430)/2) + scrollInfo["vert"]) + "px";
		c.style.left = (Math.floor((scrollInfo["width"]-330)/2) + scrollInfo["horiz"]) + "px";
		
		var c = document.getElementById("adv_browse");
		c.style.top = (Math.floor((scrollInfo["height"]-400)/2) + scrollInfo["vert"]) + "px";
		c.style.left = (Math.floor((scrollInfo["width"]-300)/2) + scrollInfo["horiz"]) + "px";
	};
	
	this.submitForm = function(fe) {
		var qstring = "&page="+fe.page.value;
		var ids = fe.elements['ids'].value.split(",");
		for(var i = 0; i < ids.length; i++) {
			for(var j = 0; j < fe.elements['questions['+ids[i]+']'].length; j++) {
				if(fe.elements['questions['+ids[i]+']'][j].checked) {
					qstring += "&questions["+ids[i]+"]="+fe.elements['questions['+ids[i]+']'][j].value;
				}
			}
		}
		
		for(var i = 0; i < fe.elements.length; i++) {
			if(fe.elements[i].name.substring(0, 10) == "quest_tbox") {
				qstring += "&"+fe.elements[i].name+"="+escape(fe.elements[i].value);
			}
		}
		
		this.ajax.setMimetype="text/html";
		this.ajax.responseFormat="text";
		this.ajax.doGet("/includes/questionnaire/src/index.php?xaction=submit"+
			qstring,
			queObj.finishSubmit, "text"); 
	};
	
	this.finishSubmit = function(ajax_html) {
		var c = document.getElementById("questionnaire_container");
		c.innerHTML = ajax_html;
	};
}

var Geometry = {};

if(window.screenLeft) { // IE and others
	Geometry.getWindowX = function() { return window.screenLeft; };
	Geometry.getWindowY = function() { return window.screenTop; };
}
else if(window.screenX) { // Firefox and others
	Geometry.getWindowX = function() { return window.screenX; };
	Geometry.getWindowY = function() { return window.screenY; };
}

if(window.innerWidth) { 
	Geometry.getViewportWidth = function() { return window.innerWidth; };
	Geometry.getViewportHeight = function() { return window.innerHeight; };
	Geometry.getHorizontalScroll = function() { return window.pageXOffset; };
	Geometry.getVerticalScroll = function() { return window.pageYOffset; };
}
else if(document.documentElement && document.documentElement.clientWidth) {
	// These functions are for IE6 when there is a DOCTYPE
	Geometry.getViewportWidth = function() { return document.documentElement.clientWidth; };
	Geometry.getViewportHeight = function() { return document.documentElement.clientHeight; };
	Geometry.getHorizontalScroll = function() { return document.documentElement.scrollLeft; };
	Geometry.getVerticalScroll = function() { return document.documentElement.scrollTop; };
}
else if(document.body.clientWidth) { 
	// These are for IE4, IE5, and IE6 without a DOCTYPE
	Geometry.getViewportWidth = function() { return document.body.clientWidth; };
	Geometry.getViewportHeight = function() { return document.body.clientHeight; };
	Geometry.getHoriztontalScroll = function() { return document.body.scrollLeft; };
	Geometry.getVerticalScroll = function() { return document.body.scrollTop; };
}
