// ===== VALIDATE CONTROL =====
function ValCtl(ctlMe, tMessage) {
	if (!(ctlMe.value)) {
		alert(tMessage);
		ctlMe.focus();
		return false;
	} else {
		return true;
	}
}


// ===== VALIDATE AMOUNT =====
function ValAmount(ctlMe, iMin, iMax, tMessage) {
//	INIT
	tAmount = ctlMe.value;
	tAmount = tAmount.replace(/ /g, '');
	tAmount = tAmount.replace(/\./g, '');
	tAmount = tAmount.replace(/\,/g, '');
	ctlMe.value = tAmount;
	
//	CHECK IF LOAN AMOUNT IS VALID
	if (!(isNaN(tAmount))){
		if ((Number(tAmount) >= iMin) && (Number(tAmount) <= iMax)){
			return true;
		}
	}

//	NOT VALID
	alert(tMessage);
	ctlMe.value = "";
	ctlMe.focus();
	return false;
}


// ===== VALIDATE FIRSTNAME =====
function ValFirstName(ctlMe, tMessage) {
//	INIT
	tFirstName = ctlMe.value;

//	CHECK IF NOT A NUMBER AND AT LEAST TWO CHARS
	if (isNaN(tFirstName)) {
		if (tFirstName.length >= 2) {

			/* Change First Name in the form to begin with Upper Case followed bo Lower Case */
			tFirstName = ((tFirstName.substr(0,1)).toUpperCase() + (tFirstName.substr(1,tFirstName.length-1)).toLowerCase());
			ctlMe.value = tFirstName;

			/* Returns valid First Name */
			return true;
		}
	}

//	NOT VALID
	alert(tMessage);
	ctlMe.value = "";
	ctlMe.focus();
	return false;
}


// ===== VALIDATE LASTNAME =====
function ValLastName(ctlMe, tMessage) {
//	INIT
	tLastName = ctlMe.value;

//	CHECK IF NOT A NUMBER AND AT LEAST TWO CHARS
	if (isNaN(tLastName)){
		if (tLastName.length >= 2) {

			/* Change Last Name in the form to begin with Upper Case followed bo Lower Case */
			tLastName = ((tLastName.substr(0,1)).toUpperCase() + (tLastName.substr(1,tLastName.length-1)).toLowerCase());
			ctlMe.value = tLastName;

			/* Returns valid Last Name */
			return true;
		}
	}

//	NOT VALID
	alert(tMessage);
	ctlMe.value = "";
	ctlMe.focus();
	return false;
}


