/*
----------------------------------------------------------------------------------
Main JavaScript validation 
----------------------------------------------------------------------------------
   Version        Date        	Name      	Desc
      1.1	  02/02/2009  		AEK/Woody		Validation for form fields 
      					                    		This file is the template for the 
      					                    		JavaScript validation files that are 
      					                    		written out for each form
      					                  		  in the format ValidateFormXXXXXXXXX.js
      					                  		  where XXXXXXXXX is the name of the 
      					                  		  GalleryTemplate having the associated 
      					                  		  form. The association is established 
      					                  		  in the TemplatesForSectionTypes table.
      					                    
      					                  		  WARNING!!!! DO NOT CHANGE THIS FILE!!! (please)
----------------------------------------------------------------------------------
*/

//Dynamic Code starts here >>>>>

//==================================
function validateFields(strMessage, strValidationFields) {
var el = document.getElementById("ItemTitle");
//var star = "*";
//var quote = "'";
//strValidationFields = strValidationFields.replace("*","'")
var strVF = strValidationFields.replace(/\*/g, "'");

var fieldValPairs = strVF.split("|");
var OK = false;
var fieldMsg = "";
var allMessages = "";
//alert ("strValidationFields = " + strValidationFields);
if (el.value == "") {
	allMessages = strMessage
	el.focus();
} 
//debugger;
if (strValidationFields != "") {
	for(var i = 0; i < fieldValPairs.length; i++) {
		fieldMsg = eval(fieldValPairs[i]);
		if ( fieldMsg == "") { //e.g. eval(      IsNumeric("i", "txtSize", "Size")      )
			//OK!
		} else {
			if (allMessages == "") {
				allMessages = fieldMsg;
			} else {	
				allMessages += "\n" + fieldMsg;
			}
		}
	}
}	

if (allMessages != "") {
	OK = false; 
    alert(allMessages);
} else {
		OK = true; 
}
return OK;
}   
//==========================

function IsNotEmpty(id, strLiteral) {
var el = document.getElementById(id);
var fieldMsg = "";
var pos = 0;
var cleanLiteral = strLiteral;
pos = cleanLiteral.indexOf("(");
if (pos != -1 ) {
	cleanLiteral = trim(cleanLiteral.substr(0,pos-1)); //remove anything in brackets from the literal to give the field name
}
if (el.value == "...... Please Select") {
	fieldMsg = cleanLiteral + " cannot be empty";	
}
if (el.value == "") {
	fieldMsg = cleanLiteral + " cannot be empty";	
}
return fieldMsg
}
//==========================

function IsValidDate(id, strLiteral) {
var el = document.getElementById(id);
var OK = true;
var posSlash1 = el.value.indexOf("/");
var posSlash2 = el.value.lastIndexOf("/");
var fieldMsg = "";
//debugger;
if (el.value == "") {
	OK = false;
} else if (posSlash1 != 2) {
	OK = false;
} else if (posSlash2 != 5) {
	OK = false;
} else if (el.value.length != 10) {
	OK = false;
} else if (!CheckIfNumeric(el.value.substr(0,2))) {
	OK = false;
} else if (!CheckIfNumeric(el.value.substr(3,2))) {
	OK = false;
} else if (!CheckIfNumeric(el.value.substr(6,4))) {
	OK = false;		
} else {
	var monthMinus1 = parseInt(el.value.substr(3,2) -1);
	if (!ValidDateIsOK(parseInt(el.value.substr(0,2)), monthMinus1, parseInt(el.value.substr(6,4)))) {
		OK = false;	
	}
}
var pos = 0;
var cleanLiteral = strLiteral;
pos = cleanLiteral.indexOf("(");
if (pos != -1 ) {
	cleanLiteral = trim(cleanLiteral.substr(0,pos-1)); //remove anything in brackets from the literal to give the field name
}
if (!OK) {
	fieldMsg = cleanLiteral + " must be a valid date in the format dd\/mm\/yyyy";	
}
return fieldMsg
}
//==========================
function trim(stringToTrim) { 
return stringToTrim.replace(/^\s+|\s+$/g,""); 
}
//==========================
function ValidDateIsOK(day,month,year){
/*
NOTE!! The month needs to be a 0 (zero) for Jan, upto 11 to Dec. (hence the wrapper function IsValidDate above)
Purpose: return true if the date is valid, false otherwise

Arguments: day integer representing day of month
month integer representing month of year
year integer representing year

Variables: dteDate - date object

*/
var dteDate;

//set up a Date object based on the day, month and year arguments
//javascript months start at 0 (0-11 instead of 1-12)
dteDate=new Date(year,month,day);

/*
Javascript Dates are a little too forgiving and will change the date to a reasonable guess if it's invalid. 
We'll use this to our advantage by creating the date object and then comparing it to the details we put it. 
If the Date object is different, then it must have been an invalid date to start with...
*/

return ((day==dteDate.getDate()) && (month==dteDate.getMonth()) && (year==dteDate.getFullYear()));
}
//==========================
function IsNumeric(strMode, id, strLiteral) {
var el = document.getElementById(id);
var strText = el.value;
var ValidChars = "0123456789";
var IsNum=true;
var validationMsg = "";
var char = "";
var msg = "";
var pos = 0;
var cleanLiteral = strLiteral;
pos = cleanLiteral.indexOf("(");
if (pos != -1 ) {
	cleanLiteral = trim(cleanLiteral.substr(0,pos-1)); //remove anything in brackets from the literal to give the field name
}

//alert("here we go strMode = " + strMode + "  id = " +id);
if (strMode == "i") { //any +ve integer
   ValidChars = "0123456789";
   msg = cleanLiteral + " must be an postive whole number ";
} else if (strMode == "d") {  //any +ve decimal
	ValidChars = "0123456789.";
	msg = cleanLiteral + " must be a positive number ";
} else if (strMode == "-") {  //any number
	ValidChars = "0123456789.-";	
	msg = cleanLiteral + " must be a number ";
} else if (strMode == "z") {  //cannot be 0
	ValidChars = "0123456789.";	//must be > 0	
	msg = cleanLiteral + " must be greater than 0 ";
} else if (strMode == "-i") {  //any  integer
	ValidChars = "0123456789-";	
	msg = cleanLiteral + " must be an whole number ";
}

if (el.value == ""){
	fieldMsg = cleanLiteral + " cannot be empty";	
	IsNum = false;
} else {
		for (i = 0; i < strText.length && IsNum == true; i++) { 
		   Char = strText.charAt(i); 
		   if (ValidChars.indexOf(Char) == -1) {
		      IsNum = false;
		   }
		}
		if (IsNum) {
			if (isNaN(strText))   {
				IsNum = false;
				msg = cleanLiteral + " must be numeric";
			} 		
		}		
}

if (!IsNum) {
	validationMsg = msg;
}
   
return validationMsg;

}   
//==================================
function CheckIfNumeric(strString)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }


//==================================
function validateName(strMessage) {
var el = document.getElementById("ItemTitle");
var OK = true;
//alert ("In!");
if (el.value == "") {
	alert (strMessage);
	el.focus();
	OK = false;
}
	
return OK;
}   
//==========================
//Dynamic Code ends here <<<<<

