// ---------------------------------------------------------------------
// Title: Form Validation Script.
// Author: Peter Tornstrand | <peter[at]tornstrand.com>
// File Name: focusForm.js
// File Dependants: include/contactForm.php, css/validate.css
// Date Created: 01/29/2001
// Last Modified: 04/20/2006 | Michael A. Karr
// ---------------------------------------------------------------------

function validateForm(frm, bolAlert) {
  var elem = frm.elements;
  // Loop all elements
  for (var i = 0; i < elem.length; ++i) {
    // If element is required see if it's filled in
    if (elem[i].className.indexOf("required") > 0) {
    // Text
      if (elem[i].type=="text" || elem[i].type=="password" || elem[i].type=="file") {
        if (elem[i].value=="") {
          elem[i].className = elem[i].className + " text_err";
          if(bolAlert) alert("You must enter "+elem[i].title);
          elem[i].focus();		  
          return false;
        }
      }
      else if (elem[i].type=="radio") {
      // Radio
        var radiogroup = elem[elem[i].name];
        var itemchecked = false;
        for(var j = 0 ; j < radiogroup.length ; ++j) {
          if(radiogroup[j].checked) {
            itemchecked = true;
            break;
          }
        }
      
        if(!itemchecked) { 
          if(bolAlert) alert("You must check one option for "+elem[i].title);
          if(elem[i].focus) elem[i].focus();
          return false;
        }
      }
      else if (elem[i].type=="checkbox") {
      // Checkbox
        var checkgroup = elem[elem[i].name];
        var itemchecked = false;
        for(var j = 0 ; j < checkgroup.length ; ++j) {
          if(checkgroup[j].checked) {
            itemchecked = true;
            break;
          }
        }
      
        if(!itemchecked) { 
          if(bolAlert) alert("You must check atleast one option for "+elem[i].title);
          if(elem[i].focus) elem[i].focus();
          return false;
        }
      }
      else if (elem[i].type=="textarea") {
      // Textarea
        if (elem[i].value=="") {
          elem[i].className = elem[i].className + " textarea_err";
          if(bolAlert) alert("You must enter "+elem[i].title);
          elem[i].focus();
          return false;
        }
      }
      else if (elem[i].type=="select-one") {
      // Select-one
        if (elem[i].selectedIndex==0) {
          elem[i].className = elem[i].className + " select_err";
          if(bolAlert) alert("You must choose "+elem[i].title);
          elem[i].focus();
          return false;
        }
      }
      else if (elem[i].type=="select-multiple") {
      // Select-multiple
      	var optionselected = false;
      	for(var j=0;j<elem[i].options.length; ++j) {
      		if (elem[i].options[j].selected) {
      			optionselected = true;
      			break;
      		}
      	}
      	if (!optionselected) {
          elem[i].className = elem[i].className + " select_err";
          if(bolAlert) alert("You must select at least one option for "+elem[i].title);
          elem[i].focus();
          return false;
        }
      }
    }
  }
  
  //////////////////////// Other validations needed
  
  
  
}

function gotFocus(elem) {
	if (elem.className.indexOf("_err") < 0) {
		if (elem.type == "text" || elem.type == "password" || elem.type == "file") {
			elem.className = elem.className + " text_focus";
		} else if (elem.type=="textarea") {
			elem.className = elem.className + " textarea_focus";
		} else if (elem.type=="select-one") {
			elem.className = elem.className + " select_focus";
		} else if (elem.type=="select-multiple") {
			elem.className = elem.className + " select_focus";
		}
	}
}

function lostFocus(elem) {
	if (elem.type == "text" || elem.type == "password" || elem.type == "file") {
		if (elem.className.indexOf("required") > 0) {
			elem.className = "text required";
		} else {
			elem.className = "text";
		}
	} else if (elem.type=="textarea") {
		if (elem.className.indexOf("required") > 0) {
			elem.className = "textarea required";
		} else {
			elem.className = "textarea";
		}
	} else if (elem.type=="select-one") {
		if (elem.className.indexOf("required") > 0) {
			elem.className = "select required";
		} else {
			elem.className = "select";
		}
	} else if (elem.type=="select-multiple") {
		if (elem.className.indexOf("required") > 0) {
			elem.className = "select required";
		} else {
			elem.className = "select";
		}
	}
}

// Phone Field Jump
function checklength(nextfield,chars,currfield) {

  x= document.contactForm[currfield].value.length
  if (x == chars) {
    eval('document.contactForm.' + nextfield + '.focus();');
  }

}

// Extra Validations
function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		var alertMessage = 'Invalid email address!';
		
		if (str.indexOf(at)==-1){
		   alert(alertMessage);
		   return false;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert(alertMessage);
		   return false;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		   alert(alertMessage);
		   return false;
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		   alert(alertMessage);
		   return false;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		   alert(alertMessage);
		   return false;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		   alert(alertMessage);
		   return false;
		 }
		
		 if (str.indexOf(" ")!=-1){
		   alert(alertMessage);
		   return false;
		 }

 		 return true;				
	}

function validateEmailField() {
	var emailID = document.getElementById('email');
	
	// Checks if email is entered.
//	if ((emailID.value==null)||(emailID.value=="")){
//		alert("Please Enter your Email Address")
//		emailID.focus();
//		return false;
//	}
	
	// Checks if email is valid address format.
	if (echeck(emailID.value)==false) {
		emailID.select();
		return false;
	}
	
	return true;
 
 }
