// JavaScript Document
// I have not included WWW catches in the AJAX shipping functions because orders are nonexistent.
var www = "http://www.";
var ajaxurl = document.URL;
var checkwww = ajaxurl.substring(7,10);
if (checkwww != "www") {www = "http://";}

function displayError(message) {
	throw("Error: " + message);
	}
	
function runSearch(si)
{
	//searchTerm = document.getElementById("searching");
		//searches = searchTerm.si.value;
	var serverParams = "S=" + si;
	if (xmlHttp)
	{
		try
		{	
			xmlHttp.open("GET", www + "rusticaspenfurniture.com/Shipping/process.php?" + serverParams, true);
			xmlHttp.onreadystatechange = searchReturn;
			xmlHttp.send("null");
		}
		catch(e)
		{
			throw("Error: " + e.toString());
		}
	}
}


function searchReturn()
{
	if (xmlHttp.readyState == 4) 
	{
	if (xmlHttp.status == 200)
		{ 
		try 
		{
			
	var xmlResponse = xmlHttp.responseXML;
	if (!xmlResponse || !xmlResponse.documentElement)
		throw("Invalid XML Structure:\n" + xmlHttp.responseText);
	var rootNodeName = xmlResponse.documentElement.nodeName;
	if (rootNodeName == "parseerror")
		throw("Invalid XML Structure:\n" + xmlHttp.responseText);
	var xmlRoot = xmlResponse.documentElement;
	if (rootNodeName != "response" || !xmlRoot.firstChild)
		throw("Invalid XML Structure:\n" + xmlHttp.responseText);
		
	var result = xmlRoot.getElementsByTagName("result")[0].firstChild.data;
	var subtotal = xmlRoot.getElementsByTagName("subtotal")[0].firstChild.data;
	var total = xmlRoot.getElementsByTagName("total")[0].firstChild.data;
	
	var writeReturn = document.getElementById("AJAX_search");
		writeReturn.innerHTML = "<h4>Displaying: " + subtotal + " out of " + total + "</h4>" + result;
	

} catch(e) {displayError(e.toString());}	// END Try Statement
} else {displayError(xmlHttp.statusText);}	// END Status200
} 											// END ReadyState4
} 											// END Function



function validate(value, field, returnfield)
{
	var serverParams = "value=" + value + "&field=" + field + "&returnfield=" + returnfield;
	if (xmlHttp)
	{
		try
		{	
			xmlHttp.open("GET", www + "rusticaspenfurniture.com/Shipping/process.php?" + serverParams, true);
			xmlHttp.onreadystatechange = validateReturn;
			xmlHttp.send("null");
		}
		catch(e)
		{
			throw("Error: " + e.toString());
		}
	}
}

function validateReturn()
{
	if (xmlHttp.readyState == 4) 
	{
	if (xmlHttp.status == 200)
		{ 
		try 
		{
			
	var xmlResponse = xmlHttp.responseXML;
	if (!xmlResponse || !xmlResponse.documentElement)
		throw("Invalid XML Structure:\n" + xmlHttp.responseText);
	var rootNodeName = xmlResponse.documentElement.nodeName;
	if (rootNodeName == "parseerror")
		throw("Invalid XML Structure:\n" + xmlHttp.responseText);
	var xmlRoot = xmlResponse.documentElement;
	if (rootNodeName != "response" || !xmlRoot.firstChild)
		throw("Invalid XML Structure:\n" + xmlHttp.responseText);
		
	var result = xmlRoot.getElementsByTagName("result")[0].firstChild.data;
	var returnfield = xmlRoot.getElementsByTagName("returnfield")[0].firstChild.data;
	var redirect = xmlRoot.getElementsByTagName("redirect")[0].firstChild.data;
	
		if (redirect == "TRUE") {
			var allowsubmit = document.getElementById("NAAS");
			allowsubmit.innerHTML = "<input type='hidden' name='gofly' value='GO' /><input type='submit' class='newaccountsubmit green' value='Create Account' />";
		} else {
			var allowsubmit = document.getElementById("NAAS");
			allowsubmit.innerHTML = "<input type='button' class='newaccountsubmit errors' value='Not Ready' />";
		}
	
	
	var writeOkError = document.getElementById(returnfield);
		writeOkError.innerHTML = result;
	

} catch(e) {displayError(e.toString());}	// END Try Statement
} else {displayError(xmlHttp.statusText);}	// END Status200
} 											// END ReadyState4
} 											// END Function


