﻿function ValidateForm() {
	var bolSubmitForm = true;
	$(".Req").each(function(){
		ErrorType = "";
		if($(this).hasClass("Text")){
			if(this.value.match(/[A-Za-z0-9]/gi)==null){
				ErrorType = "Text";
			}
		}
		if($(this).hasClass("Email")) {
			if(this.value.match(/\b[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}\b/gi)==null){
				ErrorType = "Email";
			}
		}
		if($(this).hasClass("Password")) {
			if(this.value.match(/^(?=.{7,})(((?=.*[a-z])(?=.*[0-9]))).*$/g)==null){
				ErrorType = "Password";
			}
		}
		
		
		if(this.className.indexOf("Date")>=0){
			if(this.className.indexOf("Day")>=0){
				if(isNaN(this.value) || this.value.length>2 || this.value>31){
					ErrorType = 'DateDay';
				}	
			}
			if(this.className.indexOf("Month")>=0){
				if(isNaN(this.value) || this.value.length>2 || this.value>12){
					ErrorType = 'DateMonth';
				}	
			}
			if(this.className.indexOf("Year")>=0){
				if(isNaN(this.value) || this.value.length!=4){
					ErrorType = 'DateYear';
				}	
			}
		}
		
		if($(this).hasClass("isNumber")) {
			this.value = this.value.replace(/,/g,".");
			if(isNaN(this.value)){
				ErrorType = "isNumber";
			}
		}

		if(ErrorType!=""){
			if(!$(this).hasClass("Error")) {
				$(this).after(htmlErrorMsg(ErrorType));
			}
			$(this).addClass("Error");
			bolSubmitForm = false;
		}
		else if($(this).hasClass("Error"))
		{
			$(this).removeClass("Error");
			this.parentNode.childNodes[1].style.display="none";
			//bolSubmitForm = true;
		}
		else
		{
			if(bolSubmitForm) bolSubmitForm = true;
		}
	});
	if(bolSubmitForm) {
		//$("#"+FormId).submit();
		return true;
	}
	else
	{
		return false;
	}
}

var ErrorMsgId = 0;
function htmlErrorMsg(errType) {
	var strErrorMsg = "";
	switch(errType) {
		case 'Text':
			strErrorMsg = "<b>Dette felt er obligatorisk!</b><br />Udfyld venligst den rette information.";
			break;
		case 'Email':
			strErrorMsg = "<b>E-mailformatet er ikke korrekt!</b><br />Du skal udfylde en valid e-mailadresse.<br />fx. info@faarupsommerland.dk";
			break;
		case 'Password':
			strErrorMsg = "Your password must be at least 8 characters long and contain both letters, capitals and numbers to be valid!";
			break;
		case 'DateDay':
			strErrorMsg = "<b>The day format is not correct!</b><br />Please type in a number between 1 and 31";
			break;
		case 'DateMonth':
			strErrorMsg = "<b>The month format is not correct!</b><br />Please type in a number between 1 and 12 ";
			break;
		case 'DateYear':
			strErrorMsg = "<b>The year format is not correct!</b><br />Please type in a 4 digit number.";
			break;
		case 'isNumber':
			strErrorMsg = "<b>Indholdet i dette felt er ikke korrekt!</b><br />Dette felt accepterer kun tal.";
			break;
		case 'FirstSelect':
			strErrorMsg = "<b>Du har ikke valgt en modtager!</b><br />Vælg venligst den rette modtager i listen.";
			break;
	}
	strOutput = '<div class="ErrorIcon" style="position:relative;">';
	strOutput += '<a href="javascript:void(0)" onmouseover="showErrMsg('+ErrorMsgId+',1)" onmouseout="showErrMsg('+ErrorMsgId+',0)"><img src="/files/system/Faarup/graphic/FormFieldErrorInfoIcon.gif" width="14" height="15" border="0"></a>';
	strOutput += '<div class="ErrorMsg'+ErrorMsgId+' ErrDisplay Hide" style="position:absolute; z-index:2;"><img src="/files/system/Faarup/graphic/FormFieldErrorInfoBoxArrow.gif" style="position:absolute; z-index:1; left:-6px; top:7px;" />'+strErrorMsg+'</div></div>';
	ErrorMsgId++;
	return strOutput;
}

function showErrMsg(displayNum,displayState) {
	if(displayState==1) {
		$(".ErrorMsg"+displayNum).removeClass("Hide")
	}
	else
	{
		$(".ErrorMsg"+displayNum).addClass("Hide")
	}
}


