//----------------------------------------------------------
//	Project Validation Routines
//----------------------------------------------------------


function LTrim(str)
{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
      var j=0, i = s.length;
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;
      s = s.substring(j, i);
   }
   return s;
}

function RTrim(str)
{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {

      var i = s.length - 1;       // Get length of string
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;
      s = s.substring(0, i+1);
   }

   return s;
}

function Trim(str)
{
   return RTrim(LTrim(str));
}

function ValidateTextField(varData, msg) {
	if (Trim(varData.value) == "") {
		if (msg == "") {
			msg += "A value is required for this field.\n";
		}
		alert(msg);
		varData.focus();
		varData.select();
		return false;
	}
	return true;
}

function ValidateMinMaxNumber(item, message, minvalue, maxvalue) {
	if (item.value != "") {
		if (isNaN(parseInt(item.value))) {
			alert(message);
			item.focus();
			item.select();
			return false;
		}
		if(parseInt(item.value) < minvalue || parseInt(item.value) > maxvalue) {
			alert("Please enter a value between " + minvalue + " - " + maxvalue);
			item.focus();
			item.select();
			return false;
		}
		item.value = parseInt(item.value);
	}
	return true;
}


function ValidateDate (inp, msg) {
	
	var val = inp.value
	
	if (val == "") {
		if (msg == "") {
			msg = "A value is required for this field.\n";
		}
		else {
			msg = "A value is Required for " + msg + " field.\n";
		}
		alert(msg);
		inp.focus();
		inp.select();
		return false;
	}
	
	//Date format HAVE TO BE validation "dd/mm/yyyy"
	dtString = new String(val);
	
	splitString = dtString.split("/");
	if (splitString.length != 3) {
		splitString = dtString.split(".");
		if (splitString.length != 3) {
			alert("The date format you have entered is invalid. \n Please enter the date in the format: DD/MM/YYYY");
			inp.focus();
			inp.select();
			return false;
		}
	}  
	
	var intDay, intMonth, intYear
		
	//Year validation
	intYear = parseInt(splitString[2], 10);
		if (intYear != splitString[2]) {
			alert("The year you have entered is invalid\n");
			inp.focus();
			inp.select();
			return false;
		}
		if ((intYear < 1900) || (intYear > 2099)) {
			alert("The year you have entered is invalid.\n Please enter a year between 1900-2099");
			inp.focus();
			inp.select();
			return false;
		} 
			
		
	//Month validation
	intMonth = splitString[1];
	intMonth = parseInt(splitString[1], 10);
		if (intMonth != splitString[1]) {
			alert("The month you have entered is invalid\n");
			inp.focus();
			inp.select();
			return false;
		}
		if ((intMonth < 1) || (intMonth > 12)) {
			alert("The month you have entered is invalid.\n Please enter a month between 1-12");
			inp.focus();
			inp.select();
			return false;
		} 
			
		
	//Day validation
	intDay = splitString[0];
	intDay = parseInt(splitString[0], 10);
		if (intDay != splitString[0]) {
			alert("The day you have entered is invalid\n");
			inp.focus();
			inp.select();
			return false;
		}
		
	//Leap Year determination
	var blnLeapYear
	if (intYear % 4 != 0) {
		blnLeapYear = false;
	}
	else {
		if (intYear % 400 == 0) {
			blnLeapYear = true;
		}
		else {
		//This comment is because of the lack of year, 
		//which can be devided by 100 and not by 400 in the our current range of years
		//	if (intYear % 100 == 0) {
		//		blnLeapYear = false;
		//	} 	
		//	else {
				blnLeapYear = true;
			}
		//} 	
	} 
		
	//Last Day of the Month determination
	var intLastDay
	switch (intMonth) {
		case 1:	
			intLastDay = 31;
			break;
	    case 2:	
			if (blnLeapYear) {
				intLastDay = 29;
			}
			else {
				intLastDay = 28;
			}
			break;
	    case 3:
			intLastDay = 31;
			break;
	    case 4:
			intLastDay = 30;
			break;
	    case 5:
			intLastDay = 31;
			break;
		case 6:
			intLastDay = 30;
		break;
	    case 7:
			intLastDay = 31;
			break;
	    case 8:
			intLastDay = 31;
			break;
	    case 9:
			intLastDay = 30;
			break;
	    case 10:
			intLastDay = 31;
			break;
	    case 11:
			intLastDay = 30;
			break;
	    case 12:
			intLastDay = 31;
			break;
	}
		
	if ((intDay < 1) || (intDay > intLastDay)) {
		alert("The day you have entered is invalid.\n Please enter a day between 1-" + intLastDay);
		inp.focus();
		inp.select();
		return false;
	} 
		
	return true;
}


function ValidateDateFromToInterval (inpFrom, inpTo, strMessage) {

	var valFrom = inpFrom.value;
	dtString = new String(valFrom);
	splitString = dtString.split("/");
	valFrom = splitString[1] + '/' + splitString[0] + '/' + splitString[2]
	
	var valTo = inpTo.value;
	dtString = new String(valTo);
	splitString = dtString.split("/");
	valTo = splitString[1] + '/' + splitString[0] + '/' + splitString[2]
	
	if (Date.parse(valFrom) > Date.parse(valTo)) {
		alert(strMessage);
		inpTo.focus();
		inpTo.select();
		return false;
	}
	
	return true;
}


function ValidateFileType(fileName, fileTypes) {
	//fileTypes - example: ".jpg, .gif"
		
	var intStringCount, fileType
	intStringCount = fileName.length
			
	fileType = fileName.substr(intStringCount - 4).toLowerCase();
	fileTypes = fileTypes.toLowerCase();
					
	if (fileTypes.indexOf(fileType) == -1) {
		alert("Please only upload files that end in type(s): \n\n" + (fileTypes) + "\n\nPlease select a new file and try again.");
		return false;
	}
	return true
			
}


function ValidateNumber(item, message) {
	if (isNaN(parseInt(item.value))) {
		alert(message);
		item.focus();
		item.select();
		return false;
	}
	if (parseInt(item.value) < 0) {
		alert(message);
		item.focus();
		item.select();
		return false;
	}
	item.value = parseInt(item.value)
	return true;
}


function ValidateFloat(item, message) {
	if (isNaN(parseFloat(item.value))) {
		alert(message);
		item.focus();
		item.select();
		return false;
	}
	if (parseFloat(item.value) < 0) {
		alert(message);
		item.focus();
		item.select();
		return false;
	}
	item.value = parseFloat(item.value)
	return true;
}


function ValidateEmail(item, msg) {

	str = item.value
    var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
    r = str.match(emailReg);
    if (r == null){
		alert(msg)
		item.focus();
		item.select();
		return false;
	}		
	return true;
}



function ValidateMultipleSelect(varData, msg) {
	if (varData.value == "") {
		if (msg == "") {
			msg += "A selection is required from this dropdown\n";
		}
		alert(msg);
		return false;
	}
	return true;
}





