//counting words in the textarea for CIO100 2008 awards

//description for organization
var MaximumWords 		= "150";

//form name
var FormName 			= "awardsform";

//Specify the name of the text field being monitored 
var TextFieldName 		= "TextFieldName";

// Specify the field name where where is to be displayed 
//    the number of words the user has typed. Make it 
//    blank (nothing between the quotation marks) if you 
//    aren't displaying the number of words typed.

var WordsTypedFieldName = "WordsTyped";

// Specify the field name where where is to be displayed 
//    the number of words left that may be typed. Make it 
//    blank (nothing between the quotation marks) if you 
//    aren't displaying the number of words left.

var WordsLeftFieldName = "WordsLeft";
var WordsMonitor = 0;
var MaxWords = 0;
var textfield = '';
var left;
var passWordLimit = true;
var errorMsg = '';
var truncate = false;

function setMaxWords(num){
	MaxWords = parseInt(num);
}

function setTextFieldName(name){
	textfield = 'document.' + FormName + '.' + name + '.value';
}

function setWordsTypedFieldName(typed_field_name){
	WordsTypedFieldName = typed_field_name;
}

function setWordsLeftFieldName(word_left_field_name){
	WordsLeftFieldName = word_left_field_name;
}


function WordLengthCheck(s,l) { 
	WordsMonitor = 0;
	var f = false;
	var ts = new String();
	for(var vi = 0; vi < s.length; vi++) {
		vs = s.substr(vi,1);
		if((vs >= 'A' && vs <= 'Z') || (vs >= 'a' && vs <= 'z') || (vs >= '0' && vs <= '9')) { 
			if(f == false)	{
				f = true;
				WordsMonitor++;
				
				if((l > 0) && (WordsMonitor > l)) {
					s = s.substring(0,ts.length);
					vi = s.length;
					WordsMonitor--;
					truncate = true;
				}
				
				
			}
		}
		else { f = false; }
		ts += vs;
	}
	return s;
} // function WordLengthCheck()
	var txtarea ='';
function InputWordLengthCheck(maxwords,textfieldname,question,typed_textfield,left_textfield, message) {
	errorMsg ="";
	setMaxWords(maxwords);
	setTextFieldName(textfieldname);
	setWordsLeftFieldName(left_textfield);
	setWordsTypedFieldName(typed_textfield);
	//left = 0;
	txtarea = textfieldname;
	if(MaxWords <= 0) { return; }
	var currentstring = new String();
	eval('currentstring = ' + textfield);
	var currentlength = currentstring.length;
	eval('currentstring = WordLengthCheck(' + textfield + ',' + MaxWords + ')');
	
	if (WordsLeftFieldName.length > 0) {
		left = MaxWords - WordsMonitor;
        /*if(textfieldname=='Q_T_0_CareerPathHights'){
            left = left + 82;
            WordsMonitor = WordsMonitor - 82;
        }
         if(textfieldname=='Q_T_0_Accomplishments'){
            left = left + 3;
            WordsMonitor = WordsMonitor - 3;
        }*/
		if(truncate) {
			left = 0; 
			truncate = false;
			//errorMsg += "You entered more than " + MaxWords +" words in the question for \n"+question + "\n\n";
			//eval('document.' + FormName + '.' + message + '.value = ' + 'hello');
			//alert(errorMsg + "\n\n" + "More than " + maxwords + "words are truncated.\n");
			//passWordLimit = false;
			//alert(errorMsg);
			var warning = "<br/><strong>You entered more than " + MaxWords +" words. Your entry has been truncated. Please edit your entry to the required length.</strong>\n";
			//lert(warning);
			document.getElementById(message).innerHTML = warning;
		}		
		eval('document.' + FormName + '.' + WordsLeftFieldName + '.value = ' + left);
		if(currentstring.length < currentlength) { eval(textfield + ' = currentstring.substring(0)'); }
	}
	if (WordsTypedFieldName.length > 0) {
		eval('document.' + FormName + '.' + WordsTypedFieldName + '.value = ' + WordsMonitor);
		if(currentstring.length < currentlength) { eval(textfield + ' = currentstring.substring(0)'); }
	}
	
	
	//if(errorMsg.length > 0){
	//	alert(errorMsg + "\n\n" + "More than " + maxwords + "words are truncated.\n");
	//}
	//if(left < 0){
		
		//alert(errorMsg);
		
		//if (left < -5){
			//eval('document.'+FormName+'.'+textfieldname+'.value = ""');	
			//eval(textfield + '=""');
			//left = 0;
			//now re-run this function to fix counts
			//InputWordLengthCheck(maxwords,textfieldname,question,typed_textfield,left_textfield);
		//}
	//}
	/*
	if(errorMsg.length > 0){
		alert(errorMsg);	
		errorMsg ='';	
	}
	*/
	
} // function InputWordLengthCheck()

function CheckInputWords(numCount1, numCount2){	
	
	var num1 = document.getElementById(numCount1).value;

	var num2 = document.getElementById(numCount2).value;
	//alert("num2:" + num2);
	
	var msg = "";
	if(num1 < 0){
		msg += "You entered more than 150 words on question #1 in PART I\n";
	}
	if(num2 < 0){
		msg += "You entered more than 500 words on question #1 in PART II\n";
	}
	
	//alert('hello');
	if(strlen(msg) > 0){
		//alert(msg);	
		document.getElementById('issubmitted').value = -1;
		var is = document.getElementById('issubmitted').value;
		alert(is);
	}	
}

