Advertisement
AndrewHaxalot

gct.js

Jan 18th, 2014
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //DO NOT REMOVE THE COPYRIGHT NOTICE BELOW
  2. //this document copyright 2009-2010 James Atherton
  3.  
  4. function trim(Bob)
  5. {
  6.     return Bob.replace(/^\s+|\s+$/g,"");
  7. }
  8.  
  9.  
  10. var Code="<a href='#editbox' onclick=\"Edit('c[count]'); return false\">Edit</a> <input type='checkbox' name='t[count]' value='0' onClick=\"Order(this);\"> [codetitle]\n<input type='hidden' name='T[count]' value='[codetitle]'><textarea name='c[count]' rows=5 cols=40 class='hide'>[codebody]</textarea>[hasnote]<div class=note id='n[count]'>[note]</div><br>";
  11.  
  12. function IsCode(Bob) //bob is the code string
  13. {
  14.     var Code = trim(Bob.replace(/\r/gi, ""));
  15.     var Lines=Code.split("\n");
  16.     for(var i=0; i<Lines.length; i++)
  17.     {
  18.         Lines[i]=trim(Lines[i]);
  19.         if(!(Lines[i].length==17 && Lines[i].charAt(8)==' '))
  20.             return 0;
  21.     }
  22.     Code = Code.replace(/\n/gi, "").replace(/ /gi, "");
  23.     if(Code.length%8!=0)
  24.         return 0;
  25.     for(var i=0; i<Code.length; i++)
  26.     {
  27.         if(!Code.charAt(i).match(/^[A-F0-9]$/))
  28.             return 0;
  29.     }
  30.     return Lines.length; //number of lines in the code
  31. }
  32.  
  33. function Order(Bob)
  34. {
  35.     if(Bob.checked)
  36.     {
  37.         Bob.value=IsCode(document.gct[Bob.name.replace("t", "c")].value);
  38.         if(Bob.value==0)
  39.         {
  40.             Bob.checked=false;
  41.             Edit(Bob.name.replace("t", "c"));
  42.             location.href="#editbox"; //might be annoying...
  43.             return;
  44.         }
  45.         document.gct.gctorder.value+=Bob.name+' ';
  46.     }
  47.     else
  48.         document.gct.gctorder.value=document.gct.gctorder.value.replace(Bob.name+' ', "");
  49. }
  50.  
  51. function AddCodes()
  52. {
  53.     var byline;
  54.     var endline="\n";
  55.     byline=document.gct.newcodes.value.replace(/\r/gi, "").replace(/</gi,"").replace(/>/gi,"").split(endline);
  56.    
  57.     var FinalOutput="";
  58.     for(var i=0; i<byline.length; i++)
  59.     {
  60.         var ToWrite=Code;
  61.         var Total=document.gct.total.value;
  62.         if(byline[i]!="")
  63.         {
  64.             ToWrite=ToWrite.replace("[count]", Total);
  65.             ToWrite=ToWrite.replace("[codetitle]", byline[i]);
  66.             ToWrite=ToWrite.replace("[count]", Total);
  67.             ToWrite=ToWrite.replace("[codetitle]", byline[i].ReplaceAll("'", ""));
  68.             ToWrite=ToWrite.replace("[count]", Total);
  69.             ToWrite=ToWrite.replace("[count]", Total);
  70.             ToWrite=ToWrite.replace("[count]", Total);
  71.             i++;
  72.             document.gct.total.value++;
  73.             if(i<byline.length && byline[i]=="" || i==byline.length)
  74.             {
  75.                 ToWrite="<div class=sub>"+byline[i-1]+"</div>";
  76.             }
  77.             for(; i<byline.length && byline[i]!="" && trim(byline[i]).length==17 && trim(byline[i]).charAt(8)==' ';  i++)
  78.             {
  79.                 ToWrite=ToWrite.replace("[codebody]", trim(byline[i])+endline+"[codebody]");
  80.             }
  81.             ToWrite=ToWrite.replace("[codebody]", "");
  82.                                     //if(byline[i-1]=="")
  83.                                     //  i--;
  84.             for(; i<byline.length && byline[i]!=""; i++)
  85.             {
  86.                 ToWrite=ToWrite.replace("[note]", trim(byline[i])+"<br>[note]");
  87.                 ToWrite=ToWrite.replace("[hasnote]", "<a href='#' onclick=\"Toggle('n"+Total+"'); return false;\">Note</a>");
  88.             }
  89.             ToWrite=ToWrite.replace("[note]", "");
  90.             ToWrite=ToWrite.replace("[hasnote]", "");
  91.             FinalOutput+=ToWrite;
  92.         }
  93.     }
  94.     document.getElementById("gctlist").innerHTML+=FinalOutput;
  95.     document.gct.newcodes.value="";
  96.    
  97.     //re-check selected codes
  98.     byline=trim(document.gct.gctorder.value).split(" ");
  99.     for(var i=0; i<byline.length; i++)
  100.     {
  101.         if(document.gct[byline[i]])
  102.             document.gct[byline[i]].checked=true;
  103.     }
  104.    
  105.     CancelNew(); //hides add more codes box
  106. }
  107.  
  108. function Edit(Bob)
  109. {
  110.     if(document.gct[Bob])
  111.     {
  112.         document.gct.editarea.value=document.gct[Bob].value;
  113.         document.gct.editing.value=Bob;
  114.         document.gct.editareaTitle.value=document.gct[Bob.replace("c", "T")].value;
  115.         document.gct.editareaNote.value=document.getElementById(Bob.replace("c", "n")).innerHTML.ReplaceAll("<br>", "\n").ReplaceAll("<BR>", "\n");
  116.         document.getElementById("gctedit").style.display = "block";
  117.     }
  118. }
  119.  
  120. function ApplyEdit()
  121. {
  122.     if(document.gct[document.gct.editing.value] && IsCode(document.gct.editarea.value)!=0)
  123.     {
  124.         document.gct[document.gct.editing.value].value=document.gct.editarea.value;
  125.         document.gct[document.gct.editing.value.replace("c", "t")].value=IsCode(document.gct.editarea.value);
  126.         document.gct.editing.value="";
  127.         document.gct.editarea.value="";
  128.         document.getElementById("gctedit").style.display = "none";
  129.     }
  130.     else
  131.     {
  132.         alert("This is not a code.");
  133.     }
  134. }
  135.  
  136. function CancelEdit()
  137. {
  138.     document.gct.editing.value="";
  139.     document.gct.editarea.value="";
  140.     document.getElementById("gctedit").style.display = "none";
  141. }
  142.  
  143. function ShowNew()
  144. {
  145.     document.getElementById("gctnew").style.display = "block";
  146.     location.href="#addcodes";
  147. }
  148.  
  149. function CancelNew()
  150. {
  151.     document.gct.newcodes.value="";
  152.     document.getElementById("gctnew").style.display = "none";
  153. }
  154.  
  155. function Toggle(Bob)
  156. {
  157.     if(document.getElementById(Bob).style.display!="block")
  158.         document.getElementById(Bob).style.display = "block";
  159.     else
  160.         document.getElementById(Bob).style.display = "none";
  161. }
  162.  
  163. function CreateGCT()
  164. {
  165.     document.fin.gctdata.value="00D0C0DE00D0C0DE";
  166.     var Bob=trim(document.gct.gctorder.value).replace(/t/gi,"c").split(" ");
  167.     for(var i=0; i<Bob.length; i++)
  168.     {
  169.         if(document.gct[Bob[i]])
  170.             document.fin.gctdata.value+=document.gct[Bob[i]].value.replace(/\r/gi, "").replace(/\n/gi, "").replace(/ /gi, "");
  171.     }
  172.     if(document.fin.gctdata.value=="00D0C0DE00D0C0DE")
  173.     {
  174.         alert("No codes selected");
  175.     }
  176.     else
  177.     {
  178.         document.fin.gctdata.value+="FF00000000000000";
  179.         document.fin.gctname.value=document.gct.titleid.value;
  180.         //document.gct.newcodes.value=document.fin.gctdata.value; //debug output
  181.         document.fin.submit();
  182.     }
  183. }
  184.  
  185. /*ReplaceAll function from http://www.codedigest.com*/
  186. String.prototype.ReplaceAll = function(xx,yy){
  187.     var temp = this;
  188.     var index = temp.indexOf(xx);
  189.     while(index != -1){
  190.         temp = temp.replace(xx,yy);
  191.         index = temp.indexOf(xx);
  192.     }
  193.     return temp;
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement