var root = '/lgshort/includes/';
var IE = document.all?true:false;
var makeVars = new Array("type","id","className","innerHTML");
var RefreshID = 0;
var ls_ajaxIsLocked = false;
var ls_ajaxQueue = new Array();

function logout(){Qjax.push('ajaxReturn', root+'cms.ajax.php', 'action=logout');setTimeout("location.reload(true);",1000);}
function siteManager(section){window.open(root+"site.manager.php?section="+section,"Site Manager","menubar=no,width=800,height=620,toolbar=no,location=no,scrollbars=yes");}
function postComment(user, txt, id){Qjax.push('postComment', root+'cms.ajax.php', 'action=postComment&username='+user+'&text='+txt+'&cid='+id+'&ctable=content');}
function consolidateFields(){
	get("postType").value = get("loadModuleDropdown").value;
	if(get("postNewsletter").checked){
		if(!confirm("Are you sure you want to email this to all subscribers?"))
			return false;
	}
	return true;
}
function openManager(val){switch(val){case "site":siteManager();break;case "inventory":inventoryManager();break;}}
function inventoryManager(){window.open(root+"inventory.manager.php","Inventory Manager","menubar=no,width=800,height=620,toolbar=no,location=no,scrollbars=yes");}
//site admin functions//
function importFiles(n){Qjax.push(n, root+'admin.ajax.php', 'action=importFiles&secid='+get('secid').value);}
function swapSectionEdit(val){Qjax.push("main", root+"admin.ajax.php", "action=loadSection&id="+val);}
function changeFolder(path,sec,sorted,order){Qjax.push("files", root+"admin.ajax.php", "action=changeFolder&path="+path+"&id="+sec+"&sort="+sorted+"&order="+order);}
function saveSection(val,title,url,seq){Qjax.push("main", root+"admin.ajax.php", "action=saveSection&id="+val+"&title="+title+"&sequence="+seq+"&url="+url);}
function deleteSection(val){if(confirm("Are you sure you want to delete this section?  Its never coming back."))Qjax.push("main", root+"admin.ajax.php", "action=deleteSection&id="+val);}
function deleteFile(val){if(confirm("Are you sure you want to delete this file?  Its never coming back.")){Qjax.push("noreturn", root+"admin.ajax.php", "action=deleteFile&id="+val);get("file"+val).style.display='none';}}

//general functions//
function get(val){return document.getElementById(val);}
function echo(val){document.write(val);}
function rndInt(lo,hi){return Math.floor(Math.random()*hi+1)+lo;}
function def(key,val){if(typeof(key)=='undefined')key=val;return key;}
function make(val){
	if(typeof(val)!='object')
		return document.createElement(def(val, 'div'))
	var tmp = document.createElement(def(val['type'], 'div'));
	for(var i=1;i<makeVars.length;i++)
		if(def(val[makeVars[i]], '')!='') eval("tmp."+makeVars[i]+" = '"+val[makeVars[i]]+"'");
	return tmp;
}
function setClass(id, name){
	if(IE) id.setAttribute('className', name);
	else id.setAttribute('class', name);
}
function popup(url){
 	window.open("http://lgshort.com"+url,"File/Image","menubar=no,width=610,height=610,toolbar=no,location=no,scrollbars=yes");
}

//auto page refresh//
function startRefresh(seconds){RefreshID = setInterval("window.location.reload()",(seconds*1000));}
function stopRefresh(){clearInterval(RefreshID);}

//URL-related functions//
function getURLVar(urlVarName){var urlHalves = String(document.location).split('?');var urlVarValue = '';if(urlHalves[1]){var urlVars = urlHalves[1].split('&');for(i=0; i<=(urlVars.length); i++){if(urlVars[i]){var urlVarPair = urlVars[i].split('=');if (urlVarPair[0] && urlVarPair[0] == urlVarName)urlVarValue = urlVarPair[1];}}}return urlVarValue;}
function URLEncode(plaintext){var SAFECHARS="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_.!~*'()";var HEX = "0123456789ABCDEF";var encoded = "";for (var i = 0; i < plaintext.length; i++ ) {var ch = plaintext.charAt(i);if (ch == " ") {encoded += "+";}else if (SAFECHARS.indexOf(ch) != -1) {encoded += ch;}else {var charCode = ch.charCodeAt(0);if (charCode > 255) {alert( "Unicode Character '"+ch+"' cannot be encoded using standard URL encoding.\n (URL encoding only supports 8-bit characters.)\n A space (+) will be substituted." );encoded += "+";} else {encoded += "%";encoded += HEX.charAt((charCode >> 4) & 0xF);encoded += HEX.charAt(charCode & 0xF);}}}return encoded;}
function URLDecode(encoded){var HEXCHARS = "0123456789ABCDEFabcdef";var plaintext = "";var i = 0;while (i < encoded.length) {var ch = encoded.charAt(i);if (ch == "+") {plaintext += " ";i++;} else if (ch == "%") {if (i < (encoded.length-2)&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 && HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {plaintext += unescape( encoded.substr(i,3) );i += 3;} else {alert( 'Bad escape combination near ...' + encoded.substr(i) );plaintext += "%[ERROR]";i++;}} else {plaintext += ch;i++;}}return plaintext;}

var Qjax = new function(){
	this.push = function(d,u,c){
		new Ajax.Updater({success: d}, u, {method: 'post', parameters: c, evalScripts: true });
	};
}