// $Header:   \\rlrgnttecf2\ecm\natwest.com\archives\web\secure\applicationforms_dda\javascript\valCore.js-arc   1.4   15 Oct 2004 10:31:14   Franrca  $ 
//used by all App Form pages
// ##### CHECK FIELD #####
function valField(inData, inValidationType){
	var decPointCount = 0;
	var haveDecPoint = "false";
	var letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var textpunc = "-,'&?£";
	var numpunc = ".,";
	var numpuncDec = ".";
	var strErrFlag = "z";
	var spaceflag = 0;
	var strErrMsg = "";
	
	inData += "";
	inValidationType += "";

	// If empty string passed in then simply exit
	if (inData == ""){return strErrMsg;}
	// Loop over all the characters in the field
	for(var i = 0 ; i < inData.length ; i++) {
		var chr = inData.charAt(i);
		
		//replace and backslashes as they cause problems
		if (chr == "\\"){
			chr = chr.replace("\\", "/");
		}
		//a = numbers only (no spaces)
		if(inValidationType=="a"){
			if ((chr > "9") || (chr < "0")) {
				strErrFlag = "a";
			}
		}

		//b = numbers and + and - and ( and )  (spaces also allowed, but not all spaces)
		if(inValidationType=="b"){
			if (((chr > "9") || (chr < "0")) && (chr != " ") && (chr != "+") && (chr != "-") && (chr != "(") && (chr != ")")) {
				strErrFlag = "b";
			}
		}

		//c = letters only (no spaces)
		if(inValidationType=="c"){
			if (letters.indexOf(chr)<0){
				strErrFlag = "c";
			}
		}

		//d = letters only (spaces also allowed, but not all spaces)
		if(inValidationType=="d"){
			if ((letters.indexOf(chr)<0) && (chr != " ")){
				strErrFlag = "d";
			}
		}

		//e = numbers and letters only (spaces also allowed, but not all spaces
		if(inValidationType=="e"){
			if (((chr > "9") || (chr < "0")) && (letters.indexOf(chr)<0) && (chr != " ")){
				strErrFlag = "e";
			}
		}

		//f = numbers and . and , only (spaces also allowed, but not all spaces) - decimal place allowed
		if(inValidationType=="f"){
			if(inData.charAt(0)=="0"){
				strErrFlag = "fd";
			}else if ((((chr > "9") || (chr < "0")) && (numpunc.indexOf(chr)<0)) || (chr == " ")){
				strErrFlag = "fa";
			}else{
				if(haveDecPoint=="true"){
					if (chr=="."){
						strErrFlag = "fb";
					}
					decPointCount = decPointCount + 1;
					if (decPointCount==3){
						strErrFlag = "fc";
					}
				}
				if (chr == "."){
					haveDecPoint= "true";
				}
			}
		}

		//g = letters and - and , and ' and & only (spaces also allowed, but not all spaces)
		if(inValidationType=="g"){
			if ((letters.indexOf(chr)<0) && (textpunc.indexOf(chr)<0) && (chr != " ")){
				strErrFlag = "g";
			}
		}

		//h = numbers and letters and . and , and - and ' and & and ( and ) only (spaces also allowed, but not all spaces)
		if(inValidationType=="h"){
			if (((chr > "9") || (chr < "0")) && (letters.indexOf(chr)<0) && (textpunc.indexOf(chr)<0) && (numpunc.indexOf(chr)<0) && (chr != "(") && (chr != ")") && (chr != " ")){
				strErrFlag = "h";
			}
		}

		//i = numbers and , only (spaces also allowed, but not all spaces) - no decimal place allowed
		if(inValidationType=="i"){
			if ((((chr > "9") || (chr < "0")) && (numpunc.indexOf(chr)<0)) || (chr == " ")){
				strErrFlag = "i";
			}
			if (chr=="."){
				strErrFlag = "i";
			}
		}

		//j = letters and - and . and , and ' and & only (spaces also allowed, but not all spaces)
		if(inValidationType=="j"){
			if ((letters.indexOf(chr)<0) && (textpunc.indexOf(chr)<0) && (chr != " ") && (chr != ".")){
				strErrFlag = "j";
			}
		}

		//k = numbers and letters and . and , and - and ' and & and ( and ) and / only (spaces also allowed, but not all spaces)
		if(inValidationType=="k"){
			if (((chr > "9") || (chr < "0")) && (letters.indexOf(chr)<0) && (textpunc.indexOf(chr)<0) && (numpunc.indexOf(chr)<0) && (chr != "(") && (chr != ")") && (chr != " ") && (chr != "/")){
				strErrFlag = "k";
			}
		}
		
		if(inValidationType=="l"){
			if ((chr > "9") || (chr < "0"))  {
				if (letters.indexOf(chr)<0){
					strErrFlag = "l";
				}
			}
		}

		// m = numbers and . only (spaces also allowed, but not all spaces) - decimal place allowed,
		// but must be two digits after decimal place.
		if (inValidationType=="m"){
			if ( ((chr > "9") || (chr < "0")) && (numpuncDec.indexOf(chr)<0) ){
				strErrFlag = "ma";
			}else{
				if(haveDecPoint=="true"){
					if (chr=="."){
						strErrFlag = "mb";
					}
					// This verifies that there are not more then 2 digits after decimal place.					
					decPointCount = decPointCount + 1;
					if (decPointCount==3){
						strErrFlag = "mc";
					}
				}
				if (chr == "."){
					haveDecPoint= "true";
				}
			}
		}

		// n = numbers and . only, must be no more than three digits after decimal place.
		if (inValidationType=="n"){
			if (((chr > "9") || (chr < "0")) && (chr != ".")){
				strErrFlag = "n";
			}else{
				if(haveDecPoint=="true"){
					if (chr=="."){
						strErrFlag = "n";
					}
					// This verifies that there are not more then 3 digits after decimal place.					
					decPointCount = decPointCount + 1;
					if (decPointCount>3){
						strErrFlag = "n";
					}
				}
				if (chr == "."){
					haveDecPoint= "true";
				}
			}
		}

		if (strErrFlag != "z"){
			if (strErrFlag=="k"){
				strErrMsg = "Please enter details with letters, numbers and common punctuation only.";
			}
			if (strErrFlag=="j"){
				strErrMsg = "Please enter details with letters and punctuation only.";
			}
			if (strErrFlag=="i"){
				strErrMsg = "Please enter details with numbers only and no decimal places.";
			}
			if ((strErrFlag=="a") || (strErrFlag=="b")){
				strErrMsg = "Please enter details with numbers only.";
			}
			if ((strErrFlag=="fa") || (strErrFlag=="fb") || (strErrFlag=="fc")|| 
				(strErrFlag=="ma") || (strErrFlag=="mb") || (strErrFlag=="mc")){
				strErrMsg = "Please enter details with numbers only, maximum 2 decimal places.";
			}
			if (strErrFlag=="fd"){
				strErrMsg = "Please enter details without a leading zero.";
			}
			if ((strErrFlag=="c") || (strErrFlag=="d")){
				strErrMsg = "Please enter details with letters only.";
			}
			if (strErrFlag=="g"){
				strErrMsg = "Please enter details with letters and common punctuation only.";
			}
			if ((strErrFlag=="e") || (strErrFlag=="h") || (strErrFlag=="l")) {
				strErrMsg += "Please enter details with letters and numbers only.";
			}
			if (strErrFlag=="n"){
				strErrMsg = "Please enter details with numbers only, maximum 3 decimal places.";
			}
			return strErrMsg;
		}
		if (chr == " ") {
			spaceflag = spaceflag + 1;
		} 
	}
	if (spaceflag == inData.length){
		strErrMsg = "Your details cannot be just spaces, please try again.";
		spaceflag = 0;
		return strErrMsg;
	}

	if (inValidationType=="m"){
		// Then if decimal place present there should be two digits after the decimal place:
		if ( 0 != decPointCount ){
			// ie there are decimal places present, then should be 2 digits after decimal place.
			if ( 2 != decPointCount){
				strErrMsg = "There should be 2 digits after a decimal place.";
				return strErrMsg;
			}
		}
	}

	return strErrMsg;
}

