/* Caesar shift -- the first javascript program I ever wrote :-) */

alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789<>,.';  //string for the alphabet
cipher = 'bcdefghijklmnopqrstuvwxyzaBCDEFGHIJKLMNOPQRSTUVWXYZA1234567890><.,';  //string for the encoded text

textarray = new Array();        //this will hold the user input
encrypted = new String();       //this will hold the encrypted/decrypted output
clear = new String();              //this is used to reset a string to null
stringholder = new String();              //this is used to convert an array to a string later on
i = 0;                                //I declared my counter as a global variable

function shift(){              //this function does the encrypting
   counter = 0;
   upperlower = /[a-zA-Z0-9\<\>\,\.]/;              //this is a regular expression for upper and lowercase letters
   textarray = textentered.split('');         //split the input string into an array
   for (i=0;i<= (textarray.length - 1);i++){      //this is the loop for the encrypting
      teststring = textarray[i];                   //this converts the array back into a string

    if (document.getElementById("encryptit").checked){  //routine for encrypting

     if (teststring.match(upperlower)){                 //if the character is a number
     counter = alphabet.indexOf(textarray[i]);            //it finds the position in the alphabet
     encrypted = encrypted + cipher.charAt(counter);}      //and adds the corresponding encrypted letter to the output
     else {
     encrypted = encrypted + teststring; }               //otherwise, it adds the character as is
     }

     if (document.getElementById("decryptit").checked){  //routine for decrypting

     if (teststring.match(upperlower)){                  //this routine functions in the exact opposite as the above
     counter = cipher.indexOf(textarray[i]);
     encrypted = encrypted + alphabet.charAt(counter);}
     else { encrypted = encrypted + teststring; }
     }
   }
    document.form1.textentry.value = clear;          //clear the user input
    document.form1.textentry.value = encrypted;      //substitute the encrypted/decrypted text
}


function actionable(){      //this is what happens when the shit goes down
   evilkruger('#E5D40B');
   textentered = clear;
   encrypted = clear;      //clear the fields
   textentered = document.form1.textentry.value;
   foobar = document.getElementById('foobar');
   foobar.className = 'focused';
   setTimeout("foobar.className = 'blurred';",100);

	switch(document.getElementById('encrypt_method').value) {

	case 'shift':
		shift();
	break;

	case 'md5':
	   if (document.getElementById("encryptit").checked){
		   encrypted = hex_md5(document.form1.textentry.value);
		   document.form1.textentry.value = encrypted;      //substitute the encrypted/decrypted text
	   }
	break;	
	} //close switch()
}

function testRegex() {
	evilkruger('#E5200B');
	$('match').innerHTML = '';
	$('capture').innerHTML = '';
	var needle = $('regex').value;
	var haystack = $('foobar').value;
	//console.dir( needle );
	
     var myreg = new RegExp(needle);
	 //console.dir( myreg );
     var thisMatch = myreg.exec(haystack, 'g');
	 try { console.dir( thisMatch ); } catch(e) { }

     if (thisMatch == null) {
	 	return; //no match
		} else {
			if (thisMatch['index'] || thisMatch['index']===0) {
				$('match').innerHTML = 'Matched <span class="match">' + thisMatch[0] + '</span> at char ' + thisMatch['index'];
			}
			if (thisMatch[1]) {
				$('capture').innerHTML = 'Captured: <span class="match">' + thisMatch[1] + '</span>';
			}
		}
}

function focusTwist( dir ) {
if (dir =='decryptit') {
	document.getElementById('checked1').style.backgroundColor = '#e5e5e5';
	document.getElementById('checked2').style.backgroundColor = '#eeeeee';
	} else {
	document.getElementById('checked1').style.backgroundColor = '#eeeeee';
	document.getElementById('checked2').style.backgroundColor = '#e5e5e5';
	}
}

function help(){
   switch(window.currTab) {
	case 1:
      $('help').innerHTML = 'Use the shift encoding to shift the alphabetic characters in the textarea by 1 character at a time. Probly more useful is the md5 function, which was taken from <a target="_blank" href="http://pajhome.org.uk/crypt/md5/">Paj\'s site</a>';
		break;
	case 2:
      $('help').innerHTML = 'This is a regex tester I wrote in Javascript. It supports text capturing of a single value.';
		break;
	case 3:      
      $('help').innerHTML = 'This is a loop utility used to generate text containing a series of ascending numbers, or to generate JS or PHP code that will generate this text using a loop.  Write the text you want to repeat, using $$$ as a placeholder for the ascending number. For an example, <a href="javascript: loopExample()">click here</a>';
		break;
}

   //$('help').style.display='inline';
   Effect.Appear('help', {
      'to': 0.8
   });
   setTimeout("Effect.Fade('help')", 30000);
}

function loopExample(){
   $('foobar').value = "INSERT INTO Sigmunds (mouth) VALUES ('$$$ white castle burgers'); \n --Now click \"Loop It\"\n\n";
   $('startnumber').value = '1';
   $('endnumber').value = '5';
}

