<?php
ini_set("display_errors", "on");
error_reporting(E_ALL ^ E_NOTICE);

$curFile = $_GET['curFile'];
$printMode = $_GET['printMode'];
$textListing = $_POST['textListing'];

$rawList = array('', 'armor', 'weapons', 'items1', 'items2', 'vouchers', 'Abilities', 'Magic', 'keyItems', 'BastokQuests', 'SanDorianQuests', 'WindurstQuests', 'JeunoQuests', 'OtherQuests', 'ZilartQuests', 'AhtUrhganQuests', 'GoddessQuests', 'AbysseaQuests', 'BastokMissions', 'SanDorianMissions', 'WindurstMissions', 'GoddessMissions', 'campaignOp', 'CrystallineMissions', 'KupoMissions', 'ShantottoMissions', 'Atmacite');
$wikiList = array();
$safeListIn = array("San D'Oria", " Of ", " A ", " The ", "A L'Outrance", "Au ");
$safeListOut = array("San d'Oria", " of ", " a ", " the ", "A l'Outrance", "au ");
$skillListIn = array('GreatAxe', 'GreatKatana', 'GreatSword', 'HandToHand', 'PoleArm', 'StringInstrument', 'Thrown', 'WindInstrument');
$skillListOut = array('Great Axe', 'Great Katana', 'Great Sword', 'Hand-to-Hand', 'Polearm', 'String Instrument', 'Throwing', 'Wind Instrument');
$mythicIn = array("Conqueror", "Glanzfaust", "Yagrush", "Laevateinn", "Murgleis", "Vajra", "Burtgang", "Liberator", "Aymur", "Carnwenhan", "Gastraphetes", "Kogarasumaru", "Nagi", "Ryunohige", "Nirvana", "Tizona", "Death Penalty", "Kenkonken", "Terpsichore", "Tupsimati");
$mythicOut = array("Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic");
$specialWeaponIn = array("Ochain", "Daurdabla", "Verethragna", "Twashtar", "Almace", "Caladbolg", "Farsha", "Ukonvasara", "Redemption", "Rhongomiant", "Kannagi", "Masamune", "Gambanteinn", "Hvergelmir", "Gandiva", "Armageddon", "Aegis", "Gjallarhorn", "Spharai", "Mandau", "Excalibur", "Ragnarok", "Guttler", "Bravura", "Gungnir", "Apocalypse", "Kikoku", "Amanomurakumo", "Mjollnir", "Claustrum", "Annihilator", "Yoichinoyumi", "Conqueror", "Glanzfaust", "Yagrush", "Laevateinn", "Murgleis", "Vajra", "Burtgang", "Liberator", "Aymur", "Carnwenhan", "Gastraphetes", "Kogarasumaru", "Nagi", "Ryunohige", "Nirvana", "Tizona", "Death Penalty", "Kenkonken", "Terpsichore", "Tupsimati");
$specialWeaponOut = array("Empyrean", "Empyrean", "Empyrean", "Empyrean", "Empyrean", "Empyrean", "Empyrean", "Empyrean", "Empyrean", "Empyrean", "Empyrean", "Empyrean", "Empyrean", "Empyrean", "Empyrean", "Empyrean", "Relic", "Relic", "Relic", "Relic", "Relic", "Relic", "Relic", "Relic", "Relic", "Relic", "Relic", "Relic", "Relic", "Relic", "Relic", "Relic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic");
$elementIn = array("?Element: Fire?", "?Element: Ice?", "?Element: Wind?", "?Element: Earth?", "?Element: Thunder?", "?Element: Water?", "?Element: Light?", "?Element: Dark?", "?Element: Air?", "<FIR>", "<ICE>", "<WND>", "<ERT>", "<LTG>", "<WTR>", "<LIT>", "<DRK>", "<fir>", "<ice>", "<wnd>", "<ert>", "<ltg>", "<wtr>", "<lit>", "<drk>", "&#8826;Element: Fire&#8827;", "&#8826;Element: Ice&#8827;", "&#8826;Element: Wind&#8827;", "&#8826;Element: Earth&#8827;", "&#8826;Element: Thunder&#8827;", "&#8826;Element: Water&#8827;", "&#8826;Element: Light&#8827;", "&#8826;Element: Dark&#8827;", "&#8826;Element: Air&#8827;");
$elementOut = array("{{Fire}}", "{{Ice}}", "{{Wind}}", "{{Earth}}", "{{Thunder}}", "{{Water}}", "{{Light}}", "{{Dark}}", "{{Wind}}", "{{Fire}}", "{{Ice}}", "{{Wind}}", "{{Earth}}", "{{Thunder}}", "{{Water}}", "{{Light}}", "{{Dark}}", "{{Fire}}", "{{Ice}}", "{{Wind}}", "{{Earth}}", "{{Thunder}}", "{{Water}}", "{{Light}}", "{{Dark}}", "{{Fire}}", "{{Ice}}", "{{Wind}}", "{{Earth}}", "{{Thunder}}", "{{Water}}", "{{Light}}", "{{Dark}}", "{{Wind}}");
$defCheckIn = array("Cannot Equip Handgear", "Cannot Equip Headgear", "Cannot Equip Footgear");
$defCheckOut = array('', '', '');
$raceListIn = array('HumeMale', 'HumeFemale', 'EvlaanMale', 'ElvaanFemale', 'TarutaruMale', 'TarutaruFemale');
$raceListOut = array('Hume Male', 'Hume Female', 'Evlaan Male', 'Elvaan Female', 'Tarutaru Male', 'Tarutaru Female');
$ampIn = array(' + ', ' +', '+ ', '+', ' & ', ' &', '& ', '&');
$ampOut = array(' Plus ', ' Plus ', ' Plus ', ' Plus ', ' and ', ' and ', ' and ', ' and ');
$ampOut = array(' + ', ' +', ' + ', ' +', ' and ', ' and ', ' and ', ' and ');

