
var errfound = false;
var attempts = 0;

addEvent(window,'load',initForm);

function initForm() {


	var contact = document.getElementById("contact");
	document.contact.onsubmit = Validate;

// 	var first = document.getElementById("first");
//         first.onkeyup = onkeyupFirst;

	var email = document.getElementById("email");
        email.onkeyup = onkeyupEmail;

	var phone = document.getElementById("phone");
        phone.onchange = onkeyupPhone;

	var school = document.getElementById("school");
        school.onchange = onkeyupSchool;



	var dept = document.getElementById("dept");
        dept.onchange = onkeyupDept;



}


function Validate() {
   errfound = false;


// 	var contactFirst = document.getElementById("first");
	var contactEmail = document.getElementById("email");
	var contactPhone = document.getElementById("phone");
	var contactSchool = document.getElementById("school");
	var contactDept = document.getElementById("dept");

//    if (!ValidLength(contactName.value,1))
//       error("nameErr","Please enter your name");
//    else
//       clean("nameErr");


//    if (!ValidLength(contactFirst.value,1))
//       error("firstErr","Please enter your name");
//    else
//       clean("firstErr");

   if (!ValidEmail(contactEmail.value))
      error("emailErr", "Please enter a valid email");
   else
      clean("emailErr");

   if (!ValidLength(contactPhone.value,6))
      error("phoneErr","Please enter your phone no");
   else
      clean("phoneErr");

   if (!ValidLength(contactSchool.value,2))
      error("schoolErr","Please enter your school");
   else
      clean("schoolErr");


   if (!ValidLength(contactDept.value,2))
      error("deptErr","Please enter your address");
   else
      clean("deptErr");


   attempts ++;
   return !errfound;
}


function error(elem, text) {
   var formField = document.getElementById(elem);
   formField.style.visibility = "visible";
   formField.innerHTML = text; 
   removeClass(formField, "ok_form");
   addClass(formField, "error_form");
   fadeIn(elem, 0);
   errfound = true;
}


function clean(elem) {
   document.getElementById(elem).innerHTML = "";
}

function approve(elem) {
   var formField = document.getElementById(elem);
   formField.innerHTML = "OK!";
   removeClass(formField, "error_form");
   addClass(formField, "ok_form");
   fadeOut(elem, 200);
}


function ValidLength(item, len) {
   return (item.length >= len);
}


function ValidEmail(item) {
  if (item.match(/^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+$/))
       { return true; }

  return false;
}


// function onkeyupFirst() {
//   from = document.getElementById("first");
//    if (ValidEmail(first.value)) // and innerhtml = 'error'
//       approve("firstErr");
// }

function onkeyupEmail() {
  email = document.getElementById("email");
   if (ValidEmail(email.value)) // and innerhtml = 'error'
      approve("emailErr");
}


function onkeyupPhone() {
  phone = document.getElementById("phone");
   if (ValidLength(phone.value,9))
      approve("phoneErr");
}

function onkeyupSchool() {
  school = document.getElementById("school");
   if (ValidLength(school.value,2))
      approve("schoolErr");
}



function onkeyupDept() {
  dept = document.getElementById("dept");
   if (ValidLength(dept.value,2))
      approve("deptErr");
}





//		fade effects

function fadeIn(objId,opacity) {
	 if (document.getElementById)
	  {
	   obj = document.getElementById(objId);
	   if (opacity <= 100) {
	   setOpacity(obj, opacity);
	   opacity += 10;
	   window.setTimeout("fadeIn('"+objId+"',"+opacity+")", 75);
	  }
	 }
}


function fadeOut(objId,opacity) {
	 if (document.getElementById)
	  {
	    obj = document.getElementById(objId);
	     if (opacity >= 0) {
	      setOpacity(obj, opacity);
	      opacity -= 10;
	      window.setTimeout("fadeOut('"+objId+"',"+opacity+")", 75);
	     }

	 }
}

function setOpacity(obj, opacity) {
	 opacity = (opacity == 100)?99.999:opacity;
	 obj.style.filter = "alpha(opacity:"+opacity+")";
	 obj.style.KHTMLOpacity = opacity/100;
	 obj.style.MozOpacity = opacity/100;
	 obj.style.opacity = opacity/100;
}


function addClass(target, classValue) {
    if (!hasClass(target, classValue))
    {
        if (target.className == "")
        {
            target.className = classValue;
        }
        else
        {
            target.className += " " + classValue;
        }
    }

    return true;
}




function removeClass(target, classValue) {
    var removedClass = target.className;
    var pattern = new RegExp("(^| )" + classValue + "( |$)");

    removedClass = removedClass.replace(pattern, "$1");
    removedClass = removedClass.replace(/ $/, "");

    target.className = removedClass;

    return true;
}




function hasClass(target, classValue) {
    var pattern = new RegExp("(^| )" + classValue + "( |$)");
    
    if (target.className.match(pattern))
    {
    	return true;
    }
    
    return false;
}



