<?PHP

// defines the limit between normal and generated items in item_stats table for id column.
function getBaseItemMax () {
  return 10000;
}

function WayToID($way){
	$query = "select id from ways where name=\"" . $way . "\"";

	$result = mysql_query2($query);
	$wayID = mysql_fetch_array($result, MYSQL_NUM);

	return $wayID[0];
}

function SelectProgressionEvent($current_event, $select_name){
	printf("<SELECT name=%s>", $select_name);
	$query_events = "select name from progression_events";
	$result = mysql_query2($query_events);
	while ($event = mysql_fetch_array($result, MYSQL_NUM)){
		if ($event[0] == $current_event){
			printf("<OPTION selected>%s</OPTION>", $event[0]);
		}else{
			printf("<OPTION>%s</OPTION>", $event[0]);
		}
	}
	
	printf("<OPTION ");
	if($current_event == "")
	    printf("selected ");
	
	printf(" value=\"\">NULL</OPTION>");
	

	printf("</SELECT>");
}


function SelectCastProgressionEvent($current_event, $select_name){
	printf("<SELECT name=%s>", $select_name);
	$query_events = "select name from progression_events where name like 'cast %' order by name";
	$result = mysql_query2($query_events);
	while ($event = mysql_fetch_array($result, MYSQL_NUM)){
		if ($event[0] == $current_event){
			printf("<OPTION selected>%s</OPTION>", $event[0]);
		}else{
			printf("<OPTION>%s</OPTION>", $event[0]);
		}
	}
	
	printf("<OPTION ");
	if($current_event == "")
	    printf("selected ");
	
	printf(" value=\"\">NULL</OPTION>");
	

	printf("</SELECT>");
}

function SelectCommonString($current_string, $select_name){
	printf("<SELECT name=%s>", $select_name);
	$query_events = "select id, string from common_strings";
	$result = mysql_query2($query_events);
	while ($list = mysql_fetch_array($result, MYSQL_NUM)){
		if ($list[0] == $current_string){
			printf("<OPTION selected value=\"%s\">%s - %s</OPTION>",$list[0], $list[0], $list[1]);
		}else{
			printf("<OPTION value=\"%s\">%s - %s</OPTION>", $list[0], $list[0], $list[1]);
		}
	}
	printf("</SELECT>");
}

function SelectItemCateogory($current_category, $select_name){
	printf("<SELECT name=%s>", $select_name);
	$query_events = "select category_id, name from item_categories";
	$result = mysql_query2($query_events);
	while ($list = mysql_fetch_array($result, MYSQL_NUM)){
		if ($list[0] == $current_category){
			printf("<OPTION selected value=\"%s\">%s</OPTION>", $list[0], $list[1]);
		}else{
			printf("<OPTION value=\"%s\">%s</OPTION>", $list[0], $list[1]);
		}
	}
	printf("</SELECT>");
}

function SelectBaseItem($current_itemID,$select_name)
{
    $base_item_max = getBaseItemMax();
	printf("<SELECT name=%s>", $select_name);
	$query = "select id, item_type, name, flags from item_stats where id<$base_item_max order by item_type, name";
	$result = mysql_query2($query);
	while ($list = mysql_fetch_array($result, MYSQL_NUM)){
		if ($list[0] == $current_itemID){
			printf("<OPTION selected value=\"%s\">%s : %s</OPTION>", $list[0], $list[1], $list[2]);
		}else{
			printf("<OPTION value=\"%s\">%s : %s</OPTION>", $list[0], $list[1], $list[2]);
		}
	}
	
	printf("<OPTION ");
	if($current_itemID == "-1")
		printf("selected ");
	printf("value=\"%s\">%s</OPTION>", 0, "None");
	
	printf("</SELECT>");
}


function SelectGlyphs($current_glyphID,$select_name)
{
	printf("<SELECT name=%s>", $select_name);
	$query_events = "SELECT id,name FROM item_stats WHERE flags LIKE '%GLYPH%' order by name";
	$result = mysql_query2($query_events);
	while ($list = mysql_fetch_array($result, MYSQL_NUM)){
		if ($list[0] == $current_glyphID){
			printf("<OPTION selected value=\"%s\">%s</OPTION>", $list[0], $list[1]);
		}else{
			printf("<OPTION value=\"%s\">%s</OPTION>", $list[0], $list[1]);
		}
	}
	
	printf("<OPTION ");
	if($current_glyphID == "-1")
		printf("selected ");
	printf("value=\"%s\">%s</OPTION>", 0, "None");
	
	printf("</SELECT>");
}

