var path = "/mnt/target02/350622/www.barnesville.org/web/content/";
//var path = "http://www.barnesville.org/";
var xmlHttpSearch;
var IE, FF;
if(window.ActiveXObject){
	FF = 0;
	IE = 1;
}else if(window.XMLHttpRequest){
	IE = 0;
	FF = 1;
}


//This is essential for the AJAX functionality
function getXmlHttpObject()
{
	var objxmlHttp=null;
	if (FF)     // Object of the current windows
	{
	    objxmlHttp = new XMLHttpRequest();     // Firefox, Safari, ...
	}
	else if (IE)   // ActiveX version
	{
	    objxmlHttp = new ActiveXObject('Microsoft.XMLHTTP');  // Internet Explorer
	}
	return objxmlHttp;
}

function FCKeditor_OnComplete( editorInstance ){
	editorInstance.EditorDocument.body.style.cssText += "width:300px;"
}

window.onload = function(){
	var footDiv = document.getElementById('footDiv');
	if(footDiv != undefined){
		alert('here');
	}
}

function checkAll(start, length, prefix){
	if(prefix == undefined){
		prefix = 'selection';
	}
	var one = new Image();
	one.src = "/" + "images/CheckBox_down.gif";
	for(var i = (start + 1); i <= (start + length); i++){
		if(document.getElementById(prefix + i) != undefined){
			document.getElementById(prefix + i).src = one.src;
		}
	}
}

function uncheckAll(start, length, prefix){
	if(prefix == undefined){
		prefix = 'selection';
	}
	var two = new Image();
	two.src = "/" + "images/CheckBox_up.gif";
	for(var i = (start + 1); i <= (start + length); i++){
		if(document.getElementById(prefix + i) != undefined){
			document.getElementById(prefix + i).src = two.src;
		}
	}
}


function uncheckallCMS(index){
	for(var i = 1; i < 5; i++){
		if(i != index){
			//alert(i);
			document.getElementById('logOpt' + i).checked = false;
		}
	}
}

function CMSpage(showLength, top, fileName, prefix, query){
	xmlHttpSearch = getXmlHttpObject();
	if(prefix == undefined || prefix == 'list' || prefix == ""){
		prefix = "";
		if(fileName == undefined){
			fileName = "admin/cmsNews.php";
		}
		if(query == undefined && document.getElementById(prefix +'query') != undefined){
			var query = document.getElementById(prefix + 'query').value;
		}else if(query == undefined){
			var query = "";
		}
		if(top == -3){
			top = document.getElementById(prefix + 'pageOpt').options[document.getElementById(prefix + 'pageOpt').selectedIndex].value;  
		}
		var url = "/" + fileName + "?show" + prefix + "Length=" + showLength + "&" + prefix + "top=" + top + "&" + prefix + "query=" + query;
		//alert(url);
	}else{
		//alert(prefix);
		if(top == -3){
			top = document.getElementById(prefix + 'pageOpt').options[document.getElementById(prefix + 'pageOpt').selectedIndex].value;  
		}
		var memtop = (prefix == 'mem')?top:document.getElementById('memtop').value;
		var cattop = (prefix == 'sub')?top:document.getElementById('cattop').value;
		var subcattop = (prefix == 'subcat')?top:document.getElementById('subcattop').value;
		var showmemLength = (prefix == 'mem')?showLength:document.getElementById('showmemLength').value;
		var showcatLength = (prefix == 'cat')?showLength:document.getElementById('showcatLength').value;
		var showsubcatLength = (prefix == 'subcat')?showLength:document.getElementById('showsubcatLength').value;
		var subcatquery = (query != undefined && prefix == 'subcat')?query:document.getElementById('subcatquery').value;
		var catquery = (query != undefined && prefix == 'cat')?query:document.getElementById('catquery').value;
		var memquery = (query != undefined && prefix == 'mem')?query:document.getElementById('memquery').value;
		var url = "/" + fileName + "?showmemLength=" + showmemLength + "&memtop=" + memtop + 
		"&memquery=" + memquery + "&cattop=" + cattop + "&showcatLength=" + showcatLength +
		"&catquery=" + catquery + "&subcattop=" + subcattop + "&showsubcatLength=" + showsubcatLength + 
		"&subcatquery=" + subcatquery;
		//alert(url);	
	}
	xmlHttpSearch.onreadystatechange=CMSreturn;
	xmlHttpSearch.open('GET', url, true);
	xmlHttpSearch.send(null);
}

