<!--
// PerformFieldCheck.js

   
/////////////////////////////////////////////////////
// Function to check for input entries

// Parameters:
//    The Field Name,
//    The user friendly Form Field Name as string
//    One of the options from the below list [Ignored for a multiple selection list]
//    Minimum required length [If 0 then no check is performed]
//    ...
//    ...
//    ...
// Returns true when all the entries are correct

// (1) ==> Required Numeric entry
// (2) ==> Not required but Numeric entry
// (3) ==> Required Alphabetic entry
// (4) ==> Not required but Alphabetic entry
// (5) ==> Required Date entry
// (6) ==> Not required but Date entry
// (7) ==> Required Email entry
// (8) ==> Not required but Email entry
// (9) ==> Required Alphanumeric entry
//(10) ==> Not required but Alphanumeric entry
//(11) ==> Not Required Postal Code
//(12) ==> Valid Directory Format (ex:'c:\vch')
//(13) ==> Required with Support to all characters
//(14) ==> Not Required but with Support to all characters
//(15) ==> Not Required but with Support to all characters
//(16) ==> Not Required but with Support to all characters

// Image for the erroneous field

var crlf = String.fromCharCode(13);


function PerformFieldCheck(argList) {

    var intElem                     // The element number
    var objElem                     // The Element
    var strElemName                  // The user friendly name of the element
    var intOption = 0               // The Encrypted Option selected
    var intMinLen = 0               // The Minimum amount of length needed
    var bolErr = false              // A Boolean flag
    var strContent = " "            // Contains the content of the Field
	var strReason = "";				// Contains the error message displayed to the user finally
	var today = new Date()
	var tomDate 
	var curDate
	curDate = (today.getMonth()+1)+"/"+today.getDate()+"/"+today.getFullYear()
	tomDate = (today.getMonth()+1)+"/"+(today.getDate()+1)+"/"+today.getFullYear()
	
    for(var intCount=0; intCount < PerformFieldCheck.arguments.length; intCount += 4) {
        // Pickup the Element
        objElem =  document.forms[0].elements[PerformFieldCheck.arguments[intCount]]		        
        // Pickup the Image Element
        strElemName = PerformFieldCheck.arguments[intCount+1]
        // Pickup it's contents
        if (objElem.type == "select-one")
            strContent = objElem.options[objElem.selectedIndex].value
        else
            strContent = objElem.value==null?"":objElem.value
            
		if (objElem.type == "select-multiple"){
			objElem.refresh
			strContent = objElem.options.length
         }
       
		
        // Now obtain the encrypted code
        intOption = PerformFieldCheck.arguments[intCount+2]

        // Now obtain the minimum length value
        intMinLen = PerformFieldCheck.arguments[intCount+3]

        if (intMinLen > 0)
        if (objElem.type == "select-multiple"){
			if (strContent < intMinLen) {
			        strReason = strReason + "Please select atleast " + intMinLen + " option(s) for " + strElemName + crlf + " "
			        bolErr = true
			    }
        }
        else
            if (strContent.length < intMinLen) {
                strReason = strReason + strElemName + " should be atleast " + intMinLen + " characters long" + crlf + " "
                bolErr = true
            }
        if (objElem.type != "select-multiple"){   
			//Check for blanks in required elements
			if (intOption == 1 || intOption == 3 || intOption == 5 || intOption == 7 || intOption == 9 ||intOption == 12 || intOption == 13 || intOption == 15 || intOption == 16)
			    if(strContent.length==0 || isWhiteSpaces(strContent)){
			        strReason = strReason + strElemName + " is a required entry" + crlf + " "
			        bolErr = true
			    }

			//Numeric required element
			if (((intOption == 1) || (intOption == 2)) && (strContent.length>0) && (!isNumeric(strContent))) {
				strReason = strReason + strElemName + " should be numeric" + crlf + " "
			    bolErr = true
			}

			//Alphabetic element
			if (((intOption == 3) || (intOption == 4)) && (strContent.length>0) && (!isAlphabetic(strContent))) {
			    strReason = strReason + strElemName + " should be Alphabetic" + crlf + " "
			    bolErr = true
			}

			//Date element
			if (((intOption == 5) || (intOption == 6) || (intOption == 15) || (intOption == 16)) && (strContent.length>0) && (!isDate(strContent))) {
			    strReason = strReason + strElemName + " should be a valid date" + crlf + " "
			    bolErr = true
			}

			//Date element (Effective Date i.e Less than or Equal to Current Date)
			if ((intOption == 15) && (strContent.length>0) && (!CompareDates(tomDate, strContent))) {
			    strReason = strReason + strElemName + " should be Less than or Equal to Current Date" + crlf + " "
			    bolErr = true
			}

			//Date element (Termination Date i.e Greater than Current Date)
			if ((intOption == 16) && (strContent.length>0) && (!CompareDates(strContent, curDate))) {
			    strReason = strReason + strElemName + " should be Greater than Current Date" + crlf + " "
			    bolErr = true
			}

			//Email element
			if (((intOption == 7) || (intOption == 8)) && (strContent.length>0) && (!isValidEmail(strContent))) {
			    strReason = strReason + strElemName + " should be a valid e-mail" + crlf + " "
			    bolErr = true
			}

			//Alphanumeric element
			if (((intOption == 9) || (intOption == 10)) && (strContent.length>0) && (!isAlphaNum(strContent))) {
			    strReason = strReason + strElemName + " should be Alpha-numeric" + crlf + " "
			    bolErr = true
			}

			//Postal code 
			if (((intOption == 11)) && (strContent.length>0) && (!isPostCode(strContent))) {
			    strReason = strReason + strElemName + " should be a valid Postal code" + crlf + " "
			    bolErr = true
			}    

			//Valid Directory format(c:\vch)
			if (((intOption == 12)) && (strContent.length>0) && (!isDirectory(strContent))) {
			    strReason = strReason + strElemName + " should be a valid directory path" + crlf + " "
			    bolErr = true
			}    

			// Support All characters
			// Currently this is a dummy validation.
			// This can be extended if the client is specific 
			// about "extended charcterset"
        
			if (((intOption == 13) || (intOption == 14)) && (strContent.length>0) && (false)) {
			    //strReason = strReason + strElemName + " should have visible ASCII characters" + crlf + " "
			    bolErr = true
			}
		}
    }

    if(bolErr) {
        alert('Invalid entries. Please correct:' + crlf + " " + strReason)
        return false
    }

    return true
}
//-------------------------------------------------------------------//
function isAlphaNum(strArg) {
    for (var intCount=0; intCount <strArg.length; intCount++)
        if (strArg.charAt(intCount) < ' ' || strArg.charAt(intCount) > 'z')
            return false

    return true
}
//-------------------------------------------------------------------//
function isValidEmail(strArg) {
    if ((strArg.indexOf('@') == 0) || (strArg.lastIndexOf('@') == strArg.length-1) ||
       (strArg.indexOf('.') == 0) || (strArg.lastIndexOf('.') == strArg.length-1) ||
       (strArg.indexOf('@') == -1) || (strArg.indexOf('.') == -1))
        return false
    else
        return true
}
//-------------------------------------------------------------------//
// To check for a valid number
function isNumeric(argNum) {
  for(var intCnt=0; intCnt<argNum.length; intCnt++)
    if((argNum.substring(intCnt, intCnt+1)<'0'||argNum.substring(intCnt, intCnt+1)>'9'))
      return false
  return true
}
//-------------------------------------------------------------------//
// To check for valid alphabetic string
function isAlphabetic(argAlp) {
  var bolReturn=true
  var c
  for(var intCnt=0; intCnt<argAlp.length; intCnt++) {
    c=argAlp.substring(intCnt, intCnt+1)
    bolReturn=((bolReturn)&&((c==' ')||(c>='a'&&c<='z')||(c>='A'&&c<='Z')||(c=='.')))
  }
  return bolReturn
}
//-------------------------------------------------------------------//
// To check for valid Postal Code
function isPostCode(argPCode) {
  var bolReturn=true
  var c
  for(var intCnt=0; intCnt<argPCode.length; intCnt++) {
    c=argPCode.substring(intCnt, intCnt+1)
    bolReturn=((bolReturn)&&((c==' ')||(c=='-')||(c>='a'&&c<='z')||(c>='A'&&c<='Z')||(c>='0'&&c<='9')))
  }
  return bolReturn
}
//-------------------------------------------------------------------//
// To check for valid Directory
function isDirectory(argDir) {
  var bolReturn=true
	if((argDir.length > 3)&&(((argDir.charAt(0)>='a')&&(argDir.charAt(0)<='z')) || ((argDir.charAt(0)>='A')&&(argDir.charAt(0)<='Z')))&&(argDir.charAt(1)==':')&&(argDir.charAt(2)=='\\'))
	{
		for(i=3;i<argDir.length;i++){
	      if (((argDir.charAt(i)>='a')&&(argDir.charAt(i)<='z')) || ((argDir.charAt(i)>='A')&&(argDir.charAt(i)<='Z')) || (argDir.charAt(i)=='\\'))
	       bolReturn=true
	      else
	       bolReturn=false 
	    }   
	   	for(i=2;i<argDir.length;i++){
	       if((argDir.charAt(i)=='\\')&&(argDir.charAt(i+1)=='\\'))
	        bolReturn=false
	    }	
	    if(argDir.charAt(argDir.length-1)=='\\')
	     bolReturn=false
	}       
	else
		bolReturn=false
	return bolReturn	
}
//-------------------------------------------------------------------//
// To validate a Date
function isDate(argDate) {
  var strDate
  var year=yearPart(argDate)
  var month=monthPart(argDate)
  var day=dayPart(argDate)
  if(year==0||month==0||day==0) return false

  return(isYear(year)&&isMonth(month)&&isDay(day)&&day<=daysInMonth(month,year))
}
//-------------------------------------------------------------------//
// Obtain the day part of the given string
function dayPart(dte) {
  var delimitFound=0
  var delimitOne=0
  var delimitTwo=0
  // Number of delimiters
  for(var intCnt=0; intCnt<dte.length; intCnt++)
    if(dte.substring(intCnt, intCnt+1) == "/" || dte.substring(intCnt, intCnt+1) == "-") {
      delimitFound=delimitFound+1
      if(delimitOne==0)
        delimitOne=intCnt
      else
        delimitTwo=intCnt
    }
  if(delimitFound!=2)
    return 0

  if(isNumeric(dte.substring(delimitOne+1, delimitTwo)))
    return delimitTwo-delimitOne==3?dte.substring(delimitOne+1, delimitTwo):'0'+dte.substring(delimitOne+1, delimitTwo)
  else
    return 0
}
//-------------------------------------------------------------------//
// Obtain the month part of the given string
function monthPart(dte) {
  var delimitFound=0
  var delimitOne=0
  var delimitTwo=0
  // Number of delimiters
  for(var intCnt=0; intCnt<dte.length; intCnt++)
    if(dte.substring(intCnt, intCnt+1)=="/"||dte.substring(intCnt, intCnt+1)=="-") {
      delimitFound=delimitFound+1
      if(delimitOne==0)
        delimitOne=intCnt
      else
        delimitTwo=intCnt
    }
  if(delimitFound!=2)
    return 0

  if(isNumeric(dte.substring(0, delimitOne)))
    return delimitOne==2?dte.substring(0, delimitOne):'0'+dte.substring(0, delimitOne)
  else
    return 0
}
//-------------------------------------------------------------------//
// Obtain the year part of given string
function yearPart(dte) {
  var delimitFound=0
  var delimitOne=0
  var delimitTwo=0
  // Number of delimiters
  for(var intCnt=0; intCnt<dte.length; intCnt++)
    if(dte.substring(intCnt, intCnt+1)=="/"||dte.substring(intCnt, intCnt+1)=="-") {
      delimitFound=delimitFound+1
      if(delimitOne==0)
        delimitOne=intCnt
      else
        delimitTwo=intCnt
    }
  if(delimitFound!=2)
    return 0

  if(isNumeric(dte.substring(delimitTwo+1, dte.length)))
    return dte.substring(delimitTwo+1, dte.length)
  else
    return(0)
}
//-------------------------------------------------------------------//
// To validate a year
function isYear(argYear) {
  return (isNumeric(argYear)&&((argYear.length==2)||(argYear.length==4))&&(argYear<3000||argYear==9999))
}
//-------------------------------------------------------------------//
// To validate a month
function isMonth(argMonth) {
  return (isNumeric(argMonth)&&((argMonth.length==2)||(argMonth.length==1))&&argMonth>0&&argMonth<13)
}
//-------------------------------------------------------------------//
// To validate a day
function isDay(argDay) {
  return (isNumeric(argDay)&&((argDay.length==2)||(argDay.length==1))&&argDay>0&&argDay<32)
}
//-------------------------------------------------------------------//
// To obtain days in a month
function daysInMonth(argMonth, argYear) {
  if(argMonth==2) return daysInFeb(argYear)
  if(argMonth==4||argMonth==6||argMonth==9||argMonth==11) return 30
  return 31
}
//-------------------------------------------------------------------//
// To obtains days in February based on the year
function daysInFeb(argYear) {
  return (((argYear%4==0)&&((!(argYear%100==0))||(argYear%400==0)))?29:28)
}
//-------------------------------------------------------------------//
// To determine whether only spaces are found
function isWhiteSpaces(argStr) {
  for(var intCount=0; intCount<argStr.length; intCount++)
   if(argStr.substring(intCount, intCount+1)!=' ') return false
  return true
}
//-------------------------------------------------------------------//
//VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV


