//. *********************************************************************************
/*
	AJAX Controls
*/
//. *********************************************************************************
var dynamicContent_ajaxObjects = new Array(); 
var jsCache = new Array();
var enableCache = true; 
//. *********************************************************************************
function ajax_loadContent(divId, pathToFile)
{
	if (document.getElementById(divId) == null)
	{
		divId = 'modMessages';
		return;
	}
	// remove rand if present
	if (pathToFile.indexOf('&rand=') != -1)
	{
		pathToFile = pathToFile.substr(0, pathToFile.indexOf('&rand='));
	}

	if (pathToFile.substring(0,1) != '/')
	{
		pathToFile = '/' + pathToFile + '&rand=' + new Date().getTime();
	}

	//if (!document.getElementById(divId)) { alert(divId + ' ' + pathToFile) }
	if(enableCache && jsCache[pathToFile]){
		document.getElementById(divId).innerHTML = jsCache[pathToFile];
		return;
	}
	var ajaxIndex = dynamicContent_ajaxObjects.length;
	document.getElementById(divId).innerHTML = '<img src="/images/layout/indicator_loading.gif" border="0" style="border:0px;"><br/>loading...';
	dynamicContent_ajaxObjects[ajaxIndex] = new sack();
	dynamicContent_ajaxObjects[ajaxIndex].requestFile = pathToFile;
	
	dynamicContent_ajaxObjects[ajaxIndex].onCompletion = function(){ ajax_showContent(divId,ajaxIndex,pathToFile); };  
	dynamicContent_ajaxObjects[ajaxIndex].runAJAX();  
} 

//. *********************************************************************************
function ajax_showContent(divId,ajaxIndex,pathToFile)
{
  document.getElementById(divId).innerHTML =
    dynamicContent_ajaxObjects[ajaxIndex].response;
  if(enableCache){
    jsCache[pathToFile] = 
    dynamicContent_ajaxObjects[ajaxIndex].response;
  }
  dynamicContent_ajaxObjects[ajaxIndex] = false;
}

/* Ross Crawford Code */
var gAJAXurl
var gInlineAJAXurl

//. *********************************************************************************
function nada()
{
	// NADA!
}

//. *********************************************************************************
// for feature boxes
function changeclass()
{
	if (arguments[0] != null)
	{
		selectedElement = arguments[0];
		numElements = arguments[1];
		baseName = selectedElement.id.substr(0, (selectedElement.id.lastIndexOf('_')+1))
	
		for (c=1; c <= numElements; c++)
		{
			document.getElementById(baseName + c).className = '';
		}
		
		selectedElement.className = 'selected';
	}
}

//. *********************************************************************************
function getLeftPos(popW, popH)
{
	var w, h
	w = document.body.clientWidth;
	h = document.body.scrollHeight;
	var leftPos = (w-popW)/2

	var topPos = ((h-popH)/2);// - (h*0.1);

	return Array(leftPos,topPos);
}

