var thisWorduc;
function checkJarg(myWord,showNote) {
	var spell_test=true;
	var myMessage="no test";
	spell_message_id=0;
	var thisWord = myWord.toLowerCase();
	var alphaPattern = /^([a-z])+$/;
	if (thisWord.length ==0) {
		myMessage="No letters -- it's empty. You can't submit that.";
		spell_message_id=1;
		spell_test=false;
	}  else if (!alphaPattern.test(thisWord)) {
		myMessage = "Your word must be all letters, all-one-word, with no spaces, dashes, numbers, or special characters. (It's one of the <a href='/jargorules.php'>game rules</a> to make the game more exciting.)";
		spell_message_id=2;
		spell_test=false;
	}else if (thisWord.length ==1) {
		myMessage="Just one letter!? That doesn't seem to be very creative";
		spell_message_id=3;
		spell_test=false;
	} else if (repeatedLetter(thisWord) > 2) {
		myMessage = "Looks like your keyboard got stuck on one letter. Do you want to spell it like that?";
		spell_message_id=4;
	} else if (!oneVowel(thisWord)) {
		myMessage = "That word looks like gobblygook. Are you sure you want to submit it?";
		spell_message_id=5;
	} else if (tooManyVowels(thisWord) == 4) {
		myMessage = "That wooord haas a looot of voowels.";
		spell_message_id=6;
	} else if (tooManyVowels(thisWord) > 4) {
		myMessage = "That wooooord has a lot of vowels. Are you sure it is goooooood for this definition?";
		spell_message_id=7;
	} else if (tooManyConsonants(thisWord) > 4) {
		myMessage = "That may be very dphphkult to pronounce. Are you sure it is what you want? ";
		spell_message_id=8;
	} else if (thisWord.length > 15 ) {
		myMessage="Wow, that's a really, really long word! ";
		spell_message_id=9;
	} else if (thisWord.length > 10 ) {
		myMessage="That's a long word! But it looks good";
		spell_message_id=10;
	} else {
		myMessage="That's a nice looking word! With consonants and vowels. Good start. ";
		spell_message_id=11;
	}
	
	myDiv="jargonote";
	messageType="redlinks";
	if ((spell_test==false) || (showNote == 1)) {
		writeNote(myDiv, messageType, myMessage);
	} else {
		writeNote(myDiv, messageType, '&nbsp;');
	}
	//alert(" hi " + spell_test);
	document.jargonition.spell_message_id.value = spell_message_id;
	return spell_test;
}

spellMessages = new Array(); 

spellMessages[0] = "No test";
spellMessages[1] = "No letters -- it's empty. You can't submit that.";
spellMessages[2] = "Must be all letters, all one word, with no spaces, dashes, numbers, or special characters. (It's one of the game rules to make it more challenging.)";
spellMessages[3] = "Just one letter!? That doesn't seem to be very creative";
spellMessages[4] = "Looks like your keyboard got stuck on one letter. Do you want to spell it like that?";
spellMessages[5] = "That word looks like gobblygook. Are you sure you want to submit it?";
spellMessages[6] = "That wooord haas a looot of voowels.";
spellMessages[7] = "That wooooord has a lot of vowels. Are you sure it is goooooood for this definition?";
spellMessages[8] = "That may be very dphphkult to pronounce. Are you sure it is what you want? ";
spellMessages[9] = "Wow, that's a really, really long word! ";
spellMessages[10] = "That's a long word! But it looks good";
spellMessages[11] = "That's a nice looking word! With consonants and vowels. Good start.";

function spellMessageFunction(spellMessageId) {
	var html_string="";
	html_string += "<strong>Spelling:</strong> \n"; 
	html_string += spellMessages[spellMessageId];
	html_string += "\n";
	return html_string;
}

function oneVowel(thisWord) {
	var pattern=/[aeiouy]{1}/; // at least one vowel
	var regexPattern = new RegExp(pattern);
	var regexMatch = regexPattern.exec(thisWord);	
	if (regexMatch == null) {
	   // alert("No Vowels");
	    return false;
	} else {
		//alert("has Vowels");
	  	return true;
	}
}
function tooManyVowels(thisWord) {
	var vowel_count=0;
	pattern=/[aeiou]{4,}/; // 4 or more vowels in a row not counting "y" //obsequious (4 vowels)queueing (5 vowels),archaeoaeolotropic
	var regexPattern = new RegExp(pattern);
	var regexMatch = regexPattern.exec(thisWord);
	if (regexMatch != null) {
		vowel_count=4;
		//alert("4 Vowels in row");
		pattern=/[aeiou]{5,}/; // 5
		var regexPattern = new RegExp(pattern);
		var regexMatch = regexPattern.exec(thisWord);
		if (regexMatch != null) {
			vowel_count=5;
			//alert("5 Vowels in row");
		}
	} 
	return vowel_count; 
}

