//====================================
// ==============================================================
//  ReCore Engine: CMS - Ajax Functions
// ==============================================================
//====================================

// Check External txt file for version log	

// (c)REaction Marketing

// Notes: This document contains most of the dynamic calls and data transfers
// at client-side.

// =================================
//  AJAX Send Data Function
// =================================
function sendData(queryString, condition, script, functionToCall)
{
	if ((window.loggedin == 1) && (condition == "outside"))
		queryString += "&logged_in=true";
	else if ((window.loggedin == 1) && ((condition == "inside") || (condition == "inject") || (condition == "module")))
		queryString += "&logged_in=halftrue";

	var ajaxRequest;
	try
	{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} 
	catch (e)
	{
		// Internet Explorer Browsers
		try
		{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) 
		{
			try
			{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e)
			{
				// Something went wrong
				alert("Error: Ajax Browser Detect Failure");
				return false;
			}
		}
	}
	ajaxRequest.onreadystatechange = function()
	{
		if(ajaxRequest.readyState == 4)
		{
			window.commandinitiated = 0;
			if(functionToCall != undefined)
			{
				ajaxResponse = ajaxRequest.responseText;
				eval(functionToCall);
				// This was added to support initializing the jQuery cycle
				// plugin after the ajaxResponse is returned.
				if (init_jquery) {
					init_jquery();
				}
			}
		}
	}

	accessinfo = "";
	if (window.loggedin == 1)
		accessinfo = "&accessmask=" + document.getElementById('accessmask').value;
	var editor_ovr = "";	
	var date = new Date();
	var timestamp = date.getTime();
	var refPage = document.location.href.split("#")[0];
	refPage = refPage.replace(":","xyz250");
	refPage = refPage.replace(".","xyz251");
	var queryString = queryString + '&time=' + timestamp + accessinfo + "&account=" + window.account + "&refpage=" + refPage; //fix IE cache bug
	if (window.parent.editor)
	{
		window.parent.currentPage(document.location.href.split("#")[1]);
		editor_ovr = window.parent.editor_ovr;
	}
	ajaxRequest.open("GET", window.extralevel + "../../system/modules/globals/"+ script +".php" + queryString + editor_ovr, true);
	ajaxRequest.send(null);
}
// =================================
//  Remove a module row
// =================================
function remove_module_row(what, table, rowid, returnfunction)
{
	var answer = confirm('Confirm Deletetion of '+what+'?');
	if (answer)
	{
		var queryString = '?function=remove_module_row&table='+table+'&id=' + rowid;
		sendData(queryString, "inside", "functions", returnfunction);	
	}
}
// =================================
//  Swap Numeric field up/down
// =================================
function swap_numeric_field(table, field, id, direction, returnfunction)
{
	var queryString = '?function=swap_numeric_row&table='+table+'&field='+field+'&id=' + id+'&direction=' + direction;
	sendData(queryString, "inside", "functions", returnfunction);	
}
// =================================
//  Direct Editable Module Field
// =================================
function edit_module_field(table, field, current_value, id, returnfunction)
{
	document.getElementById(field+id).innerHTML = "<input id='new"+id+"' value='"+current_value+"'><a id='link"+id+"'><img src='images/admin/checkok.png' border='0'></a><span id='cache"+id+"'style='visibility:hidden; position: absolute;'>" + document.getElementById(field+id).innerHTML + "</span>";
	document.getElementById('link'+id).href	    = "javascript:edit_module_field_p2(\""+table+"\",\""+field+"\",\""+id+"\",\""+returnfunction+"\");";	
}
function edit_module_field_p2(table, field, id, returnfunction)
{
	sendData("?function=edit_module_field&table="+table+"&field="+field+"&id="+id+"&value="+document.getElementById('new'+id).value, "inside", "functions", returnfunction);	
}
// =================================
//  Editable Module Field
// =================================
//if prepend = "box" this function will cycle from box0-box(infinite) until it reaches a box that does not exist in the document
function get_checked_boxes(prepend)
{
	i = 0;
	var change = "";
	while (document.getElementById(prepend+i))
	{	
		if(document.getElementById(prepend+i).checked)
		{
			change = change+document.getElementById(prepend+i).value+",";	
		}
		i++;	
	}	
	change = change.slice(0, -1);	
	return change;
}
 //this function does not require multiple fields