function CMSreturn(){
	if( xmlHttpSearch.readyState == 4 || xmlHttpSearch.readyState == 'complete'){
		//alert((xmlHttpSearch.responseText).substr((xmlHttpSearch.responseText).length-1000));
		document.getElementById('items').innerHTML = xmlHttpSearch.responseText;		
	}
}

function switchProd(id){
	var one = new Image();
	one.src = "/" + "images/CheckBox_up.gif";
	var two = new Image();
	two.src = "/" + "images/CheckBox_down.gif";
	var tech = document.getElementById(id);
	if(tech.src == one.src){
		tech.src = two.src;
	}else if(tech.src = two.src){
		tech.src = one.src;
	}	
}

function updateProds(){
	var top = document.getElementById('top').value;
	var showLength = document.getElementById('showLength').value;
	var query = document.getElementById('query').value;
	xmlHttpSearch = getXmlHttpObject();
	var url = "/"+ "cmsFeatured.php?showLength=" + showLength + "&top=" + top + "&query=" + query;
	xmlHttpSearch.onreadystatechange=CMSreturn;
	xmlHttpSearch.open('GET', url, true);
	xmlHttpSearch.send(null);
}
	
function makeEdit(id){
	document.getElementById(id).style.display = 'none';
	document.getElementById(id + 'b').style.display = 'block';
	document.getElementById(id + 'b').focus();
}

function unmakeEdit(id, fileName, source, prefix){
	//alert(id + '-' + fileName + '-' + source + '-' + prefix);
	if(prefix == undefined){
		prefix = "";
	}
	var one = document.getElementById(prefix + id);
	var two = document.getElementById(prefix + id + 'b');
	/*if(one == undefined){
		alert("*" + prefix + id);
	}else if(two == undefined){
		alert("**" + id + 'b');
	}else if(document.getElementById(id + 'id') == undefined){
		alert("***" + id + 'id');
	}*/
	var ids = document.getElementById(id + 'id').value;
	var index = document.getElementById(id + 'index').value;
	if(one.innerHTML != two.value){
		xmlHttpSearch = getXmlHttpObject();
		var url = "/" + fileName + "?action=EDIT&list=" + (two.value).replace("'", "\'") + "&prefix=" + prefix + "&id=" + ids + "&source=" + source + "&index=" + index;
		xmlHttpSearch.onreadystatechange=function(){
		if( xmlHttpSearch.readyState == 4 || xmlHttpSearch.readyState == 'complete'){
				if(document.getElementById(id + 'outer') == undefined){
					//alert(id);
				}
				//alert(xmlHttpSearch.responseText);
				document.getElementById(id + 'outer').innerHTML = xmlHttpSearch.responseText;
			}			
		}
		xmlHttpSearch.open('GET', url, true);
		xmlHttpSearch.send(null);	
	}
	one.style.display = 'block';
	two.style.display = 'none';
}

function makeImgEdit(divId, id){
	document.getElementById(divId).innerHTML = "<iframe id='addFrame' src='upload.php?index=1&id=" + id + "&prefix=logos' width='140' height='60' frameborder='0' scrolling='no'></iframe>";
}

function editMember(id){
	xmlHttpSearch = getXmlHttpObject();
	var url = "/" + "admin/cmsDirectory.php?action=BLOCK&id=" + id;
	xmlHttpSearch.onreadystatechange=function(){
		if( xmlHttpSearch.readyState == 4 || xmlHttpSearch.readyState == 'complete'){
			var floater = document.getElementById('floater');
			floater.innerHTML = xmlHttpSearch.responseText;
			floater.className = "floater";
			floater.style.display = "block";
		}
	}
	xmlHttpSearch.open('GET', url, true);
	xmlHttpSearch.send(null);	
}

function makeConn(id, level){
	xmlHttpSearch = getXmlHttpObject();
	var url = "/" + "admin/cmsDirectory.php?action=CONNECT&id=" + id + "&level=" + level;
	xmlHttpSearch.onreadystatechange=function(){
		if( xmlHttpSearch.readyState == 4 || xmlHttpSearch.readyState == 'complete'){
			var floater = document.getElementById('floater');
			//alert(xmlHttpSearch.responseText);
			floater.innerHTML = xmlHttpSearch.responseText;
			floater.className = "floater";
			floater.style.display = "block";
		}
	}
	xmlHttpSearch.open('GET', url, true);
	xmlHttpSearch.send(null);
}

