var mapName = '';

//whooo boy big mama function of FUN
function saveMap()
{
	var newMapName = prompt("Map Name:", mapName );

	if( newMapName == '' )
	{
		alert( 'You must give the map a name.' );
		return;
	}
	else if( newMapName == mapName )
		if( !confirm( "Really overwrite '"+mapName+"'?" ) )
			return;

	if( document.all )
		var savePane = document.all['loadMap'];
	else
		var savePane = document.getElementById('loadMap');

	savePane.style.visibility = 'hidden';

	var saveBody = '';
	
	saveBody += "<div style='padding: 3px;'>\n";
	saveBody += "<form action='save_map.php' method=POST name='mapSaveForm'>\n";
	saveBody += "<input type=hidden name='OPTION' value='SAVEMAP' />\n";
	saveBody += "<input type=button value='submit' onClick='this.form.submit();' />\n";

	//save general info
	saveBody += "<br><b>General Info</b><br>";
	saveBody += "mapName <input type=text name=mapName value='"+newMapName+"' /><br>\n";
	saveBody += "gridColor <input type=text name=gridColor value='";
	saveBody += document.forms.toolboxForm.gridColor.options[document.forms.toolboxForm.gridColor.selectedIndex].value;
	saveBody += "' /><br />\n";
	saveBody += "gridBG <input type=text name=gridBG value='";
	saveBody += document.forms.toolboxForm.gridBG.options[document.forms.toolboxForm.gridBG.selectedIndex].value;
	saveBody += "' /><br />\n";
	saveBody += "gridBG <input type=text name=gridWidth value='";
	saveBody += document.forms.gridSizeForm.boxW.value;
	saveBody += "' /><br />\n";
	saveBody += "gridBG <input type=text name=gridHeight value='";
	saveBody += document.forms.gridSizeForm.boxH.value;
	saveBody += "' /><br />\n";
	
	//save layer names
	saveBody += "<hr><b>Layer Data</b><br>";
	for( var index = 0; index < document.forms.toolboxForm.layerSelect.options.length; index ++ )
	{
		var option = document.forms.toolboxForm.layerSelect.options[ index ];
		saveBody += "layer[" + option.value + "] <input type=text name='layer[" + option.value + "]' value='";
		saveBody += option.text + "' /><br>\n";
	}

	//save cell data
	saveBody += "<hr><b>Cell Data</b><br>";
  if( document.all )
  {
    var dragboxContainer = document.all['dragboxContainer'];
  }
  else
  {
    var dragboxContainer = document.getElementById('dragboxContainer');
  }

	var count=0;
  for( var current_box = dragboxContainer.firstChild; current_box != null; current_box = current_box.nextSibling )
  {
    if ( current_box.isBox )
    {
			saveBody += "cell[" + count + "][MyX] <input type=text name='cell[" + count + "][MyX]' value='";
			saveBody += current_box.myX + "' /><br>\n";
			saveBody += "cell[" + count + "][MyY] <input type=text name='cell[" + count + "][MyY]' value='";
			saveBody += current_box.myY + "' /><br>\n";
			saveBody += "cell[" + count + "][MyLayer] <input type=text name='cell[" + count + "][MyLayer]' value='";
			saveBody += current_box.myLayer + "' /><br>\n";
			saveBody += "cell[" + count + "][MyColor] <input type=text name='cell[" + count + "][MyColor]' value='";
			saveBody += current_box.myColor + "' /><br>\n";
			saveBody += "cell[" + count + "][MyBG] <input type=text name='cell[" + count + "][MyBG]' value='";
			saveBody += current_box.myBG + "' /><br>\n";
			saveBody += "cell[" + count + "][MyIcon] <input type=text name='cell[" + count + "][MyIcon]' value='";
			saveBody += current_box.myIcon + "' /><br>\n";
			saveBody += "cell[" + count + "][MyWidth] <input type=text name='cell[" + count + "][MyWidth]' value='";
			saveBody += current_box.myWidth + "' /><br>\n";
			saveBody += "cell[" + count + "][MyHeight] <input type=text name='cell[" + count + "][MyHeight]' value='";
			saveBody += current_box.myHeight + "' /><br>\n";
			saveBody += "cell[" + count + "][MyName] <input type=text name='cell[" + count + "][MyName]' value='";
			saveBody += escape( current_box.myName ) + "' /><br>\n";
			saveBody += "cell[" + count + "][MyNote] <input type=text name='cell[" + count + "][MyNote]' value='";
			saveBody += escape( current_box.myNote ) + "' /><br>\n";
			saveBody += "cell[" + count + "][MyHP] <input type=text name='cell[" + count + "][MyHP]' value='";
			saveBody += escape( current_box.myHP ) + "' /><br>\n";
			saveBody += "cell[" + count + "][MyMaxHP] <input type=text name='cell[" + count + "][MyMaxHP]' value='";
			saveBody += escape( current_box.myMaxHP ) + "' /><br>\n";

			saveBody += "<br>\n";
			count ++;
		}
	}

	if( count == 0 )
	{
		alert( "No Cell Data" );
		return;
	}

	saveBody += "</form>\n";
	saveBody += "</div>";

	savePane.innerHTML = saveBody;
//	savePane.style.visibility = 'visible';

	document.forms.mapSaveForm.submit();
}

function deleteMap()
{
	if( !confirm("Really delete '"+mapName+"'?") )
		return;

  if( document.all )
    var delPane = document.all['loadMap'];
  else
    var delPane = document.getElementById('loadMap');

  delPane.style.visibility = 'hidden';

	var delBody = '';
  
	delBody += "<form action='save_map.php' method=POST name='mapDelForm'>\n";
  delBody += "<input type=hidden name='OPTION' value='DELMAP' />\n";
  delBody += "<input type=hidden name='mapID' value='"+mapID+"' />\n";

	delPane.innerHTML = delBody;
	
	document.forms.mapDelForm.submit();
}
