//====================================
// ==============================================================
//  Interactive 3D Portfolio: CMS - Globals
// ==============================================================
//====================================

// V. 1.00

// "Interactive 3D Portfolio" content mangement system allows either
// a veteran or a aspiring 3D artist to quickly create and customize
// an efficient interactive portfolio website. It is meant to act
// like a reel with options. Any person viewing your interactive
// reel will be able to quickly identify your skillsets because of its
// easability in use. This often keeps the viewers attention rather than
// getting bored of a simple video reel and giving you no more than 30
// seconds of their time.

// (c)Snc Design - Quality Graphics and Code - [www.sncdesign.net]

// Notes: This document contains most of the dynamic calls and data transfers
// at client-side.

// =================================
//  AJAX Send Data Function
// =================================
function sendData(queryString, condition, formtype)
{
	var altcondition = "";
	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;
			}
		}
	}
	if (condition == "item")
	{
		condition    = "content";
		altcondition = "item";
	}
	else if (condition == "i_functions")
	{
		condition    = "functions";
		altcondition = "i_functions";
	}
	else if (condition == "inside")
	{
		condition    = "forms";
		altcondition = "inside";
	}
	ajaxRequest.onreadystatechange = function()
	{
		if(ajaxRequest.readyState == 4)
		{
			if (condition == "functions")
			{
				if (altcondition == "i_functions")
				{
					var suid = document.getElementById("suid").value;
					loadContent("item", suid); //reload when data has passed
				}
				else
					loadContent("content"); //reload when data has passed
			}
			else if (condition == "content")
			{
				if (altcondition == "item")
					condition = "item";
				document.getElementById(condition).innerHTML = ajaxRequest.responseText;
				if (altcondition == "item")
				{
					if ((document.getElementById('flashbanner').innerHTML == "") || (document.getElementById('flashbanner').innerHTML == "<span class=\"whitetext\">Flash windows closed while adding/editing items.</span>"))
					{
						addPreviewer();
						enlargeImage(0);
					}
				}
				if (window.loggedin == 1)
					adminTools(altcondition);
			}
			else if (condition == "login")
			{
				document.getElementById('ajaxDiv').innerHTML = ajaxRequest.responseText;
				if(document.getElementById('ajaxDiv').innerHTML == "Correct username and password.")
				{
					if(document.getElementById('remember_me').checked == true)
						document.cookie = "remember_me=true; expires=15/02/2020 00:00:00";
					winLaunch('dynamicForm');
					window.loggedin = 1;
					adminTools(condition);
				}
			}
			else if (condition == "forms")
			{
				if (altcondition == "inside")
				{
					document.getElementById('content').innerHTML = '<span id="progressloader">' + ajaxRequest.responseText + '</span>';
				}
				else
				{
					document.getElementById('AjaxDynamic').innerHTML = ajaxRequest.responseText;
					if (formtype == "reel")
						document.getElementById('dynamicFormTable').width = 1100;
					else if ((formtype == "blog") || (formtype == "resume"))
						document.getElementById('dynamicFormTable').width = 545;
					else
						document.getElementById('dynamicFormTable').width = 400;
	
					winLaunch('dynamicForm');
				}
			}
		}
	}
	var date = new Date();
	var timestamp = date.getTime();
	var queryString = queryString + '&time=' + timestamp; //fix IE cache bug
	ajaxRequest.open("GET", "includes/ajaxphpscripts/" + condition + ".php" + queryString, true);
	ajaxRequest.send(null); 
}


// =================================
//  Load and Launch Various Small Forms
// =================================
function launchForm(i)
{	
	var value = "";
	if (window.loggedin == 1)
	{
		value = "&logged_in=true";
	}
	var queryString = '?menu=' + i + value;
	sendData(queryString, "forms", i);
}
// =================================
//  Load and Launch Various Small Forms Inside
// =================================
function launchInside(i, n)
{	
	var value = "";
	if (window.loggedin == 1)
	{
		value = "&logged_in=true";
	}
	var queryString = '?menu=' + i + '&id=' + n + value;
	sendData(queryString, "inside", i);
}

// =================================
//  AJAX addCategory Part
// =================================
//Uses AJAX to add categories to the apge

function addCat(i)
{	
	var values      = returnValues(i);
	var name	= document.getElementById(values[0] + 'newcategory').value;
	var queryString = '?function=addcat&name=' + name + values[1];
	sendData(queryString, values[2]);
}