// ===== VALIDATE PERSONAL NUMBER =====
function ValPNo(ctlMe, tMessage) {
//	INIT
	tPNo = ctlMe.value;
	bNaPNo = false;

//	RETURNS VALID IF EMPTY
	if (tPNo.length == 0) { return true }

//	IF FULL TEXTBOX, CHECK FOR '-' 
	i = 0;
	if (tPNo.length == 11) {
		while ((i < 11) && (!(bNaPNo))) {
			if (tPNo.substr(i,1) == "-") {
				if (i == 6){
					tPNo = tPNo.substr(0,6) + tPNo.substr(7,4);  // Removes the '-' from tPNo
				} else {
					bNaPNo = true;  // Not a PNo
				}
			}
			i++;
		}

		/* Check if tPNo is not a number or PNo */
		if (!(bNaPNo)) {
			if ((isNaN(tPNo)) || (tPNo.length == 11)) {
				bNaPNo = true;  // Not a number or PNo
			}
		}
	}

//	IF PNO FORMAT, VALIDATE IT
	if (!(bNaPNo)) {
		if ((tPNo.length == 10) && (!(isNaN(tPNo)))) {

			/* Check if valid month and day in tPNo */
			imonth = tPNo.substr(2,2);
			iday = tPNo.substr(4,2);
			switch (imonth) {
				case "01":
				case "03":
				case "05":
				case "07":
				case "08":
				case "10":
				case "12":
					if ((iday < 1) || (iday > 31)) {
						bNaPNo = true;  // Not a PNo
					}
					break;
					
				case "04":
				case "06":
				case "09":
				case "11":
					if ((iday < 1) || (iday > 30)) {
						bNaPNo = true;  // Not a PNo
					}
					break;

				case "02":
					if ((iday < 1) || (iday > 29)) {
						bNaPNo = true;  // Not a PNo
					}
					break;
					
				default:
					bNaPNo = true;  // Not a PNo
			}

			if (!(bNaPNo)) {
				/* Calculate checksum number in tPNo */
				i = 0;
				iValue = 0;
				iSum = 0;
				while ((i < (tPNo.length-1)) && (!(bNaPNo))) {
					tChar = tPNo.substr(i,1);
					if (!(isNaN(tChar))) {
						iValue = tChar * (((i+1)%2)+1);
						if (iValue > 9) {
							iValue -= 9;
						}
						iSum += iValue;
					}else{
						bNaPNo = true;  // Not a number or PNo
					}
					i++;
				}
	
				if (!(bNaPNo)) {
					/* Check if calculated checksum was the same as entered by user */
					if ((i == (tPNo.length-1)) && (((10-(iSum%10))%10) == (tPNo.substr((tPNo.length-1),1)))) {
		
						/* Adds the '-' to PNo in the form */
						if (ctlMe.value.length == 10) {
							ctlMe.value = tPNo.substr(0,6) + "-" + tPNo.substr(6,4);
						}
		
						/* Returns valid PNo */
						return true;
					}
				}
			}
		}
	}

//	NOT VALID
	if (ctlMe.value != "" && tMessage != "") {
		alert(tMessage);
	}
	ctlMe.value = "";
	ctlMe.focus();
	return false;
}

// ===== VALIDATE PERSONAL NUMBER =====
function ValPNoCo(ctlMe, ctlMe2, tMessage) {
//	NOT VALID
	if (ctlMe2 != "" && ctlMe.value == ctlMe2.value) {
		alert(tMessage);
		ctlMe2.value = "";
		ctlMe2.focus();
		return false;
	}
	return true;
}

//===== VALIDATE CO =====
function ValNameCo(ctlMe, ctlMe2, ctlMe3, ctlMe4, tMessage) {
//	NOT VALID
	if (ctlMe.value == ctlMe3.value && ctlMe2.value == ctlMe4.value) {
		alert(tMessage);
		ctlMe3.value = "";
		ctlMe4.value = "";
		ctlMe3.focus();
		return false;
	}
	return true;
}

// ===== VALIDATE PHONE =====
function ValPhone(ctlMe, tMessage) {
//	INIT
	tPhone = ctlMe.value;

	if (tPhone.length > 7) {
		return true;
	}

//	NOT VALID
	alert(tMessage);
	ctlMe.value = "";
	ctlMe.focus();
	return false;
}


// ===== VALIDATE E-MAIL =====
function ValEmail(ctlMe, tMessage) {
//	INIT
	tEmail = ctlMe.value;

//	IF FULL TEXTBOX, CHECK FOR '-' 
	i = 1;
	bNaEmail = true;  // Not an E-mail
	while (i < tEmail.length) {
		if (tEmail.substr(i,1) == "@") {
			j = i + 2;
			i = tEmail.length;
			while (j < tEmail.length) {
				if (tEmail.substr(j,1) == ".") {
					if(tEmail.search(",") == -1) {
						bNaEmail = false;  // This is a E-mail OR?
						switch ((tEmail.substr((j+1),3)).toLowerCase()) {
							case "com":
							case "net":
							case "org":
							case "se ":
							case "se":
							case "nu ":
							case "nu":
							case "it ":
							case "it":
							case "us ":
							case "us":
							case "ac ":
							case "ac":
							case "be ":
							case "be":
							case "fr ":
							case "fr":
								/* Change E-mail in the form to Lower Case only */
								tEmail = tEmail.toLowerCase();
								ctlMe.value = tEmail;
	
								/* Returns valid E-mail adress */
								return true;
								break;
							default:	
								bNaEmail = false;  // This is not an Email
						}
						j = tEmail.length;
					}
				}
				j++;
			}
		}
		i++;
	}

//	NOT VALID
	alert(tMessage);
//	ctlMe.value = "";
	ctlMe.focus();
	return false;
}