$specialIn = array("Cure II", "Cure", "Reraise II", "Reraise", "Reraise III", "Reraise II", "Reraise", "Converts");
$specialOut = array("C()re II", "Cur3", "R4ise II", "Ra1se", "R3raise III", "R3r4ise II", "R3ra1se", "C0nverts");
$specialOutII = array('"Cure II"', '"Cure"', '"Reraise II"', '"Reraise"', '"Reraise III"', '"Reraise II"', '"Reraise"', '"Converts"');
$quotesIn = array("Absorb", "Absorb-TP", "Accomplice", "Addendum: Black", "Addendum: White", "Afflatus Solace", "Aggressor", "Alacrity", "Allies' Roll", "Amorph Killer", "Ancient Circle", "Angon.", "Aquan Killer", "Aquaveil", "Arcana Killer", "Arcane Circle", "Ascetic's Fury", "Aspir", "Atonement", "Auspice", "Avatar's Favor", "Ballad", "Banish", "Barrage", "Beast Killer", "Berserk", "Bind", "Bird Killer", "Blade: Hi", "Blade: Kamu", "Blade: Metsu", "Blaze Spikes", "Blind", "Blink", "Blitzer's Roll", "Blizzard", "Blood Boon", "Blood Pact", "Blood Rage", "Boost", "Bounty Shot", "Breath", "Burst Affinity", "Call Beast", "Camlann's Torment", "Camouflage", "Carol", "Caster's Roll", "Catastrophe", "Celerity", "Chain Affinity", "Chakra", "Chant du Cygne", "Charm", "Choke", "Climactic Flourish", "Cloudsplitter", "Collaborator", "Composure", "Conserve MP", "Conserve TP", "Conspirator", "Convert", "Coronach", "Counter", "Counterstance", "Courser's Roll", "Courtly Measure", "Cover", "Curaga", "Curse", "Dagan", "Dark Arts", "Death", "Death Blossom", "Demon Killer", "Despoil", "Dia", "Dispel", "Divine Benison", "Divine Caress", "Divine Emblem", "Divine Veil", "Dodge", "Double Attack", "Double Shot", "Dragon Killer", "Drain", "Drakesbane", "Dread Spikes", "Dreadbug.", "Dual Wield", "Ducal Recall", "Ebullience", "Efflux", "Elegy", "Elemental Seal", "Elemental Siphon", "Empty Killer", "Enaero", "Enblizzard", "Endark", "Enfire", "Enlight", "Enmity Douse", "Ensilence", "Enstone", "Enthunder", "Enwater", "Etude", "Expiacion", "Fast Cast", "Feather Step", "Fencer", "Final Heaven", "Finale", "Fire", "Flee", "Focus", "Footwork", "Futae", "Garland of Bliss", "Gate of Tartarus", "Geirskogul", "Gifted to the champion of Aht Urhgan", "Gilfinder", "Glory Slash", "Hasso", "Haste", "Hide", "High Jump", "Holy Circle", "Hymnus", "Ice Spikes", "Immanence", "Impact", "Impetus", "Innin", "Insurgency", "Invisible", "Jig", "Jishnu's Radiance", "Jump", "Kick Attacks", "Killer", "King's Justice", "Klimaform", "Knights of Round", "Last Resort", "Leaden Salute", "Light Arts", "Lizard Killer", "Lullaby", "Madrigal", "Magic Atk. Bonus", "Magic Def. Bonus", "Mambo", "Mana Cede", "Mana Wall", "Mandalic Stab", "Maneuver", "March", "Martial Arts", "Mazurka", "Meditate", "Mercy Stroke", "Metatron Torment", "Migawari", "Mijin Gakure", "Military Parade", "Minne", "Minuet", "Mordant Rime", "Mug", "Myrkr", "Mystic Boon", "Namas Arrow", "Nether Void", "Ninja tool expertise", "Occult Acumen", "Omniscience", "Onslaught", "Overload", "Paeon", "Parsimony", "Penury", "Perfect Counter", "Perpetuance", "Phalanx", "Phantom Roll", "Plantoid Killer", "Poison", "Poisona", "Prelude", "Primal Rend", "Protect", "Protect II", "Provoke", "Pyrrhic Kleos", "Quadruple Attack", "Quick Draw", "Quietus", "Rampart", "Randgrith", "Random Deal", "Rapid Shot", "Rapture", "Ready", "Recycle", "Refresh", "Regain", "Regen", "Repair", "Requiem", "Resist Bind", "Resist Blind", "Resist Charm", "Resist Curse", "Resist Gravity", "Resist Paralyze", "Resist Petrify", "Resist Poison", "Resist Silence", "Resist Sleep", "Resist Slow", "Resist Stun", "Resist Virus", "Restraint", "Retaliation", "Retrace", "Reverse Flourish", "Reward", "Rudra's Storm", "Saboteur", "Samba", "Scavenge", "Scherzo", "Scourge", "Seigan", "Sekkanoki", "Sengikori", "Sentinel", "Shadowbind", "Sharpshot", "Shell", "Shield Bash", "Shield Def. Bonus", "Shield Mastery", "Shock Spikes", "Sic", "Signet", "Skillchain Bonus", "Sleep", "Slow", "Snapshot", "Sneak", "Sneak Attack", "Soul Jump", "Souleater", "Spikes", "Spirit Jump", "Spirit Link", "Spur", "Steal", "Stealth", "Step", "Steps", "Stone", "Stoneskin", "Store TP", "Stout Servant", "Striking Flourish", "Stringing Pummel", "Stun", "Sublimation", "Subtle Blow", "Tachi: Fudo", "Tachi: Kaiten", "Tachi: Rana", "Tactical Guard", "Tactical Parry", "Tactical Switch", "Tactician's Roll", "Tame", "Tartarus Torpor", "Teleport", "Teleport-Altep", "Teleport-Dem", "Teleport-Holla", "Teleport-Mea", "Teleport-Vahzl", "Teleport-Yhoat", "Third Eye", "Threnody", "Thunder", "Tomahawk.", "Torcleaver", "Treasure Hunter", "Trick Attack", "Triple Attack", "Triple Shot", "True Shot", "Trueflight", "Ukko's Fury", "Undead Killer", "Unlimited Shot", "Uriel Blade", "Utsusemi", "Velocity Shot", "Vermin Killer", "Victory Smite", "Vidohunir", "Violent Flourish", "Virelai", "Waltz", "Warcry", "Warding Circle", "Warp", "Water Spirit", "Weapon Bash", "Wildfire", "Yonin", "Zanshin", "feral drain", "incidents");
$quotesOut = array("\"Absorb\"", "\"Absorb-TP\"", "\"Accomplice\"", "\"Addendum: Black\"", "\"Addendum: White\"", "\"Afflatus Solace\"", "\"Aggressor\"", "\"Alacrity\"", "\"Allies' Roll\"", "\"Amorph Killer\"", "\"Ancient Circle\"", "\"Angon.\"", "\"Aquan Killer\"", "\"Aquaveil\"", "\"Arcana Killer\"", "\"Arcane Circle\"", "\"Ascetic's Fury\"", "\"Aspir\"", "\"Atonement\"", "\"Auspice\"", "\"Avatar's Favor\"", "\"Ballad\"", "\"Banish\"", "\"Barrage\"", "\"Beast Killer\"", "\"Berserk\"", "\"Bind\"", "\"Bird Killer\"", "\"Blade: Hi\"", "\"Blade: Kamu\"", "\"Blade: Metsu\"", "\"Blaze Spikes\"", "\"Blind\"", "\"Blink\"", "\"Blitzer's Roll\"", "\"Blizzard\"", "\"Blood Boon\"", "\"Blood Pact\"", "\"Blood Rage\"", "\"Boost\"", "\"Bounty Shot\"", "\"Breath\"", "\"Burst Affinity\"", "\"Call Beast\"", "\"Camlann's Torment\"", "\"Camouflage\"", "\"Carol\"", "\"Caster's Roll\"", "\"Catastrophe\"", "\"Celerity\"", "\"Chain Affinity\"", "\"Chakra\"", "\"Chant du Cygne\"", "\"Charm\"", "\"Choke\"", "\"Climactic Flourish\"", "\"Cloudsplitter\"", "\"Collaborator\"", "\"Composure\"", "\"Conserve MP\"", "\"Conserve TP\"", "\"Conspirator\"", "\"Convert\"", "\"Coronach\"", "\"Counter\"", "\"Counterstance\"", "\"Courser's Roll\"", "\"Courtly Measure\"", "\"Cover\"", "\"Curaga\"", "\"Curse\"", "\"Dagan\"", "\"Dark Arts\"", "\"Death\"", "\"Death Blossom\"", "\"Demon Killer\"", "\"Despoil\"", "\"Dia\"", "\"Dispel\"", "\"Divine Benison\"", "\"Divine Caress\"", "\"Divine Emblem\"", "\"Divine Veil\"", "\"Dodge\"", "\"Double Attack\"", "\"Double Shot\"", "\"Dragon Killer\"", "\"Drain\"", "\"Drakesbane\"", "\"Dread Spikes\"", "\"Dreadbug.\"", "\"Dual Wield\"", "\"Ducal Recall\"", "\"Ebullience\"", "\"Efflux\"", "\"Elegy\"", "\"Elemental Seal\"", "\"Elemental Siphon\"", "\"Empty Killer\"", "\"Enaero\"", "\"Enblizzard\"", "\"Endark\"", "\"Enfire\"", "\"Enlight\"", "\"Enmity Douse\"", "\"Ensilence\"", "\"Enstone\"", "\"Enthunder\"", "\"Enwater\"", "\"Etude\"", "\"Expiacion\"", "\"Fast Cast\"", "\"Feather Step\"", "\"Fencer\"", "\"Final Heaven\"", "\"Finale\"", "\"Fire\"", "\"Flee\"", "\"Focus\"", "\"Footwork\"", "\"Futae\"", "\"Garland of Bliss\"", "\"Gate of Tartarus\"", "\"Geirskogul\"", "\"Gifted to the champion of Aht Urhgan\"", "\"Gilfinder\"", "\"Glory Slash\"", "\"Hasso\"", "\"Haste\"", "\"Hide\"", "\"High Jump\"", "\"Holy Circle\"", "\"Hymnus\"", "\"Ice Spikes\"", "\"Immanence\"", "\"Impact\"", "\"Impetus\"", "\"Innin\"", "\"Insurgency\"", "\"Invisible\"", "\"Jig\"", "\"Jishnu's Radiance\"", "\"Jump\"", "\"Kick Attacks\"", "\"Killer\"", "\"King's Justice\"", "\"Klimaform\"", "\"Knights of Round\"", "\"Last Resort\"", "\"Leaden Salute\"", "\"Light Arts\"", "\"Lizard Killer\"", "\"Lullaby\"", "\"Madrigal\"", "\"Magic Atk. Bonus\"", "\"Magic Def. Bonus\"", "\"Mambo\"", "\"Mana Cede\"", "\"Mana Wall\"", "\"Mandalic Stab\"", "\"Maneuver\"", "\"March\"", "\"Martial Arts\"", "\"Mazurka\"", "\"Meditate\"", "\"Mercy Stroke\"", "\"Metatron Torment\"", "\"Migawari\"", "\"Mijin Gakure\"", "\"Military Parade\"", "\"Minne\"", "\"Minuet\"", "\"Mordant Rime\"", "\"Mug\"", "\"Myrkr\"", "\"Mystic Boon\"", "\"Namas Arrow\"", "\"Nether Void\"", "\"Ninja tool expertise\"", "\"Occult Acumen\"", "\"Omniscience\"", "\"Onslaught\"", "\"Overload\"", "\"Paeon\"", "\"Parsimony\"", "\"Penury\"", "\"Perfect Counter\"", "\"Perpetuance\"", "\"Phalanx\"", "\"Phantom Roll\"", "\"Plantoid Killer\"", "\"Poison\"", "\"Poisona\"", "\"Prelude\"", "\"Primal Rend\"", "\"Protect\"", "\"Protect II\"", "\"Provoke\"", "\"Pyrrhic Kleos\"", "\"Quadruple Attack\"", "\"Quick Draw\"", "\"Quietus\"", "\"Rampart\"", "\"Randgrith\"", "\"Random Deal\"", "\"Rapid Shot\"", "\"Rapture\"", "\"Ready\"", "\"Recycle\"", "\"Refresh\"", "\"Regain\"", "\"Regen\"", "\"Repair\"", "\"Requiem\"", "\"Resist Bind\"", "\"Resist Blind\"", "\"Resist Charm\"", "\"Resist Curse\"", "\"Resist Gravity\"", "\"Resist Paralyze\"", "\"Resist Petrify\"", "\"Resist Poison\"", "\"Resist Silence\"", "\"Resist Sleep\"", "\"Resist Slow\"", "\"Resist Stun\"", "\"Resist Virus\"", "\"Restraint\"", "\"Retaliation\"", "\"Retrace\"", "\"Reverse Flourish\"", "\"Reward\"", "\"Rudra's Storm\"", "\"Saboteur\"", "\"Samba\"", "\"Scavenge\"", "\"Scherzo\"", "\"Scourge\"", "\"Seigan\"", "\"Sekkanoki\"", "\"Sengikori\"", "\"Sentinel\"", "\"Shadowbind\"", "\"Sharpshot\"", "\"Shell\"", "\"Shield Bash\"", "\"Shield Def. Bonus\"", "\"Shield Mastery\"", "\"Shock Spikes\"", "\"Sic\"", "\"Signet\"", "\"Skillchain Bonus\"", "\"Sleep\"", "\"Slow\"", "\"Snapshot\"", "\"Sneak\"", "\"Sneak Attack\"", "\"Soul Jump\"", "\"Souleater\"", "\"Spikes\"", "\"Spirit Jump\"", "\"Spirit Link\"", "\"Spur\"", "\"Steal\"", "\"Stealth\"", "\"Step\"", "\"Steps\"", "\"Stone\"", "\"Stoneskin\"", "\"Store TP\"", "\"Stout Servant\"", "\"Striking Flourish\"", "\"Stringing Pummel\"", "\"Stun\"", "\"Sublimation\"", "\"Subtle Blow\"", "\"Tachi: Fudo\"", "\"Tachi: Kaiten\"", "\"Tachi: Rana\"", "\"Tactical Guard\"", "\"Tactical Parry\"", "\"Tactical Switch\"", "\"Tactician's Roll\"", "\"Tame\"", "\"Tartarus Torpor\"", "\"Teleport\"", "\"Teleport-Altep\"", "\"Teleport-Dem\"", "\"Teleport-Holla\"", "\"Teleport-Mea\"", "\"Teleport-Vahzl\"", "\"Teleport-Yhoat\"", "\"Third Eye\"", "\"Threnody\"", "\"Thunder\"", "\"Tomahawk.\"", "\"Torcleaver\"", "\"Treasure Hunter\"", "\"Trick Attack\"", "\"Triple Attack\"", "\"Triple Shot\"", "\"True Shot\"", "\"Trueflight\"", "\"Ukko's Fury\"", "\"Undead Killer\"", "\"Unlimited Shot\"", "\"Uriel Blade\"", "\"Utsusemi\"", "\"Velocity Shot\"", "\"Vermin Killer\"", "\"Victory Smite\"", "\"Vidohunir\"", "\"Violent Flourish\"", "\"Virelai\"", "\"Waltz\"", "\"Warcry\"", "\"Warding Circle\"", "\"Warp\"", "\"Water Spirit\"", "\"Weapon Bash\"", "\"Wildfire\"", "\"Yonin\"", "\"Zanshin\"", "\"feral drain\"", "\"incidents\"");
//$compList = array("Head", "Neck", "Body", "Hands", "Waist", "Back", "Rings", "Earrings", "Legs", "Feet", "Shields");
//$compList = array("Hand-to-Hand", "Dagger", "Sword", "Great Sword", "Axe", "Great Axe", "Scythe", "Polearm", "Katana", "Great Katana", "Staff", "Club", "Ranged", "Archery", "Marksmanship", "Ammunition", "Throwing ", "-", "Wind Instrument", "String Instrument", "Pet Items", "Grips", "Fishing Gear");

