var cheight = 0;
var cwidth = 0;
var txtfocus = '';
var keyshift = '';
//window.addEventListener("resize", load_bg(), true);
//document.addEventListener('keydown', find_key_down, true);
//document.onkeydown = function ( event ) {event = event || window.event;}
//window.addEventListener("onkeydown", find_key_down(event), true);

if(document.addEventListener)
	{
	document.addEventListener('keydown', onkeydown_handler, true);
	document.addEventListener('resize', load_bg, false);
	} 
else if(document.attachEvent)
	{
	document.attachEvent('onkeydown', onkeydown_handler);
	document.attachEvent('onresize', load_bg);
	}
	
function onkeydown_handler(ev)
	{
	ev=ev || window.event; // for IE
	/*
	p = 80
	c = 67
	l = 76
	i = 73
	b = 66
	r = 82
	g = 71
	backspace = 8
	shit + = 16 then 107
	*/
	// 16 == shift
	if(ev.keyCode == 16 && txtfocus.length > 0) 
		{
		keyshift = txtfocus;
		//document.getElementById('article_keywords').value = keyshift;
		}
	//alert(ev.keyCode);
	if(ev.keyCode == 80 && txtfocus == 'article_body' && keyshift == 'article_body')
		{
		article_preview();
		keyshift = '';
		}
	// 13 == return
	if(ev.keyCode == 13 && (txtfocus == 'login_password' || txtfocus == 'search' || txtfocus == 'register_password')) 
		{
		if(txtfocus == 'login_password')
			{
			login_user();
			}
		if(txtfocus == 'register_password')
			{
			register_user();
			}
		if(txtfocus == 'search')
			{
			check_search();
			}
		}
	}

function redirect_front_page(int)
	{
	setTimeout('location.href="http://www.ichillicothe.com/"', int);
	}

function set_focus(id)
	{
	txtfocus = id;
	}
	
function unset_focus(id)
	{
	txtfocus = '';
	}

function load_bg()
	{
	if((cheight != document.documentElement.clientHeight) || (cwidth != document.documentElement.clientWidth) || cheight <= 0)
		{
		setTimeout('loading_bg();',100);
		}
	if(document.documentElement && document.documentElement.clientHeight > 0)
		{
		cheight = document.documentElement.clientHeight;
		cwidth =  document.documentElement.clientWidth;
		//alert(cheight);
		}
	else
		{
		cheight = document.body.clientHeight
		cwidth = document.body.clientWidth;
		}
	if(screen.availHeight)
		{
		cheight = screen.availHeight;
		cwidth = screen.availWidth;
		}
	/*
	if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
	*/
	}