function edit_module_fields(table, field, ids, changedvalue, returnfunction)
{
		sendData("?function=edit_module_field&table="+table+"&field="+field+"&id="+ids+"&value="+changedvalue, "inside", "functions", returnfunction);
}
// =================================
//  Load and Launch Various Static Floating Forms
// =================================
function launchForm(page, i)
{
	window.commandinitiated = 1;
	sendData("?page=" + page + "&id=" + i + "&formtype=outside", "outside", "forms", "returnLaunchForm(ajaxResponse)");
}
function returnLaunchForm(r)
{
	document.getElementById('AjaxDynamic').innerHTML = r;
	if (window.document.getElementById('dynamicForm').style.visibility == "visible")
	{
			document.getElementById('AjaxDynamic').innerHTML = r;
	}
	else
	{
		document.getElementById('AjaxDynamic').innerHTML = r;
		winLaunch('dynamicForm');
	}
}
// =================================
//  Load and Launch Various Static Inline Forms
// =================================
function launchInside(page, i)
{
	var date = new Date();
	window.commandinitiated = 1;
	var add = "";
	if(i != undefined) // ((i != undefined) && (window.location.hash.split(':')[1] != i))
	{
		add = ":"+i;
	}
	if (window.location.hash.replace("#", "") == parseurl())
	{
		window.top.window.oldrequest = 1;	
	}
	window.location.hash = page+add;
	document.getElementById('anchor_iframe').src= "blank.php?time="+date.getTime()+"&functionstring=" + page+add;
}
// =================================
//  Parse the Iframe for it's current location
// =================================
function parseurl()
{
	var url=window.frames.anchor_iframe.location.href;
	var functionstring=url.split("?")[1];
	
	if(!functionstring)
		return null;
	
	functionstring=functionstring.split("functionstring=")[1];
	functionstring=unescape(functionstring);
	return functionstring;
}
// =================================
//  Filling replaceable results in Dynamic Layouts
// =================================
function injectLayout(page, i)
{
	window.commandinitiated = 1;
	sendData("?page=" + page + "&id=" + i + "&formtype=inject", "inject", "forms", "returnInsideInjectLayout('" + page + "',ajaxResponse)");	
}
function returnInsideInjectLayout(page,r)
{
	var spanid;
	var output_pages = r.split(";;Break;;");
	for (var page in output_pages)
	{
		if (page == 0)
		{
			document.getElementById("content").innerHTML = output_pages[page];
			document.title = document.getElementById("pagetitle").value;
		}
		else
		{
			spanid = "content"+page;
			document.getElementById(spanid).innerHTML = output_pages[page];
		}
	}
}
// =================================
//  Load and Launch Various Small Forms Inside
// =================================
function reloadModule(module, i)
{
	var date = new Date();			
	window.commandinitiated  = 1;
	window.iframe_ignore	= 1;
	sendData("?page=" + module + "&id=" + i + "&formtype=module", "module", "forms", "returnAJAXReplace('" + module + "',ajaxResponse)");
	
	var add = ":"+i.replace(":","||");

	if(window.location.hash.indexOf(":") != -1)
	{	
		var page = window.location.hash.split(':')[0].replace("#","");
	}
	else
	{
		var page = window.location.hash.replace("#","");	
	}
	window.location.hash = page+add;
	document.getElementById('anchor_iframe').src= "blank.php?time="+date.getTime()+"&functionstring=" + page+add;
}
// =================================
//  Launch Module
// =================================
function launchModuleinID(module, module_settings, id, returncall, displaylink)
{
	if(id.indexOf(",") == -1)
		displayresponse("<img src='images/animated/loading.gif'>", id);
		
	if ((returncall != "undefined") && (returncall != undefined) && (returncall != ""))
		var returncall = "";

	sendData("?page=" + module + "&module=" + module + "&formtype=module&id=" + module_settings + "&pagetag=" + displaylink, "module", "forms","returnAJAXReplace('" + id + "',ajaxResponse);"+returncall);
	
	if ((displaylink != "undefined") && (displaylink != undefined) && (displaylink != ""))
	{
		var date = new Date();
		window.commandinitiated  = 1;
		window.iframe_ignore	= 1;
		window.location.hash 	= displaylink;
		document.getElementById('anchor_iframe').src = "blank.php?time="+date.getTime()+"&functionstring=" + displaylink;
	}
}
// =================================
//  Simple AJAX return replace function
// =================================
function returnAJAXReplace(id,r)
{
	if(id.indexOf(",") != -1) //Replace Multiple Cells
	{
		var currentid;
		var id		  = id.split(",");
		var output_pages = r.split(";;ModuleBreak;;");
		for (var page in output_pages)
		{
			if ((currentid == id[page]) || (id[page] == "") || (id[page] == "undefined") || (id[page] == undefined))
			{
				document.getElementById(currentid).innerHTML = document.getElementById(currentid).innerHTML + output_pages[page];	
			}
			else
			{
				document.getElementById(id[page]).innerHTML = output_pages[page];
				currentid = id[page];
			}
		}
	}
	else //Replace Single Cell
	{
		document.getElementById(id).innerHTML = r;
	}
	if (document.getElementById("modulepagetitle")) //Set a custom pagetitle if it exists
		document.title = document.getElementById("modulepagetitle").value;
}