function sealConn(from, level, count){
	var pathes = "";
	var cat = -1;
	if(count == -1){
		count = document.getElementById('memcount').value;
		var cat = document.getElementById('cat').options[document.getElementById('cat').selectedIndex].value;  
	}
	for(var i = 1; i <= count; i++){
		if(document.getElementById('sel' + i).checked == true){
			pathes += "&sel" + i + "=" + (document.getElementById('sel' + i).value);
		}else{
			pathes += "&sel" + i + "=-" + (document.getElementById('sel' + i).value);
		}
	}
	xmlHttpSearch = getXmlHttpObject();
	var url = "/" + "admin/cmsDirectory.php?action=MAKECONNECT&id=" + from + "&level=" + level + "&cat=" + cat + "&count=" + count + pathes;
	//alert(url);
	xmlHttpSearch.onreadystatechange=function(){
		if( xmlHttpSearch.readyState == 4 || xmlHttpSearch.readyState == 'complete'){
			var floater = document.getElementById('floater');
			floater.innerHTML = xmlHttpSearch.responseText;
			floater.className = "floater";
			floater.style.display = "none";
		}
	}
	xmlHttpSearch.open('GET', url, true);
	xmlHttpSearch.send(null);
}

function loadsubs(id){
	var cat = document.getElementById('cat').options[document.getElementById('cat').selectedIndex].value;  
	xmlHttpSearch = getXmlHttpObject();
	var url = "/" + "admin/cmsDirectory.php?action=CONNECT&id=" + cat + "&second=true&mem=" + id;
	xmlHttpSearch.onreadystatechange=function(){
		if( xmlHttpSearch.readyState == 4 || xmlHttpSearch.readyState == 'complete'){
			var floater = document.getElementById('memsubdiv');
			floater.innerHTML = xmlHttpSearch.responseText;
		}
	}
	xmlHttpSearch.open('GET', url, true);
	xmlHttpSearch.send(null);
}

function changeMem(fileName, id){
	var user = document.getElementById('memName').value;
	var add = document.getElementById('memAddress').value;
	var city = document.getElementById('memCity').value;
	var state = document.getElementById('memState').value;
	var zip = document.getElementById('memZip').value;
	var phone = document.getElementById('memPhone').value;
	var email = document.getElementById('memEmail').value;
	var urlInfo = document.getElementById('memWebsite').value;
	var company = document.getElementById('memOrg').value;
	var top = document.getElementById('memtop').value;
	var memlength = document.getElementById('showmemLength').value;
	var memquery = document.getElementById('memquery').value;
	var url = "/" + fileName + "?action=MAKECHANGE&memtop=" + top + "&showmemLength=" + memlength + "&memquery=" + memquery + "&id=" + id + "&user=" + user + "&add=" + add + "&city=" + city + "&state=" + state + "&zip=" + zip + "&phone=" + phone + "&email=" + email + "&url=" + urlInfo + "&company=" + company;
	xmlHttpSearch.onreadystatechange=CMSreturn;
	xmlHttpSearch.open('GET', url, true);
	xmlHttpSearch.send(null);	
}

function callUpSubs(){
	xmlHttpSearch = getXmlHttpObject();
	var url = "/" + "pages/Member Directory.php?trying=true&up=new";
	xmlHttpSearch.onreadystatechange=DirectoryReturn;
	xmlHttpSearch.open('GET', url, true);
	xmlHttpSearch.send(null);	
}

function callCatsD(id){
	xmlHttpSearch = getXmlHttpObject();
	var url = "/" + "pages/Member Directory.php?trying=true&cat=" + id;
	xmlHttpSearch.onreadystatechange=DirectoryReturn;
	xmlHttpSearch.open('GET', url, true);
	xmlHttpSearch.send(null);	
}

function callSubsD(id, catId){
	xmlHttpSearch = getXmlHttpObject();
	var url = "/" + "pages/Member Directory.php?trying=true&subcat=" + id + "&cat=" + catId;
	xmlHttpSearch.onreadystatechange=DirectoryReturn;
	xmlHttpSearch.open('GET', url, true);
	xmlHttpSearch.send(null);	
}