function loading_bg()
    {
	//loads the $_SESSION['message']
	check_system_msg();
	if(document.getElementById('header'))
		{
		document.getElementById('header_bg').style.top = eval(document.getElementById('header').offsetTop + 5) + 'px';
		document.getElementById('header_bg').style.left = eval(document.getElementById('header').offsetLeft + 5) + 'px';
		document.getElementById('header_bg').style.height = document.getElementById('header').offsetHeight + 'px';
		document.getElementById('header_bg').style.width = document.getElementById('header').offsetWidth + 'px';
		document.getElementById('header_bg').style.display = 'block';
		}
	document.getElementById('mainbody').style.height = document.getElementById('mainbody').offsetTop + 10 + 'px';
	if(document.getElementById('columnc'))
		{
		if(document.getElementById('columna'))
			{
			var StartHeight=Math.max(document.getElementById('columna').offsetHeight,document.getElementById('columnb').offsetHeight,document.getElementById('columnc').offsetHeight);
			document.getElementById('mainbody').style.height = eval(StartHeight + 10) +'px';
			document.getElementById('columna').style.height = StartHeight + 'px';
			document.getElementById('columnb').style.minHeight = StartHeight + 'px';
			document.getElementById('columnc').style.height = StartHeight + 'px';
			document.getElementById('columna_bg').style.top = eval(document.getElementById('columna').offsetTop + 5) + 'px';
			document.getElementById('columna_bg').style.left = eval(document.getElementById('columna').offsetLeft + 5) + 'px';
			document.getElementById('columna_bg').style.height = document.getElementById('columna').offsetHeight + 'px';
			document.getElementById('columna_bg').style.width = document.getElementById('columna').offsetWidth + 'px';
			document.getElementById('columna_bg').style.display = 'block';
			}
		else
			{
			var StartHeight=Math.max(document.getElementById('columnb').offsetHeight,document.getElementById('columnc').offsetHeight);
			document.getElementById('mainbody').style.height = eval(StartHeight + 10) +'px';
			document.getElementById('columnb').style.minHeight = StartHeight + 'px';
			document.getElementById('columnc').style.height = StartHeight + 'px';
			}
		
		document.getElementById('columnb_bg').style.top = eval(document.getElementById('columnb').offsetTop + 5) + 'px';
		document.getElementById('columnb_bg').style.left = eval(document.getElementById('columnb').offsetLeft + 5) + 'px';
		document.getElementById('columnb_bg').style.minHeight = document.getElementById('columnb').offsetHeight + 'px';
		document.getElementById('columnb_bg').style.width = document.getElementById('columnb').offsetWidth + 'px';
		document.getElementById('columnb_bg').style.display = 'block';
		
		document.getElementById('columnc_bg').style.top = eval(document.getElementById('columnc').offsetTop + 5) + 'px';
		document.getElementById('columnc_bg').style.left = eval(document.getElementById('columnc').offsetLeft + 5) + 'px';
		document.getElementById('columnc_bg').style.height = document.getElementById('columnc').offsetHeight + 'px';
		document.getElementById('columnc_bg').style.width = document.getElementById('columnc').offsetWidth + 'px';
		document.getElementById('columnc_bg').style.display = 'block';
		}
	else if(document.getElementById('columnb'))
		{
		var StartHeight=document.getElementById('columnb').offsetHeight;
		document.getElementById('mainbody').style.height = eval(StartHeight + 10) +'px';
		document.getElementById('columnb').style.minHeight = StartHeight + 'px';
		document.getElementById('columnb_bg').style.top = eval(document.getElementById('columnb').offsetTop + 5) + 'px';
		document.getElementById('columnb_bg').style.left = eval(document.getElementById('columnb').offsetLeft + 5) + 'px';
		document.getElementById('columnb_bg').style.minHeight = document.getElementById('columnb').offsetHeight + 'px';
		document.getElementById('columnb_bg').style.width = document.getElementById('columnb').offsetWidth + 'px';
		document.getElementById('columnb_bg').style.display = 'block';
		}
	/*
    document.getElementById('body_bg').style.top = eval(document.getElementById('body').offsetTop + 5) + 'px';
    document.getElementById('body_bg').style.left = eval(document.getElementById('body').offsetLeft + 5) + 'px';
    document.getElementById('body_bg').style.height = document.getElementById('body').offsetHeight + 'px';
    document.getElementById('body_bg').style.width = document.getElementById('body').offsetWidth + 'px';
    document.getElementById('body_bg').style.display = 'block';
	*/
	if(document.getElementById('footer'))
		{
		document.getElementById('footer_bg').style.top = eval(document.getElementById('footer').offsetTop + 5) + 'px';
		document.getElementById('footer_bg').style.left = eval(document.getElementById('footer').offsetLeft + 5) + 'px';
		document.getElementById('footer_bg').style.height = document.getElementById('footer').offsetHeight + 'px';
		document.getElementById('footer_bg').style.width = document.getElementById('footer').offsetWidth + 'px';
		document.getElementById('footer_bg').style.display = 'block';
		}
    }

function load_search()
	{
	document.getElementById("msgbox").style.display='block';
	document.getElementById("msgbox_msg").innerHTML = 'Loading ' + document.getElementById("search_select").value + ' search...';
	center_msgbox();
	var xmlhttp=new XMLHttpRequest();
	xmlhttp.onreadystatechange= function() 
		{ 
		if (xmlhttp.readyState==4)
		if (xmlhttp.status==200)
			read_load_search(xmlhttp);
		}
	var myStr = "type=" + document.getElementById("search_select").value;
	xmlhttp.open("POST","/script_searchbox_load.php",true);
	xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	xmlhttp.send(myStr);
	}

function read_load_search(req) 
	{
	document.getElementById("search_box").innerHTML = req.responseText;
	close_msgbox();
	}
	
function load_search_result(q)
	{
	document.getElementById("msgbox").style.display='block';
	document.getElementById("msgbox_msg").innerHTML = 'Searching google.com for ' + q + '...';
	center_msgbox();
	check_search();

	/*
	var xmlhttp=new XMLHttpRequest();
	xmlhttp.onreadystatechange= function() 
		{ 
		if (xmlhttp.readyState==4)
		if (xmlhttp.status==200)
			read_load_search_result(xmlhttp);
		}
		//cx=partner-pub-6806779048771608:lj88e0-72oc&cof=FORID:10&ie=ISO-8859-1
	var myStr = "?cx=partner-pub-6806779048771608:lj88e0-72oc&cof=FORID:10&ie=ISO-8859-1&q="+document.getElementById('g_q').value;
	xmlhttp.open("GET","script_searchresults_load.php" + myStr,true);
	//xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; ISO-8859-1");
	xmlhttp.send(null);
	*/
	}
	