function tooManyConsonants(thisWord) {
	var consonant_count=0;
	// Does it have too many consonants
	pattern=/[bcdfghjklmnpqrtvwxz]{5,}/ ; // 5 or more consonants in a row not counting s or y
	var regexPattern = new RegExp(pattern);
  	var regexMatch = regexPattern.exec(thisWord);
  	if (regexMatch != null) {
		consonant_count=5;
		//alert("5 Consonants in row");
	}	
	return consonant_count; 
}
function repeatedLetter(thisWord) {
	var repeat_count=0;
	pattern=/(.)\1{2,}/; // same letter more than 2 times in a row
	var regexPattern = new RegExp(pattern);
	var regexMatch = regexPattern.exec(thisWord);	
	if (regexMatch != null) {
		repeat_count=3;
		//alert("same letter 3 times in row");
	}
	return repeat_count;
}	



function writeNote(myDiv, messageType, myNote)  {
	var emptyString = /^\s*$/ 
	var nbsp = 160;
  	var thisMessage;
  	if (emptyString.test(myNote)) {
    	thisMessage = String.fromCharCode(nbsp);    
  	} else {
  		//if (messageType=="redlinks"){
    		thisMessage = '<p class="' + messageType + '">' + myNote + '</p>';
    	//} else {
    		//thisMessage = myNote;
    	//}
	}
	if (document.getElementById){
		myDivId = document.getElementById(myDiv);
		myDivId.innerHTML = '';
		myDivId.innerHTML = thisMessage;
	}
	else if (document.all){
		myDivId = document.all[myDiv];
		myDivId.innerHTML = thisMessage;
	}
	else if (document.layers){
		myDivId = document.layers[myDiv];
		text2 = thisMessage;
		myDivId.document.open();
		myDivId.document.write(text2);
		myDivId.document.close();
	}
	var divId = document.getElementById(myDiv);  
  	divId.className = messageType;
}

function dictionaryNotes(inDictionary, myWord) {
	var defurl='http://www.tfd.com/' + myWord;
	var thisMessage;
	var firstLetter = myWord.substring(0, 1).toUpperCase();
    var restOfWord = myWord.substring(1, myWord.length).toLowerCase();
	thisWorduc = firstLetter + restOfWord;
	var thisWord = thisWorduc;
	//alert(thisWord);
	if (inDictionary == 3) {
		thisMessage = "<!--Error: No value for is_defined -->";
	} else if (inDictionary == 1) {
		thisMessage = "<strong>Existing:</strong> " + thisWord + " is an existing word. ";
		thisMessage = thisMessage + "(Based a match found for <a href=\""+ defurl + "\">" + thisWord + "</a> found by TheFreeDictionary.)</p> \n";
		thisMessage = thisMessage + "<p> The goal is to create a new word.";
		thisMessage = thisMessage + " However, if you think that " + thisWord+ " is perfect for this definition, then use it!";
	} else if (inDictionary == 0) {
		if (spellMessageId > 8) {
			thisMessage =  "<strong>Existing:</strong> Cool! &quot;" + thisWord+ "&quot; does not match any existing <a href=\""+ defurl + "\">dictionary</a> words.";
		} else {
			thisMessage =  "<p><strong>Existing:</strong> As expected, with such random letters " + thisWord+ " does not match any existing <a href=\""+ defurl + "\">dictionary</a> words. ";
		}
		thisMessag = thisMessage + " (Based a match search for <a href=\""+ defurl + "\">" + thisWord + "</a> by TheFreeDictionary.)";
	} else if (inDictionary == 2) {
		thisMessage =  "<!-- Error: fetch for $defurl failed -->";
	}
	return thisMessage;
}

function jargoogle(myWord) {
    var jarJax='jargoogle';
	var postString = "jarjax="+ jarJax + "&my_word="+ myWord;
	var myDiv='jargoogle';
	new ajax('/jargoscrape.php', {postBody: postString, update: myDiv });
}


