var lastFunction;
var currFunction = "student";

function elementVisibility(elementId) {
	if(document.getElementById(elementId).style.display == 'inline') {
		document.getElementById(elementId).style.display = 'none';
		return "invisible";
	}
	else {
		document.getElementById(elementId).style.display = 'inline';
		return "visible";
	}
}

function toggleGreekEditor() {
	if(elementVisibility('greekEditor') == "visible")
		document.brickForm.price.value = parseInt(document.brickForm.price.value) + 8;
	else
	{
		document.brickForm.price.value = parseInt(document.brickForm.price.value) - 8;
		clearLines();
	}

	elementVisibility('greekButtons');
}

function toggleSymbolEditor() {
	if(elementVisibility('symbolEditor') == "visible")
		;//document.brickForm.price.value = parseInt(document.brickForm.price.value) + 8;
	else
	{
		//document.brickForm.price.value = parseInt(document.brickForm.price.value) - 8;
		clearLines();
	}

	elementVisibility('greekButtons');
}

function insertSymbol(textBoxId) {
	var selectedSymbol = getSelectedRadioValue(document.brickForm.greekLetters);
	document.getElementById(textBoxId).value = document.getElementById(textBoxId).value + selectedSymbol;
	updatePreview();
}

function clearLines() {
	document.brickForm.lineOne.value = "";
	document.brickForm.lineTwo.value = "";
	document.brickForm.lineThree.value = "";
	document.brickForm.lineFour.value = "";
	document.brickForm.lineFive.value = "";
	document.brickForm.lineSix.value = "";
	updatePreview();
}

function updatePreview() {
	var brickText = "";
	//brickText += document.brickForm.lineOne.value.replace("^000", "<img src=\"content-images/greek/alpha.jpg\">");
	brickText +=  document.brickForm.lineOne.value.toUpperCase() + "<br />";
	brickText +=  document.brickForm.lineTwo.value.toUpperCase() + "<br />";
	brickText +=  document.brickForm.lineThree.value.toUpperCase() + "<br />";
	brickText +=  document.brickForm.lineFour.value.toUpperCase() + "<br />";
	brickText +=  document.brickForm.lineFive.value.toUpperCase() + "<br />";
	brickText +=  document.brickForm.lineSix.value.toUpperCase() + "<br />";
	document.getElementById("brickText").innerHTML = brickText;
}

function toggleFunction() {
	//if(elementVisibility('nNumberField') == "visible")
	//	document.brickForm.price.value = parseInt(document.brickForm.price.value) - 100;
	//else
	//	document.brickForm.price.value = parseInt(document.brickForm.price.value) + 100;
	var selectedFunction = getSelectedRadioValue(document.brickForm.classification);
	lastFunction = currFunction;
	if(selectedFunction == "student")
		currFunction = "student";
	else
		currFunction = "other";
	
	if(lastFunction != currFunction)
		if(currFunction == "student")
		{
			elementVisibility('nNumberField')
			document.brickForm.price.value = parseInt(document.brickForm.price.value) - 100;
		}
		else
		{
			elementVisibility('nNumberField')
			document.brickForm.price.value = parseInt(document.brickForm.price.value) + 100;
		}
}

var xmlHttp;
function addBrick()
{
	xmlHttp=GetXmlHttpObject();
	if(xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	}

	//var url = "http://www.unf.edu/~wieg0001/bricks/addBrick.php";
	
	if(validate())
	{
		var url = "http://www.unf.edu/~n00008055/bricks/addBrick.php";
		
		url += "?firstName=" + document.brickForm.firstName.value;
		url += "&lastName=" + document.brickForm.lastName.value;
		url += "&nNumber=" + document.brickForm.nNumber.value;
		url += "&price=" + document.brickForm.price.value;
		url += "&lineOne=" + document.brickForm.lineOne.value;
		url += "&lineTwo=" + document.brickForm.lineTwo.value;
		url += "&lineThree=" + document.brickForm.lineThree.value;
		url += "&lineFour=" + document.brickForm.lineFour.value;
		url += "&lineFive=" + document.brickForm.lineFive.value;
		url += "&lineSix=" + document.brickForm.lineSix.value;
	
		xmlHttp.onreadystatechange=addBrickStateChanged;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}
	else
		alert('Please double check the fields marked with a red asterisk.');
}

function addBrickStateChanged() 
{ 
	if(xmlHttp.readyState == 4)
	{
		var xmlResponse = xmlHttp.responseXML;

		//var result = "success";
		//
//		var j = 0; //extra counter for phone numbers... they need to start at 0
//		
//		nrPeople = xmlResponse.getElementsByTagName('person');
//		for (i=0;i<nrPeople.length;i++)
//		{
//			resultTable += "<tr id=";
//			if((i % 2) == 0)
//				resultTable += "'even-row'";
//			else
//				resultTable += "'odd-row'";
//			resultTable += ">";
//			
//			var type = xmlResponse.getElementsByTagName('function')[i].firstChild.nodeValue;
//			resultTable += "<td>" + type + "</td>";
//			resultTable += "<td>" + xmlResponse.getElementsByTagName('dealerId')[i].firstChild.nodeValue + "</td>";
//			resultTable += "<td>" + xmlResponse.getElementsByTagName('firstName')[i].firstChild.nodeValue + "</td>";
//			resultTable += "<td>" + xmlResponse.getElementsByTagName('lastName')[i].firstChild.nodeValue + "</td>";
//			if(type == "Dealer")
//				resultTable += "<td>" + xmlResponse.getElementsByTagName('area')[i].firstChild.nodeValue + "</td>";
//			else
//				resultTable += "<td></td>";
//			if(type == "Customer")
//				resultTable += "<td>" + xmlResponse.getElementsByTagName('phoneNr')[j++].firstChild.nodeValue + "</td>";
//			else
//				resultTable += "<td></td>";
//			resultTable += "</tr>";	
//		}
//		resultTable += "</table>";
		
		//document.getElementById("result").innerHTML = result;
		if(document.brickForm.price.value == 100)
			parent.location="https://www.alumni.unf.edu/NetCommunity/SSLPage.aspx?pid=428&srcid=428";
		else
			parent.location="https://www.alumni.unf.edu/NetCommunity/SSLPage.aspx?pid=426&srcid=426";
	}
}

function validate()
{
	var validationResult = true;
	if (document.brickForm.firstName.value == "")
	{
		document.getElementById("firstEI").style.display = "inline";
		validationResult = false;
	}
	if (document.brickForm.lastName.value == "")
	{
		document.getElementById("lastEI").style.display = "inline";
		validationResult = false;
	}
	if (document.brickForm.nNumber.value == "" && getSelectedRadioValue(document.brickForm.classification) == "student")
	{
		document.getElementById("nNumberEI").style.display = "inline";
		validationResult = false;
	}
		
	return validationResult;
}

function GetXmlHttpObject()
{
	var xmlHttp=null;
	
	try	
	{
		xmlHttp=new XMLHttpRequest();
	}
	catch(e)
	{
		// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}

	return xmlHttp;
}

function getSelectedRadio(buttonGroup) {
   if (buttonGroup[0]) {
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            return i
         }
      }
   } else {
      if (buttonGroup.checked) { return 0; }
   }
   return -1;
}

function getSelectedRadioValue(buttonGroup) {
   var i = getSelectedRadio(buttonGroup);
   if (i == -1) {
      return "";
   } else {
      if (buttonGroup[i]) {
         return buttonGroup[i].value;
      } else {
         return buttonGroup.value;
      }
   }
}