function countSyllLine($line){

  $line = clearPUnctuation($line);
  
  var $arrLine = $line.split(" ");

  var $syllables = 0;
  for($l=0;$l<$arrLine.length;$l++){
    $syllables += countSyllWord($arrLine[$l]);
  }
  return $syllables;
}

function clearPUnctuation($inString){
  var $arrPunc = new Array(",","\"","!","?",":",";","(",")","[","]","{","}","<",">","`","~","@","#","$","%","^","&","*","-","_","=","+","\´");
  for($p=0; $p<$arrPunc.length; $p++){
    $inString = $inString.split($arrPunc[$p]).join("");
  }
  return $inString;
}

function countSyllWord($inWord){
	var $syllables = 0;
	
	// check acronym
	if( $syllables == 0 ){
		$syllables = checkAcronymWord($inWord);
	}
	
	// check compound words
	if( $syllables == 0 ){
		$syllables = checkCompoundWord($inWord);
	}
	
	// finally check word
	if( $syllables == 0 ){
		$syllables = checkWord($inWord);
	}
	
	return $syllables;
}

function checkSpecialCaseWord($inWord){
  var $inWord = $inWord.toUpperCase();
  var $syllables = 0;

  // check for special case words
  switch( $inWord ){
    case "ACHE":
    case "ACHES":
      $syllables = 1;
    break;
    case "CAN'T":
      $syllables = 1;
    break;
    case "CAFE":
    case "CAFES":
      $syllables = 2;
    break;
    case "COLLEGIATE":
      $syllables = 3;
    break;
    case "COLONEL":
    case "COLONELS":
    case "COLONEL'S":
      $syllables = 2;
    break;
    case "CREATIVE":
    case "CREATIVES":
      $syllables = 3;
    break;
    case "EVERY": // outlier, this could be 2 or 3 syllables
      $syllables = 2;
    break;
    case "GEORGE":
      $syllables = 1;
    break;
    case "IDIOT":
      $syllables = 3;
    break;
    case "IDIOTS":
      $syllables = 3;
    break;
    case "INERTIA":
      $syllables = 3;
    break;
    case "MAYBE":
      $syllables = 2;
    break;
    case "NAKED":
      $syllables = 2;
    break;
    case "OK":
      $syllables = 2;
    break;
    case "PEOPLE":
    case "PEOPLES":
      $syllables = 2;
    break;
    case "POET":
    case "POETS":
    case "POET'S":
      $syllables = 2;
    break;
    case "POETIC":
      $syllables = 3;
    break;
    case "POETRY":
      $syllables = 3;
    break;
    case "RHYTHM":
      $syllables = 2;
    break;
    case "WASN'T":
      $syllables = 2;
    break;
    case "WHATSOEVER":
      $syllables = 4;
    break;
    case "WICKED":
      $syllables = 2;
    break;
    case "WON'T":
      $syllables = 1;
    break;
    default:
    break;
  }

  return $syllables;
}

function checkAcronymWord($inWord){
  var $inWord = $inWord.toUpperCase();
  var $arrWord = $inWord.split(".");
  var $syllables = 0;

  for( $w=0; $w<$arrWord.length; $w++){
    if( $arrWord[$w].length == 1 ){
      $syllables += 1;
    }
  }  

  return $syllables;
}

function checkCompoundWord($inWord){
  // the compound word is two words combined
  var $inWord = $inWord.toUpperCase();
  var $syllables = 0;
  
  var $arrCompoundWords = new Array("EVERY","SPACE","SOME","HEAD","FACE","LIFT","TIME","MAN","DAY");
  
  // search for word
  for($a=0; $a<$arrCompoundWords.length; $a++){
    var $numIndex = $inWord.search( $arrCompoundWords[$a] );
    if( $numIndex > -1 ){
      // we found a compound word
      var $subWord1;
      var $subWord2; 
      // now check if it's at the beginning or end of the word
      if( $numIndex == 0 ){
        // it's at the beginning
        $subWord1 = $arrCompoundWords[$a];
        $subWord2 = $inWord.substring( $arrCompoundWords[$a].length );
      } else{
        // it's at the end
        $subWord1 = $inWord.substring(0, $arrCompoundWords[$a].length );
        $subWord2 = $arrCompoundWords[$a];
      }
      // send each word to the checkWord function
      $syllables = checkWord($subWord1) + checkWord($subWord2);
      break;
    }
  }
  // then return each syllable count
  return $syllables;
}

