//----------------------------------------------------------
//	Project Validation Routines
//----------------------------------------------------------

function AdmButton(obj, action) {
	switch(action) {
		case 'over' :
				obj.className = "button_over";
			break;
		case 'out' :
				obj.className = "button";
			break;
	}
}


function AdmTRMouseOver(tr) {
	tr.style.backgroundColor='';
}
	
function AdmTRMouseOut(tr) {
	tr.style.backgroundColor='';
}


function Validate1to255Interval(varData, msg) {
	if (varData.value == "") {
		if (msg == "") {
			msg += "A value is required for this field\n";
		}
		alert(msg);
		varData.focus();
		varData.select();
						
		return false;
	}
	else {
		//  to be changed with each digit determination, because ot the parseInt function
		// somewhere in time
		var varIntValue;
		varIntValue = parseInt(varData.value);
		if (!((varIntValue > 0) && (varIntValue < 256))) {
			msg = "Please enter a value between 1-255";
			alert(msg);
			varData.focus();
			varData.select();
			return false;
		}
	}
return true;			
}


function openWindowOriginal( image_name ) {
	window.open(image_name,'image_name','scrollbars=1,status=0,toolbar=0,resizable=1,menubar=0');
}


function ValidateMultipleSelect(varData, msg) {
	if (varData.value == "") {
		if (msg == "") {
			msg += "A selection is required from this dropdown\n";
		}
		alert(msg);
		return false;
	}
	return true;
}


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 ValidateActiveditText(varData, msg) {
	if (varData.DOM.body.innerHTML.length == 0) {
		if (msg == "") {
			msg += "A value is required for this field.\n";
		}
		alert(msg);
		varData.focus();
		varData.DOM.body.focus();
		return false;
	}
	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 ValidateEmail(item) {
	var lsAT;
	var lsDOT;

	lsAT = item.value.indexOf("@");
	lsDOT = item.value.indexOf(".");
	
	if (lsAT == -1 || lsDOT == -1 || item.value.indexOf(" ") != -1 ) {
		alert("The Email Address you have entered is invalid")
		item.focus();
		item.select();
		return false;
	}	
	return true;
}


function ValidateEmailAlert(item) {
	var lsAT;
	var lsDOT;

	lsAT = item.value.indexOf("@");
	lsDOT = item.value.indexOf(".");
	
	if (lsAT == -1 || lsDOT == -1 || item.value.indexOf(" ") != -1 ) {
		if(! confirm("The Email Address you have entered is invalid.\n Are you sure you want to proceed?")){
		//alert("Sorry, you have missed @ or . !")
		item.focus();
		item.select();
		return false;}
	}	
	return true;
}


function ValidatePrice(item) {
	
	if (item.value != "") {
		if (isNaN(parseFloat(item.value))) {
			alert("The price you have entered is invalid");
			item.focus();
			item.select();
			return false;
		}
	}
	item.value = parseFloat(item.value)
	return true;
}


function ValidateZip(item) {

	if (item.value != "") {
			
		var re, r, str
				
			str = item.value
			re = /[a-z][a-z][0-9][0-9]\s[0-9][a-z][a-z]|[a-z][a-z][0-9]\s[0-9][a-z][a-z]|[a-z][0-9][0-9]\s[0-9][a-z][a-z]|[a-z][0-9]\s[0-9][a-z][a-z]/i;
			r = str.match(re);
				
			if (r == null){
				alert("This is not a valid Zip");
				item.focus();
				item.select();
				return false;
			}
			else {
				if (r[0] != str) {
					alert("The zip you have entered is invalid");
					item.focus();
					item.select();
					return false;
				}
			}			
	}
	return true;

}


function ValidateNumber(item, message) {
	if (isNaN(parseInt(item.value))) {
		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;
	}
	item.value = parseFloat(item.value)
	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 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 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
			
}
	
