// *** index.php functions ***


// toggle Add Location pop-up form
function showAddLocation() {
	if (document.getElementById("addlocationdiv").style.visibility=='visible') {
		document.getElementById("addlocationdiv").style.visibility='hidden';
	}
	else {

		// *** dev: may want to clear current values of the form fields here, so the form is fresh

		document.getElementById("addlocationdiv").style.visibility='visible';
		document.getElementById("locationName").focus();
		showCaptcha();
	}
}

function showCaptcha() {
	if (document.getElementById("captchadiv").style.visibility=='visible') document.getElementById("captchadiv").style.visibility='hidden';
	else {
		document.getElementById("captchadiv").style.visibility='visible';
		document.getElementById("captchacode").focus();
	}
}

// display Add Pins To Location pop-up form
function showAddPinsToLocation() {
	if (document.getElementById("addpinstolocationdiv").style.visibility=='visible') document.getElementById("addpinstolocationdiv").style.visibility='hidden';
	else {
		document.getElementById("addpinstolocationdiv").style.visibility='visible';
	}
}

// display editable location fields (and hide the read-only info)
function showEditLocation() {
	document.getElementById("ReadOnlyLocationTitleDiv").style.display='none';
	document.getElementById("ReadOnlyLocationResultsDiv").style.display='none';
	document.getElementById("EditLocationTitleDiv").style.display='block';
	document.getElementById("EditLocationResultsDiv").style.display='block';
	document.getElementById("EditLocationTitleDiv").style.visibility='visible';
	document.getElementById("EditLocationResultsDiv").style.visibility='visible';
}

// display read-only location information
function showReadOnlyLocation() {
	document.getElementById("EditLocationTitleDiv").style.display='none';
	document.getElementById("EditLocationResultsDiv").style.display='none';	
	document.getElementById("ReadOnlyLocationTitleDiv").style.display='block';
	document.getElementById("ReadOnlyLocationResultsDiv").style.display='block';
	document.getElementById("ReadOnlyLocationTitleDiv").style.visibility='visible';
	document.getElementById("ReadOnlyLocationResultsDiv").style.visibility='visible';
}


// ajax script
var url = "ajax.php"; 
var http = false;

function handleHttpResponse() {

	if (http.readyState == 4) {

		if (http.status == 200) {

			// split the comma delimited response into an array
			results = http.responseText.split(",");

			//alert(http.responseText);

  			// check if this is to modify a location or add a new one
			if (results[0] == 'ModifyLocation') {


				if (results[1] > 0) {
					// success
					//alert('success');
				}
				else {
					// problem with update
					//alert('problem');
				}
			
			}
			
			else {

  
				// check if the location already exists in the database
				if (results[2] == 0) {
					// display an error that the location is already in there
					document.getElementById("addlocationmsg").innerHTML = "<br><font color=red>Error: A location with the same name already exists in the database. (<font size=-2>If it is a chain, please include the city or neighborhood as part of the Location Name field.</font>)</font>";
				}
				else if (results[0] > 0) {
					// display validation errors
					document.getElementById("addlocationmsg").innerHTML = "<br><font color=red>" + results[1] + "</font>";
				}
				else {
	
					// check if this is the second time displaying the "add pinball machines to location" form by seeing if it's currently visible
					if (document.getElementById("addpinstolocationdiv").style.visibility=='visible') {
	
						// display the current list of pins at the location
						document.getElementById("listPinsAtLocation").innerHTML = results[0];
						document.getElementById("finishedlink").innerHTML = "Finished? <a href=index.php?location=" + document.getElementById("pinslocationID").value + "><font color=#000000><b>View the new location listing.</b></font></a>";
	
					}
	
					// this is the first time displaying the "add pinball machines to location" form, so toggle the location form off
					else {
	
	
						// set these for the add machine form
						document.getElementById("pinslocationname").innerHTML = results[1];
						document.getElementById("pinslocationID").value = results[2];
	
		
						// toggle location form off
						showAddLocation();
		
						// display new form
						showAddPinsToLocation();
					}
	
				}	// checking validation
	
			}	// checking if modifying or adding

          }	// checking status 200

        }	// checking readystate

}


function processForm($var) {


	// gather the form variables to send to the processing script


	if ($var == "addlocation") {

		// add a new location
		var forminput = "action=" + $var + "&locationName=" + encodeURI( 
document.getElementById("locationName").value )
+ "&locationAddress=" + encodeURI( document.getElementById("locationAddress").value )
+ "&locationCity=" + encodeURI( document.getElementById("locationCity").value )
+ "&locationType=" + encodeURI( document.getElementById("locationType").value )
+ "&locationDescription=" + encodeURI( document.getElementById("locationDescription").value )
+ "&locationURL=" + encodeURI( document.getElementById("locationURL").value );
	
	}
	
	else if ($var == "modifylocation") {
		
		var forminput = "action=" + $var + "&locationID=" + encodeURI(document.getElementById("location").value )
+ "&locationName=" + encodeURI( document.getElementById("NewLocationName").value )
+ "&locationType=" + encodeURI( document.getElementById("NewLocationType").value )
+ "&locationAddress=" + encodeURI( document.getElementById("NewLocationAddress").value )
+ "&locationCity=" + encodeURI( document.getElementById("NewLocationCity").value )
+ "&locationDescription=" + encodeURI( document.getElementById("NewLocationDescription").value )
+ "&locationURL=" + encodeURI( document.getElementById("NewLocationURL").value );
		
	
	}
	
	else if ($var == "captcha") {
		// validate field
		if (document.getElementById("captchacode").value.toLowerCase() == "mars") {
			// show addLocation form
			showAddLocation();			
		}
		else {
			document.getElementById("captchamsg").innerHTML = "<font color=red>Sorry, please try again.</font>";
		}
	}	

	else {


		// add a new machine to a location
		var forminput = "action=" + $var + "&locationID=" + encodeURI(document.getElementById("pinslocationID").value )
		+ "&pinID=" + encodeURI(document.getElementById("drpAddPinToLocation").value );

//		alert("LocationID: " + document.getElementById("pinslocationID").value + " PinID: " + document.getElementById("drpAddPinToLocation").value);


	}

	// don't make the ajax call for the captcha
	if ($var != "captcha") {
		// setup the HTTP object
		http = false;
	        if (window.XMLHttpRequest) {	// Mozilla, Safari

	                http = new XMLHttpRequest();
	                if (http.overrideMimeType) {
	                        http.overrideMimeType('text/xml');
	                 }
		} 
		else if (window.ActiveXObject) { // IE
	                try {
	                        http = new ActiveXObject("Msxml2.XMLHTTP");
	                 }
	                catch (e) {
	                        try {
	                                http = new ActiveXObject("Microsoft.XMLHTTP");
	                        }
	                        catch (e) {}
	                }
	      }
	      if (!http) {
		alert('ERROR: Cannot create XMLHTTP instance. Please contact the administrator.');
		return false;
	        }


		// pass the values into the processing script
		http.onreadystatechange = handleHttpResponse; 
		http.open('post', url, true); 
		http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		http.setRequestHeader("Content-length", forminput.length);
		http.setRequestHeader("Connection", "close");
		http.send(forminput);
	}

}