function saveOrder()
{
	var serverParams = "mode=SAVEORDER";
	if (xmlHttp)
	{
		try
		{	
			xmlHttp.open("GET", "process.php?" + serverParams, true);
			xmlHttp.onreadystatechange = saveOrderReturn;
			xmlHttp.send("null");
		}
		catch(e)
		{
			throw("Error: " + e.toString());
		}
	}
}

function saveOrderReturn()
{
	if (xmlHttp.readyState == 4) 
	{
	if (xmlHttp.status == 200)
		{ 
		try 
		{
			
	var xmlResponse = xmlHttp.responseXML;
	if (!xmlResponse || !xmlResponse.documentElement)
		throw("Invalid XML Structure:\n" + xmlHttp.responseText);
	var rootNodeName = xmlResponse.documentElement.nodeName;
	if (rootNodeName == "parseerror")
		throw("Invalid XML Structure:\n" + xmlHttp.responseText);
	var xmlRoot = xmlResponse.documentElement;
	if (rootNodeName != "response" || !xmlRoot.firstChild)
		throw("Invalid XML Structure:\n" + xmlHttp.responseText);
		
	var saved = xmlRoot.getElementsByTagName("saveok")[0].firstChild.data;
	var savekey = xmlRoot.getElementsByTagName("savekey")[0].firstChild.data;
	var savemode = xmlRoot.getElementsByTagName("savemode")[0].firstChild.data;
	
		if (saved == "TRUE" && savemode == "TEMPORARY") {
			var displaysave = document.getElementById("AJAX_SaveOrder");
			displaysave.innerHTML = "<h1 class='green'>Order Saved</h1><h3>Your Key is <span class='unsaved'>'" + savekey + "'</span>.</h3><p>You can use this to retrieve this order anytime.</p>";
		} 
		else if (saved == "TRUE" && savemode == "PERMANENT") {
			var displaysave = document.getElementById("AJAX_SaveOrder");
			displaysave.innerHTML = "<h1 class='green'>Your Order Is Saved</h1><p>You can access it anytime by logging in and coming to the Order Page</p>";
		}
		else {
			var displaysave = document.getElementById("AJAX_SaveOrder");
			displaysave.innerHTML = "There was an error while saving your order. An error report has been sent to the administrator.";
		}

} catch(e) {displayError(e.toString());}	// END Try Statement
} else {displayError(xmlHttp.statusText);}	// END Status200
} 											// END ReadyState4
} 											// END Function



function retrieveOrder(retrievalkey, retrievaltype) {
	var serverParams = "mode=RETRIEVEORDER&key=" + retrievalkey + "&rtype=" + retrievaltype;
	if (xmlHttp)
	{
		try
		{	
			xmlHttp.open("GET", "process.php?" + serverParams, true);
			xmlHttp.onreadystatechange = retrieveOrderReturn;
			xmlHttp.send("null");
		}
		catch(e)
		{
			throw("Error: " + e.toString());
		}
	}
}


function retrieveOrderReturn()
{
	if (xmlHttp.readyState == 4) 
	{
	if (xmlHttp.status == 200)
		{ 
		try 
		{
			
	var xmlResponse = xmlHttp.responseXML;
	if (!xmlResponse || !xmlResponse.documentElement)
		throw("Invalid XML Structure:\n" + xmlHttp.responseText);
	var rootNodeName = xmlResponse.documentElement.nodeName;
	if (rootNodeName == "parseerror")
		throw("Invalid XML Structure:\n" + xmlHttp.responseText);
	var xmlRoot = xmlResponse.documentElement;
	if (rootNodeName != "response" || !xmlRoot.firstChild)
		throw("Invalid XML Structure:\n" + xmlHttp.responseText);
		
	var reviewretrieved = xmlRoot.getElementsByTagName("displayretrieved")[0].firstChild.data;
	
	var displayRetrieved = document.getElementById("AJAX_FlatOrder");
	displayRetrieved.innerHTML = reviewretrieved;


	

} catch(e) {displayError(e.toString());}	// END Try Statement
} else {displayError(xmlHttp.statusText);}	// END Status200
} 											// END ReadyState4
} 											// END Function


function flatOrder() 
{
	var serverParams = "mode=REVIEWORDER";
	if (xmlHttp)
	{
		try
		{	
			xmlHttp.open("GET", "process.php?" + serverParams, true);
			xmlHttp.onreadystatechange = reviewOrder;
			xmlHttp.send("null");
		}
		catch(e)
		{
			throw("Error: " + e.toString());
		}
	}
}