function read_load_search_result(req)
	{
	document.getElementById('google_search_results').innerHTML = req.responseText;
	document.getElementById("msgbox_msg").innerHTML = req.responseText.length;
	check_search();
	//close_msgbox();
	}
	
function check_search()
	{
	if(document.getElementById('search_select') && document.getElementById('search_select').value == 'google.com')
		{
		var lt = findXY('columnb');
		var top = lt.substring(eval(lt.indexOf(String.charCodeAt(255)) + 1)) ;
		var left = lt.substring(0,lt.indexOf(String.charCodeAt(255)));
		document.getElementById('google_search_results').style.top = top + 'px';
		document.getElementById('google_search_results').style.left = left + 'px';
		document.getElementById('google_search_results').style.height = eval(document.getElementById('columnb').offsetHeight) - 1 + 'px';
		document.getElementById('google_search_results').style.display = 'block';
		
		document.getElementById('google_search_results_bg').style.top = eval(document.getElementById('google_search_results').offsetTop + 5) + 'px';
		document.getElementById('google_search_results_bg').style.left = eval(document.getElementById('google_search_results').offsetLeft + 5) + 'px';
		document.getElementById('google_search_results_bg').style.height = document.getElementById('google_search_results').offsetHeight + 'px';
		document.getElementById('google_search_results_bg').style.width = document.getElementById('google_search_results').offsetWidth + 'px';
		document.getElementById('google_search_results_bg').style.display = 'block';
		document.getElementById('columnb_bg').style.display = 'none';
		document.getElementById('columnc_bg').style.display = 'none';
		}
	else
		{
		document.getElementById('columnb_bg').style.display = 'block';
		document.getElementById('columnc_bg').style.display = 'block';
		document.getElementById('google_search_results').style.display = 'none';
		document.getElementById('google_search_results_bg').style.display = 'none';
		location.href = "http://www.ichillicothe.com/search/?s="+document.getElementById('search').value;
		}
	}
    
function center_msgbox()
	{
	var lt = findXY('columnb');
	var top = lt.substring(eval(lt.indexOf('|') + 1)) ;
	var left = lt.substring(0,lt.indexOf('|'));
	var scrollH = findscrollY();
	//alert(scrollH);
	document.getElementById("msgbox").style.left = eval(left) + 85 + 'px';
	document.getElementById("msgbox").style.top = eval(top) + 50 + scrollH + 'px';
	document.getElementById('msgbox_bg').style.top = eval(document.getElementById('msgbox').offsetTop + 5) + 'px';
    document.getElementById('msgbox_bg').style.left = eval(document.getElementById('msgbox').offsetLeft + 5) + 'px';
    document.getElementById('msgbox_bg').style.height = document.getElementById('msgbox').offsetHeight + 'px';
    document.getElementById('msgbox_bg').style.width = document.getElementById('msgbox').offsetWidth + 'px';
	document.getElementById('msgbox_bg').style.zIndex = '10';
	document.getElementById('msgbox_bg').style.display = 'block';
	}

function fix_msg_bg()
	{
	document.getElementById('msgbox_bg').style.top = eval(document.getElementById('msgbox').offsetTop + 5) + 'px';
    document.getElementById('msgbox_bg').style.left = eval(document.getElementById('msgbox').offsetLeft + 5) + 'px';
    document.getElementById('msgbox_bg').style.height = document.getElementById('msgbox').offsetHeight + 'px';
    document.getElementById('msgbox_bg').style.width = document.getElementById('msgbox').offsetWidth + 'px';
	}
	
function close_msgbox()
    {
	document.getElementById('msgbox_bg').style.display='none';
	document.getElementById("msgbox_loading").style.display='none';
    document.getElementById("msgbox").style.display='none';
	document.getElementById("msgbox_msg").style.display='block';
	document.getElementById('msgbox_msg').innerHTML = '';
	document.getElementById('msgbox_body').innerHTML = '';
	document.getElementById('msgbox_title').innerHTML = '';
    }
	
function findscrollY()
	{
	var scrollH = 0;
	if(document.documentElement.scrollTop)
		{
		scrollH = document.documentElement.scrollTop;
		}
	else if(document.body.scrollTop)
		{
		scrollH = document.body.scrollTop;
		}
	else if(window.pageYOffset)
		{
		scrollH = window.pageYOffset;
		}
	else
		{
		scrollH = 0;
		}
	return scrollH;
	}
	