// To Compare two dates
// Returns true if Date1 > Date2 else false
function CompareDates(Date1, Date2) {
	var dteDate1 = new Date(Date1)
	var dteDate2 = new Date(Date2)  
	
	if (dteDate1.getYear() > dteDate2.getYear())
		return true;
	else 
		if (dteDate1.getYear() == dteDate2.getYear())
			if (dteDate1.getMonth() > dteDate2.getMonth())
				return true;
			else 
				if (dteDate1.getMonth() == dteDate2.getMonth())
					if (dteDate1.getDate() > dteDate2.getDate()) 
						return true;
					else
						return false;
}
//-->

function RemoveSpacesnQuotes(objFormElementName)
{
	var strToBeChanged = document.forms[0].elements[objFormElementName].value;
	var strAfterChange = "";
	intLoopIndex = 0;
	while(intLoopIndex < strToBeChanged.length) {
		if (strToBeChanged.charAt(intLoopIndex) == ' ') {
			strAfterChange = strAfterChange + " ";
			for(j = ++intLoopIndex; (j< strToBeChanged.length) && (strToBeChanged.charAt(j) == ' '); ++j)
				++intLoopIndex;
		}		
		if (strToBeChanged.charAt(intLoopIndex) == "'")
			strAfterChange = strAfterChange + "'";
			
		strAfterChange = strAfterChange + strToBeChanged.charAt(intLoopIndex++);	
	}
	document.forms[0].elements[objFormElementName].value = strAfterChange;
	return true;
}//End Of Function

