function initialiseForms() {
	var formList = document.getElementsByClassName('filterTextForm');
	for (var ele in formList) {
		var formele = formList[ele];
		formele.onsubmit = function () {return searchFormSubmit(this);}

		var inputList = document.getElementsByClassName('filterText');
		for (var ele in inputList) {
			inputList[ele].onfocus = function() { filterTextFocus(this); };
			inputList[ele].onblur = function() { filterTextBlur(this); };
			filterTextBlur(inputList[ele]);
		}

		var inputList = document.getElementsByClassName('filterTextActive');
		for (var ele in inputList) {
			inputList[ele].onfocus = function() { filterTextFocus(this); };
			inputList[ele].onblur = function() { filterTextBlur(this); };
			filterTextBlur(inputList[ele]);
		}
	}
}
function clearFilters(formObj) {
	for (var i = 0; i < formObj.elements.length; i++) {
		if (formObj.elements[i].className == 'filterText') {
			filterTextFocus(formObj.elements[i]);
		}

        if (formObj.elements[i].className == 'suggestText') {
            filterSuggestTextFocus(formObj.elements[i]);
        }
	}
}

function clearHiddenID(e, hiddenID) {
  if(e.value == ''){
    document.getElementById(hiddenID).value='';
  }
}

function checkFilters(formObj) {
  if (document.getElementById('keyword') && document.getElementById('keyword').value == '' && module == 'businesses') {
    return false;
  }
  return true;
}


function searchFormSubmit(e) {
  clearFilters(e); 
  bCheck = checkFilters(e);
  if(bCheck == false) {
    alert('Please enter a search term.');
  }
  return bCheck;
}

function filterTextFocus(inputObj) {
	if (inputObj.value == inputObj.title) {
		inputObj.value = '';
		inputObj.className = 'filterTextActive';
	}
}

function filterSuggestTextFocus(inputObj) {
    chunk = inputObj.value.slice(0,4);
    if (chunk == 'e.g.') {
        inputObj.value = '';
    }
}

function changeBusinessType(e) {
  if(e.value == 'other') {
    document.getElementById('otherBusinessType').style.display = 'table-row';
  } else {
    document.getElementById('businessTypeName').value = '';
    document.getElementById('otherBusinessType').style.display = 'none';
  }
}

function setHiddenID(e,hiddenID, aTypes) {
    document.getElementById(hiddenID).value = aTypes[e.value];
}

function filterTextBlur(inputObj) {
	if (inputObj.value == '') {
		inputObj.value = inputObj.title;
		inputObj.className = 'filterText';
	}
}

// should be given an array of objects of the form
// {field:"field.id",type:"(input|select)",check:"(notblank|http),dname:"Field Name to show user"}
function validateForm(aValidate) {

  var oField, field, val, i;

  // skipvalidation if there's a skipvalidation field set to skip
  if (document.getElementById('skipvalidation') && 'skip' == document.getElementById('skipvalidation').value) {
    return true;
  }
  
  // if we haven't been sent an array, assume nothing to validate
  if ((aValidate.constructor != Array) || (0 == aValidate.length)) {
    return true;
  }

  for (i = 0; i < aValidate.length; i++) {

    oField = aValidate[i];

    // get the field (if it exists)
    field = document.getElementById(oField.field);
    if (null == field) {
      continue;
    }

    // get its type
    if ('input' == oField.type) {
      val = field.value;
    } else if ('select' == oField.type) {
      val = field[field.selectedIndex].value;
    } else {
      continue;
    }

    // what are we checking?
    // - fields that shouldn't be blank
    if ('notblank' == oField.check && '' == val) {
      alert(oField.dname + " may not be blank.");
      field.focus();
      return false;
    }

    // - fields that should start http(s)://
    if (('http' == oField.check) && ('' != val) && ('eg. http://www.yourdomain.com' != val) && !val.match(/^(http(s?):\/\/)/i)) {
      alert(oField.dname + " must begin http or https");
      field.focus();
      return false;
    }
  }

  // everything validated (or there was nothing to do) so allow submit
  return true;
}

addOnload(initialiseForms);
