function restrict_field_length( max_length, e, field_id ){

	var max_length=max_length.split(" ");
	if( max_length.length==1 ){
		var unit_type="characters";
	}else if(max_length=="characters" || max_length[1]=="words"){
		var unit_type=max_length[1];
	}
	max_length=max_length[0];
	
	//If this isn't an event
	if( e==null ){
	
	     //Take the maximum number
		if(unit_type=="characters"){
			document.getElementById(field_id).value=document.getElementById(field_id).value.substring( 0, max_length );
		}else if(unit_type=="words"){
			word_string=get_word_count(document.getElementById(field_id).value, "string", max_length);
			document.getElementById(field_id).value=word_string;
		}
		
		//Don't add the selected value
		return false;
	
	}
	// IE
	else if(window.event){
	
	  var keynum = e.keyCode;
	  
	}
	// Netscape/Firefox/Opera
	else if(e.which){
	
	  var keynum = e.which;

	}
	
	var keychar = String.fromCharCode(keynum);
	
	// Characters
	if(unit_type=="characters"){
		//What is the new length of the field (this function is executed before the key's value is added)
		var input_length=document.getElementById(field_id).value.length;
		
		//If the current length is greater than the maximum (allow backspace and delete buttons)
		if( input_length>=max_length && keynum!=8 && typeof keynum!="undefined" ){
		
		     //Take the maximum number
			document.getElementById(field_id).value=document.getElementById(field_id).value.substring( 0, max_length );
			
			//Don't add the selected value
			return false;
		
		}
	}
	// Words
	else if(unit_type=="words"){
		//What is the new length of the field (this function is executed before the key's value is added)
		var input_length=get_word_count(document.getElementById(field_id).value, "count", max_length);
		
		//If the current length is greater than the maximum (allow backspace and delete buttons)
		if( input_length>max_length && keynum!=8 && typeof keynum!="undefined" ){
		
		     //Take the maximum number
			document.getElementById(field_id).value=get_word_count(document.getElementById(field_id).value, "string", max_length);
			
			//Don't add the selected value
			return false;
		
		}
	}

}

function get_word_count(text, type, max_length){
	
	//Break the words up using spaces
	var words=text.split(" ");
	//Loop through checking to make sure that each is an actual word
	var word_count=0;
	var word_string="";
	for( var i=0; i<words.length; i++ ){
		if(string_replace(" ","",words[i])!=""){
			word_count++;
		}
		if( word_count<=max_length ){
			if(i!=0){
				word_string+=" ";
			}
			word_string+=words[i];
		}
	}

	if(type=="string"){
		return word_string;
	}
	return word_count;
	
}

function show_field_length( id, field_id, max_length ){

	var max_length=max_length.split(" ");
	if( max_length.length==1 ){
		var unit_type="characters";
	}else if(max_length=="characters" || max_length[1]=="words"){
		var unit_type=max_length[1];
	}
	max_length=max_length[0];
	
	// Words
	if(unit_type=="words"){
		//DEBUGGING
		if(field_id=='comment_preview__c6089b43de1ea63ae801bc1637423c78835b1d9b'){
			//alert(field_id+"\n\n"+document.getElementById(field_id).value+"\n\n"+get_word_count(document.getElementById(field_id).value, "count", max_length));
		}
		document.getElementById(id).innerHTML=get_word_count(document.getElementById(field_id).value, "count", max_length);
	}
	// Characters
	else{
		document.getElementById(id).innerHTML=document.getElementById(field_id).value.length;
	}

}

function toggle_visibility( id, action ){

	if( action!="hide" && action!="show" ){
		//Determine the current state
		var current_state=document.getElementById(id).className;
		if( current_state=="hide" ){
			action="show";
		}else{
			action="hide";
		}
	}
	document.getElementById(id).className=action;

}

function respond( id ){
	
	//Show or hide the response field
	toggle_visibility( id );

	//Change the description of the link
	//Get the current link description
	var current_link_description=document.getElementById(id+"_link").innerHTML;
	if( current_link_description=="Respond" ){
		document.getElementById(id+"_link").innerHTML="Cancel Response";
	}else{
		document.getElementById(id+"_link").innerHTML="Respond";
	}

}

function view_responses( id ){
	
	//Show or hide the response field
	toggle_visibility( id );

	//Change the description of the link
	//Get the current link description
	var current_link_description=document.getElementById(id+"_link").innerHTML;
	if( current_link_description=="View Responses" ){
		document.getElementById(id+"_link").innerHTML="Hide Responses";
	}else{
		document.getElementById(id+"_link").innerHTML="View Responses";
	}

}

//Replace a string with another string
function string_replace(findChars,replacementChars,text){
	//Replace Characters
	var newText="";
	for(var r=0;r<text.length;r++){
		//Set char string to compare
		var charString="";
		//starting at r, create charString to compare findChars with (according to number of chars)
		for(var s=r;s<r+findChars.length&&s<text.length;s++){
			charString+=text.charAt(s);
		}
		//If the charString matches findChars
		if(charString==findChars){
			//add charString to nextText
			newText+=replacementChars;
			//and move on, starting at next unchecked char
			r=r+eval(findChars.length-1);
		}
		else{
			//Add letter to nextText
			newText+=text.charAt(r);
		}
	}
	return newText;
}