function callMem(id, catId, subId){
	xmlHttpSearch = getXmlHttpObject();
	var url = "/" + "pages/Member Directory.php?trying=true&mem=" + id + "&subcat=" + subId + "&cat=" + catId;
	xmlHttpSearch.onreadystatechange=DirectoryReturn;
	xmlHttpSearch.open('GET', url, true);
	xmlHttpSearch.send(null);
}

function DirectoryReturn(){
	if( xmlHttpSearch.readyState == 4 || xmlHttpSearch.readyState == 'complete'){
		//alert((xmlHttpSearch.responseText).substr(1000));
		document.getElementById('memOuter').innerHTML = xmlHttpSearch.responseText;		
	}
}

function deleteItem(id, fileName, query, prefix){
	if(prefix == undefined){
		prefix = "cat";
	}
	//alert(query);
	xmlHttpSearch = getXmlHttpObject();
	if(prefix != 'list'){
		var showLength = document.getElementById('show' + prefix + 'Length').value;
		var top = document.getElementById(prefix + 'top').value;
		var url = "/" + fileName + "?action=DELETE&id=" + id + "&show" + prefix + "Length=" + showLength + "&" + prefix + "top=" + top + "&" + prefix + "query=" + query + "&source=" + prefix;
	}else{
		var showLength = document.getElementById('showLength').value;
		var top = document.getElementById('top').value;
		var url = "/" + fileName + "?action=DELETE&id=" + id + "&showLength=" + showLength + "&top=" + top + "&query=" + query + "&source=" + prefix;	
	}
	//alert(url);
	xmlHttpSearch.onreadystatechange=CMSreturn;
	xmlHttpSearch.open('GET', url, true);
	xmlHttpSearch.send(null);
}

function sortItems(query, fileName, prefix){
	if(prefix == undefined){
		prefix = "";
	}
	var showLength = document.getElementById('show' + prefix + 'Length').value;
	if(document.getElementById(prefix + 'top') == undefined){
		//alert(prefix + 'top');
	}
	var top = document.getElementById(prefix + 'top').value;
	xmlHttpSearch = getXmlHttpObject();
	var url = "/" + fileName + "?action=SORT&" + prefix + "query=" + query + "&show" + prefix + "Length=" + showLength + "&" + prefix + "top=" + top;
	xmlHttpSearch.onreadystatechange=CMSreturn;
	xmlHttpSearch.open('GET', url, true);
	xmlHttpSearch.send(null);
}

function deleteLists(fileName, query, prefix){
	//alert("*" + fileName + "*" + query + "*" + prefix + "*");
	
	var one = new Image();
	one.src = "/" + "images/CheckBox_down.gif";
	if(prefix == 'list'){
		var showLength = document.getElementById('showLength').value;
	}else{
		var showLength = document.getElementById('show' + prefix + 'Length').value;
	}
	if(prefix == 'list'){
		var top = document.getElementById('top').value;
	}else{
		var top = document.getElementById(prefix + 'top').value;
	}	
	for(var i = (parseInt(top) + 1); i <= (parseInt(top) + parseInt(showLength)); i++){
		//if(document.getElementById('selection' + prefix + i) == undefined){
			if(document.getElementById('selection' + prefix + i).src == one.src){
				if(prefix == ""){
					var id = document.getElementById("news" + i + 'id').value;
				}else{
					var id = document.getElementById(prefix + i + 'id').value;
				}
				deleteItem(id, fileName, query, prefix);
			}
		//}else{
		//	alert('*selection' + prefix + i + "*");
		//}
	}
}

function createNews(fileName, query){
	var news = FCKeditorAPI.GetInstance('newsInput');
	news = news.GetXHTML( true );
	news = news.replace("&", "and");
	xmlHttpSearch = getXmlHttpObject();
	var title = document.getElementById('newsTitleInput').value;
	title = title.replace("&", "and");
	if(document.getElementById('showLength') == undefined){
		alert('showLength');
	}
	if(document.getElementById('top') == undefined){
		alert('top');
	}
	var showLength = document.getElementById('showLength').value;
	var top = document.getElementById('top').value;
	var url = "/" + fileName + "?action=ADD&news=" + news + "&title=" + title + "&showLength=" + showLength + "&top=" + top + "&query=" + query;
	//alert(url);
	xmlHttpSearch.onreadystatechange=CMSreturn;
	xmlHttpSearch.open('GET', url, true);
	xmlHttpSearch.send(null);
}