//used by all App Forms pages which include a date field
//##### VALIDATE DATE ####

//datetype can be:
//a - for any dd/mm/yyyy date
//aa - for any mm/yyyy date over 1900 (used for student course date)
//b - for any mm/yyyy date
//ba - for any mm/yyyy date over 1900 (used for student course date)
//c - for a dd/mm/yyyy date that is NOT a future date
//d - for a dd/mm/yyyy date that MUST BE a future date
//e - for a mm/yyyy date that is NOT a future date
//f - for a mm/yyyy date that MUST BE a future date
//g - for a mm/yy date that MUST BE a future date
//h - for any yyyy date
//i - for a yyyy date that is NOT a future date
//j - for a yyyy date that MUST BE a future date 
function valDate(inData, inDateType){
	var strErrMsg = "";
	inData += "";
	var validDateFormat = inData.split("/");
	inDateType += "";	
	var year="";
	var day="";
	var month="";
	var testDate="";

	// If empty string passed in then simply exit
	if (inData == ""){return strErrMsg;}

	if (((inDateType=="a") ||(inDateType=="aa") ||(inDateType=="c") || (inDateType=="d")) && (validDateFormat.length <3)){
		strErrMsg = "Please enter a valid date in the format dd/mm/yyyy.";
	}else if (((inDateType=="b") ||(inDateType=="ba") ||(inDateType=="e") || (inDateType=="f")) && (validDateFormat.length <2)){
		strErrMsg = "Please enter the date in the format mm/yyyy.";
	}else if ((inDateType=="g") & (validDateFormat.length <2)){
		strErrMsg = "Please enter the date in the format mm/yy.";
	}else if (((inDateType=="h") ||(inDateType=="i") || (inDateType=="j")) && (validDateFormat.length >1)){
		strErrMsg = "Please enter the date in the format yyyy.";
	}
	
	if(strErrMsg == ""){
		if((inDateType=="a") || (inDateType=="aa") || (inDateType=="c") || (inDateType=="d")){
			var day = validDateFormat[0];
			var month =validDateFormat[1];
			var year = validDateFormat[2];
	
			if((day.length != 2) || (month.length != 2) || (year.length != 4)){
			strErrMsg = "Please enter a valid date in the format dd/mm/yyyy.";
			}
			if(inDateType=="aa"){
				var century;
				century = year.substring(0,2);
				if (century<19){
				//year must be 1900 plus
					strErrMsg = "Please enter a valid year.";
				}
			}	
		}else if((inDateType=="b") || (inDateType=="ba") ||(inDateType=="e") || (inDateType=="f")){
			var month =validDateFormat[0];
			var year = validDateFormat[1];
			var day=1;
			
			if ((month.length != 2) || (year.length != 4)){
				strErrMsg = "Please enter a valid date in the format mm/yyyy.";
			}
			if(inDateType=="ba"){
				var century;
				century = year.substring(0,2);
				if (century<19){
				//year must be 1900 plus
					strErrMsg = "Please enter a valid year.";
				}
			}	
			
	
		}else if(inDateType=="g"){
			var month =validDateFormat[0];
			var year = validDateFormat[1];
			var day=1;
	
			if ((month.length != 2) || (year.length != 2)){
				strErrMsg = "Please enter a valid date in the format mm/yy.";
			}
			if (year > 50) {
				year = "19" + year
			}else{
				year = "20" + year
			}
		}else if((inDateType=="h") || (inDateType=="i") || (inDateType=="j")){
			var year = validDateFormat[0];
			var month = 1;
			var day=1;

			if (inData.substr(0,1) == " "){
				strErrMsg = "Please enter a valid date in the format yyyy.";
			}				
			if (year.length != 4){
				strErrMsg = "Please enter a valid date in the format yyyy.";
			}
		}
		
		if(strErrMsg == ""){
			var test=new Date(year,(month-1),day);
			if ((test.getFullYear()==year) && ((month-1)==test.getMonth()) && (day==test.getDate())){
				if((inDateType=="e") || (inDateType=="f")){
					testDate = "01/" + inData;
				}else if(inDateType=="g"){
					testDate = "01/" + validDateFormat[0] + "/" + year;
				}else if((inDateType=="h") || (inDateType=="i") || (inDateType=="j")){
					testDate = "01/01/" + year;	
				}else{
					testDate = inData;
				}
	
				if((inDateType=="c") || (inDateType=="e") || (inDateType=="i")){
					if(isgreaterThanToday(testDate)){
						strErrMsg = "The date cannot be a future date, please re-enter.";
					}
				}else if((inDateType=="d") || (inDateType=="f") || (inDateType=="g") || (inDateType=="j")){
					if(!isgreaterThanToday(testDate)){
						strErrMsg = "The date must be a future date, please re-enter.";
					}
				}
			}else{
				strErrMsg = "Please enter a correct date.";
			}	
		}
	}		
	return strErrMsg;
}