function reviewOrder()
{
	if (xmlHttp.readyState == 4) 
	{
	if (xmlHttp.status == 200)
		{ 
		try 
		{
			
	var xmlResponse = xmlHttp.responseXML;
	if (!xmlResponse || !xmlResponse.documentElement)
		throw("Invalid XML Structure:\n" + xmlHttp.responseText);
	var rootNodeName = xmlResponse.documentElement.nodeName;
	if (rootNodeName == "parseerror")
		throw("Invalid XML Structure:\n" + xmlHttp.responseText);
	var xmlRoot = xmlResponse.documentElement;
	if (rootNodeName != "response" || !xmlRoot.firstChild)
		throw("Invalid XML Structure:\n" + xmlHttp.responseText);
		
	var totalreview = xmlRoot.getElementsByTagName("totalreview")[0].firstChild.data;
	var displayReview = document.getElementById("AJAX_FlatOrder");
			displayReview.innerHTML = totalreview;
	var allorders = xmlRoot.getElementsByTagName("allorders")[0].firstChild.data;
	var displayOrders = document.getElementById("AJAX_RetrieveOrder");
			displayOrders.innerHTML = allorders;
		
	var orderone = document.getElementById("monbeds");
	var ordertwo = document.getElementById("montad");
	var orderthree = document.getElementById("mondesk");
	var orderfour = document.getElementById("monecent");
	var orderfive = document.getElementById("monmisc");
	
	orderone.style.backgroundPosition="0 -50px";
	ordertwo.style.backgroundPosition="0 -50px";
	orderthree.style.backgroundPosition="0 -50px";
	orderfour.style.backgroundPosition="0 -50px";
	orderfive.style.backgroundPosition="0 -50px";
			
} catch(e) {displayError(e.toString());}	// END Try Statement
} else {displayError(xmlHttp.statusText);}	// END Status200
} 											// END ReadyState4
} 											// END Function




function switchOrder(whichone)
{
	var serverParams = "mode=STANDARDORDER&W=" + whichone;
	if (xmlHttp)
	{
		try
		{	
			xmlHttp.open("GET", "process.php?" + serverParams, true);
			xmlHttp.onreadystatechange = switchItAll;
			xmlHttp.send("null");
		}
		catch(e)
		{
			throw("Error: " + e.toString());
		}
	}
}

function switchItAll()
{
	if (xmlHttp.readyState == 4) 
	{
	if (xmlHttp.status == 200)
		{ 
		try 
		{
			
	var xmlResponse = xmlHttp.responseXML;
	if (!xmlResponse || !xmlResponse.documentElement)
		throw("Invalid XML Structure:\n" + xmlHttp.responseText);
	var rootNodeName = xmlResponse.documentElement.nodeName;
	if (rootNodeName == "parseerror")
		throw("Invalid XML Structure:\n" + xmlHttp.responseText);
	var xmlRoot = xmlResponse.documentElement;
	if (rootNodeName != "response" || !xmlRoot.firstChild)
		throw("Invalid XML Structure:\n" + xmlHttp.responseText);
		
	var whichone = xmlRoot.getElementsByTagName("whichone")[0].firstChild.data;
	var insertforms = xmlRoot.getElementsByTagName("insertforms")[0].firstChild.data;
	
	var orderone = document.getElementById("monbeds");
	var ordertwo = document.getElementById("montad");
	var orderthree = document.getElementById("mondesk");
	var orderfour = document.getElementById("monecent");
	var orderfive = document.getElementById("monmisc");
	

	if (whichone == 1) {orderone.style.backgroundPosition="0 0";
		ordertwo.style.backgroundPosition="0 -50px";
		orderthree.style.backgroundPosition="0 -50px";
		orderfour.style.backgroundPosition="0 -50px";
		orderfive.style.backgroundPosition="0 -50px";}
	else if (whichone == 2) {orderone.style.backgroundPosition="0 -50px";
		ordertwo.style.backgroundPosition="0 0";
		orderthree.style.backgroundPosition="0 -50px";
		orderfour.style.backgroundPosition="0 -50px";
		orderfive.style.backgroundPosition="0 -50px";}
	else if (whichone == 3) {orderone.style.backgroundPosition="0 -50px";
		ordertwo.style.backgroundPosition="0 -50px";
		orderthree.style.backgroundPosition="0 0";
		orderfour.style.backgroundPosition="0 -50px";
		orderfive.style.backgroundPosition="0 -50px";}
	else if (whichone == 4) {orderone.style.backgroundPosition="0 -50px";
		ordertwo.style.backgroundPosition="0 -50px";
		orderthree.style.backgroundPosition="0 -50px";
		orderfour.style.backgroundPosition="0 0";
		orderfive.style.backgroundPosition="0 -50px";}
	else if (whichone == 5) {orderone.style.backgroundPosition="0 -50px";
		ordertwo.style.backgroundPosition="0 -50px";
		orderthree.style.backgroundPosition="0 -50px";
		orderfour.style.backgroundPosition="0 -50px";
		orderfive.style.backgroundPosition="0 0";}
	
	var fillOrder = document.getElementById("AJAX_FlatOrder");
			fillOrder.innerHTML = insertforms;
	var fixSaveOrder = document.getElementById("AJAX_RetrieveOrder");
			fixSaveOrder.innerHTML = "";
	
} catch(e) {displayError(e.toString());}	// END Try Statement
} else {displayError(xmlHttp.statusText);}	// END Status200
} 											// END ReadyState4
} 											// END Function


