  for (var i=0; i<document.forms.length; i++) {
    document.forms[i].onsubmit = function() {return validateForm(this);}
  }
  initFields();

function formatString(obj) {
  if (obj.value) { //check for value
    return obj.value.charAt(0).toUpperCase() + obj.value.substring(1, obj.value.length); 
  }
  return;  
}

function checkValue(obj) {
  var re = "";
  if (obj.className.indexOf("str") != -1) {
    obj.value = formatString(obj);	
	re = /^\D+$/i;	
  }
  else if (obj.className.indexOf("postcode") != -1) {
    obj.value = obj.value.toUpperCase();
    re = /^([a-z]{1,2}\d{1,2}\s\d{1}[a-z]{2})|([a-z]{2}\d{2}\s\d{2}[a-z]{2})$/i;//CHECK
  }
  else if (obj.className.indexOf("phone") != -1) {
    re = /^\(?\d{3,19}\)?\s?\d{3,12}$/;
  }
  else if (obj.className.indexOf("email") != -1) {
    re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/i;
  } 
  else if (obj.className.indexOf("file") != -1) {
	re = /^(file|http):(\/\/|\\\\).+\.(gif|jpg)/i;
  }   
  if (re) {
    if (!re.test(obj.value)) {
      writeError(obj, " Invalid data");
	  return;
    }
  }
}

function initFields() {
  for (var i=0; i<document.forms.length; i++) {
    arrElements = document.forms[i].elements;
    for (j=0; j < arrElements.length; j++) {
      arrElements[j].onfocus = toggleFieldColor; 
      arrElements[j].onblur = toggleFieldColor;  
    }
  }
}
 
function toggleFieldColor(evt){
  var evt = (evt) ? evt : ((window.event) ? window.event : null);
  if (evt.type == "blur") {
    this.style.backgroundColor = "";
  }
  if (evt.type == "focus") {
   this.style.backgroundColor = "#FFFFD7"
  } 
}
	
function validateForm(thisForm) {
  validForm = true;
  firstError = null;
  errorstring = "";
  var arrElements = thisForm.elements;  
  for (var i=0; i<arrElements.length; i++) {
    if (!arrElements[i].value && !arrElements[i].className.indexOf("reqd")) {
	    writeError(arrElements[i]," Required data!");	
	}
	else if (arrElements[i].value) {
	  arrElements[i].value = arrElements[i].value.replace(/^\s+|\s+$/, "");
	  checkValue(arrElements[i]);
	}
  }

  //start crossCheck
  if (document.getElementById("txtEmail")) {
    var obj1 = document.getElementById("txtEmail"); 
    var obj2 = document.getElementById("txtEmailConfirm");   
    crossCheck(obj1,obj2);
  }
  //end crossCheck
  if (firstError)
    firstError.focus();
  if (validForm)
    var submitIt = confirm("Are you sure you wish to submit this form?");
  if (submitIt)
    return true;		
  return false;
}

function writeError(obj,message) {
  validForm = false;
  if (!firstError)
    firstError = obj;
  if (obj.parentNode.hasError) return; 
  if (document.getElementById) {
	var sp = document.createElement("span");
	sp.className = "errorTxt";
	sp.appendChild(document.createTextNode(message));
	obj.style.border = "1px solid #CD0506";
	obj.parentNode.appendChild(sp);
	obj.parentNode.hasError = sp;
	obj.onchange = removeError;
  }
  else {
    errorstring += obj.name + ': ' + message + '\n';
	obj.hasError = true;
  }
}

function removeError() { 
  this.style.border = "1px solid black"; 
  this.parentNode.removeChild(this.parentNode.hasError);
  this.parentNode.hasError = null;
  this.onchange = null;
}

function crossCheck(obj1,obj2) {
  if (obj1.value != obj2.value) {
    writeError(obj2," Email not matched!");
  }
}
function confirmDelete() {
  var agree=confirm("Are you sure you wish to permanently delete this record?");
  if (agree)
    return true;
  else
	return false;
}
