// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

// Following function is to count and limit text area field in forms
function textCounter(field, countfield, maxlimit) {
if (field.value.length > maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
else 
countfield.value = maxlimit - field.value.length;
}

function humanizeCase(str)
{   
    return str.replace(/[^\w]+/g, " ").replace(/\s+/g, " ").replace(/\s([a-z])/g, function(t,a) { return " " + a.toUpperCase(); }).replace(/^([a-z])/, function(t, a) { return a.toUpperCase();})
}

function is_non_english(str) {
	if( str.match(/[a-zA-Z]/) == null ) {
		return true;
	}
	else {
		return false;
	}
}

function trimString(str, count) {
	if( is_non_english(str) ) {
		return str;
	}
	if( str.length > count ) {
      str = str.substring(0, (count - 1)) + ".."; 
    }
    return str;
}

function getFromSelect(elem, from){
	if(from == "value") {
		return elem.options[elem.selectedIndex].value;
	}
	else if(from == "text") {
		return elem.options[elem.selectedIndex].text;
	}
}

function change_parent_select_text(elem_id, opt_val, txt) {
	for(i=0; i < parent.$(elem_id).options.length; i++) {
		if( parent.$(elem_id).options[i].value == opt_val) {
			parent.$(elem_id).options[i].text = txt;
			break;
		}
	}
}

function remove_from_parent_select(elem_id, opt_val) {
	for(i=0; i < parent.$(elem_id).options.length; i++) {
		if( parent.$(elem_id).options[i].value == opt_val ) {
			parent.$(elem_id).remove(i);
			break;
		}
	}
}