//. *********************************************************************************
function show_layer(divID, ajaxURL, popW, popH, Xpos, Ypos, actiontype)
{
	if (actiontype == '')
	{
		actiontype = 'window';
	}
	// remove rand if present
	if (ajaxURL.indexOf('&rand=') != -1)
	{
		ajaxURL = ajaxURL.substr(0, ajaxURL.indexOf('&rand='));
	}
	
//	if (Xpos == '')
//	{
		aryPos = getLeftPos(popW, popH);
		Xpos = aryPos[0];
//	}
//	if (Ypos == '')
//	{
		aryPos = getLeftPos(popW, popH);
		Ypos = aryPos[1];
//	}

	switch (divID.toLowerCase())
	{
		case 'modmessages': divID_ajax = divID + '_content';
		case 'modinboxmessage': divID_ajax = divID + '_content';
		//default: divID_ajax = divID;
	}

	//ajaxURL = '/' + ajaxURL;
	if (divID != 'sendalert')
	{
		if ( (gAJAXurl == ajaxURL && document.getElementById(divID).style.display == 'block') || ajaxURL == 'ajax/blank.php' )
		{
			document.getElementById(divID).style.display = 'none';
			ajax_loadContent(divID_ajax, 'ajax/blank.php');
		}else{
			//document.getElementById(divID).style.display = 'block';
			/*document.getElementById(divID).style.position = 'absolute';
			document.getElementById(divID).style.zindex = 600;
			if (popW.toString().indexOf('%') == -1) { popW = popW + 'px'; }
			if (popH.toString().indexOf('%') == -1) { popH = popH + 'px'; }
			document.getElementById(divID).style.width = popW;
			document.getElementById(divID).style.height = popH;
			if (Xpos.toString().indexOf('%') == -1) { Xpos = Xpos + 'px'; }
			if (Ypos.toString().indexOf('%') == -1) { Ypos = Ypos + 'px'; }
			document.getElementById(divID).style.left = Xpos;
			document.getElementById(divID).style.top = Ypos;
			// load AJAX Content
			ajax_loadContent(divID_ajax, ajaxURL);
			gAJAXurl = ajaxURL;
			document.getElementById(divID).style.display = 'block';
			*/
			pathToFile = '/' + ajaxURL;
			new LITBox(pathToFile, {type:'window', overlay:false, height:popH, width:popW, resizable:false, overlay:true, opacity:1});
		}
	}else{
		new LITBox(ajaxURL, {type:actiontype, overlay:false, height:popH, width:popW, resizable:false, overlay:true, opacity:1});
	}
}
//. *********************************************************************************
function multiselect_operation(targetfield, needle, replacment)
{
	if (replacment == 'remove')
	{
		var stringreg = needle + ','
		var re = new RegExp(stringreg, 'gi');
		targetfield.value = targetfield.value.replace(re, '');
	}else{
		//. append
		targetfield.value = targetfield.value + replacment + ',';
	}
}
//. *********************************************************************************
function show_fixed_layer(divID, ajaxURL, popW, popH)
{
	if ( gInlineAJAXurl == ajaxURL && (document.getElementById(divID).style.display == 'block') || ajaxURL == 'blank.html' )
	{
		document.getElementById(divID).style.display = 'none';
		ajax_loadContent(divID, 'blank.html');
	}else{
		//document.getElementById(divID).style.position = 'absolute';
		document.getElementById(divID).style.width = popW;
		document.getElementById(divID).style.height = popH;
		// load AJAX Content
		ajax_loadContent(divID, ajaxURL);
		gInlineAJAXurl = ajaxURL;
		document.getElementById(divID).style.display = 'block';
		slidePane(200,divID);
	}
}

//. *********************************************************************************
function setjoin(theform, ajaxtarget, prefix, table, recordID, divID)
{
	theform = document.forms[arguments[0]];
	querystring = '';
	for(c = 0; c < theform.elements.length; c++)
	{
		if (theform.elements[c].id.indexOf(prefix) > -1)
		{
			querystring += theform.elements[c].id + '=' + theform.elements[c].value + '&';
		}
	}
	ajaxURL = 'ajax/' + ajaxtarget + '?prefix=' + prefix + '&table=' + table + '&' + querystring.substr(0, (querystring.length-1)) + '&id=' + recordID;
	ajax_loadContent(divID, ajaxURL);
}

//. *********************************************************************************
function populateFiltersField(formname, formID)
{
	if (!formID) { formID = formname; }
	//document.forms[formname].elements['filters'].value = '';
	for (c = 0; c < document.forms[formname].elements.length; c++)
	{
		if (document.forms[formname].elements[c].name.indexOf('src') != -1)
		{
			if (document.forms[formname].elements[c].value != '')
			{
				document.forms[formname].elements[formID + '_filters'].value += document.forms[formname].elements[c].name.substr(3) + ',' + escape(document.forms[formname].elements[c].value) + ',' + document.forms[formname].elements['extras_' + document.forms[formname].elements[c].name.substr(3)].value + '|';
			}
		}
	}
	//document.forms[formname].submit();
}
//. *********************************************************************************
/* Ross Crawford Code */