//####USED BY CHECK DATE WHEN THE VALIDATOR IS 'c' and 'e'
//####REQUIRED BY DATE OF BIRTH FIELDS ETC>>> WHEN DATE MUST BE CHECKED AGAINST FUTURE DATE
function getFullYear(inDateObj) {
	var y = inDateObj.getYear();
	if (y < 1000) {y+=1900;}
	return y;
}

function isgreaterThanToday(inDateString) {
	var today = new Date();
	inDateString += "";

	//split up TODAYS DATE
	var yearNow = getFullYear(today);
	var monthNow = today.getMonth() + 1;
	var dayNow = today.getDate();

	//split up TEST DATE
	var testYear = inDateString.substring(6,10);
	var testMonth = inDateString.substring(3,5);
	var testDay = inDateString.substring(0,2);

	if(testYear > yearNow){
		return true;
	}else if(testYear < yearNow){
		return false;
	}else{
		//it's the same year so check months:
		if(testMonth > monthNow){
			return true;
		}else if(testMonth < monthNow){
			return false;
		}else{
			//its the same year and month, so check days
			if(testDay > dayNow){
				return true;
			} else {
				return false;
			}
		}
	}
}

function isGreaterThanOrEqualToToday(inDateString) {
	var today = new Date();
	inDateString += "";

	//split up TODAYS DATE
	var yearNow = getFullYear(today);
	var monthNow = today.getMonth() + 1;
	var dayNow = today.getDate();

	//split up TEST DATE
	var testYear = inDateString.substring(6,10);
	var testMonth = inDateString.substring(3,5);
	var testDay = inDateString.substring(0,2);

	if(testYear > yearNow){
		return true;
	}else if(testYear < yearNow){
		return false;
	}else{
		//it's the same year so check months:
		if(testMonth > monthNow){
			return true;
		}else if(testMonth < monthNow){
			return false;
		}else{
			//its the same year and month, so check days
			if(testDay >= dayNow){
				return true;
			} else {
				return false;
			}
		}
	}
}