//Sort Text Lines (Javascript) is Copyright 2010 TextMechanic.com All Rights Reserved

function cleartext(){
document.form_sort.input_output.value = '';}

function linecnt(){
if(document.form_sort.input_output.value.length > 0){
f=document.form_sort;
f.line_count.value=f.input_output.value.split(/\n/).length;}
else document.form_sort.line_count.value = '0';}

Array.prototype.clean = function(remove_item){
var a;
for (a = 0; a < this.length; a++){
if (this[a] == remove_item){
this.splice(a,1);a--;}
}return this;}

function sortci(){
function cisort(a,b){
var delimiter = document.form_sort.s_delimiter.value;
var snum = document.form_sort.s_num.value - 1;
if (document.form_sort.r_arr.checked == true){
var x = a.toString().toLowerCase().split(delimiter).reverse().slice(snum).join(delimiter);
var y = b.toString().toLowerCase().split(delimiter).reverse().slice(snum).join(delimiter);} else {
var x = a.toString().toLowerCase().split(delimiter).slice(snum).join(delimiter);
var y = b.toString().toLowerCase().split(delimiter).slice(snum).join(delimiter);}
nC = String.fromCharCode(0),
xN = x.replace(/([-]{0,1}[0-9.]{1,})/g, nC + '$1' + nC).split(nC),
yN = y.replace(/([-]{0,1}[0-9.]{1,})/g, nC + '$1' + nC).split(nC);
for (var cLoc=0, numS = Math.max( xN.length, yN.length); cLoc < numS; cLoc++){
if ((parseFloat(xN[cLoc]) || xN[cLoc]) < (parseFloat(yN[cLoc]) || yN[cLoc]))
return -1;
else if ((parseFloat(xN[cLoc]) || xN[cLoc]) > (parseFloat( yN[cLoc]) || yN[cLoc]))
return 1;}return 0;}
var text = document.form_sort.input_output.value;
text = text.replace(/\r/g,'');
text = text.split('\n').sort(cisort).clean('').join('\n');
document.form_sort.input_output.value = text;}

function sortcs(){
function cssort(a,b){
var delimiter = document.form_sort.s_delimiter.value;
var snum = document.form_sort.s_num.value - 1;
if (document.form_sort.r_arr.checked == true){
var x = a.toString().split(delimiter).reverse().slice(snum).join(delimiter);
var y = b.toString().split(delimiter).reverse().slice(snum).join(delimiter);} else {
var x = a.toString().split(delimiter).slice(snum).join(delimiter);
var y = b.toString().split(delimiter).slice(snum).join(delimiter);}
nC = String.fromCharCode(0),
xN = x.replace(/([-]{0,1}[0-9.]{1,})/g, nC + '$1' + nC).split(nC),
yN = y.replace(/([-]{0,1}[0-9.]{1,})/g, nC + '$1' + nC).split(nC);
for (var cLoc=0, numS = Math.max( xN.length, yN.length); cLoc < numS; cLoc++){
if ((parseFloat(xN[cLoc]) || xN[cLoc]) < (parseFloat(yN[cLoc]) || yN[cLoc]))
return -1;
else if ((parseFloat(xN[cLoc]) || xN[cLoc]) > (parseFloat( yN[cLoc]) || yN[cLoc]))
return 1;}return 0;}
var text = document.form_sort.input_output.value;
text = text.replace(/\r/g,'');
text = text.split('\n').sort(cssort).clean('').join('\n');
document.form_sort.input_output.value = text;}

function sortbylength(){
function cisort(a,b){
var x = a.toString().toLowerCase(), y = b.toString().toLowerCase(),
nC = String.fromCharCode(0),
xN = x.replace(/([-]{0,1}[0-9.]{1,})/g, nC + '$1' + nC).split(nC),
yN = y.replace(/([-]{0,1}[0-9.]{1,})/g, nC + '$1' + nC).split(nC);
for (var cLoc=0, numS = Math.max( xN.length, yN.length); cLoc < numS; cLoc++){
if ((parseFloat(xN[cLoc]) || xN[cLoc]) < (parseFloat(yN[cLoc]) || yN[cLoc]))
return -1;
else if ((parseFloat(xN[cLoc]) || xN[cLoc]) > (parseFloat( yN[cLoc]) || yN[cLoc]))
return 1;}return 0;}
function complengths(a,b){
if (a.length < b.length)
return -1;
if (a.length > b.length)
return 1;return 0;}
var text = document.form_sort.input_output.value;
text = text.replace(/\r\n/g,'\n');
text = text.split('\n').sort(cisort).sort(complengths).clean('').join('\n');
document.form_sort.input_output.value = text;}

Array.prototype.shuffle = function (){
var x = this.length;
if (x == 0) return false;
while (--x){
var y = Math.floor(Math.random()*(x + 1));
var temp_x = this[x];
var temp_y = this[y];
this[x] = temp_y;
this[y] = temp_x;}
return this;}

function sortrandom(){
var text = document.form_sort.input_output.value;
text = text.replace(/\r\n/g,'\n');
text = text.split('\n').shuffle().clean('').join('\n');
document.form_sort.input_output.value = text;}

function reverseorder(){
var text = document.form_sort.input_output.value;
text = text.replace(/\r\n/g,'\n');
text = text.split('\n').reverse().clean('').join('\n');
document.form_sort.input_output.value = text;}

function rduplin(){
var count = 0;
var text = document.form_sort.input_output.value;
text = text.replace(/\r/g,'');
var listvalues = new Array();
var newlist = new Array();
listvalues = text.split('\n');
var hash = new Array();
for (var i=0; i<listvalues.length; i++){
if (hash[listvalues[i].toLowerCase()] != 1) {newlist = newlist.concat(listvalues[i]); hash[listvalues[i].toLowerCase()] = 1}
else {count++;}}
document.form_sort.input_output.value = newlist.clean('').join('\n');
document.form_sort.removedbutton.value = count + ' removed.';}

function rduplincs(){
var count = 0;
var text = document.form_sort.input_output.value;
text = text.replace(/\r/g,'');
var listvalues = new Array();
var newlist = new Array();
listvalues = text.split('\n');
var hash = new Array();
for (var i=0; i<listvalues.length; i++){
if (hash[listvalues[i]] != 1) {newlist = newlist.concat(listvalues[i]); hash[listvalues[i]] = 1}
else {count++;}}
document.form_sort.input_output.value = newlist.clean('').join('\n');
document.form_sort.removedbutton.value = count + ' removed.';}

function buttonreset(){
document.form_sort.removedbutton.value = 'Remove Duplicate';}

function space2line(){
var inputtext = document.form_sort.input_output.value;
inputtext = inputtext.replace(/\r/g,'');
inputtext = inputtext.replace(/\n/g,' ');
inputtext = inputtext.split(' ');
inputtext = inputtext.clean('');
inputtext = inputtext.clean(' ');
inputtext = inputtext.join('\n');
document.form_sort.input_output.value = inputtext;}

function SelectAll(id) {
document.getElementById(id).focus();
document.getElementById(id).select();}