
	// clean CAPS out of input or text fields
		function cleanCAPS(str) {
			capsallowed = 3; // Lowercase if more than ## CAPS in a row
			do {
				eval("re = /([A-Z]{" + (capsallowed+1) + ",})/g;");
				myArray = str.match(re);
				if (myArray) {
					eval("re = /" + myArray[0] + "/;");
					str = str.replace(re, ""+myArray[0].toLowerCase());
				   }
				} while (myArray);
			return str;
			}

	// clean all CAPS out of text fields if more than 'x' words are 'x' long min and all matched words in captials 
		function cleanAllCAPSif(str) {
			var count = 3; // x or more words match then convert str
			// var str = prepStr(str);
			var matchList = 0;
			var val = 1;
			var regexp = /[A-Z][A-Z][A-Z][A-Z]/g;
			var capsallowed = 0;
			
			// Count uppercase words longer than 'x'
			while ((match = regexp.exec(str)) != null) {
				matchList += val;
			}
			
			// if 'x' many matches of uppercase words then make all text lower case
			if (matchList >= count) {
				do {
					eval("re = /([A-Z]{" + (capsallowed+1) + ",})/g;");
					myArray = str.match(re);
					if (myArray) {
						eval("re = /" + myArray[0] + "/;");
						str = str.replace(re, ""+myArray[0].toLowerCase());
					   }
					} while (myArray);
			}
			return str;
			}
  
	// Define a function to format strings for easier manipulation
		function prepStr(str) {
			str = str.toLowerCase();
			str = str.replace(/\\['"-]/g, '');
			str = str.replace(/\W/g, ' ');
			str = str.replace(/\s+/g, ' ');
			return str;
		}

		
	// Convert 1st letter to uppercase, studlyCaps
		function capitaliseCase(str) {
			var theCase = 1;
			var tempArray = str.split(' ');
			
			// Make the first character of each word upper- or lowercase
			// depending on the value of theCase
			for (var i = 0; i < tempArray.length; i++) {
			if (theCase) {
			  tempArray[i] = tempArray[i].charAt(0).toUpperCase() + tempArray[i].substring(1);
			  }
			else {
			  tempArray[i] = tempArray[i].charAt(0).toLowerCase() + tempArray[i].substring(1);
			  }
			}
			return tempArray.join(' ');
		  }
		

	// Textarea text counter with progress meter
		function progressMeterShow() {
				document.getElementById("progressbar1").style.display='block';		// show progressbar1 div
		}
		function progressMeterHide() {
				document.getElementById("progressbar1").style.display='none';		// hide progressbar1 div
		}

		function textCounter(field,counter,maxlimit) {
			// text width//
			var fieldWidth =  parseInt(field.offsetWidth);
			var charcnt = field.value.length;        

			// trim the extra text
			if (charcnt > maxlimit) { 
				field.value = field.value.substring(0, maxlimit);
			}
			else { 
				// progress bar percentage
				var percentage = parseInt(100 - (( maxlimit - charcnt) * 100)/maxlimit) ;
				document.getElementById(counter).style.width =  parseInt((fieldWidth*percentage)/100)+"px";
				document.getElementById(counter).innerHTML="Word limit: "+percentage+"%"
				// color correction on style from CCFFF -> CC0000
				setcolor(document.getElementById(counter),percentage,"background-color");
			}
		}

		function setcolor(obj,percentage,prop){
			obj.style[prop] = "rgb(80%,"+(100-percentage)+"%,"+(100-percentage)+"%)";
		}