function RemoveSpaces(objFormElementName)
{
	var strToBeChanged = document.forms[0].elements[objFormElementName].value;
	var strAfterChange = "";
	intLoopIndex = 0;
	while(intLoopIndex < strToBeChanged.length) {
		if (strToBeChanged.charAt(intLoopIndex) == ' ') {
			strAfterChange = strAfterChange + " ";
			for(j = ++intLoopIndex; (j< strToBeChanged.length) && (strToBeChanged.charAt(j) == ' '); ++j)
				++intLoopIndex;
		}		
		strAfterChange = strAfterChange + strToBeChanged.charAt(intLoopIndex++);	
	}
	document.forms[0].elements[objFormElementName].value = strAfterChange;
	return true;
}//End Of Function


// This function validates the contents of a form element and gives a message to enter a valid date
// After giving the appropriate message, it still keeps the focus on the same form element.

function ValidateDate(strElement){
var strContentInTextBox = document.forms[0].elements[strElement].value;
						
if ((strContentInTextBox.length > 0) && (!isDate(strContentInTextBox))){
alert('Enter a Valid Date');
document.forms[0].elements[strElement].focus();
}				
}

//=========================================================================================================

//''Functions to move options from one select list to another
		function moveSelected(piSource, piTarget){
		var arrDeleteMe = new Array();
		var x = 0;
		for(var i=0; i<piSource.options.length; i++) {
			if(piSource.options[i].selected && piSource.options[i].value != "") {
			var oOption = new Option();
			oOption.value = piSource.options[i].value;
			oOption.text = piSource.options[i].text;
			
			piTarget.options[piTarget.options.length] = oOption;
			piSource.options[i].value = "";
			piSource.options[i].text = "";
			}
		}
		removeOptions(piSource);
		}		
		
		function removeOptions(listbox)  {
			for(var i=0; i<listbox.options.length; i++) {
			if(listbox.options[i].value == "")  {
			for(var j=i; j<listbox.options.length-1; j++)  {
			listbox.options[j].value = listbox.options[j+1].value;
			listbox.options[j].text = listbox.options[j+1].text;
			}
			var ln = i;
			break;
			   }
			}
			if(ln < listbox.options.length)  {
			listbox.options.length -= 1;
			removeOptions(listbox);
			}
		}

//''======================================================================================================

function selectAllValues(piSelectObject)
{
	for (i = 0; i < piSelectObject.options.length; i++)
	{
		piSelectObject.options[i].selected = true;
	}
}
//''======================================================================================================
function selectAllAndMove(piSource, piTarget)
{
	selectAllValues(piSource);
	moveSelected(piSource, piTarget);
}

//''======================================================================================================
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
