

var isValid;
var errorShow;

function setValid(bool){
	if (bool == false){
		isValid = false;
	}
}

function addError(str){
		errorShow += str + "\n";
}

function Validate(oForm){
	isValid = true;
	errorShow="";

    for(var i=0; i<oForm.length; i++) {
        var currElement = oForm.elements[i];
        var currID = currElement.id;
        var currName = currElement.name;
        var val = currID.split("_");
        var valMethod = val[1];
        var extra = val[2];
        var fieldName = currElement.parentNode.childNodes[0].nodeValue;

		try {
			var fieldName = currElement.parentNode.childNodes[0].nodeValue;
			if (typeof eval(valMethod) == "function") {
					var eString = currElement.value;
					if (extra != null){
						if (fieldName == null ){ fieldName ='undefined'};
						setValid(eval(valMethod + "(\"" + eString+ "\",\"" + trimEnters(''+fieldName) + "\",\"" + extra + "\")")  );
					} else {
						setValid(eval(valMethod + "(\"" + eString+ "\",\"" + trimEnters(''+fieldName) + "\")")  );
					}
			}
		} catch(varException){addError(varException.message + "JS ERROR 101 " + valMethod);isValid=false;}
    }
    if (errorShow != ""){
        alert(errorShow);
    }
    return isValid;
}

function checkPasswords(strText,fieldName,extra){

	if (document.getElementsByName(extra)[0].value != strText){
		addError("Passwords must be the same"); 	
		return false;
	}

	if (  passCheck(strText) != true ){
		addError("Passwords don't meet the requirements"); 	
		return false
	}
	return true;
}

function passCheck(pass1){
	if (! pass1.match(/[A-Z]/) ){
		return false;
	}
	if (! pass1.match(/[a-z]/) ){
		return false;
	}
	
	
	if (!  pass1.match(/.*[0-9].*[0-9]/) ){
		return false;
	}
		
	if ( pass1.length < 7 ){
			return false;
	}
	return true;
}

// Helper Methods
function trimWhitespace(strText) {
	while (strText.substring(0,1) == ' '){
		strText = strText.substring(1, strText.length); 
	}
	while (strText.substring(strText.length-1, strText.length) == ' '){
		strText = strText.substring(0, strText.length-1);
	}
	return strText; 
}

function trimEnters(strText) {
	while (strText.substring(0,1) == '\r'){
		strText = strText.substring(1, strText.length); 
	}
	while (strText.substring(strText.length-1, strText.length) == '\r'){
		strText = strText.substring(0, strText.length-1);
	}
	while (strText.substring(0,1) == '\n'){
		strText = strText.substring(1, strText.length); 
	}
	while (strText.substring(strText.length-1, strText.length) == '\n'){
		strText = strText.substring(0, strText.length-1);
	}
	return strText; 
}

// Validation methods
function isSame(eString, fieldName){
	eString = trimWhitespace(eString); 
	if(eString == null || eString == "") {
		addError(fieldName + "must not be blank."); 
		return false;
	}
	return true;
}

function isNotEmpty(eString, fieldName){
	eString = trimWhitespace(eString); 
	if(eString == null || eString == "") {
		addError(fieldName + "must not be blank."); 
		return false;
	}
	return true;
} 

function isEmail(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   addError("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   addError("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    addError("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    addError("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    addError("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    addError("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    addError("Invalid E-mail ID")
		    return false
		 }
 		 return true					
	}
	function isURL(str) {
	str = trimWhitespace(str);

	  if (str.indexOf("http://") == -1 && str.indexOf("https://") == -1){
            addError("URL moet beginnen met http://")
			return false;
	  }
	  else if (str == "http://"){
            addError("URL is oncompleet");
			return false;
	  }else if (str.indexOf("http://") > 0){
            addError("URL is onjuist");
		return false;
		}

	  str = str.substring(7, str.length);
	  if (str.indexOf(".") == -1){
            addError("URL is onjuist");
		return false;
	  }else if (str.indexOf(".") == 0){
            addError("URL is onjuist");
		return false;
	  }else if (str.charAt(str.length - 1) == "."){
            addError("URL is onjuist");
		return false;
	 }

	  if (str.indexOf("/") != -1) {
		str = str.substring(0, str.indexOf("/"));
		if (str.charAt(str.length - 1) == "."){
            addError("URL is onjuist")
		  return false;
		}
	  }

	  if (str.indexOf(":") != -1) {
		if (str.indexOf(":") == (str.length - 1)){
            addError("URL is onjuist");
		  	return false;
		}	else if (str.charAt(str.indexOf(":") + 1) == ".") {
            addError("URL is onjuist");
		  	return false;
		}
		str = str.substring(0, str.indexOf(":"));
		if (str.charAt(str.length - 1) == ".") {
            addError("URL is onjuist");
		  	return false;
		}
	  }

	  return true;

	}



	function isHostname (str) {
		str = trimWhitespace(str);
		if (str.length < 1 ){
            addError("Onjuiste hostname lengte")
            return false;
		}
 		if ( ! str.match(/^[a-zA-Z0-9\-\.]+$/i) ){
            addError("Onjuiste hostname")
            return false;
        }	
 		if (  str.match(/^[\.\-]/i) ){
            addError("Onjuiste hostname")
            return false;
        }	
 		if (  str.match(/[\.\-]$/i) ){
            addError("Onjuiste hostname")
            return false;
        }	
		return true;
	}

	function isPC(str) {
		  str = trimWhitespace(str); 
     if (! str.match(/[1-9][0-9]{3} ?[a-zA-Z]{2}/) ){
     	addError("Onjuiste postcode")
     	return false;
     }
     return true;
	}

