

function GetXmlHttpObject()
{
	var objXMLHttp=null;
	
	if (window.XMLHttpRequest) {
		objXMLHttp=new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	return objXMLHttp;
}


function rps_filter_request(input)
{
	if (!input || (typeof(input) !== 'string')) { return ''; }
	var inputlc = input.toLowerCase();
	var body_tag_start = inputlc.indexOf('<body');
	var body_start = (-1 !== body_tag_start) ? inputlc.indexOf('>', body_tag_start) : 0;
	var body_start_index = (-1 == body_start) ? 0 : body_start + 1;
	
	var body_tag_end = inputlc.indexOf('</body>');
	var body_tag_end_index = (-1 === body_tag_end) ? inputlc.length : body_tag_end;
	
	return input.substring(body_start_index, body_tag_end_index);
	//var out =  (typeof(input) == 'string') ? input.replace(/^.*<body[^>]*>/im, '').replace(/<\/body>.*$/im, '') : '';
}


function rps_get_request(url, return_handler)
{
	var xmlHttp=GetXmlHttpObject();
	
	if (xmlHttp==null) {
		//alert ("Browser does not support HTTP Request");
		return;
	}
	
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
	xmlHttp.onreadystatechange = function()
		{
			try {
				if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
				{ 
					if (xmlHttp.status == 200) {
						return_handler(rps_filter_request(xmlHttp.responseText));
					} else {
						//alert('There was a problem with your request. Please contact your website administrator. Page not found error.');
					}
				}
			} catch(e) {
				//alert('There has been a problem while processing your request: ' + e.description);
			}
		}
}


function rps_set_fade(element, style, static_time, step_time)
{
	var fade = element.rpsFade = new Object;
	
	fade.style = style;
	fade.counter = fade.style.length - 1;
	fade.increment = -1;
	fade.step = step_time;
	fade.wait = static_time;
	
	fade.update = function(new_function)
		{
			fade.counter += fade.increment;
			var timeout;
			
			if (fade.counter < 0) {
				
				fade.counter = 0;
				fade.increment *= -1;
				fade.reset();
				
			} else {
				
				if (fade.counter >= fade.style.length) {
					
					fade.counter = fade.style.length - 1;
					fade.increment *= -1;
					timeout = fade.wait;
					
				} else {
					
					element.style.color = fade.style[fade.counter];
					timeout = fade.step;
					
				}
				
				window.setTimeout(function(){fade.update();}, timeout);
			}
		}
	
	return fade;
}


function rps_set_request_cache(element, url, element_update_handler)
{
	var request = element.rpsRequestCache = new Object;
	request.url = url;
	request.cache = new Array();
	request.index = 0;
	request.elementUpdateHandler  = element_update_handler;
	
	request.updateHandler  = function(response_text)
		{
			request.cache[request.index] = response_text;
			request.elementUpdateHandler(response_text);
		}
	
	request.update = function()
		{
			request.index = (request.index > (request.url.length - 2)) ? 0 : request.index + 1;
			
			if (request.cache[request.index] == null) {
				
				rps_get_request(request.url[request.index], request.updateHandler);
				
			} else if (request.cache[request.index]) {
				
				request.elementUpdateHandler(request.cache[request.index]);
				
			}
		}
	
	return request.update;
}


function rps_set_rotate_request_cache(element, url, interval)
{
	var update_function = rps_set_request_cache(element, url, function(response_text) { element.innerHTML = response_text;});
	
	window.setInterval(function(){update_function();}, interval);
}


function rps_set_rotate_request_cache_fade(element, url, style, wait, step)
{
	var fade = rps_set_fade(element, style, wait, step);
	
	var update_function = rps_set_request_cache(element, url, function(response_text) { element.innerHTML = response_text; fade.update(); });
	
	fade.reset = update_function;
	
	window.setTimeout(function(){update_function();}, wait);
}


