﻿function autoClear() {
	if (document.formRandomizer.textRandom.value == "Type or paste your words here") {
		document.formRandomizer.textRandom.value = "";
	}
}
function copyText() {
   document.formRandomizer.textRandom.focus();
   document.formRandomizer.textRandom.select(); 
   strSelectedText = document.selection.createRange();
   strSelectedText.execCommand("Copy");
}
function clearText() {
   document.formRandomizer.textRandom.value = "";
}
function randomSort(a,b) {
    return(parseInt(Math.random() * 10) % 2);
}
function randomizeText() {
    //Get the value of the text box
    var s = document.formRandomizer.textRandom.value;
    //Determine how this browser handles line breaks
    var strLineBreak = "\n";
    if (s.indexOf("\r\n") > -1) strLineBreak = "\r\n";
    else if (s.indexOf("\r") > -1) strLineBreak = "\r";
    //Check if dropping the formatting
    var isFormatDropped = document.formRandomizer.radioFormat[1].checked;
    //If formatting is dropped then remove the line breaks
    if (isFormatDropped) s = s.replace(/(\r\n|\r|\n)/g, " ");
    else {
        //Normalize the line breaks
        s = s.replace(/(\r\n)/g, "\n");
        s = s.replace(/(\r)/g, "\n");
    }
    //Split the text into lines (handled as a single line if formatting is dropped)
    var arrayLines = s.split("\n");
    if (arrayLines) {
        //Process each line
        for (var i=0; i<arrayLines.length; i++) {
            //If keeping format, then preserve the ending punctuation mark
            var strPMark = "";
            if (!isFormatDropped && (arrayLines[i].length > 0)) {
                //Check if last character on the line is a non-alphanumeric character
                var intLastCharIndex = arrayLines[i].length - 1;
                if (intLastCharIndex == arrayLines[i].search(/\W$/)) {
                   strPMark = arrayLines[i].substr(intLastCharIndex); 
                }
            }
            //Split the line into words (while ignoring punctuation)
            //The expression ((-\w+)|('\w+))* allows for hyphenated words and contractions
            var arrayWords = arrayLines[i].match(/\w+((-\w+)|('\w+)|(’\w+))*/g);
            if (arrayWords) {
                //Rebuild the line without punctuation, except for the ending mark
                arrayLines[i] = arrayWords.join(" ") + strPMark;
                //Randomize the words
                var intLastIndex = arrayWords.length - 1;
                if (intLastIndex > 0) {
                    //Randomly move the last word
                    var intSwapIndex = parseInt(Math.random() * intLastIndex);
                    var strTemp = arrayWords[intLastIndex];
                    arrayWords[intLastIndex] = arrayWords[intSwapIndex];
                    arrayWords[intSwapIndex] = strTemp;
                    //Now randomly sort all of the words
                    arrayWords.sort(randomSort);
                    //Check if randomization returned a different line
                    var strNewLine = arrayWords.join(" ") + strPMark;
                    if (strNewLine == arrayLines[i]) {
                        //Randomly move the first word to ensure that at least two words change places
                        intSwapIndex = 1 + parseInt(Math.random() * intLastIndex);
                        strTemp = arrayWords[0];
                        arrayWords[0] = arrayWords[intSwapIndex];
                        arrayWords[intSwapIndex] = strTemp;
                        strNewLine = arrayWords.join(" ") + strPMark;
                    }
                    arrayLines[i] = strNewLine;
                }
            } else {
                arrayLines[i] = "";
            }
        }
        //Rebuild the text by joining the lines
        s = arrayLines.join(strLineBreak);
    } else {
        s = "";
    }
    //Set the new value of the text box
    document.formRandomizer.textRandom.value = s;
}