//Sort Text Lines (Javascript) is Copyright 2008 My-i-p.com

function inputcontrol(){
if (document.form_sort.count_warning.value != 'Been warned!'){
var inputtext = document.form_sort.input_output.value;
var len = inputtext.split('\n').length;
if (len > 10001) {var overload = confirm("WARNING: You have entered over 10,000 lines of text which could slow down or even lock-up your computer! Please click \"Cancel\" to stop text loading or click \"OK\" to proceed with text loading at your own risk. After clicking \"OK\" no more text line overload warnings will be issued and \"Been warned!\" will display next to the line count display.");
if (overload == true) {document.form_sort.count_warning.value = 'Been warned!'; document.form_sort.input_output.value = inputtext;} if (overload == false) {document.form_sort.input_output.value = 'Text loading cancelled.'; document.form_sort.count_warning.value = 'Warning issued.';}}}}

function cleartext(){
document.form_sort.input_output.value = '';}

function expandr(){
var hgt = document.form_sort.input_output.rows;
hgt = hgt + 5;
document.form_sort.input_output.rows = hgt;}
function contractr(){
var hgt = document.form_sort.input_output.rows;
hgt = hgt - 5;
document.form_sort.input_output.rows = hgt;}

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 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;}
var text = document.form_sort.input_output.value;
text = text.replace(/\r\n/g,'\n');
text = text.split('\n').sort(cisort).clean('').join('\n');
document.form_sort.input_output.value = text;}

function sortcs(){
function cssort(a,b){
var x = a.toString(), y = b.toString(),
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\n/g,'\n');
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;}

function sortrandom(){
function rsort(){
return(Math.round(Math.random())-0.5);}
var text = document.form_sort.input_output.value;
text = text.replace(/\r\n/g,'\n');
text = text.split('\n').sort(rsort).sort(rsort).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 SelectAll(id) {
document.getElementById(id).focus();
document.getElementById(id).select();}