//===== VALIDATE MONTHLY INCOME =====
function ValMonthlyIncome(ctlMe, tMessage) {
//	INIT
	tMonthlyIncome = ctlMe.value;
	tMonthlyIncome = tMonthlyIncome.replace(/ /g, '');
	tMonthlyIncome = tMonthlyIncome.replace(/\./g, '');
	tMonthlyIncome = tMonthlyIncome.replace(/\,/g, '');
	ctlMe.value = tMonthlyIncome;
	
//	CHECK IF MONTHLY INCOME IS VALID
	if (!(isNaN(tMonthlyIncome))) {
		if (Number(tMonthlyIncome) == 0) {
			return true;
		} else if (Number(tMonthlyIncome) > 100) {
			return true;
		}
	}

//	NOT VALID
	alert(tMessage);
	ctlMe.value = "";
	ctlMe.focus();
	return false;
}

// ===== VALIDATE MONTHLY INCOME =====
//function ValMonthlyIncome(ctlMe, tMessage) {
////	INIT
//	tMonthlyIncome = ctlMe.value;
//	tMonthlyIncome = tMonthlyIncome.replace(/ /g, '');
//	tMonthlyIncome = tMonthlyIncome.replace(/\./g, '');
//	tMonthlyIncome = tMonthlyIncome.replace(/\,/g, '');
//	ctlMe.value = tMonthlyIncome;
//	
////	CHECK IF MONTHLY INCOME IS VALID
//	if (!(isNaN(tMonthlyIncome))) {
//		if (Number(tMonthlyIncome) >= 4167) {
//			return true;
//		}
//	}
//
////	NOT VALID
//	alert(tMessage);
//	ctlMe.value = "";
//	ctlMe.focus();
//	return false;
//}


// ===== VALIDATE MONTHLY INCOME LOW =====
function ValMonthlyIncomeLow(ctlMe, tMessage) {
//	INIT
	tMonthlyIncome = ctlMe.value;
	tMonthlyIncome = tMonthlyIncome.replace(/ /g, '');
	tMonthlyIncome = tMonthlyIncome.replace(/\./g, '');
	tMonthlyIncome = tMonthlyIncome.replace(/\,/g, '');
	ctlMe.value = tMonthlyIncome;

//	CHECK IF MONTHLY INCOME IS TO LOW
	if (!(isNaN(tMonthlyIncome))) {
		if (Number(tMonthlyIncome) >= 4167) {
			return true;
		}
	}

//	NOT VALID
	alert(tMessage);
	ctlMe.value = "";
	ctlMe.focus();
	return false;
}


// ===== VALIDATE MONTHLY INCOME HIGH =====
function ValMonthlyIncomeHigh(ctlMe, tMessage) {
//	INIT
	tMonthlyIncome = ctlMe.value;
	tMonthlyIncome = tMonthlyIncome.replace(/ /g, '');
	tMonthlyIncome = tMonthlyIncome.replace(/\./g, '');
	tMonthlyIncome = tMonthlyIncome.replace(/\,/g, '');
	ctlMe.value = tMonthlyIncome;

//	CHECK IF MONTHLY INCOME IS TO HIGH
	if (!(isNaN(tMonthlyIncome))) {
		if (Number(tMonthlyIncome) <= 80000) {
			return true;
		}
	}

//	NOT VALID
	alert(tMessage);
	ctlMe.value = "";
	ctlMe.focus();
	return false;
}

// ===== VALIDATE EMPLOYMENT =====
function ValEmploymentType() {							
	if(document.getElementById('cboEmploymentTypeId').value=='') { 
		alert('Var vänlig välj anställningsform.');
		document.getElementById('cboEmploymentTypeId').focus();
		return false;
	}
	return true;
}