function createCat(fileName, query, prefix){
	if(prefix == undefined){
		prefix = 'cat';
	}
	var cat = document.getElementById(prefix + 'Text').value;
	cat = cat.replace('&', 'and');
	cat = cat.replace("'", "\'");
	xmlHttpSearch = getXmlHttpObject();
	var showLength = document.getElementById('show' + prefix + 'Length').value;
	var top = document.getElementById(prefix + 'top').value;
	var url = "/" + fileName + "?action=ADD&" + prefix + "=" + cat + "&show" + prefix + "Length=" + showLength + "&" + prefix + "top=" + top + "&" + prefix + "query=" + query;
	xmlHttpSearch.onreadystatechange=CMSreturn;
	xmlHttpSearch.open('GET', url, true);
	xmlHttpSearch.send(null);
}

function createListing(fileName, query){
	var duties = FCKeditorAPI.GetInstance('jobduties');
	duties = duties.GetXHTML( true );
	var location = document.getElementById('joblocation').value;
	var salary = document.getElementById('jobsalary').value;
	var listing = document.getElementById('joblisting').value;
	var date = document.getElementById('jobdate').value;
	var contactInfo = document.getElementById('jobcontactInfo').value;
	xmlHttpSearch = getXmlHttpObject();
	if(document.getElementById('showlistLength') == undefined){
		alert('showlistLength');
	}
	if(document.getElementById('listtop') == undefined){
		alert('listtop');
	}
	var showLength = document.getElementById('showlistLength').value;
	var top = document.getElementById('listtop').value;
	var url = "/" + fileName + "?action=ADD&listing=" + listing + "&location=" + location + "&salary=" + salary + "&date=" + date + "&contactInfo=" + contactInfo + "&showlistLength=" + showLength + "&duties=" + duties + "&listtop=" + top + "&listquery=" + query;
	//alert(url);
	xmlHttpSearch.onreadystatechange=CMSreturn;
	xmlHttpSearch.open('GET', url, true);
	xmlHttpSearch.send(null);
}

function login(){
	var username = document.getElementById('user').value;
	var password = document.getElementById('pass').value;
	xmlHttpSearch = getXmlHttpObject();
	var url = "/admin/login.php?action=LOGIN&username=" + username + "&password=" + password;
	//alert(url);
	xmlHttpSearch.onreadystatechange=loginReturn;
	xmlHttpSearch.open('GET', url, true);
	xmlHttpSearch.send(null);
}

function logout(){
	document.getElementById('loginBox').style.display = 'block';
	document.getElementById('logoutBox').style.display = 'none';			
	document.getElementById('items').innerHTML = "";
	document.getElementById('logOpt').checked = false;
}

function loginReturn(){
	if( xmlHttpSearch.readyState == 4 || xmlHttpSearch.readyState == 'complete'){
		var res = xmlHttpSearch.responseText;
		if(res == 'Pass'){
			document.getElementById('loginBox').style.display = 'none';
			document.getElementById('logoutBox').style.display = 'block';
			uncheckallCMS(0);
			document.getElementById('logOpt1').click();
			CMSpage(3, 0, "admin/cmsNews.php");
		}else if(res == 'Fail'){
			document.getElementById('loginBox').style.display = 'block';
			document.getElementById('logoutBox').style.display = 'none';		
		}else{
			alert('oops login' + res);
		}
	}
}

function parseMembers(prefix, url, itemS){ //m.`user`
	var let = document.getElementById('parse' + prefix).options[document.getElementById('parse' + prefix).selectedIndex].value;
	var showLength = document.getElementById('show' + prefix + 'Length').value;
	var query = document.getElementById(prefix + 'query').value;
	if(let == 'All'){
		var sortQuery = "";
	}else{
		var sortQuery = "AND (LEFT(" + itemS + ", 1) = '" + let.toUpperCase() + "' OR LEFT(" + itemS + ", 1) = '" + let.toLowerCase() + "') GROUP BY " + itemS;
	}
	//alert(sortQuery);
	CMSpage(showLength, 0, url, prefix, sortQuery);
}