

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}



function focusLabels() {
  if (!document.getElementsByTagName) return false;
  var labels = document.getElementsByTagName("label");
  for (var i=0; i<labels.length; i++) {
    if (!labels[i].getAttribute("for")) continue;
    labels[i].onclick = function() {
      var id = this.getAttribute("for");
      if (!document.getElementById(id)) return false;
      var element = document.getElementById(id);
      element.focus();
    }
  }
}

function resetFields(whichform) {
  for (var i=0; i<whichform.elements.length; i++) {
    var element = whichform.elements[i];
    if (element.type == "submit") continue;
    if (!element.defaultValue) continue;
    element.onfocus = function() {
    //if (this.value == this.defaultValue) {
     // this.value = "";
     //}
    }
    element.onblur = function() {
      //if (this.value == "") {
     //  // this.value = this.defaultValue;
      //}
    }
  }
}

function validateForm(whichform) {
  for (var i=0; i<whichform.elements.length; i++) {
    var element = whichform.elements[i];
    if (element.className.indexOf("required") != -1) {
      if (!isFilled(element)) {
        //alert("Please fill in the "+element.name+" field.");
	   alert("Please fill in the "+element.title+" field.");
        return false;
      }
    }
    if (element.className.indexOf("email") != -1) {
      if (!isEmail(element)) {
        //alert("The "+element.name+" field must be a valid email address.");
		alert("The "+element.title+" field must be a valid email address.");
        return false;
      }
    }
  }
  return true;
}

function isFilled(field) {
  if (field.value.length < 1) {
    return false;
  } else {
    return true;
  }
}

function isEmail(field) {
  // are regular expressions supported?
  var str = field.value;
	var supported = 0;
  if (window.RegExp) {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }
  if (!supported){
		if ((str.indexOf(".") == 0) || (str.indexOf("@") == 0)){
			return false;
		} else {
			return true;
		}
	}
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  if (!r1.test(str) && r2.test(str)){
		return true;
	} else {
		return false;
	}
}

function prepareForms() {
  for (var i=0; i<document.forms.length; i++) {
    var thisform = document.forms[i];
    resetFields(thisform);
    thisform.onsubmit = function() {
     	if (typeof ae_onSubmit == 'function'){
				ae_onSubmit();
			}
			return validateForm(this);
    }
  }
}












function popWindows() {
  if (!document.getElementsByTagName) return false;
  var lnks = document.getElementsByTagName("a");
  for (var i=0; i<lnks.length; i++) {
  if (lnks[i].className == "popUpPlain") {
      lnks[i].onclick = function() {
        popUpPlain(this.getAttribute("href"));
        return false;
      }
    }
	
	 else if (lnks[i].className == "popUpPriv") {
      lnks[i].onclick = function() {
        popUpPriv(this.getAttribute("href"));
        return false;
      }
    }
  }
}


function popUpPlain(winURL) {
window.open(winURL,"popUpPlain","width=520,height=275,left=0,top=0;");
}


function popUpPriv(winURL) {
window.open(winURL,"popUpPriv","toolbar=yes,location=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,width=700,height=555,left=150,top=0;");
}




addLoadEvent(popWindows);

















addLoadEvent(focusLabels);
addLoadEvent(prepareForms);