$armorList = array();
$armorList['Head'] = 1;
$armorList["Neck"] = 2;
$armorList["Body"] = 3;
$armorList["Hands"] = 4;
$armorList["Waist"] = 5;
$armorList["Back"] = 6;
$armorList["Rings"] = 7;
$armorList["Ears"] = 8;
$armorList["Legs"] = 9;
$armorList["Feet"] = 10;
$armorList["Sub"] = 11;

$weaponList = array();
$weaponList["HandToHand"] = 1;
$weaponList["Dagger"] = 2;
$weaponList["Sword"] = 3;
$weaponList["GreatSword"] = 4;
$weaponList["Axe"] = 5;
$weaponList["GreatAxe"] = 6;
$weaponList["Scythe"] = 7;
$weaponList["PoleArm"] = 8;
$weaponList["Katana"] = 9;
$weaponList["GreatKatana"] = 10;
$weaponList["Staff"] = 11;
$weaponList["Club"] = 12;
$weaponList["Ranged"] = 13;
$weaponList["Archery"] = 14;
$weaponList["Marksmanship"] = 15;
$weaponList["Ammunition"] = 16;
$weaponList["Thrown"] = 17;
$weaponList["-"] = 18;
$weaponList["WindInstrument"] = 19;
$weaponList["StringInstrument"] = 20;
$weaponList["Special"] = 21;
$weaponList["Grips"] = 22;
$weaponList["Fishing"] = 23;

