//====================================
// ==============================================================
//  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, launchwindow)
{
	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";
	}
	if (condition == "i_functions")
	{
		condition    = "functions";
		altcondition = "i_functions";
	}
	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;
					resetHome()
				}
			}
			else if (condition == "forms")
			{
				document.getElementById('AjaxDynamic').innerHTML = ajaxRequest.responseText;
				if (formtype == "login")
				{
					setWindow(250, 320, 'wall3', 'border3');
				}
				else if ((formtype == "blog") || (formtype == "resume"))
					setWindow(575, 515, 'wall2', 'border2');
				else
				{
					if (window.loggedin == 1)
						setWindow(485, 800, 'wall4', 'border4');
					else
						setWindow(485, 515, 'wall2', 'border2');


				}
				if (launchwindow == "no")
				{
					var replaceme = document.getElementById('richtextContent').value
					replaceme = replaceme.replace(/"/g, 'xyz');
					document.getElementById('newscontent').value = replaceme;
					document.getElementById('newstitle').value = document.getElementById('richtextTitle').value
					textWindow('text');
				}
				else
					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, launchwindow, n)
{	
	if (launchwindow != "no")
		launchwindow = "yes";
	var value = "";
	if (window.loggedin == 1)
	{
		value = "&logged_in=true";
	}
	var queryString = '?menu=' + i + '&id=' + n + value;
	sendData(queryString, "forms", i, launchwindow);
}

// =================================
//  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");
}