function findXY(id)
	{
	var obj = document.getElementById(id);
	var curleft = 0;
	var curtop = 0;
	if (obj.offsetParent)
		{
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent)
			{
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
			}
		}
	return curleft + '|' + curtop;
	}
	
function unsafe_string(id,x)
	{
	if(id.length <= 0 && x.length > 0)
		{
		x = x.replace(/\[NUM\]/g, "#");
		x = x.replace(/\[AMP\]/g, "&");
		x = x.replace(/\[Q\]/g, "'");
		x = x.replace(/\[QQ\]/g, '"');
		x = x.replace(/\[SC\]/g, ";");
		x = x.replace(/\[GT\]/g, ">");
		x = x.replace(/\[LT\]/g, "<");
		x = x.replace(/\[EQ\]/g, "=");
		return x;
		}
	else if(id.length > 0 && document.getElementById(id))
		{
		var myElem = document.getElementById(id).value;
		myElem = myElem.replace(/\[NUM\]/g, "#");
		myElem = myElem.replace(/\[AMP\]/g, "&");
		myElem = myElem.replace(/\[Q\]/g, "'");
		myElem = myElem.replace(/\[QQ\]/g, '"');
		myElem = myElem.replace(/\[SC\]/g, ";");
		myElem = myElem.replace(/\[GT\]/g, ">");
		myElem = myElem.replace(/\[LT\]/g, "<");
		myElem = myElem.replace(/\[EQ\]/g, "=");
		document.getElementById(id).value = myElem;
		}
	else
		{
		alert('Error Cleaning String');
		}
	}
	