// =================================
//  AJAX deleteCategory
// =================================
//Uses Ajax to edit categories on a page

function deleteObject(i, itemcat)
{
	values         	   = returnValues(i);
	if (itemcat == 'deletecat')
	{
		var name   = document.getElementById(values[0] + 'catname' + values[3]).value;
		var answer = confirm('Delete Category "' + name + '"?');
	}
	else	
	{
		var name   = document.getElementById(values[0] + 'itemname' + values[3]).value;
		var answer = confirm('Delete Item "' + name + '"?')
	}
	if (answer)
	{
		var queryString = '?id=' + values[3] + '&function=' + itemcat + values[1];
		sendData(queryString, values[2]);	
	}
}

// =================================
//  AJAX editCategory Part I
// =================================
//Uses Ajax to edit categories on a page

function editCat(i)
{
	var values         	= returnValues(i);
	var name 		= document.getElementById(values[0] + 'catname' + values[3]).value;
	var ajaxDisplay 	= document.getElementById(values[0] + 'catform' + values[3]);
	ajaxDisplay.innerHTML   = '<table width="174" height="33"><tr><td><input type="text" name="category" id="' + values[0] + 'category" value="' + name + '" size="18" maxlength="12"></td>' +
				  '<td><a href="#" ><img src="images/admin/ok.gif" class="buttons" id="ok' + values[3] + '" onclick="editCatII(\''+ values[0] + values[3] + '\');" alt="Ok" title="Ok" border="0"' +
				  'onmouseover="buttonEffect(\'ok' + values[3] + '\', \'TRUE\');" onmouseout="buttonEffect(\'ok' + values[3] + '\', \'FALSE\');"></a></td></tr></table>';
}

// =================================
//  AJAX editCategory Part II
// =================================
// Uses AJAX to edit on the page

function editCatII(i)
{
	var values      = returnValues(i);
	var name 	= document.getElementById(values[0] + 'category').value;
	var queryString = '?id=' + values[3] + '&function=editcat&name=' + name + values[1];
	sendData(queryString, values[2]);
}

// =================================
//  AJAX up/down Category/Item
// =================================
// Uses AJAX to move categories on the page

function upDown(i, updown)
{
	var values      = returnValues(i);
	var queryString = '?id=' + values[3] + '&function=' + updown + values[1];
	sendData(queryString, values[2]);
}

// =================================
//  AJAX LoadContent
// =================================
// Uses AJAX to load beginning page content or items
// of the advanced window.

function loadContent(content_type, suid)
{
	var extraQuery = "";
	var prepend    = "";
	if(!suid) 
		suid   = "";
	else
		extraQuery = "&suid=" + suid;
	if (document.cookie == "remember_me=true")
		window.loggedin = 1;
	if (content_type == "item") 
		prepend = "i_";
	document.getElementById(prepend + 'progressloader').innerHTML = "<table cellpadding=\"0\" cellspacing=\"0\"><tr><td class=\"whitetext\">Working...</td><td><img src=\"images/animated/loading.gif\"></td></tr></table>";
	var queryString = '?prepend=' + prepend + extraQuery;
	sendData(queryString, content_type);
}

// =================================
//  AJAX Password Check
// =================================
// Uses AJAX to check passwords without reloading the page

function ajaxPassCheck()
{
	document.getElementById('ajaxDiv').innerHTML = '<img src="images/animated/loading.gif">';
	var user = document.getElementById('LoginName').value;
	var pass = document.getElementById('Password').value;
	var reee = document.getElementById('remember_me').value;
	var queryString = '?user=' + user + '&pass=' + pass + '&reee=' + reee;
	sendData(queryString, "login");
}

// =================================
//  Initiates Hidden Admin Windows
// =================================

function launchadminWindows(i, editadd)
{
	var values = returnValues(i);
	if (values[0] != "")
	{
		document.getElementById('imagepreviewer').innerHTML = "<span class=\"whitetext\">Flash windows closed while adding/editing items.</span>";
		document.getElementById('flashbanner').innerHTML = "<span class=\"whitetext\">Flash windows closed while adding/editing items.</span>";
	}
	var queryString = '?menu=thumbmenu&id=' + values[3] + '&function=' + editadd + values[1];
	sendData(queryString, "forms");
}