function checkDictionary(myWord) {
	//alert('checkDictionary ' + myWord);
    var jarJax='dictionary';
	var postString = "jarjax="+ jarJax + "&my_word="+ myWord;
	//alert( postString);
	var myDiv='dictionary';
	new ajax('/jargoscrape.php', {postBody: postString,  onComplete: dictionaryComplete});
	//alert( "done: " + request.responseText);
}
function jarniqueness (myWord, spellMessageId, defId) {
    var jarJax='jarnique';
	var postString = "jarjax="+ jarJax + "&my_word="+ myWord + "&spell_message_id=" + spellMessageId +"&definition_id=" + defId;
	var myDiv='jarnique';
	new ajax('/jargoscrape.php', {postBody: postString, onComplete: showJarnique});
}

function showJarnique(request){
	var isUnique;
	isUnique=request.responseText;
	//alert (isUnique );
	//alert ( thisWorduc );
	//alert (" " +spellMessageId );
	 if ( isUnique == 1){
		// it's unique  
      	if (spellMessageId > 8) {
			jMessage = "<strong>Unique:</strong> Good! No other players have submitted " + thisWorduc + ".";
		} else {
			jMessage = "<strong>Unique:</strong> And guess what, no one else has submitted " + thisWorduc + ", probably because it doesn't make sense.";
		}
     } else {
        //It's NOT unique  
        if (spellMessageId > 8) {
        	 jMessage ="<strong>Unique:</strong> Guess what! Some other players have thought of " + thisWorduc + "!  Please try to create another word.";
        	} else {
			jMessage = "<strong>Unique:</strong> Weird! Some other players have thought of the same word! Please try to create another word.";
		}
		//document.save.submitButton.value = 'Join ' + thisWorduc + ' group';
		hideLayer('savejarg');
		showLayer('nojarg');
    }
	writeNote('jarnique', 'def', jMessage);
}
function getPhonemes(myWord) {
	//alert(" " + myWord);
    var jarJax='phonemes';
	var postString = "jarjax="+ jarJax + "&my_word="+ myWord;
	var myDiv='phono';
	new ajax('/jargoscrape.php', {postBody: postString, update: myDiv });
}


function dictionaryComplete(request){
  	inDictionary=request.responseText;
   	//alert(inDictionary + " " + myWord);
 	dMessage=dictionaryNotes(inDictionary, myWord);
	//alert(dMessage);
	if (inDictionary ==1) {
		heightLoading.toggle();
		writeNote('spelling', 'def', dMessage);
		heightSpell.toggle(); //open
	} else {
		heightLoading.toggle();
		sMessage=spellMessageFunction(spellMessageId);
		writeNote('spelling', 'def', sMessage);
		heightSpell.toggle(); //open
		writeNote('dictionary', 'def', dMessage);
		heightDict.toggle();
		displayDiv('jarnique');
		displayDiv('jargoogle');
		displayDiv('phono');
		//heightJarnique.toggle();
		//heightJargoogle.toggle();
		//heightPhono.toggle();
		heightMeaning.toggle();
		jarniqueness(myWord, spellMessageId, defId);
		jargoogle(myWord);
		getPhonemes(myWord);	
	}
	
	heightSave.toggle();
	heightAnother.toggle();
	
}
function displayDiv(thisDivName) {
	var thisId=document.getElementById(thisDivName);
	thisId.style.display ='block';
}
function hideDiv(thisDivName) {
	var thisId=document.getElementById(thisDivName);
	thisId.style.display ='none';
}
function jargonalysis(myWord) {
	var dMessage;
	var sMessage;
	
	//alert('spellMessageId ' + spellMessageId);
	if ( spellMessageId ==-1) {
		checkJarg(myWord);
	} else if (spellMessageId < 4){
		spellTest=false;
	} else {
		spellTest=true;
		checkDictionary(myWord);
		
	}
	
}
function showLayer(whichLayer){
	if (document.getElementById){
		// this is the way the standards work
		var style2 = document.getElementById(whichLayer).style;
		style2.display = 'block';
	} else if (document.all){
		// this is the way old msie versions work
		var style2 = document.all[whichLayer].style;
		style2.display = 'block'
	} else if (document.layers) {
		// this is the way nn4 works
		var style2 = document.layers[whichLayer].style;
		style2.display = 'block';
	}
}
function hideLayer(whichLayer){
	if (document.getElementById){
		// this is the way the standards work
		var style2 = document.getElementById(whichLayer).style;
		style2.display = 'none';
	} else if (document.all){
		// this is the way old msie versions work
		var style2 = document.all[whichLayer].style;
		style2.display = 'none'
	} else if (document.layers) {
		// this is the way nn4 works
		var style2 = document.layers[whichLayer].style;
		style2.display = 'none';
	}
}
