// JavaScript Document
// This page contains the following scripts:
// Click the text next to Check Box to activate Check Box
// This script converts text entered into a form to first letter capital ie, suffolk = Suffolk
// To control the maximum amount of characters which can be entered into a Text Field 
// This script prevents Google Toolbar from highlighting form fields which can be automatically filled
// To ensure fields starting with the word required are completed by the user
// To ensure two fields are identical
// To create additional input boxes when selecting appropriate radio button
// To create additional input boxes when required ie, select other for new text box to appear
// Email this page to a friend
// To ensure a Radio Button has been selected



<!-- Click the text next to Check Box to activate Check Box -->
function changeBox(cbox) {
box = eval(cbox);
box.checked = !box.checked;
}


<!-- This script converts text entered into a form to first letter capital ie, suffolk = Suffolk -->
function changeCase(frmObj) {
var index;
var tmpStr;
var tmpChar;
var preString;
var postString;
var strlen;
tmpStr = frmObj.value.toLowerCase();
strLen = tmpStr.length;
if (strLen > 0)  {
for (index = 0; index < strLen; index++)  {
if (index == 0)  {
tmpChar = tmpStr.substring(0,1).toUpperCase();
postString = tmpStr.substring(1,strLen);
tmpStr = tmpChar + postString;
}
else {
tmpChar = tmpStr.substring(index, index+1);
if (tmpChar == " " && index < (strLen-1))  {
tmpChar = tmpStr.substring(index+1, index+2).toUpperCase();
preString = tmpStr.substring(0, index+1);
postString = tmpStr.substring(index+2,strLen);
tmpStr = preString + tmpChar + postString;
         }
      }
   }
}
frmObj.value = tmpStr;
}


<!-- To control the maximum amount of characters which can be entered into a Text Field --> 
function textCounter(field, countfield, maxlimit)
{
if (field.value.length > maxlimit)
field.value = field.value.substring(0, maxlimit);
else 
countfield.value = maxlimit - field.value.length;
}


<!-- This script prevents Google Toolbar from highlighting form fields which can be automatically filled -->
 if(window.attachEvent)
  window.attachEvent("onload",setListeners);

function setListeners(){
  inputList = document.getElementsByTagName("input");
  for(i=0;i<inputList.length;i++){
    inputList[i].attachEvent("onpropertychange",restoreStyles);
    inputList[i].style.backgroundColor = "";
  }
  selectList = document.getElementsByTagName("select");
  for(i=0;i<selectList.length;i++){
    selectList[i].attachEvent("onpropertychange",restoreStyles);
    selectList[i].style.backgroundColor = "";
  }
}

function restoreStyles(){
  if(event.srcElement.style.backgroundColor != "" && event.srcElement.style.backgroundColor != "#ffffff"){
    event.srcElement.style.backgroundColor = "#ffffff"; /* color of choice for AutoFill */
    document.all['googleblurb'].style.display = "block";
  }
}


<!-- To ensure fields starting with the word required are completed by the user -->
function checkrequired(which) {
var pass=true;
if (document.images) {
for (i=0;i<which.length;i++) {
var tempobj=which.elements[i];
if (tempobj.name.substring(0,8)=="required") {
if (((tempobj.type=="text"||tempobj.type=="textarea")&&
tempobj.value=='')||(tempobj.type.toString().charAt(0)=="s"&&
tempobj.selectedIndex==0)) {
pass=false;
break;
         }
      }
   }
}
if (!pass) {
shortFieldName=tempobj.id.substring(0,50).toUpperCase();
alert("Please ensure the "+shortFieldName+" field was completed in full");
return false;
}
else
return true;
}


<!-- To ensure two fields are identical -->
function tmt_compareField(f1,f2,rule,errorMsg){
	var myErr = "";
	if(eval("MM_findObj('"+f1+"').value"+rule+"MM_findObj('"+f2+"').value")){
		alert(unescape(errorMsg));myErr += 'errorMsg';}
	document.MM_returnValue = (myErr == "");
}


<!-- To create additional input boxes when selecting appropriate radio button -->
function toggleMe(obj, a){
  var e=document.getElementById(a);
  if(!e)return true;
    e.style.display="block"
  return true;
}

function toggleMe2(obj, a){
  var e=document.getElementById(a);
  if(!e)return true;
    e.style.display="none"
  return true;
}


<!-- To create additional input boxes when required ie, select other for new text box to appear -->
function testOther(obj){
  var val = obj.options[obj.selectedIndex].value; 
  document.getElementById('other').style.display = (val=='other') ? "":"none";
}


<!-- Email this page to a friend -->
function initMail(form) {
text = "Check out this page:  " + window.location;
form.message.value = "Hi " + form.sendto.value + " (" + form.to.value + "):\n\n"
 + text + "\n\nYour Friend,\n" + form.sendername.value + "(" + form.senderemail.value + ")";
return (form.to.value != "");
}


<!-- Checks that a radio button has been selected -->
function checkRadios() {
 var el = document.forms[0].elements;
 for(var i = 0 ; i < el.length ; ++i) {
  if(el[i].type == "radio") {
   var radiogroup = el[el[i].name]; // get the whole set of radio buttons.
   var itemchecked = false;
   for(var j = 0 ; j < radiogroup.length ; ++j) {
    if(radiogroup[j].checked) {
	 itemchecked = true;
	 break;
	}
   }
   if(!itemchecked) { 
    alert("Please choose an answer for "+el[i].name+".");
    if(el[i].focus)
     el[i].focus();
	return false;
   }
  }
 }
 return true;
} 