

function alphaOrder(){

	var alphaText = document.getElementById("oldText").value;
	
	var lbsep = document.getElementById("lbsep").checked;
	var cusep = document.getElementById("cusep").value;
	var cosep = document.getElementById("cosep").checked;
	var sortNumerical = document.getElementById("sortNumerical").checked;
	var sortRev = document.getElementById("sortRev").checked;
	
	var removeDuplicates = document.getElementById("removeDuplicates").checked;
	var removePunctuation = document.getElementById("removePunctuation").checked;
	
	if(cusep.length > 0){
		spsep = false;
		document.getElementById("spsep").checked = spsep;
		cosep = false;
		document.getElementById("cosep").checked = cosep;
		lbsep = false;
		document.getElementById("lbsep").checked = cosep;
	}

	if(lbsep != 1 || lbsep !=  true){
	
		if (cusep.length > 0){
			var theSep = cusep;
		}else{
			if(cosep == 1 || cosep ==  true){
				var theSep = ",";
			}else{
				var theSep = " ";
			}
		}
		alphaText = alphaText.replace(/(\r\n|\n|\r)/gm,"");
		nodsp = /\s+/g;
		alphaText = alphaText.replace(nodsp," ");
	}else{
		var theSep = "\n";
	}
	
	var alphaTextArray = alphaText.split(theSep);
	
	if(theSep == "\n"){
		var i = 0;
		while(i < alphaTextArray.length){
			if(removePunctuation == true){
				alphaTextArray[i] = alphaTextArray[i].replace (/[.,?!;:(){}\[\]]/g, " ");
			}
			var nodsp = /\s+/g;
			alphaTextArray[i] = alphaTextArray[i].replace(nodsp," ");
		i++;
		}	
	}else{
		var i = 0;
		while(i < alphaTextArray.length){
			if(removePunctuation == true){
				alphaTextArray[i] = alphaTextArray[i].replace (/[.,?!;:(){}\[\]]/g, " ");
			}
			var nodsp = /\s+/g;
			alphaTextArray[i] = alphaTextArray[i].replace(nodsp," ");
		i++;
		}
	}
	
	if(removeDuplicates == true){
		var i = alphaTextArray.length-1;
		while(i > -1){
			var ii = alphaTextArray.length-1;
			while(ii > -1){
				if(i != ii){
					if (alphaTextArray[i] == alphaTextArray[ii]){
						alphaTextArray.splice (ii, 1);
					}
				}
			ii--;
			}
		i--;
		}
	}
	
	if(sortNumerical ==  true){
		alphaTextArray.sort(sortNumber);
	}else{
		alphaTextArray.sort(sortTextReal);
	}
	
	if(sortRev == 1 || sortRev ==  true){
		alphaTextArray.reverse();
	}

	var i = alphaTextArray.length-1;
	while(i > -1){
		if(alphaTextArray[i] == " "){
			alphaTextArray.splice (i, 1);
		}
		if(theSep != "\n"){
			if(alphaTextArray[i] == "\n"){
				alphaTextArray.splice (i, 1);
			}
		}
	i--;
	}

	alphaText = alphaTextArray.join(theSep);
	
	if(theSep != "\n"){
		nodsp = /\s+/g;
		alphaText = alphaText.replace(nodsp," ");
	}
	
	//lTrim
	while (alphaText.substring(0,1) == ' '){
		alphaText = alphaText.substring(1, alphaText.length);
	}
	while (alphaText.substring(0,1) == '\n'){
		alphaText = alphaText.substring(1, alphaText.length);
	}
	//rTrim
	while (alphaText.substring(alphaText.length-1, alphaText.length) == ' '){
		alphaText = alphaText.substring(0,alphaText.length-1);
	}
	while (alphaText.substring(alphaText.length-1, alphaText.length) == '\n'){
		alphaText = alphaText.substring(0,alphaText.length-1);
	}

	document.getElementById("newText").value = alphaText;
}

function sortNumber(a,b){
	if (isNaN(a) == false && isNaN(b) == false){
		return (a-b);
	}else{//For when Numbers and words are mixed in a numeric sort
		if (isNaN(a) == true && isNaN(b) == true){
			var aa;var bb;
			aa = a.toLowerCase(); bb = b.toLowerCase();
			if(aa>bb){
				return 1;
			}
			if(aa<bb){
				return -1;
			}
			return 0;
		}else if (isNaN(a) == false){
			return 1;
		}else{
			return -1;
		}
	}
}

function sortTextReal(a,b){
	var aa;var bb;
	aa = a.toLowerCase(); bb = b.toLowerCase();
	if(aa>bb){
		return 1;
	}
	if(aa<bb){
		return -1;
	}
	return 0;
}