function ValEmployment() {
	if(document.getElementById('cboEmploymentTypeId').value!=8 && document.getElementById('cboEmploymentTypeId').value!=9) {
		if(document.getElementById('txtEmployedSince').value=='') {
			if(document.getElementById('cboEmploymentTypeId').value==7) {
				alert('Var vänlig fyll i när du blev pensionär.');
			} else {
				alert('Var vänlig fyll i när du blev anställd.');
			}
			document.getElementById('txtEmployedSince').focus();
			return false;
		}
	}
	if(document.getElementById('cboEmploymentTypeId').value!=7 && document.getElementById('cboEmploymentTypeId').value!=8 && document.getElementById('cboEmploymentTypeId').value!=9) {
		if(document.getElementById('txtEmployer').value=='') { 
			alert('Var vänlig fyll i din arbetsgivare.');
			document.getElementById('txtEmployer').focus();
			return false;
		}
		if(document.getElementById('txtProfession').value=='') { 
			alert('Var vänlig fyll i ditt yrke.');
			document.getElementById('txtProfession').focus();
			return false;
		}
	}
	return true;
}

// ===== VALIDATE CHECKBOX =====
function ValChk(ctlMe, tMessage) {
	if (!(ctlMe.checked)) {
		alert(tMessage);
		ctlMe.focus();
		return false;
	} else {
		return true;
	}
}

function ValAmountRedeem() {
	if(document.getElementById('txtAmount').value!="") {
		document.getElementById('spanAmountRedeem').innerHTML='&nbsp;&nbsp;(0-'+document.getElementById('txtAmount').value+' kr)';
		document.getElementById('spanAmountRedeem').style.display='block';
	} else {
		document.getElementById('spanAmountRedeem').style.display='none';
	}
	if(parseInt(document.getElementById('txtAmount').value)<parseInt(document.getElementById('txtAmountRedeem').value)) {
		alert('Belopp att lösa kan inte vara större än önskat låneblopp.');
		document.getElementById('txtAmountRedeem').value="";
		document.getElementById('txtAmountRedeem').focus();
	}
}

function ValEmployedSince(obj) {
	str = obj.value;
	var iEmploymentTypeId = document.getElementById('cboEmploymentTypeId').value;
	if(iEmploymentTypeId != 9 && iEmploymentTypeId != 8) {
		document.getElementById('txtEmployedSince').value = str.replace( /([a-ö])/g , "");
		document.getElementById('txtEmployedSince').value = document.getElementById('txtEmployedSince').value.replace( /([A-Ö])/g , "");
		document.getElementById('txtEmployedSince').value = document.getElementById('txtEmployedSince').value.replace( / /g , "");
		if (iEmploymentTypeId == 3 || iEmploymentTypeId == 5) {
			if(str.search(/-/) == -1 || str.length != 9) {
				alert('Var vänlig fyll i år och månad på din anställningsperiod enligt ååmm-ååmm (fr.o.m. år och månad – t.o.m. år och månad).');
				document.getElementById('txtEmployedSince').focus();
				return false;
			}
		} else {
			if(str.search(/-/) != -1 && str.length != 4) {
				if(iEmploymentTypeId == 7) {
					alert('Var vänlig fyll år och månad som du blev pensionär enligt formatet ååmm.');
				} else if(iEmploymentTypeId == 2) { 
					alert('Var vänlig fyll år och månad som du blev företagare enligt formatet ååmm.');
				} else {
					alert('Var vänlig fyll år och månad som du blev anställd enligt formatet ååmm.');
				}
				document.getElementById('txtEmployedSince').focus();
				return false;
			}
		}
	}
	return true;
}

//===== VALIDATE AMOUNT =====
function ValPrice(Price, AmountInput, tMessage) {
//	INIT
	PriceVal = Price.value;
	AmountInputVal = AmountInput.value;
	
//	CHECK IF LOAN AMOUNT IS VALID
	if (!(isNaN(PriceVal))){
		if (parseInt(PriceVal*0.2) <= AmountInputVal){
			return true;
		}
	}

//	NOT VALID
	alert(tMessage);
	AmountInput.focus();
	return false;
}

function ValRegNo(RegNo, Model, tMessage) {
//	INIT
	RegNoVal = RegNo.value;
	if(RegNoVal == "") {
//		NOT VALID
		alert(tMessage);
		RegNo.focus();
		return false;
	} else {
		return true;
	}
}
//-->