function orderMods(fieldreturn, valueof, field)
{
	var serverParams = "mode=MODORDER&FTS=" + field + "&FR=" + fieldreturn + "&VO=" + valueof;
	if (xmlHttp)
	{
		try
		{	
			xmlHttp.open("GET", "process.php?" + serverParams, true);
			xmlHttp.onreadystatechange = modReturn;
			xmlHttp.send("null");
		}
		catch(e)
		{
			throw("Error: " + e.toString());
		}
	}
}


function modReturn()
{
	if (xmlHttp.readyState == 4)
	{
	if (xmlHttp.status == 200)
		{
		try
		{
			
	var xmlResponse = xmlHttp.responseXML;
	if (!xmlResponse || !xmlResponse.documentElement)
		throw("Invalid XML Structure:\n" + xmlHttp.responseText);
	var rootNodeName = xmlResponse.documentElement.nodeName;
	if (rootNodeName == "parseerror")
		throw("Invalid XML Structure:\n" + xmlHttp.responseText);
	var xmlRoot = xmlResponse.documentElement;
	if (rootNodeName != "response" || !xmlRoot.firstChild)
		throw("Invalid XML Structure:\n" + xmlHttp.responseText);
		
	var fieldreturn = xmlRoot.getElementsByTagName("fieldreturn")[0].firstChild.data;
	var origfield = xmlRoot.getElementsByTagName("field")[0].firstChild.data;
	var thereturn = xmlRoot.getElementsByTagName("thereturn")[0].firstChild.data;

	var displayMod = document.getElementById(fieldreturn);
	displayMod.innerHTML = thereturn;

} catch(e) {displayError(e.toString());}	// END Try Statement
} else {displayError(xmlHttp.statusText);}	// END Status200
} 											// END ReadyState4
} 											// END Function




function newOrder()
{
	var serverParams = "mode=NEWORDER";
	if (xmlHttp)
	{
		try
		{	
			xmlHttp.open("GET", "process.php?" + serverParams, true);
			xmlHttp.onreadystatechange = newOrderReturn;
			xmlHttp.send("null");
		}
		catch(e)
		{
			throw("Error: " + e.toString());
		}
	}
}


function newOrderReturn()
{
	if (xmlHttp.readyState == 4)
	{
	if (xmlHttp.status == 200)
		{
		try
		{
			
	var xmlResponse = xmlHttp.responseXML;
	if (!xmlResponse || !xmlResponse.documentElement)
		throw("Invalid XML Structure:\n" + xmlHttp.responseText);
	var rootNodeName = xmlResponse.documentElement.nodeName;
	if (rootNodeName == "parseerror")
		throw("Invalid XML Structure:\n" + xmlHttp.responseText);
	var xmlRoot = xmlResponse.documentElement;
	if (rootNodeName != "response" || !xmlRoot.firstChild)
		throw("Invalid XML Structure:\n" + xmlHttp.responseText);
		
	var resetmessage = xmlRoot.getElementsByTagName("message")[0].firstChild.data;

	var displayMessage = document.getElementById("AJAX_FlatOrder");
	displayMessage.innerHTML = resetmessage;

} catch(e) {displayError(e.toString());}	// END Try Statement
} else {displayError(xmlHttp.statusText);}	// END Status200
} 											// END ReadyState4
} 											// END Function









