/********************************************************************************************************
 	This script will validate data entered by Users with edit rights.  The functions check for the following:
		- values from input Textbox, Select Option, and TextArea Controls
		- validate values according to its type which may either 
			= be a required string values isEmpty()
			= be a required numeric values isNumeric()
			= be a numeric value when enter isOnlyNumeric()
********************************************************************************************************/

  var entryInfo = "";  	// Keeping track of information type being edit

  function isSelected(inputField, fieldLabel, theSize, zeroDeci) {
        if (inputField.value == "") {	// check if value from control is empty or not in Select Option list
        	alert("The required " + fieldLabel + " field is not selected.")
            inputField.focus();
            return false;
        }
        else return true;
  }
  
  function isEmpty(inputField, fieldLabel, size, zeroDeci) {
  		var startIdx;
        for (var startIdx = 0; startIdx < inputField.value.length; startIdx++) {
            if (inputField.value.charAt(startIdx) != " ")     break;	// check if value in is empty
		}
        if (startIdx == inputField.value.length) {  // if the string length is 0
            alert("The required " + fieldLabel + " field is empty.")
            inputField.focus()		//send focus to the empty control field
            return false;
        }
        else return true;
   }
   
   function isNumeric(inputField, fieldLabel, numSize, deciSize) {
			// first check if the required field is empty
  	 if (!(isEmpty(inputField, fieldLabel,numSize, deciSize)))
	 	return false;
	 else {  // validate the value, check if it's numeric
	 	if((isNumericOnly(inputField, fieldLabel, numSize, deciSize))== false)	return false;
	 }
	 return true
   }
   
   function isNumericOnly(inputField, fieldLabel, numSize, deciSize) {  // check for numeric values only not for empty values
	  	oneDecimal = false		// assigning flag checking for decimal to false
	  	var strIn = inputField.value
	  	var inputStr = strIn.toString()		// convert the input value to a string
		var actualNumSize = numSize - deciSize - 1
	  	for(var i = 0; i < inputStr.length; i++) {
			var oneChar = inputStr.charAt(i)		// run a check thru the string value
			if(oneChar == "." && !oneDecimal) {		// checking if the character is a decimal
				oneDecimal = true;	// assigning the decimal flag
				continue;
			}
			if(i > actualNumSize &&  oneDecimal==false) {  // too many integers as compare to the value from table column
				alert("The required " + fieldLabel + " must be equal or less than " + (actualNumSize+ 1 ) + " interger with "  + deciSize + " decimal place")
            	inputField.focus();		// send focus to the control
				return false
			}
			if(oneChar < "0" || oneChar > "9") {	// values is not a number 0-9
            	alert("The required " + fieldLabel + " should be a numeric value.");   // send mesg
            	inputField.focus();		// send focus to the control
				return false;
			}
	  	}
	    return true;
   }
   
   var month=0;
   var year=0;
   var day=0;
   function isTheDate(inputField, fieldLabel, size, blank) {
  	  var theDateIn = inputField.value;
	  if(theDateIn == "") {		// check if the date is not enter or empty
		alert("Please enter a date value for " + fieldLabel + " as follow: MM/DD/YYYY")	//send message to ask for user's input
		//inputField.focus()  // send focus to the control being checked
		return false;
	  } else {
	   	if(theDateIn.indexOf("/") == 1) {
		  	month = theDateIn.charAt(0);
		  	if(theDateIn.charAt(3) == "/")        	day = theDateIn.charAt(2)
		  	else if(theDateIn.charAt(4) == "/")   	day = theDateIn.slice(2, 4)
	    } else if(theDateIn.indexOf("/")==2) {
		  	month = theDateIn.slice(0,2);
		  	if(theDateIn.charAt(4) == "/") 			day = theDateIn.charAt(3)
		  	else if(theDateIn.charAt(5) == "/")  	day = theDateIn.slice(3, 5)
	 	} else {
		   alert("Please enter the date in correctly as follows: MM/DD/YYYY");
		   inputField.focus();
		   return false;
		}
		if(theDateIn.charAt(theDateIn.length -5) == "/") {
			year = theDateIn.slice(theDateIn.length-4, theDateIn.length)
			if(year < 1000) {
				alert("The year value for a date must be greater than 1000.  Please try entering a valid date.")
				inputField.focus();
				return false;
			} else if(!(isDatesLegal(month, day, year))) {
		   		inputField.focus();
				return false;
			}
		 } else {
			alert("Please enter the date in the correct format as follows: MM/DD/YYYY");
			return false;
	     }
      }
	  return true;
   }
   
   function isDatesLegal(theMonth, theDay, theYear) {
 	 if (!((theYear % 4 == 0 && theYear % 100 != 0) || (theYear% 400 == 0))) {
          if ((month == "02" || month == "2") && day > 28) {
              alert("February in a non-leap year has only 28 days.");
              return false;
          }
      }  
	  if (theMonth < 1 || theMonth > 12) {
            alert("No such month: " + theMonth);
            return false;
      } else if (theMonth == "04" || theMonth == "4" || theMonth == "06" || theMonth == "6" ||  
				theMonth == "09" || theMonth == "9" ||  theMonth == "11" ) {
            if (day > 30) {
               alert("Invalid day value! \nThe month you specified has only 30 days.");
               return false;
            }
      } else if ((theMonth == "02" || theMonth == "2") && day > 28) {
           if (!((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))) {
               alert("February in a non-leap year has only 28 days.")
               return false;
           }
           else if (day > 29) {
           alert("February in a leap year has only 29 days.")
                    return false;
		   }
      }
	  if (theDay < 1 || theDay > 31) {
         	alert("Invalid Calendar day: " + theDay);
         	return false;
      } else if (theDay > 30 && (month == "04" || month == "4"  || month == "06" || month == "6"
                          || month == "09" || month == "9" || month == "11")) {
         	alert("The month you specified has only 30 days.")
         	return false;
      } else if (theDay > 28 && (month == "02" || month == "2")) {
         	if (!((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))) {
             	alert("February in a non-leap year has only 28 days.");
             	return false;
			} else if (theDay > 29) {
          	 	alert("February in a leap year has only 29 days.");
          	 	return false;
          	}
	  }
      return true;
    }

	function isEmail(inputField, fieldLabel, size, blank) {
	  if (inputField.value == "") {
		alert ("\n The E-Mail field is blank. \n\n Please enter your E-Mail address.")
		inputField.focus();
		return false;
	  }
	  if (inputField.value.indexOf('@',0) == -1 || inputField.value.indexOf('.',0) == -1) {
		alert ("\n The E-Mail field requires a \"@\" and a \".\"be used. \n\nPlease re-enter your E-Mail address.")
		return false;
	  }
	  return true;
	}
   
   function insertValidate(valList) {  // this will validate the input values then send them to be add
		if (entryInfo == "comments")
				document.dataForm.action = "mailto:nlggallery@yahoo.ca?Subject=Original Comments!";
		else if (entryInfo == "emailFriend")
				document.dataForm.action = "mailto:nlggallery@yahoo.ca?Subject=Original NLG Artwork";
        return true;
   }
  

