window.onload = function () {
  for (var i=0; i<document.forms.length; i++) {
    document.forms[i].onsubmit = function() {return validateForm(this);}
  }
  initPage();
  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 initPage() {
  if (document.getElementById) {
    pageName = document.getElementById("pageID").value;
    initMenu();
    var searchField = document.getElementById("fieldSearch");
    searchField.onfocus = toggleFieldEvt;
    searchField.onblur = toggleFieldEvt;
    if (pageName == "Home") {
      initNews();  
    }
  }
}

function initMenu() {
  var arrMenu = document.getElementById("containerMenu").getElementsByTagName("a"); 
  for (var i=0; i<arrMenu.length; i++) {
    arrMenu[i].onclick = toggleMenu
	if (pageName == arrMenu[i].innerHTML) {
  	  openMenu(arrMenu[i]);
	}
  }
}

function openMenu(obj){
  obj.parentNode.parentNode.style.display = 'block';
  obj.style.background = 'none';
  obj.style.backgroundColor = '#000099';
  obj.innerHTML += "<span id='thisPageArrow'><img src='../images/bullet_downState.gif' alt='arrow' height='15' width='12' /></span>";
}

function toggleFieldEvt(evt) {		  
  var evt = (evt) ? evt : ((window.event) ? window.event : null);
  if (evt.type == "blur") {
    this.style.backgroundColor = "#424254";
    this.style.color = "#CCCCCC";
	if (this.value == '') 
	  this.value = this.defaultValue;
  }  
  if (evt.type == "focus") {
   this.style.backgroundColor = "#D9D9E8"
   this.style.color = "black"
    if (this.defaultValue == this.value)
      this.value = '';
   }   
}
//start Navbar
var autoClose=1;
var oldObj=null;
var bullet=null;

function toggleMenu() { 
  var obj = this.parentNode.childNodes;//return array
  for(var i=0; i<obj.length; i++){
    if(obj[i].className == "subMenu"){
	  obj = obj[i];//store target
	  var blnSubMenu = true;
	}
  }  
  if(oldObj && autoClose==1){
    oldObj.style.display='none';	 
	bullet.style.listStyleImage='url(../images/bullet_upState.gif)';	
  }
  if(blnSubMenu){
    if(obj.style.display =='block'){ 
      obj.style.display ='none';	
    }
    else{  
      this.style.listStyleImage='url(../images/bullet_downState.gif)';
	  obj.style.display ='block';
	  oldObj = obj;
	  bullet = this;
    }
  }
} //END NAVBAR  