$attachmentListIn = array("Maneuver", "Fire", "Ice", "Wind", "Earth", "Thunder", "Lightning", "Water", "Light", "Dark");
$attachmentListOut = array("no", "no", "no", "no", "no", "no", "no", "no", "no", "no");

$lines = array("\r\n", "\n", "\r");

$toRoman = array('0', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X', 'XI', 'XII', 'XIV', 'XV', 'XVI', 'XVII', 'XVIII', 'XIX', 'XX', 'XXI', 'XXII', 'XXIII', 'XXIV', 'XXV');
$toHindu = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25');

$duplicates = array("San d'Orian Sword", "Judge's Sword", "Dancer's", "dancer's");

/*$properties array();
$properties['Weapons'] = array("Datum Type", "ID Number", "Wiki Name", 'Game Name', 'Singluar Long Name', 'Plural Long Name', 'Flags', 'Description', 'Stack', 'Target', 'Level', 'Slot', 'Race', 'Job', 'Damage', 'Delay', 'DPS', 'Weapon Type', "Number of Charges", "Cast Time", "Charge Time", "Recharge Time");
$properties['Armor'] = array("Datum Type", "ID Number", "Wiki Name", 'Game Name', 'Singluar Long Name', 'Plural Long Name', 'Flags', 'Description', 'Stack', 'Target', 'Level', 'Slot', 'Race', 'Job', 'Def', 'Shield Size', "Number of Charges", "Cast Time", "Charge Time", "Recharge Time");
$properties['General'] = array("Datum Type", "ID Number", "Wiki Name", 'Game Name', 'Singluar Long Name', 'Plural Long Name', 'Flags', 'Description', 'Stack', 'Target', "Moghancement Element", "Storage");
$properties['Usable'] = array("Datum Type", "ID Number", "Wiki Name", 'Game Name', 'Singluar Long Name', 'Plural Long Name', 'Flags', 'Description', 'Stack', 'Target', 'Use Time');
*/

if (!$textListing) {
    print "<form id=\"form1\" name=\"form1\" method=\"post\" action=\"Parse-O-Matic.php\">
  <label>Raw Text<br />
  <textarea name=\"textListing\" id=\"textListing\" cols=\"45\" rows=\"5\"></textarea>
  </label>
  <p>
    <label>
    <input type=\"submit\" name=\"Submit\" id=\"Submit\" value=\"Submit\" />
    </label>
    <label>
    <input type=\"reset\" name=\"Reset\" id=\"Reset\" value=\"Reset\" />
    </label>
  </p>
</form>";
} else {
    //$array = @file($rawList[$curFile].'.txt');
    $handleType = '';
    $subHandle = '';
    if (strpos($textListing, "FFXI Item") !== false) {
        $goodString = "FFXI Item";
        $goodNum = 7;
        $handleType = 'Item';
    } elseif (strpos($textListing, "Dialog Table Entry") !== false) {
        $goodString = "Dialog Table Entry";
        $goodNum = 2;
        $handleType = 'Diaglog';
    } else {
        $goodString = "String Table Entry";
        if (strpos($textListing, "-Permanent Key Items") !== false && strpos($textListing, "-Magical Maps") !== false && strpos($textListing, "job gestures: scholar") !== false) {
            $goodNum = 6;
            $handleType = 'Key Item';
        } elseif (strpos($textListing, "Use ninjutsu.") !== false && strpos($textListing, "Mag. Burst Bonus") !== false && strpos($textListing, "Reduces HP consumption when using Souleater.") !== false) {
            $goodNum = 2;
            $handleType = 'Ability';
        } elseif (strpos($textListing, "Revives target from KO.") !== false && strpos($textListing, "Absorbs a certain amount of damage from physical and magical attacks.") !== false && strpos($textListing, "Lowers the evasion of enemies within a fan-shaped area originating from the caster.") !== false) {
            $goodNum = 2;
            $handleType = 'Magic';
        } elseif (strpos($textListing, "Adjust your maximum HP and MP.") !== false && strpos($textListing, "Adjust various abilities for WAR lv.75 or above.") !== false && strpos($textListing, "Adjust various abilities for PUP lv.75 or above.") !== false) {
            $goodNum = 2;
            $handleType = 'Merit';
        } else {
            $goodNum = 3;
            $handleType = 'Quest';
        }
    }


    if (strlen($textListing) == 0) {
        print 'Empty text listing';
    } else {
        $combine = str_replace(array("\r\n", "\r", "\n", "<br />"), '[br]', $textListing);
        if ($handleType == 'Quest') {
            $combine = str_replace('+', '**', $combine);
        }
        $combine = preg_replace('/(\t)/', '|', $combine);
        if ($handleType != "Item" && $handleType != 'Key Item') {
            //$combine = htmlentities($combine, ENT_IGNORE, "UTF-8");
        }
        $data = explode($goodString, $combine);
        if (strlen($data[0]) == 0) {
            array_shift($data);
        }
        foreach ($data as $key => $datum) {
            $splode = explode('|', $datum);
            $splode[0] = $goodString;
            foreach ($splode as $k => $bit) {
                $bit = trim($bit);
                if (substr($bit, -4) == '[br]') {
                    $bit = substr($bit, 0, -4);
                }
                $splode[$k] = $bit;
            }
            $data[$key] = implode('|', $splode);
        }
        if ($handleType == 'Item') {
            $test = explode('|', $data[0]);
            $tyTest = explode('(', $test[1]);
            $reTest = str_replace(')', '', $tyTest[1]);
            $subHandle = '';
            $itemHandlesIn = array(0, 4096, 10240, 16384, 27648, 28672, 999999999999);
            $itemHandlesOut = array('General Item', 'Usable Item', 'Armor', 'Weapon', 'Armor', 'Voucher');
            foreach($itemHandlesOut as $k => $n) {
                $subHandle = ($reTest >= $itemHandlesIn[$k] && $reTest < $itemHandlesIn[$k+1]) ? $n : $subHandle;
            }
            $subHandle = ($subHandle == $reTest) ? "Unknown" : $subHandle;
            $level = array();
            $name = array();
            $type = array();
            $desc = array();
            $newData = array();
            //print_r($data);
            //print "<br />\r\n" . $data[0] . " <br /> " . $tyTest[0] . " | " . $tyTest[1] . " | restest =  $reTest | subHandle = $subHandle\r\n<br />\r\n<br />";
            foreach($data as $key => $curRow) {
                $curRow = trim($curRow);
                $stuff = explode('|', $curRow);
                if (strlen($stuff[7]) >1) {
                    if (substr($stuff[7], -4) == '[br]') {
                        $stuff[7] = str_replace('[br]', '| ', $stuff[7]);
                        $curRow = implode('|', $stuff);
                        $stuff = explode('|', $curRow);
                    }
                    $curName = trim($stuff[7]);
                    if ($subHandle == "Armor") {
                        $level[$key] = $stuff[11];
                        $name[$key] = $stuff[7];
                        $type[$key] = $armorList[$stuff[12]];
                        $desc[$key] = $stuff[8];
                    } elseif ($subHandle == "Weapon") {
                        $level[$key] = $stuff[11];
                        $name[$key] = $stuff[7];
                        $desc[$key] = strlen($stuff[8]);
                        if ($stuff[18] == 'None' && $stuff[12] == 'Sub') {
                            $type[$key] = $weaponList['Grips'];
                        } elseif ($stuff[18] == 'None' && ($stuff[12] == 'Range' || $stuff[12] == 'Ammo')) {
                            $type[$key] = $weaponList['-'];
                        } elseif ($stuff[18] == 'Ranged') {
                            $type[$key] = $weaponList['Archery'];
                        } else {
                            $type[$key] = $weaponList[$stuff[18]];
                        }
                    } else {
                        $type[$key] = ($stuff[4] == "Furnishing" | $stuff[4] == "Flowerpot" | $stuff[4] == 'Mannequin') ? 1 : 2;
                    }
                    //print $stuff[7]."\r\n<br />";
                    $newData[$key] = implode('|', $stuff);
                }
            }
            if ($subHandle == "Armor" || $subHandle == "Weapon") {
                array_multisort($type, SORT_ASC, $level, SORT_ASC, $name, SORT_ASC, $desc, SORT_ASC, $newData);
                $data = array();
                foreach($newData as $key => $curRow) {
                    $killLine = false;
                    $stuff = explode('|', $curRow);
                    foreach ($stuff as $k => $t) {
                        $stuff[$k] = trim($t);
                    }
                    $curName = trim($stuff[$goodNum]);
                    $wikiName = str_replace($ampIn, $ampOut, $curName);
                    $wikiName = html_entity_decode($wikiName, ENT_IGNORE, "UTF-8");
                    $spCheck = str_replace($specialWeaponIn, 'SpCheck', $wikiName);
                    if ($wikiList[$curName] == true || $wikiList[$wikiName] == true || $spCheck == 'SpCheck') {
                        $specialCheck = str_replace($specialWeaponIn, $specialWeaponOut, $wikiName);
                        $lv = $stuff[11];
                        $dupTest = str_ireplace($duplicates, "Do not use", $stuff[9]);
                        //print "$curName: $dupTest- " . $wikiList[$wikiName]. "\r\n<br />";
                        if ($specialCheck == "Relic" || $specialCheck == 'Empyrean' || $specialCheck == 'Mythic') {
                            if ($specialCheck == 'Mythic' && strpos($stuff[8], '[br]') === false) {
                                $comp = "(Incomplete)";
                            } else {
                                if ($wikiList[$curName . " (Level $lv)"] == true) {
                                    $checkNum = 1;
                                    $doCheck = true;
                                    while ($doCheck == true) {
                                        $checkNum ++;
                                        $roman = $toRoman[$checkNum];
                                        if ($wikiList[$curName . " (Level $lv $roman)"] == false) {
                                            $comp = "(Level $lv $roman)";
                                            $doCheck = false;
                                            break;
                                        }
                                    }
                                } else {
                                    $comp = "(Level $lv)";
                                }
                            }
                        } elseif (strpos($dupTest, "Do not use") !== false) {
                            $killLine = true;
                        } elseif ($lv >= 75) {
                            $comp = "(Level $lv)";
                        } else {
                            $killLine = true;
                        }
                        $wikiName .= " $comp";
                    }
                    $wikiList[$wikiName] = true;
                    $imHex = explode("(", $stuff[1]);
                    $imHex[1] = str_replace(')', '', $imHex[1]);
                    $idNum = substr('00000000', 0, (8-strlen($imHex[1]))).$imHex[1];
                    $sLongName = $stuff[9];
                    $pLongName = $stuff[10];
                    $description = str_replace($elementIn, $elementOut, $stuff[8]);
                    $race = str_replace($raceListIn, $raceListOut, $stuff[13]);
                    if ($subHandle == "Armor") {
                        $stuff[12] = trim($stuff[12]);
                        if ($stuff[12] == 'Ears') {
                            $stuff[12] = 'Earrings';
                        } elseif ($stuff[12] == 'Sub') {
                            $stuff[12] = 'Shields';
                        }
                        $defense = 0;
                        $defCheck = str_replace($defCheckIn, $defCheckOut, $description);
                        $defCheck = str_replace("<br />", ' ', $defCheck);
                        $defCheck = str_replace("[br]", ' ', $defCheck);
                        $defCheck = trim($defCheck);
                        $descExplode = explode(" ", $defCheck);
                        if (strstr($descExplode[0], "DEF:") !== false ) {
                            $defense = str_replace('DEF:', '', $descExplode[0]);
                            $defense = str_replace('"', '', $defense);
                            if (strstr($defense, "~") !== false) {
                                $defense = str_replace('~', '', $defense);
                            }
                        }
                        $newStuff = array("FFXI ".$stuff[4], $idNum, $wikiName, $curName, $sLongName, $pLongName, $stuff[2], $description, $stuff[3], $stuff[6], $stuff[11], $stuff[12], $race, $stuff[14], $defense, $stuff[15], $stuff[16], $stuff[17], $stuff[18], $stuff[19]);
                    } elseif ($subHandle == "Weapon") {
                        $stuff[18] = str_replace($skillListIn, $skillListOut, $stuff[18]);
                        if ($stuff[18] == 'None' && $stuff[12] == 'Sub') {
                            $stuff[18] = 'Grips';
                        } elseif ($stuff[18] == 'None' && ($stuff[12] == 'Range' || $stuff[12] == 'Ammo')) {
                            $stuff[18] = '-';
                        } elseif ($stuff[18] == 'Ranged') {
                            $skill = 'Archery';
                        } elseif ($stuff[18] == 'Special') {
                            $stuff[18] = 'Pet Items';
                        } elseif ($stuff[18] == 'Fishing') {
                            $stuff[18] = 'Fishing Gear';
                        }
                        $delayExplode = explode("(", $stuff[16]);
                        $delay = ($stuff[18] == "Hand-to-Hand") ? trim(str_replace(')', '', $delayExplode[1])) : trim($delayExplode[0]);
                        $newStuff = array("FFXI ".$stuff[4], trim($idNum), $wikiName, $curName, $sLongName, $pLongName, $stuff[2], $description, $stuff[3], $stuff[6], $stuff[11], $stuff[12], $race, $stuff[14], $stuff[15], $delay, $stuff[17], $stuff[18], $stuff[20], $stuff[21], $stuff[22], $stuff[23]);
                    }
                    if ($killLine == false) {
                       $data[$key] = implode('|', $newStuff);
                    }
                }
            } elseif ($subHandle == 'General Item' || $subHandle == 'Usable Item') {
                if ($subHandle == 'General Item') {
                    array_multisort($type, SORT_ASC, $newData);
                }
                $data = array();
                foreach($newData as $key => $curRow) {
                    $stuff = explode('|', $curRow);
                    foreach ($stuff as $k => $t) {
                        $stuff[$k] = trim($t);
                    }
                    $curName = $stuff[$goodNum];
                    $wikiName = html_entity_decode($wikiName, ENT_IGNORE, "UTF-8");
                    $wikiName = str_replace($ampIn, $ampOut, $curName);
                    if (strstr($stuff[2], "Scroll") !== false && (stristr($stuff[9], "scroll of") !== false || stristr($stuff[9], "Schema") !== false || stristr($stuff[9], "pact") !== false) || $curName == 'Klimaform') {
                        $wikiName .= " (Scroll)";
                    }
                    $wikiName = str_replace(array("and #9794;", "and #9792;"), array('M', 'F'), $wikiName);
                    if (strpos($wikiName, "and #") !== false) {
                        $wikiName = str_replace('and ', '&', $wikiName);
                    }
					$imHex = explode("(", $stuff[1]);
                    $imHex[1] = str_replace(')', '', $imHex[1]);
                    $idNum = substr('00000000', 0, (8-strlen($imHex[1]))).$imHex[1];
                    $sLongName = $stuff[9];
                    $pLongName = $stuff[10];
                    $description = str_replace($elementIn, $elementOut, $stuff[8]);
                    if (stristr($wikiName, "Mannequin") !== false) {
                        if (strstr($description, "female") !== false) {
                            $wikiName = str_replace("?", "F", $wikiName);
                        } elseif (strstr($description, "male") !== false) {
                            $wikiName = str_replace("?", "M", $wikiName);
                        }
                    } elseif (strstr($description, "[br]Functionality:[br]") !== false) {
                        $functDesc = explode('[br]Functionality:[br]', $description);
                        $functDesc[1] = str_replace("?", "", $functDesc[1]);
                        $functList = explode('[br]', $functDesc[1]);
                        foreach ($functList as $tk => $tm) {
                            $tm = trim($tm);
                            $test = str_replace($attachmentListIn, $attachmentListOut, $tm);
                            $ts = $test[0];
                            if ($ts == strtoupper($ts)) {
                                $functList[$tk] = "&nbsp;&nbsp;°" . $tm;
                            } else {
                                $functList[$tk] = "&nbsp;&nbsp;&nbsp;" . $tm;
                            }
                        }
                        $functDesc[1] = implode('[br]', $functList);
                        $description = implode('[br]Functionality:[br]', $functDesc);
                    }
                    $wikiList[$wikiName] = true;
                    if ($subHandle == 'General Item') {
                        $newStuff = array("FFXI ".$stuff[4], $idNum, $wikiName, $curName, $sLongName, $pLongName, $stuff[2], $description, $stuff[3], $stuff[6], $stuff[14], $stuff[15]);
                    } else {
                        $newStuff = array("FFXI ".$stuff[4], $idNum, $wikiName, $curName, $sLongName, $pLongName, $stuff[2], $description, $stuff[3], $stuff[6], $stuff[11]);
                    }
                    $data[$key] = implode('|', $newStuff);
                }
            } else {
                $data = array();
                foreach($newData as $key => $curRow) {
                    $stuff = explode('|', $curRow);
                    foreach ($stuff as $k => $t) {
                        $stuff[$k] = trim($t);
                    }
                    $curName = trim($stuff[$goodNum]);
                    $wikiName = str_replace($ampIn, $ampOut, $curName);
                    $wikiName = html_entity_decode($wikiName, ENT_IGNORE, "UTF-8");
                    if (strpos($wikiName, "and #") !== false) {
                        //$wikiName = str_replace('and ', '&', $wikiName);
                    }

                    $imHex = explode("(", $stuff[1]);
                    $imHex[1] = str_replace(')', '', $imHex[1]);
                    $idNum = substr('00000000', 0, (8-strlen($imHex[1]))).$imHex[1];
                    $sLongName = $stuff[9];
                    $pLongName = $stuff[10];
                    $description = str_replace($elementIn, $elementOut, $stuff[8]);
                    $wikiList[$wikiName] = true;

                    $newStuff = array("FFXI ".$stuff[4], $idNum, $wikiName, $curName, $sLongName, $pLongName, $stuff[2], $description, $stuff[3], $stuff[6]);
                    $data[$key] = implode('|', $newStuff);
                }
            }
        } elseif ($handleType == 'Key Item') {
            $tempArr = array();
            $arrKey = array();
            $curArr = 0;
            $tempArr[0] = array();
            foreach ($data as $key => $curData) {
                $dat = explode('|', $curData);
                $dat[$goodNum] = trim($dat[$goodNum]);
                if (substr($dat[$goodNum], 0, 1) == '-' ) {
                    $arrKey[$curArr] = substr($dat[$goodNum], 1);
                    $curArr++;
                    $tempArr[$curArr] = array();
                    
                } elseif ($dat[$goodNum] != 'Spiffy Synth.' && $dat[$goodNum] != 'KEY ITEM' && $dat[$goodNum] != '?????????' && strlen($dat[$goodNum]) > 1) {
                    if (substr($dat[8], 0, 4) == '[br]') {
                        $dat[8] = substr($dat[8], 4);
                        $curData = implode('|', $dat);
                    }
                    array_push($tempArr[$curArr], $curData);
                }
            }
            $data = array();
            foreach ($tempArr as $i => $tempBatch) {
                $curType = $arrKey[$i];
                foreach ($tempArr[$i] as $j => $currow) {
                    $splode = explode('|', $currow);
                    $splode[count($splode)-1] = $curType;
                    $currow = implode('|', $splode);
                    array_push($data, $currow);
                }
            }
        } elseif ($handleType == 'Diaglog') {
            $stuff = implode('|!|', $data);
            $stuff = str_replace($elementIn, $elementOut, $stuff);
            $data = explode('|!|', $stuff);
        } elseif ($handleType == 'Merit') {
            $holder = array();
            $c = count($data);
            for ($i = 0; $i < $c; $i+=2) {
                $data[$i] = explode('|', $data[$i]);
                $data[$i+1] = explode('|', $data[$i+1]);
                if (count($data[$i][2]) > 0 && strpos($data[$i][2], 'MeripoName') === false)  {
                    $arr = array($data[$i][0], $data[$i][2], $data[$i+1][2], '');
                    array_push($holder, implode('|', $arr));
                }
            }
            $data = $holder;
        }
        foreach($data as $curRow) {
            $thisrow = explode('|', $curRow);
            if ($handleType == 'Item') {
                //print $curRow."\r\n<br />";
                if (strlen($thisrow[3]) > 1) {
                    print $curRow."\r\n<br />";
                }
            } elseif ($handleType == 'Magic') {
                if (strstr($thisrow[2], "(magic ") === false && $thisrow[2] != 'jet-stream-attack' && $thisrow[2] != "." && $thisrow[2] != "" && $thisrow[2] != "(NULL)" && $thisrow[2] != 'dummy') {
                    print $curRow."\r\n<br />";
                }
            } else {
                //print $curRow."\r\n<br />";
                if (strlen($thisrow[$goodNum]) > 1) {
                    print $curRow."\r\n<br />";
                }
            }
        }
    }
}
    