function SelectNPCs($current_npc, $select_name, $invulnerable) {
	printf("<SELECT name=%s>", $select_name);

    if ($invulnerable=="vulnerable")
	    $query_events = "select c.id, c.name,s.name from characters c, sectors s where c.loc_sector_id=s.id and npc_master_id!=0 and npc_impervious_ind='N' order by s.name, c.name";
    else if ($invulnerable=="invulnerable")
	    $query_events = "select c.id, c.name,s.name from characters c, sectors s where c.loc_sector_id=s.id and npc_master_id!=0 and npc_impervious_ind='Y' order by s.name, c.name";
    else if ($invulnerable=="both")
	    $query_events = "select c.id, c.name,s.name from characters c, sectors s where c.loc_sector_id=s.id and npc_master_id!=0 order by s.name, c.name";

	$result = mysql_query2($query_events);
	while ($list = mysql_fetch_array($result, MYSQL_NUM)){
		if ($list[0] == $current_npc){
			printf("<OPTION selected value=\"%s\">%s (%s - %s)</OPTION>", $list[0], $list[1], $list[0], $list[2]);
		}else{
			printf("<OPTION value=\"%s\">%s (%s - %s)</OPTION>", $list[0], $list[1], $list[0], $list[2]);
		}
	}
	printf("</SELECT>");
}

function SelectSkills($current_skill,$select_name){
	printf("<SELECT name=%s>", $select_name);
	$query_events = "select skill_id, name from skills";
	$result = mysql_query2($query_events);
	while ($list = mysql_fetch_array($result, MYSQL_NUM)){
		if ($list[0] == $current_skill){
			printf("<OPTION selected value=\"%s\">%s</OPTION>", $list[0], $list[1]);
		}else{
			printf("<OPTION value=\"%s\">%s</OPTION>", $list[0], $list[1]);
		}
	}
	printf("</SELECT>");
}


function SelectSectors($current_sector,$select_name){
	printf("<SELECT name=%s>", $select_name);
	$query_events = "select id, name from sectors";
	$result = mysql_query2($query_events);
	while ($list = mysql_fetch_array($result, MYSQL_NUM)){
		if ($list[0] == $current_sector){
			printf("<OPTION selected value=\"%s\">%s</OPTION>", $list[0], $list[1]);
		}else{
			printf("<OPTION value=\"%s\">%s</OPTION>", $list[0], $list[1]);
		}
	}
	printf("</SELECT>");
}

function SelectRace($current_race,$select_name){
	printf("<SELECT name=%s>", $select_name);
	$query_events = "select race_id, name, sex from race_info order by name, sex";
	$result = mysql_query2($query_events);
	while ($list = mysql_fetch_array($result, MYSQL_NUM)){
		if ($list[0] == $current_race){
			printf("<OPTION selected value=\"%s\">%s - %s</OPTION>", $list[0], $list[1], $list[2] );
		}else{
			printf("<OPTION value=\"%s\">%s - %s</OPTION>", $list[0], $list[1], $list[2]);
		}
	}
	printf("</SELECT>");
}

function SelectSpawnRule($current_spawnrule,$select_name){
	printf("<SELECT name=%s>", $select_name);
	printf("<OPTION value=0>Not Loaded in Game</OPTION>");
	$query_events = "select id, fixed_spawn_sector, fixed_spawn_x, fixed_spawn_y, fixed_spawn_z, loot_category_id from npc_spawn_rules order by fixed_spawn_sector, fixed_spawn_x";
	$result = mysql_query2($query_events);
	while ($list = mysql_fetch_array($result, MYSQL_NUM)){
		if ($list[0] == $current_spawnrule){
			printf("<OPTION selected value=\"%s\">%s (%s,%s,%s) loot:%s</OPTION>", $list[0], $list[1], $list[2], $list[3], $list[4], $list[5] );
		}else{
			printf("<OPTION value=\"%s\">%s (%s,%s,%s) loot:%s</OPTION>", $list[0], $list[1], $list[2], $list[3], $list[4], $list[5] );
		}
	}
	printf("</SELECT>");
}

function SelectWeapon($current_weapon,$select_name){
    $base_item_max = getBaseItemMax();
	printf("<SELECT name=%s>", $select_name);
	printf("<OPTION value=-1>None</OPTION>");
	$query_events = "select id, armorvsweapon_type, name from item_stats where category_id=1 and id<$base_item_max order by armorvsweapon_type, name";
	$result = mysql_query2($query_events);
	while ($list = mysql_fetch_array($result, MYSQL_NUM)){
		if ($list[0] == $current_weapon){
			printf("<OPTION selected value=\"%s\">[%s] %s </OPTION>", $list[0], $list[1], $list[2]);
		}else{
			printf("<OPTION value=\"%s\">[%s] %s </OPTION>", $list[0], $list[1], $list[2]);
		}
	}
	printf("</SELECT>");
}

function SelectActionLocation($current_action, $select_name){
	printf("<SELECT name=%s>", $select_name);

    if ($select_name=="triggertype") {
      $elem[0]="SELECT";
      $elem[1]="PROXIMITY";
    } else if ($select_name=="responsetype") {
      $elem[0]="EXAMINE";
      $elem[1]="SCRIPT";
    }
    
	for ($i=0; $i<sizeof($elem); $i++) {
		if ($elem[$i] == $current_action){
			printf("<OPTION selected>%s</OPTION>", $elem[$i]);
		}else{
			printf("<OPTION>%s</OPTION>", $elem[$i]);
		}
	}

	printf("</SELECT>");
}

function SelectQuestScriptByName($current_name, $select_name){
	printf("<SELECT name=%s>", $select_name);

    printf("<OPTION value=-1>None</OPTION>");

	$query_events = "select name from quests order by name";
	$result = mysql_query2($query_events);
	while ($list = mysql_fetch_array($result, MYSQL_NUM)){
		if ($list[0] == $current_name){
			printf("<OPTION selected value=\"%s\">%s</OPTION>", $list[0], $list[0]);
		}else{
			printf("<OPTION value=\"%s\">%s</OPTION>", $list[0], $list[0]);
		}
	}

	
	printf("</SELECT>");
}

function SelectAreas($current_sector,$select_name){

    $areas = array("hydlaa_plaza","hydlaa_jayose","sewers","laanxdungeon","arena","ojaroad1","ojaroad2","akkaio","bdroad1","bdroad2","bdoorsout","bdoorsin","npcroom");

	printf("<SELECT name=%s>", $select_name);

	for ($i=0; $i<sizeof($areas);$i++) {
		if ($areas[$i] == $current_sector){
			printf("<OPTION selected value=\"%s\">%s</OPTION>", $areas[$i], $areas[$i]);
		} else {
			printf("<OPTION value=\"%s\">%s</OPTION>", $areas[$i], $areas[$i]);
		}
	}
	printf("</SELECT>");
}

function getDataFromArea($area) {

  // sectors
  if ($area=="hydlaa_plaza") {
      $data[0] = "(loc_sector_id=15 or loc_sector_id=52)";
      $data[1] = 535;
      $data[2] = 180;
      $data[3] = 2.2;
  } else if ($area=="hydlaa_jayose") {
      $data[0] = "(loc_sector_id=49 or loc_sector_id=40)";
      $data[1] = -250;
      $data[2] = -150;
      $data[3] = 3.2;
  } else if ($area=="sewers") {
      $data[0] = "(loc_sector_id>26 and loc_sector_id<38)";
      $data[1] = 645;
      $data[2] = 175;
      $data[3] = 2.5;
  } else if ($area=="laanxdungeon") {
      $data[0] = "(loc_sector_id>43 and loc_sector_id<49)";
      $data[1] = 1452;
      $data[2] = 815;
      $data[3] = 6.73;
  } else if ($area=="arena") {
      $data[0] = "(loc_sector_id>3 and loc_sector_id<15)";
      $data[1] = 470;
      $data[2] = 456;
      $data[3] = 4.43;
  } else if ($area=="ojaroad1") {
      $data[0] = "(loc_sector_id=22)";
      $data[1] = 603;
      $data[2] = 600;
      $data[3] = 0.9;
  } else if ($area=="ojaroad2") {
      $data[0] = "(loc_sector_id=59)";
      $data[1] = 632;
      $data[2] = 625;
      $data[3] = 1;
  } else if ($area=="akkaio") {
      $data[0] = "(loc_sector_id>16 and loc_sector_id<20)";
      $data[1] = 445;
      $data[2] = 465;
      $data[3] = 5.3;
  } else if ($area=="bdroad1") {
      $data[0] = "(loc_sector_id=60)";
      $data[1] = 491;
      $data[2] = 493;
      $data[3] = 0.76;
  } else if ($area=="bdroad2") {
      $data[0] = "(loc_sector_id=61)";
      $data[1] = 669;
      $data[2] = 667;
      $data[3] = 1.07;
  } else if ($area=="bdoorsout") {
      $data[0] = "(loc_sector_id=67)";
      $data[1] = 832;
      $data[2] = -749;
      $data[3] = 0.55;
  } else if ($area=="bdoorsin") {
      $data[0] = "(loc_sector_id=66)";
      $data[1] = 656;
      $data[2] = -977;
      $data[3] = 2.2;
  } else if ($area=="npcroom") {
      $data[0] = "(loc_sector_id=3)";
      $data[1] = 387;
      $data[2] = -403;
      $data[3] = 3.8;

  }

  return $data;
}


function myquery ($query){
	$result = mysql_query2($query);
	if (mysql_errno())
		echo "<P>MySQL error " . mysql_errno() . ": " . mysql_error() . "\n<br>When executing:<br>\n$query\n<br></P>";
	return $result;
}

?>