function checkWord($inWord){
  var $inWord = $inWord.toUpperCase();
  var $word = $inWord;
  var $arr; // this var needs to be reset each time before it's used since "$word" gets modified along the way
  
  var $syllables = 0;
  
  // first check for special case word
  $syllables = checkSpecialCaseWord($inWord);
  
  if( $syllables == 0 ){
    // apparently no special case words, so move on...
  
    // check for conjunction "n't" at the end of the word
    $strConjunction = "N'T";
    if( $word.substring( $word.length - 3 ) == $strConjunction ){
      $word = $word.substring( 0, $word.length - $strConjunction.length );
      if( $word.length > 2 ){ // must be longer than 2 letters because of words "ain't", "don't", "can't"
        $syllables++;
      }
    }
    
    // already checked for "n't" conjuction, now we can get rid of the apostrophe
    $word = $inWord.split("'").join("");
    
    // check past tense
    if( $word.substring( $word.length - 2 ) == "ED" ){
      // make sure word is more than 3 letters long
      if( $word.length > 3 ){
        // check the last four letters
        if( $word.substring( $word.length - 4 ) != "DRED"){
          // check the last three letters
          switch( $word.substring( $word.length - 3 ) ){
          case "DED":
          case "TED":
          case "AED":
          case "EED":
          case "IED":
          case "OED":
          case "UED":
          // count "E" as a normal syllable
          break;
          default:
          // else subtract syllable
          $syllables--;
          break;
          }
        }
      }
    }
    
    // check suffix
    // count suffix and then remove for subsequent evals
    var $arrSuffix = new Array(/LY/);
    for($i=0; $i<$arrSuffix.length;$i++){
      if( $word.search($arrSuffix[$i]) > -1 ){
        $syllables++;
        $word = $word.replace($arrSuffix[$i], "");
      }
    }
    
    // check plural 
    if( $word.substring($word.length-1) == "S" ){
      // check if ends in "ES", as in "hitches", "bulges", "noses"
      if( $word.substring($word.length-2) == "ES" ){
        if( $word.substring($word.length-4) == "THES" ){
          // "breathes", "tithes", etc
          if( $word.length > 3 ){
              // remove the "s"
              $word = $word.substring(0,$word.length-1);
            }
        } else {
          switch( $word.substring($word.length-3) ){
          case "DES": // as in "hides", "rides"
          //case "HES": // as in "aches", "breathes", "tithes", 
          case "KES": // as in "flakes", "cakes", "stakes"
          case "TES": // as in "crates", "rates", "fates"
          case "MES": // as in "grimes", "slimes", "times"
          case "NES": // as in "lines", "nines"
            // remove the "s" as the "silent e" checker will work next
            if( $word.length > 3 ){
              // remove the "s"
              $word = $word.substring(0,$word.length-1);
            }
            break;
            default:
            // nothing
            break;
          }
        }
      } else {
        // ends with an "S" not an "ES", so remove the "S"
        if( $word.length > 3 ){
          // remove the "s"
          $word = $word.substring(0,$word.length-1);
        }
      }
    }

    // resolve if the word ends with a silent "E"
    var $arr = $word.split("");
    if( $arr[$arr.length-1] == "E" ){
      // doesn't seem to work on: have, these, 
      var $subtract = 'TRUE';

      // okay, the word ends with an "E", 
      // but we have to check for vowel combos that end with "E"
      var $arrLast2Letters = new Array("EE","IE","AE","OE","UE");
      for($a=0; $a<$arrLast2Letters.length; $a++){
        if( $word.substring( $arr.length-2 ) == $arrLast2Letters[$a] ){
          $subtract = 'FALSE';
        }
      }

      var $arrFullWords = new Array("BE","HE","ME","SHE","THE","WE","YE");
      for($f=0; $f<$arrFullWords.length; $f++){
        if( $word == $arrFullWords[$f] ){
          $subtract = 'FALSE';
        }
      }
      
      var $arrLast3Letters = new Array("BLE","DLE","GLE","PLE","TLE","CLE","KLE","FLE","PHE");
      for($a=0; $a<$arrLast3Letters.length; $a++){
        if( $word.substring($arr.length-3) == $arrLast3Letters[$a] ){
          $subtract = 'FALSE';
        }
      }
      
      // in all other cases, subtract a syllable for the silent "E" that will be counted later
      if( $subtract == 'TRUE' ){
        $syllables--;
      }
    }
    
    // check for "ING" at the end of the word
    $strING = "ING";
    if( $word.substring( $word.length - 3 ) == $strING ){
      $word = $word.substring( 0, $word.length - $strING.length ); 
      $syllables++;
    }

    // check if word starts with "Y"
    // if it does, then it's probably a consonant
    $arr = $word.split("");
    if( $arr[0] == "Y" ){
      $word = $word.substring(1,$word.length);
    }
    
    // check special case words with "IA" as third & second to last letters, ie "ASIAN", "SPECIAL", "FACIAL"
    var $strIA = "IA";
    if( $word.substring($word.length-3, $word.length-1) == $strIA ){
      // word must be longer than 4 letters else it should count as 2 syllables
      if( $word.length > 4 ){
        // we only want "IA" to count as 1 syllable, so subtract from the total count
        $syllables--;
      }
    }
    
    // check for special case "IO" words like "CUSHION", "FASHION", "RATIO", etc
    var $strIO = "IO";
    if( $word.search( $strIO ) > -1 ){
    
      // check special case words with "TIO", ie "RATIO", "RATION", "PARTITION"
      // also check special case words with "SHION", ie "CUSHION", "FASHION"
      // otherwise, "IO" is counted as 2 syllables
      var $strTIO = "TIO";
      var $strSHION = "SHION";
      if( $word.search( $strTIO ) > -1 || $word.search( $strSHION ) > -1 ){
        // we only want "IO" to count as 1 syllable, so subtract from the total count
        $syllables--;
      } 
    }
      
    // resolve vowel combinations
    // first, check 3 letter vowel combinations
    var $arr3LetterVowelCombos=new Array(/EAU/,/IOU/);
    for($i=0; $i<$arr3LetterVowelCombos.length;$i++){
      if( $word.search($arr3LetterVowelCombos[$i]) > -1 ){
        $syllables++;
        $word = $word.replace($arr3LetterVowelCombos[$i], "");
      }
    }
    // where in most cases these 2 vowels equal 1 syllable
    var $arrVowelCombos=new Array(/AA/,/AE/,/AI/,/AO/,/AU/,/AY/,
    /EA/,/EE/,/EI/,/EU/,/EY/,/IE/,/II/,
    /IU/,/OA/,/OE/,/OI/,/OO/,/OU/,/OY/,
    /UE/,/UO/,/UU/,/UY/);
    for($i=0; $i<$arrVowelCombos.length;$i++){
      if( $word.search($arrVowelCombos[$i]) > -1 ){
        $syllables++;
        $word = $word.replace($arrVowelCombos[$i], "");
      }
    }

    // finally, count remaining vowels
    var $arrVowels=new Array(/A/,/E/,/I/,/O/,/U/,/Y/);
    for($j=0; $j<$arrVowels.length;$j++){
      var $index = $word.search($arrVowels[$j]);
      while( $index > -1 ){
        $syllables++;
        var $str1 = $word.substring(0,$index);
        var $str2 = $word.substring($index+1);
        $word = $str1.concat($str2);
        $index = $word.search($arrVowels[$j]);
      }
    }
  
  }

  return $syllables;
}

function getArrVowels(){
  var $arr = new Array(/A/,/E/,/I/,/O/,/U/,/Y/);
  return $arr;
}
function getArrConsonants(){
  var $arr = new Array(/B/,/C/,/D/,/F/,/G/,/H/,/J/,/K/,/L/,/M/,/N/,/P/,/Q/,/R/,/S/,/T/,/V/,/W/,/X/,/Z/);
  return $arr;
}