/* Stuff for Loop */

   function copyme() {
     document.form1.textentry.select();
     therange=document.selection.createRange();
     therange.execCommand("Copy");
  }


  function loopAction(){
     if (document.form1.radio2[0].checked) gentext();
     if (document.form1.radio2[1].checked) genjavascript();
     if (document.form1.radio2[2].checked) genphp();
  }

  function blank(){
     document.form1.textentry.value = '';
     document.form1.startnumber.value = '';
     document.form1.endnumber.value = '';
  }

  
  function gentext(){
     var textentry = document.form1.textentry.value;
     var startnumber = document.form1.startnumber.value;
     var endnumber = document.form1.endnumber.value;
     var howmany = endnumber - startnumber + 1;
     var printthis = '';
     textentry = document.form1.textentry.value;
     textentryhold = document.form1.textentry.value;
     document.form1.textentry.value = '';
   
     for (i=0;i<howmany;i++){
        var printthis = '';
        textentry = textentry.replace(/\$\$\$/g, startnumber);
        printthis = textentry + "\n";
        document.form1.textentry.value += printthis;
        textentry = textentryhold;
        startnumber++;
        }
  }


  function genjavascript(){
     var textentry = document.form1.textentry.value;
     replacewith = '\\"';
     textentry = textentry.replace(/"/g, replacewith);
     replacewith = "\\'";
     textentry = textentry.replace(/'/g, replacewith);
     textentry = '\'' + textentry + '\'';
     replacewith = '\' + startnumber + \'';
     textentry = textentry.replace(/\$\$\$/g, replacewith);
     var printthis = 'var startnumber = '+ document.form1.startnumber.value +';\nvar endnumber = '+ document.form1.endnumber.value +'; \nvar howmany = endnumber - startnumber + 1;\nfor (i=0;i<howmany;i++){\n';
     printthis += 'holdthis = ' + textentry;
     printthis += ';\n';
     printthis += 'startnumber++;\n';
     printthis += '}';
   
     document.form1.textentry.value = printthis;
  }


  function genphp(){
     var textentry = document.form1.textentry.value;
     //replacewith = '\\"';
     //textentry = textentry.replace(/"/g, replacewith);
     replacewith = "\\'";
     textentry = textentry.replace(/'/g, replacewith);
     textentry = '\'' + textentry + '\'';
     replacewith = '\' . \"$startnumber\" . \'';
     textentry = textentry.replace(/\$\$\$/g, replacewith);
     var printthis = '$startnumber = '+ document.form1.startnumber.value +';\n$endnumber = '+ document.form1.endnumber.value +';\n$howmany = $endnumber - $startnumber + 1;\nfor ($i=0;$i<$howmany;$i++){\n';
     printthis += '$holdthis = ' + textentry;
     printthis += ';\n';
     printthis += '$startnumber++;\n';
     printthis += '}';
   
     document.form1.textentry.value = printthis;
  }



  function appendtext(){

  var textentry = document.form1.textentry.value;
  var texttoappend = document.form1.texttoappend.value;
  var printthis = "";
  var delimit = /\n/;
  var remove = "test";

  var splitentry = textentry.split(/\n/);
  var counter = splitentry.length;

  for (i=0;i<counter;i++){
     remove = splitentry[i];
     remove = remove.replace(/\n|\r/, "");
     printthis += remove + texttoappend + "\n";
     }
     document.form1.textentry.value = printthis;
  }



  function appendnumber(){

     var textentry = document.form1.textentry.value;
     var startnumber = document.form1.startnumber.value;
     var endnumber = document.form1.endnumber.value;
     var howmany = endnumber - startnumber + 1;
     var printthis = "";
     var delimit = /\n/;
     var remove = "test";
   
     var splitentry = textentry.split(/\n/);
   
     for (i=0;i<howmany;i++){
     remove = splitentry[i];
     if (remove != null) remove = remove.replace(/\n|\r/, "");
     if (remove != null) printthis += remove + startnumber + "\n";
     startnumber++;
     }
     document.form1.textentry.value = printthis;
  }

/*-----------------------------------------------------------
    Toggles element's display value
    Input: any number of element id's
    Output: none 
    ---------------------------------------------------------*/
function toggleDisp() {
    for (var i=0;i<arguments.length;i++){
        var d = $(arguments[i]);
        if (d.style.display == 'none')
            d.style.display = 'block';
        else
            d.style.display = 'none';
    }
}
/*-----------------------------------------------------------
    Toggles tabs - Closes any open tabs, and then opens current tab
    Input:     1.The number of the current tab
                    2.The number of tabs
                    3.(optional)The number of the tab to leave open
                    4.(optional)Pass in true or false whether or not to animate the open/close of the tabs
    Output: none 
    ---------------------------------------------------------*/
function toggleTab(num,numelems,opennum,animate) {
    window.currTab = num;
    if ($('tabContent'+num).style.display == 'none'){
        for (var i=1;i<=numelems;i++){
            if ((opennum == null) || (opennum != i)){
                var temph = 'tabHeader'+i;
                var h = $(temph);
                if (!h){
                    var h = $('tabHeaderActive');
                    h.id = temph;
                }
                var tempc = 'tabContent'+i;
                var c = $(tempc);
                if(c.style.display != 'none'){
                    if (animate || typeof animate == 'undefined')
                        Effect.toggle(tempc,'blind',{duration:0.5, queue:{scope:'menus', limit: 3}});
                    else
                        toggleDisp(tempc);
                }
            }
        }
        var h = $('tabHeader'+num);
        if (h)
            h.id = 'tabHeaderActive';
        if(h.blur) h.blur();
        var c = $('tabContent'+num);
        c.style.marginTop = '2px';
        if (animate || typeof animate == 'undefined'){
            Effect.toggle('tabContent'+num,'blind',{duration:0.5, queue:{scope:'menus', position:'end', limit: 3}});
        }else{
            toggleDisp('tabContent'+num);
        }
    }
}

Event.observe( window, 'load', function(){
	toggleTab(1,3);
} );

