var blockTypes=[],blockType="",param="";blockTypes.gamemode={};blockTypes.menumode={};blockTypes.onactivate={};blockTypes.onactorequip={};blockTypes.onactorunequip={};blockTypes.onadd={};blockTypes.onclose={};blockTypes.oncombatend={};blockTypes.ondeath={};blockTypes.ondestructionstagechange={};blockTypes.ondrop={};blockTypes.onequip={};blockTypes.ongrab={};blockTypes.onhit={};blockTypes.onhitwith={};blockTypes.onload={};blockTypes.onmagiceffecthit={};blockTypes.onmurder={};blockTypes.onopen={};
blockTypes.onpackagechange={};blockTypes.onpackagedone={};blockTypes.onpackagestart={};blockTypes.onrelease={};blockTypes.onreset={};blockTypes.onsell={};blockTypes.onstartcombat={};blockTypes.ontrigger={};blockTypes.ontriggerenter={};blockTypes.ontriggerleave={};blockTypes.onunequip={};blockTypes.saytodone={};blockTypes.scripteffectfinish={};blockTypes.scripteffectstart={};blockTypes.scripteffectfinish={};
var input,output=[],error,hasScriptName,linesOutOfBlock,valid=new Boolean(true),conditionalLevel,elseDone=[],inBlock=new Boolean(false),blockNumber,currentLine,currentLineNumber,index,keywordLength,indentLevel,indentIncrement,whitespace=[" ","\t"],numbers=["0","1","2","3","4","5","6","7","8","9"];function isInList(a,c){for(var b in a)if(c==a[b])return true;return false}function skipCharList(a,c,b){for(;isInList(a,c.substr(b,1));)b++;return b}
function skipToCharList(a,c,b){for(;!isInList(a,c.substr(b,1))&&b!=c.length;)b++;return b}
function validateVariableDeclaration(a,c){keywordLength=a.length;if(currentLine.length==keywordLength)error+='Variable name required for "'+a+'" variable declaration on line '+(currentLineNumber+1)+"<br />";else{index=skipCharList(whitespace,currentLine,keywordLength);if(currentLine.substr(index,1)==";")error+='Variable name required for "'+a+'" variable declaration on line '+(currentLineNumber+1)+"<br />";else if(index==keywordLength)valid=false}if(valid==true)if(inBlock==true)error+='"'+a+'" variable declaration on line '+
(currentLineNumber+1)+" illegal within Begin/End block<br />";else{if(!blockNumber&&hasScriptName!=-1&&c==false)error+='*"'+a+'" variable declaration on line '+(currentLineNumber+1)+" not allowed in result script<br />"}else inBlock==false&&linesOutOfBlock++}
function validateScript(){input=document.getElementById("scriptInput").value.split("\n").length>document.getElementById("scriptInput").value.split("\r\n").length?document.getElementById("scriptInput").value.split("\n"):document.getElementById("scriptInput").value.split("\r\n");output=[];error="";linesOutOfBlock=hasScriptName=0;valid=true;conditionalLevel=0;elseDone=[];inBlock=false;for(currentLineNumber=indentIncrement=indentLevel=blockNumber=0;currentLineNumber<input.length;currentLineNumber++){currentLine=
input[currentLineNumber];for(valid=true;isInList(whitespace,currentLine.substr(0,1));)currentLine=currentLine.substring(1);for(;isInList(whitespace,currentLine.substr(currentLine.length-1,1));)currentLine=currentLine.substr(0,currentLine.length-1);if(currentLine.length){output[currentLineNumber]=currentLine+"\n";currentLine=currentLine.toLowerCase();if(!currentLine.indexOf("scn")||!currentLine.indexOf("scriptname")){keywordLength=currentLine.indexOf("scn")?10:3;if(currentLine.length==keywordLength)error+=
"EditorID required for ScriptName declaration on line "+(currentLineNumber+1)+"<br />";else{index=skipCharList(whitespace,currentLine,keywordLength);if(currentLine.substr(index,1)==";")error+="EditorID required for ScriptName declaration on line "+(currentLineNumber+1)+"<br />";else if(isInList(numbers,currentLine.substr(index,1)))error+="EditorID specified in ScriptName declaration on line "+(currentLineNumber+1)+" starts with a number<br />";else if(index==keywordLength)valid=false}if(valid==true)switch(hasScriptName){case -1:error+=
"Extra ScriptName declaration on line "+(currentLineNumber+1)+"<br />";break;case 1:error+="ScriptName declaration on line "+(currentLineNumber+1)+" invalid.  ScriptName declarations must take place at the top of the script<br />";break;default:hasScriptName=-1}else inBlock==false&&linesOutOfBlock++}else{if(!hasScriptName)if(currentLine.indexOf(";"))hasScriptName=1;if(currentLine.indexOf("if"))if(currentLine.indexOf("elseif"))if(currentLine.indexOf("else"))if(currentLine.indexOf("endif"))if(currentLine.indexOf("begin"))if(currentLine.indexOf("end"))if(currentLine.indexOf("long"))if(currentLine.indexOf("short"))if(currentLine.indexOf("int"))if(currentLine.indexOf("float"))if(currentLine.indexOf("reference"))if(currentLine.indexOf("ref"))if(currentLine.indexOf("set"))currentLine.indexOf(";")&&
inBlock==false&&linesOutOfBlock++;else{keywordLength=3;if(currentLine.length==keywordLength)error+='Variable name required for "set" command on line '+(currentLineNumber+1)+"<br />";else{index=skipCharList(whitespace,currentLine,keywordLength);if(currentLine.substr(index,1)==";")error+='Variable name required for "set" command on line '+(currentLineNumber+1)+"<br />";else if(index==keywordLength)valid=false;else{index=skipToCharList(whitespace.concat([";"]),currentLine,index);if(currentLine.substr(index,
1)==";")error+='Accompanying "to" required for "set" command on line '+(currentLineNumber+1)+"<br />";else{index=skipCharList(whitespace,currentLine,index);if(currentLine.substr(index,2)!="to")error+='Accompanying "to" required for "set" command on line '+(currentLineNumber+1)+"<br />";else{keywordLength=index+2;index=skipToCharList(whitespace.concat([";","("]),currentLine,index);if(index!=keywordLength)error+='Accompanying "to" required for "set" command on line '+(currentLineNumber+1)+"<br />";
else if(index==currentLine.length||currentLine.substr(index,1)==";")error+='Expression required for "set" command on line '+(currentLineNumber+1)+"<br />";else if(currentLine.substr(index,1)!="("){index=skipCharList(whitespace,currentLine,index);if(currentLine.substr(index,1)==";")error+='Expression required for "set" command on line '+(currentLineNumber+1)+"<br />"}}}}}inBlock==false&&linesOutOfBlock++}else validateVariableDeclaration("ref",true);else validateVariableDeclaration("reference",true);
else validateVariableDeclaration("float",false);else validateVariableDeclaration("int",false);else validateVariableDeclaration("short",false);else validateVariableDeclaration("long",false);else{keywordLength=3;if(currentLine.length>keywordLength){index=skipCharList(whitespace,currentLine,keywordLength);if(index==keywordLength&&currentLine.substr(index,1)!=";")valid=false;else if(currentLine.substr(index,1)!=";")error+="Condition not allowed for End statement on line "+(currentLineNumber+1)+"<br />"}if(valid==
true){indentLevel=0;if(inBlock==true)inBlock=false;else error+=blockNumber?"Extra End statement on line "+(currentLineNumber+1)+"<br />":"End statement on line "+(currentLineNumber+1)+" not valid unless preceeded by Begin statement<br />";if(conditionalLevel==1){error+=blockNumber?'Missing "endif" statement in block '+blockNumber+"<br />":'Missing "endif" statement<br />';conditionalLevel=0}else if(conditionalLevel){error+=blockNumber?conditionalLevel+' missing "endif" statements in block '+blockNumber+
"<br />":conditionalLevel+' missing "endif" statements<br />';conditionalLevel=0}}else inBlock==false&&linesOutOfBlock++}else{keywordLength=5;if(currentLine.length==keywordLength)error+="Blocktype required for Begin statement on line "+(currentLineNumber+1)+"<br />";else{index=skipCharList(whitespace,currentLine,keywordLength);if(currentLine.substr(index,1)==";")error+="Blocktype required for Begin statement on line "+(currentLineNumber+1)+"<br />";else if(index==keywordLength)valid=false;else{keywordLength=
index;index=skipToCharList(whitespace.concat(";"),currentLine,index);blockType=currentLine.substring(keywordLength,index);blockTypes[blockType]||(error+="Invalid blocktype for Begin statement on line "+(currentLineNumber+1)+"<br />")}}if(valid==true){indentLevel=0;indentIncrement=1;if(inBlock==true){error+="Missing End statement in block "+blockNumber+"<br />";blockNumber++}else{inBlock=true;blockNumber++;if(linesOutOfBlock){error+=linesOutOfBlock>1?linesOutOfBlock+" lines of potentially valid code outside of Begin/End blocks before block number "+
blockNumber+" (line "+(currentLineNumber+1)+")<br />":linesOutOfBlock+" line of potentially valid code outside of Begin/End blocks before block number "+blockNumber+" (line "+(currentLineNumber+1)+")<br />";linesOutOfBlock=0}}}else inBlock==false&&linesOutOfBlock++}else{keywordLength=5;if(currentLine.length>keywordLength){index=skipCharList(whitespace,currentLine,keywordLength);if(index==keywordLength&&currentLine.substr(index,1)!=";")valid=false;else if(currentLine.substr(index,1)!=";")error+='Condition not allowed for "endif" statement on line '+
(currentLineNumber+1)+"<br />"}if(valid==true)if(conditionalLevel){elseDone[conditionalLevel]=0;conditionalLevel--;indentLevel--}else error+='Extra "endif" statement on line '+(currentLineNumber+1)+"<br />";inBlock==false&&linesOutOfBlock++}else{keywordLength=4;if(currentLine.length>keywordLength){index=skipCharList(whitespace,currentLine,keywordLength);if(index==keywordLength&&currentLine.substr(index,1)!=";")valid=false;else if(currentLine.substr(index,1)!=";")error+='Condition not allowed for "else" statement on line '+
(currentLineNumber+1)+"<br />"}if(valid==true){indentLevel--;indentIncrement=1;if(elseDone[conditionalLevel])error+='"else" statement on line '+(currentLineNumber+1)+' not valid after "else" statement on line '+elseDone[conditionalLevel]+"<br />";else if(!conditionalLevel){error+='"else" statement on line '+(currentLineNumber+1)+' not valid unless preceeded by "if" statement<br />';conditionalLevel++;indentLevel++}elseDone[conditionalLevel]=currentLineNumber+1}inBlock==false&&linesOutOfBlock++}else{keywordLength=
6;if(currentLine.length==keywordLength)error+='Condition required for "elseif" statement on line '+(currentLineNumber+1)+"<br />";else{index=skipCharList(whitespace,currentLine,keywordLength);if(isInList([";",")"],currentLine.substr(index,1)))error+='Condition required for "elseif" statement on line '+(currentLineNumber+1)+"<br />";else if(index==keywordLength&&currentLine.substr(index,1)!="(")valid=false}if(valid==true){indentLevel--;indentIncrement=1;if(elseDone[conditionalLevel])error+='"elseif " statement on line '+
(currentLineNumber+1)+' not valid after "else" statement on line '+elseDone[conditionalLevel]+"<br />";else if(!conditionalLevel){error+='"elseif" statement on line '+(currentLineNumber+1)+' not valid unless preceeded by "if" statement<br />';conditionalLevel++;indentLevel++}}inBlock==false&&linesOutOfBlock++}else{keywordLength=2;if(currentLine.length==keywordLength)error+='Condition required for "if" statement on line '+(currentLineNumber+1)+"<br />";else{index=skipCharList(whitespace,currentLine,
keywordLength);if(index!=keywordLength||currentLine.substr(index,1)==";"){if(isInList([";",")"],currentLine.substr(index,1)))error+='Condition required for "if" statement on line '+(currentLineNumber+1)+"<br />"}else if(currentLine.substr(index,1)!="(")valid=false}if(valid==true){indentIncrement=1;conditionalLevel++;if(conditionalLevel>10)error+='Too many nested "if" conditions on line '+(currentLineNumber+1)+"<br />"}inBlock==false&&linesOutOfBlock++}}for(index=0;index<indentLevel;index++)output[currentLineNumber]=
"\t"+output[currentLineNumber];indentLevel+=indentIncrement;indentIncrement=0}else output[currentLineNumber]="\n"}if(inBlock==true)error+="Missing End statement in block "+blockNumber+"<br />";if(linesOutOfBlock)if(blockNumber)error+=linesOutOfBlock>1?linesOutOfBlock+" lines of potentially valid code outside of Begin/End blocks after block number "+blockNumber+"<br />":linesOutOfBlock+" line of potentially valid code outside of Begin/End blocks after block number "+blockNumber+"<br />";else if(hasScriptName==
-1)error+=linesOutOfBlock>1?linesOutOfBlock+" lines of potentially valid code outside of Begin/End blocks<br />":linesOutOfBlock+" line of potentially valid code outside of Begin/End blocks<br />";if(conditionalLevel==1)error+=blockNumber&&inBlock?'Missing "endif" statement in block '+blockNumber+"<br />":'Missing "endif" statement<br />';else if(conditionalLevel)error+=blockNumber&&inBlock?conditionalLevel+' missing "endif" statements in block '+blockNumber+"<br />":conditionalLevel+' missing "endif" statements<br />';
currentLine=output.join("");document.getElementById("scriptInput").value=currentLine.substr(0,currentLine.length-1);if(error){input=error.split("<br />");error="";for(currentLineNumber in input){currentLine=input[currentLineNumber];if(currentLine.indexOf("*"))currentLine+="<br />";else currentLine=linesOutOfBlock&&!blockNumber&&!hasScriptName!=-1?currentLine.substring(1)+"<br />":"";error+=currentLine}document.getElementById("error").innerHTML=error;document.getElementById("error").style.color="#ff0000"}else if(output){document.getElementById("error").innerHTML=
"No errors were found";document.getElementById("error").style.color="#008000"}else document.getElementById("error").innerHTML=""};