function make_html_img(str, user, id)
	{
	var userdir = 'users/'+user+'/'+id+'/';
	var intNS = 0;
	var intS = 0;
	var intE = 0;
	var strimg = '';
	var strlink = '';
	var strnewlink = '';
	var strimgfull = '';
	var strreplace = '';
	
	while(intNS < str.length && str.indexOf('[/img]', intNS) > 0)
		{
		intS = str.indexOf('[img', intNS);
		intE = str.indexOf('[/img]', intS) + 6;	
		strimgfull = str.substring(intS, intE);
		strreplace = str.substring(intS, intE);
		intNS = intE;	
		intS = strreplace.indexOf(']]', 0) + 2;
		intE = strreplace.indexOf('[/img]', intS);	
		strimg = strreplace.substring(intS, intE);
		strreplace = strreplace.replace('\[img ', '<img ');
		strreplace = strreplace.replace('\[\/img\]', '" />');
		strreplace = strreplace.replace('\[align\]', 'align="');
		strreplace = strreplace.replace('\[\/align\]', '"');
		strreplace = strreplace.replace('\[alt\]', 'alt="');
		strreplace = strreplace.replace('\[\/alt\]', '"');
		strreplace = strreplace.replace('\[title\]', 'title="');
		strreplace = strreplace.replace('\[\/title\]', '"');
		if(strimg.indexOf('http://',0) > 0)
			{	
			strreplace = strreplace.replace(']', ' src="');
			}
		else
			{
			strreplace = strreplace.replace(']', ' src="'+userdir);
			}
		intcnt = 1;
		str = str.replace(strimgfull, strreplace);
		}
	intNS = 0;
	
	while(intNS < str.length && str.indexOf('[/url]', intNS) > 0)
		{
		intS = str.indexOf('[url=', intNS);
		intE = str.indexOf('[/url]', intS) + 6;	
		strimgfull = str.substring(intS, intE);
		strreplace = str.substring(intS, intE);
		intNS = intE;	
		
		intS = strreplace.indexOf(']', 0) + 1;
		intE = strreplace.indexOf('[/url]', intS);	
		
		strtext = strreplace.substring(intS, intE);
		intS = strreplace.indexOf('[url=', 0) + 5;
		intE = strreplace.indexOf(']', intS);
		
		strlink = strreplace.substring(intS, intE);
		strnewlink = '<a class="article_links" href="'+strlink+'">'+strtext+'</a>';
		str = str.replace(strimgfull, strnewlink);
		}

	str = str.replace(/\[url\=/gi, '<a class="article_links" href="');
	str = str.replace(/\[\/url\]/gi, '</a>');
	str = str.replace(/\[b\]/gi, '<strong>');
	str = str.replace(/\[\/b\]/gi, '</strong>');
	str = str.replace(/\[i\]/gi, '<i>');
	str = str.replace(/\[\/i\]/gi, '</i>');
	str = str.replace(/\[u\]/gi, '<u>');
	str = str.replace(/\[\/u\]/gi, '</u>');
	str = str.replace(/\[s\]/gi, '<s>');
	str = str.replace(/\[\/s\]/gi, '</s>');
	
	str = str.replace(/\[p\]/gi, '<p>');
	str = str.replace(/\[\/p\]/gi, '</p>');
	
	str = str.replace(/\[pre\]/gi, '<p>');
	str = str.replace(/\[\/pre\]/gi, '</p>');
	
	str = str.replace(/\[br\]/gi, '<br />');
	return str;
	}

function safe_string(id,x)
	{
	if(x)
		{
		if(id == '' && x.length >= 0)
			{
			x = x.replace(/#/g, "[NUM]");
			x = x.replace(/&/g, "[AMP]");
			x = x.replace(/'/g, "[Q]");
			x = x.replace(/"/g, '[QQ]');
			x = x.replace(/;/g, "[SC]");
			x = x.replace(/>/g, "[GT]");
			x = x.replace(/</g, "[LT]");
			x = x.replace(/=/g, "[EQ]");
			return x;
			}
		}
	else
		{
		x = '';
		return x;
		}
	if(id.length > 0 && document.getElementById(id))
		{
		var myElem = document.getElementById(id).value;
		myElem = myElem.replace(/#/g, "[NUM]");
		myElem = myElem.replace(/&/g, "[AMP]");
		myElem = myElem.replace(/'/g, "[Q]");
		myElem = myElem.replace(/"/g, '[QQ]');
		myElem = myElem.replace(/;/g, "[SC]");
		myElem = myElem.replace(/>/g, "[GT]");
		myElem = myElem.replace(/</g, "[LT]");
		myElem = myElem.replace(/=/g, "[EQ]");
		document.getElementById(id).value = myElem;
		}
	else
		{
		//alert('Error Cleaning String');
		}
	}
	
function get_weather()
	{
	
	document.getElementById('status_box').innerHTML = 'loading: weather...';
	document.getElementById("urweather").innerHTML = 'Loading Weather...';
	$.ajax({
		type: "POST",
		url: "/script_weather_popup.php",
		data: "weather=GET",
		cache: false,
		async: false,
		dataType: "html",
		success: function(req){
			//alert(req);
			var intS = req.indexOf("[current]") + 9;
			var intE = req.indexOf("[/current]");
			document.getElementById("urweather").innerHTML = req.substring(intS,intE);
			intS = req.indexOf("[wdata]") + 7;
			intE = req.indexOf("[/wdata]");
			document.getElementById("weatherdropdown").innerHTML = req.substring(intS,intE);
			document.getElementById('status_box').innerHTML = '';
			load_bg();
			}
		});
	/*
	var xmlhttp=new XMLHttpRequest();
	xmlhttp.onreadystatechange= function() 
		{ 
		if (xmlhttp.readyState==4)
		if (xmlhttp.status==200)
			read_weather(xmlhttp);
		}
	
	var myStr = "weather=GET";
	//xmlhttp.open("POST","/script_weather_popup_xml.php",true);
	xmlhttp.open("POST","/script_weather_popup.php",true);
	xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	xmlhttp.send(myStr);
	*/
	}
/*
function read_weather(req) 
	{
	var intS = req.responseText.indexOf("[current]") + 9;
	var intE = req.responseText.indexOf("[/current]");
	document.getElementById("urweather").innerHTML = req.responseText.substring(intS,intE);
	intS = req.responseText.indexOf("[wdata]") + 7;
	intE = req.responseText.indexOf("[/wdata]");
	document.getElementById("weatherdropdown").innerHTML = req.responseText.substring(intS,intE);
	document.getElementById('status_box').innerHTML = '';
	load_bg();
	//document.getElementById("urweather").innerHTML = req.responseText;
	//reading_errors(req);
	//center_msgbox();
	}
*/
	
function show_weather_popup(x)
	{
	var lt = findXY('urweather');
	var top = lt.substring(eval(lt.indexOf('|') + 1)) ;
	var left = lt.substring(0,lt.indexOf('|'));
	if(document.getElementById('weather_open').innerHTML == '+' || x == 0)
		{
		document.getElementById('weatherdropdown').style.top = eval(top) + 24 + 'px';
		document.getElementById('weatherdropdown').style.left = eval(left) + 0 + 'px';
		document.getElementById('weatherdropdown').style.zIndex = '4';
		document.getElementById('weatherdropdown').style.display = 'block';
		document.getElementById('weather_open').innerHTML = '-';
		}
	else
		{
		document.getElementById('weatherdropdown').style.display = 'none';
		document.getElementById('weather_open').innerHTML = '+';
		}
	}
