﻿ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Module : Validations
Created By : SPatel.
Description : A page containing validation functions to be used.
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

// validion flag
var isControlValidated = true;
// end

String.prototype.trim = function()
{
   return this.replace(/^\s+|\s+$/g, "");
}
;
String.prototype.ltrim = function()
{
   return this.replace(/^\s+/, "");
}
;
String.prototype.rtrim = function()
{
   return this.replace(/\s+$/, "");
}
;


// Make ErrorMsgControl to empty.
function MakeErrMsgCtrlEmtpy(psMsgControlName)
{

   var psMsgControl = window.document.getElementById(psMsgControlName);
   psMsgControl.innerHTML = "";
   focusControl = null;
}
;

// Make ErrorMsgControl to empty.
function MakeErrMsgCtrlEmtpy_New(psMsgControlName)
{
   var psMsgControl = window.document.getElementById(psMsgControlName);
   psMsgControl.innerHTML = "<div class=\"DvErrorMsg\"></div>";
   focusControl = null;
}
;
var focusControl;
// For Validation of blank field and field with only spaces
// poControlType = [0 : textbox, radiobutton, checkbox, dropdownlist; 1 : radiobuttonlist; 2 : checkboxlist]
// excludeZero = 1 : consider zero(0) value as null;
// displayError = true
function ValidateNull(poControlName, poControlType, psMessage, psMsgControlName, displayError, excludeZero)
{
   var str = " ";
   var count = 0;
   var string;
   var maxLength;
   var psMsgControl = window.document.getElementById(psMsgControlName);
   var isValid = true;

   if (poControlType == 0)
   {
      var poControl = window.document.getElementById(poControlName);
      string = poControl.value.Trim();
   }
   else if(poControlType == 1) // radiobuttonlist
   {
      string = GetRadioListData(poControlName);
   }
   else if(poControlType == 2) // checkboxlist
   {
      string = GetCheckboxListData(poControlName);
   }
   else if(poControlType == 3) // Listboxlist
   {
      string = GetListboxData(poControlName);
   }

   if ((excludeZero == 1) && (string == "0"))
   {
      isValid = false;
   }
   else if((string == null) || (string == ""))
   {
      isValid = false;
   }
   else
   {
      maxLength = string.length;
      while(count < maxLength)
      {
         if(string == str)
         {
            poControl.value = "";
            isValid = false;
            break;
         }
         else
         {
            str += " ";
            count ++ ;
         }
      }
   }

   if( ! isValid )
   {
      if (displayError == undefined || displayError == true)
      {
         if ( focusControl == null && poControl != undefined)
         {
            focusControl = poControl;
            if(focusControl.style.visibility == "visible" || focusControl.style.visibility == "")
            {
               if(focusControl.type.toLowerCase() == 'text')
               focusControl.select();
               focusControl.focus();
            }
         }
         AddErrorMessage( psMsgControl, psMessage );

      }

      return false
   }

   // Validate successfully.
   return true;
}
;




//// Validates maxlength
function ValidateLength(poControlName, psLength, psMessage, psMsgControlName)
{
   var poControl = window.document.getElementById(poControlName);
   var psMsgControl = window.document.getElementById(psMsgControlName);

   var lsLength = poControl.value.length;
   if (lsLength > psLength)
   {

      isControlValidated = false;
      AddErrorMessage( psMsgControl, psMessage );
      if ( focusControl == null)
      {
         focusControl = poControl;
         if(focusControl.style.visibility == "visible" || focusControl.style.visibility == "")
         {
            focusControl.select();
            focusControl.focus();
         }
      }
      return false;
   }
   // return true;
}
;

function ValidateMultiLength(poControlName, psLength, psSeperator, psMessage, psMsgControlName)
{
   var poControl = document.getElementById(poControlName);
   var psMsgControl = document.getElementById(psMsgControlName);
   var valueArray = poControl.value.split( psSeperator );
   // for( poControlValue in valueArray )
   for(var i = 0; i < valueArray.length; i ++ )
   {
      // var strValue = valueArray[poControlValue];
      var strValue = valueArray[i];
      strValue = strValue.trim();
      if( strValue.length > psLength )
      {
         isControlValidated = false;
         // 	           psMsgControl.innerHTML += '<li>' + psMessage + '</li>';
         // 	           psMsgControl.style.display = '';
         AddErrorMessage( psMsgControl, psMessage );
         if(focusControl == null && poControl != undefined)
         {
            focusControl = poControl;
            if(focusControl.style.visibility == "visible" || focusControl.style.visibility == "")
            {
               focusControl.select();
               focusControl.focus();
            }
         }
         return false;
      }
   }
}
;

//// Validates minimum length
function ValidateMinLength(poControlName, psLength, psMessage, psMsgControlName)
{
   var poControl = window.document.getElementById(poControlName);
   var psMsgControl = window.document.getElementById(psMsgControlName);

   var lsLength = poControl.value.length;
   if (lsLength < psLength)
   {

      isControlValidated = false;
      // 		    psMsgControl.innerHTML += '<li>' + psMessage + '</li>';
      AddErrorMessage( psMsgControl, psMessage );
      if(focusControl == null && poControl != undefined)
      {
         focusControl = poControl;
         if(focusControl.style.visibility == "visible" || focusControl.style.visibility == "")
         {
            focusControl.select();
            focusControl.focus();
         }
      }
      return false;
   }

}
;

function ValidateRTENull(poControlName, psMessage, psMsgControlName)
{
   var psMsgControl = window.document.getElementById(psMsgControlName);
   var lsLength = document.forms[0].elements[poControlName].value.length;
   if( lsLength == 0)
   {
      isControlValidated = false;
      AddErrorMessage( psMsgControl, psMessage );

      // psMsgControl.innerHTML += '<li>' + psMessage + '</li>';
      return false;
   }
}
;

function ValidateRTELength(poControlName, psLength, psMessage, psMsgControlName)
{
   var psMsgControl = window.document.getElementById(psMsgControlName);
   var lsLength = document.forms[0].elements[poControlName].value.length;
   if (lsLength > psLength)
   {
      isControlValidated = false;
      // psMsgControl.innerHTML += '<li>' + psMessage + '</li>';
      AddErrorMessage(psMsgControl, psMessage);
      return false;
   }
}
;


// To Check Valid Url
function ValidUrl(poControlName, psMessage, psMsgControlName)
{
   var Url = document.getElementById(poControlName);
   if(Url.value.match("/^(((http(s?))|(ftp))\:\/\/)(www.|[a-zA-Z].)[a-zA-Z0-9\-\.]+\.(com|edu|gov|mil|net|org|biz|info|name|museum|us|ca|uk|in)(\:[0-9]+)*(\/($|[a-zA-Z0-9\.\,\;\?\'\\\+&%\$#\=~_\-]+))*$/") || Url.value.match("/^mailto\:\w+([\.\-]\w+)*\@\w+([\.\-]\w+)*\.\w{2,4}$/i"))
   {
      return true;
   }
   else
   {
      var psMsgControl = window.document.getElementById(psMsgControlName);
      isControlValidated = false;
      AddErrorMessage(psMsgControl, psMessage);
      if (focusControl)
      {
         focusControl = Url;
         if(focusControl.style.visibility == "visible" || focusControl.style.visibility == "")
         {
            focusControl.select();
            focusControl.focus();
         }
      }
      return false;
   }
}
;
function IsValidUrl(poControlName, psMessage, psMsgControlName)
{
   var Url = document.getElementById(poControlName);
   var tomatch = /http:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/;
   if(tomatch.test(Url.value))
   {
      return true;
   }
   else
   {
      isControlValidated = false;
      if ( focusControl == null)
      {
         focusControl = Url;
         if(focusControl.style.visibility == "visible" || focusControl.style.visibility == "")
         {
            focusControl.select();
            focusControl.focus();
         }
      }
      return false;
   }
}
;


function IsValidSearchUrl(poControlName)
{
   var Url = document.getElementById(poControlName);
   
   var tomatch = /http:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/;
   
   if (tomatch.test(Url.value))
   {
      return true;
   }   
   else
   {
      isControlValidated = false;
      if ( focusControl == null)
      {
         focusControl = Url;
         if(focusControl.style.visibility == "visible" || focusControl.style.visibility == "")
         {
            focusControl.select();
            focusControl.focus();
         }
      }
      return false;
   }
}
;
function textCounter(field, maxlimit , countfield)
{


   field = document.getElementById(field);
   if (field.value.length > maxlimit) // if too long...trim it !
   {
      field.value = field.value.substring(0, maxlimit);
   }
   else if ( countfield )
   {
      countfield  = document.getElementById(countfield);
      countfield.value = maxlimit - field.value.length;
   }

}
;

// Check the validation and will display the errror or will continue for further save process.
// After it return true, you can call the function which save the records in database.
function ProceedToSaveRecords(errCtrlId)
{
   var errCtrl = window.document.getElementById(errCtrlId);
   if (isControlValidated == false)
   {
      errCtrl.style.display = "";
      return false;
   }
   else
   {
      errCtrl.style.display = "none";
   }

   return true;
}
;

function AddErrorMessage(psMsgControl, psMessage)
{

   isControlValidated = false;

   var msgContainerControl = psMsgControl;
   for(var i = 0; i < 3; i ++ )
   {
      if(msgContainerControl.className == 'DvMsgContainer')
      break;
      if(msgContainerControl.parentNode)
      msgContainerControl = msgContainerControl.parentNode;
   }
   var lastDiv;
   var counterDiv = msgContainerControl;
   var nestingLevel = 0;
   while(true)
   {
      for(var i = 0; i < counterDiv.childNodes.length;
      i ++ )
      {
         var node = counterDiv.childNodes[i];
         if(node.tagName == "DIV")
         {
            lastDiv = node;
            break;
         }
      }
      if( ! lastDiv)
      break;
      counterDiv = lastDiv;
      lastDiv = undefined;
   }
   if(counterDiv) psMsgControl = counterDiv;
   //    if(msgContainerControl)
   //    {
   //        if(msgContainerControl.childNodes.length > 0)
   //        {
   //            if(msgContainerControl.childNodes.length == 1)
   //                psMsgControl = msgContainerControl.childNodes[0];
   //            else psMsgControl = msgContainerControl.childNodes[1];
   //        }
   //    }

   var error;
   if(psMsgControl.innerHTML.length == 0)
   {
      // psMsgControl.innerHTML = "<div id='e' class='DvMessage'>Error Message</div><UL>";
      // psMsgControl.innerHTML = "<div class='DvMessage'>Error Message</div>";
      psMsgControl.innerHTML = "<span>Error</span>";

   }
   if ( (psMsgControl.innerHTML.indexOf('<UL>') != - 1) || (psMsgControl.innerHTML.indexOf('<ul>') != - 1))
   {
      error = psMsgControl.innerHTML;
      error = error.replace('</UL>', '');
      error = error.replace('</ul>', '');
      error = error.replace('<UL>', '');
      error = error.replace('<ul>', '');
      psMsgControl.innerHTML = error;
   }
   psMsgControl.innerHTML += '<li>' + psMessage + '</li>';
   // psMessage

   if(psMsgControl.innerHTML.indexOf('</span>') != - 1)
   error = psMsgControl.innerHTML.replace('Error</span>', 'Error</span><UL>');
   else
   error = psMsgControl.innerHTML.replace('Error</SPAN>', 'Error</SPAN><UL>');
   error = error + '</UL>';

   psMsgControl.innerHTML = error;

   // code to bring focus on the error message added by Shahnawaz Panhalkar [03 May]

   if(msgContainerControl)
   {
      if(msgContainerControl.childNodes.length > 0)
      {
         if(msgContainerControl.childNodes[0].tagName != "A")
         {
            msgContainerControl.innerHTML = "<A id='e'></A>" + msgContainerControl.innerHTML;
         }
         msgContainerControl.style.display = 'block';
      }
   }
   // window.location.href = '#e';
   // ---------------------------------------------------------------
}
;

// excludeDecimal = 1 : consider decimal(0.0) value as null;
function IsValidNumber(poControlName, psMessage, psMsgControlName, excludeDecimal)
{
   var ctrlId = window.document.getElementById(poControlName);
   var ctrlValue = ctrlId.value;
   var psMsgControl = window.document.getElementById(psMsgControlName);
   var regExp = "";

   if(excludeDecimal == 1)
   regExp = /[^0-9]/;
   else
   regExp = /[^0-9.]/;

   if(ctrlValue != '')
   {
      if(ctrlValue == '00')
      {
         isControlValidated = false;
         AddErrorMessage(psMsgControl, psMessage);
         if ( focusControl == null)
         {
            focusControl = ctrlId;
            if(focusControl.style.visibility == "visible" || focusControl.style.visibility == "")
            {
               focusControl.select();
               focusControl.focus();
            }
         }
         return false;
      }
      else
      {
         if (regExp.test(ctrlValue))
         {
            isControlValidated = false;
            AddErrorMessage(psMsgControl, psMessage);
            if ( focusControl == null)
            {
               focusControl = ctrlId;
               if(focusControl.style.visibility == "visible" || focusControl.style.visibility == "")
               {
                  focusControl.select();
                  focusControl.focus();
               }
            }
            return false;
         }

      }

   }

   return true;
};


//Code By D.G. on June 18, 2008, for Compare the Value, with its Max Limit value
function CompareWithMaxValues(poControlName, psMessage, psMsgControlName, maxValue)
{
   var ctrlId = window.document.getElementById(poControlName);
   var ctrlValue = ctrlId.value;
   var psMsgControl = window.document.getElementById(psMsgControlName);
   var regExp = "";

   if(ctrlValue > maxValue)
   {
     isControlValidated = false;
     AddErrorMessage(psMsgControl, psMessage);
     if ( focusControl == null)
     {
        focusControl = ctrlId;
        if(focusControl.style.visibility == "visible" || focusControl.style.visibility == "")
        {
           focusControl.select();
           focusControl.focus();
        }
     }
     return false;
   }
   return true;
};


// Validate whether startDate is less or equal to endDate.
function CompareDates(psStartDate, psEndDate,  psMessage, psMsgControlName)
{
   //   var startDate, endDate;
   //   var date1, date2;
   //   var month1, month2;
   //   var year1, year2;
   //
   //   startDate = window.document.getElementById(psStartDate).value;
   //   endDate = window.document.getElementById(psEndDate).value;
   //
   //   var psMsgControl = window.document.getElementById(psMsgControlName);
   //
   //   date1 = startDate.substring(0, startDate.indexOf (seperator));
   //   month1 = startDate.substring(startDate.indexOf (seperator) + 1, startDate.lastIndexOf (seperator));
   //   year1 = startDate.substring(startDate.lastIndexOf (seperator) + 1, startDate.length);
   //
   //   date2 = endDate.substring (0, endDate.indexOf (seperator));
   //   month2 = endDate.substring (endDate.indexOf (seperator) + 1, endDate.lastIndexOf (seperator));
   //   year2 = endDate.substring (endDate.lastIndexOf (seperator) + 1, endDate.length);

   //   if (year1 > year2) {AddErrorMessage(psMsgControl, psMessage);  return false; }
   //   else if ((year2 == year1) && (month1 > month2)) {AddErrorMessage(psMsgControl, psMessage);  return false; }
   //   else if ((year2 == year1) && (month2 == month1) && (date1 > date2)) {AddErrorMessage(psMsgControl, psMessage);  return false; }
   //   else {return true};

   var CurrentDate = new Date();
   var StartDate = new Date();
   var EndDate = new Date();
   var returnValue = false ;
   var StartDateValue = window.document.getElementById(psStartDate).value;
   var EndDateValue = window.document.getElementById(psEndDate).value;
   var psMsgControl = window.document.getElementById(psMsgControlName);

   if (StartDateValue != "")
   {
      var dateArray = StartDateValue.split('/');
      var dayValue = dateArray[0];
      var monthValue = dateArray[1];
      monthValue = eval(parseInt(monthValue) - 1);
      var yearValue = dateArray[2];
      StartDate.setFullYear(yearValue, monthValue, dayValue);
   }
   if (EndDateValue != "")
   {
      var dateArray = EndDateValue.split('/');
      var dayValue = dateArray[0];
      var monthValue = dateArray[1];
      monthValue = eval(parseInt(monthValue) - 1);
      var yearValue = dateArray[2];
      EndDate.setFullYear(yearValue, monthValue, dayValue);
   }

   if(StartDateValue != "" && EndDateValue != "")
   {
      if (EndDate < StartDate)
      {
         AddErrorMessage(psMsgControl, psMessage);
         return false;
      }
      else
      {
         return true;
      }
   }
   else
   {
      return true;
   }


};


// Validate StartDate <= EndDate
function CompareEventDates(psStartDate, psEndDate,  psMessage, psMsgControlName)
{
   var CurrentDate = new Date();
   var StartDate = new Date();
   var EndDate = new Date();
   var returnValue = false ;
   var StartDateValue = window.document.getElementById(psStartDate).value;
   var EndDateValue = window.document.getElementById(psEndDate).value;
   var psMsgControl = window.document.getElementById(psMsgControlName);

   if (StartDateValue != "")
   {
      var dateArray = StartDateValue.split('/');
      var dayValue = dateArray[0];
      var monthValue = dateArray[1];
      monthValue = eval(parseInt(monthValue) - 1);
      var yearValue = dateArray[2];
      StartDate.setFullYear(yearValue, monthValue, dayValue);
   }
   if (EndDateValue != "")
   {
      var dateArray = EndDateValue.split('/');
      var dayValue = dateArray[0];
      var monthValue = dateArray[1];
      monthValue = eval(parseInt(monthValue) - 1);
      var yearValue = dateArray[2];
      EndDate.setFullYear(yearValue, monthValue, dayValue);
   }

   if(StartDateValue != "" && EndDateValue != "")
   {
      if (StartDate > EndDate)
      {
         AddErrorMessage(psMsgControl, psMessage);
         return false;
      }
      else
      {
         return true;
      }
   }
   else
   {
      return true;
   }
};
function CompareEventDateTime(psStartDate, psEndDate,  psMessage, psMsgControlName)
{
   var CurrentDate = new Date();
   var StartDate = new Date();
   var EndDate = new Date();
   var returnValue = false ;
   var StartDateValue = window.document.getElementById(psStartDate).value;
   var EndDateValue = window.document.getElementById(psEndDate).value;
   var psMsgControl = window.document.getElementById(psMsgControlName);

   if (StartDateValue != "")
   {
      var dateArray = StartDateValue.split('/');
      var dayValue = dateArray[0];
      var monthValue = dateArray[1];
      monthValue = eval(parseInt(monthValue) - 1);
      var yearValue = dateArray[2];
      var hour = dateArray[3];
      var minute = dateArray[4];
      StartDate.setFullYear(yearValue, monthValue, dayValue);
      StartDate.setHours(hour, minute, 0, 0);
   }
   if (EndDateValue != "")
   {
      var dateArray = EndDateValue.split('/');
      var dayValue = dateArray[0];
      var monthValue = dateArray[1];
      monthValue = eval(parseInt(monthValue) - 1);
      var yearValue = dateArray[2];
      var hour = dateArray[3];
      var minute = dateArray[4];
      EndDate.setFullYear(yearValue, monthValue, dayValue);
      EndDate.setHours(hour, minute, 0, 0);
   }

   if(StartDateValue != "" && EndDateValue != "")
   {
      if (StartDate > EndDate)
      {
         AddErrorMessage(psMsgControl, psMessage);
         return false;
      }
      else
      {
         return true;
      }
   }
   else
   {
      return true;
   }
};



// Function used to get date difference in years
function days_between(fromDate, toDate)
{
   // The number of milliseconds in one day
   var year = 1000 * 60 * 60 * 24 * 365;

   // Convert both dates to milliseconds
   // toDate = Date.parse(toDate);
   // fromDate = Date.parse(fromDate);

   var date1_ms = toDate.getTime();
   var date2_ms = fromDate.getTime();

   // Calculate the difference in milliseconds
   var difference_ms = date1_ms - date2_ms;

   // Convert back to days and return
   return Math.round(difference_ms / year)
};

String.prototype.Trim = function()
{
   return this.replace(/^\s+|\s+$/, '');

}
;

function CompairValues(psFirstControl, psSecondControl, psMessage, psMsgControlName)
{
   var firstValue = window.document.getElementById(psFirstControl).value;
   var secondValue = window.document.getElementById(psSecondControl).value;
   if (firstValue.Trim() != secondValue.Trim())
   {
      var psMsgControl = window.document.getElementById(psMsgControlName);
      AddErrorMessage(psMsgControl, psMessage);
      if ( focusControl == null)
      {
         focusControl = window.document.getElementById(psSecondControl);
         if(focusControl.style.visibility == "visible" || focusControl.style.visibility == "")
         {
            focusControl.select();
            focusControl.focus();
         }
      }
      return false;
   }
   return true;

};

function IsValidateEmailByValue(emailAdd)
{
 var EmailRegExp =  /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})$/;
  // var EmailRegExp =  / ^ ([a - zA - Z0 - 9_\.\ - ]) + \@(([a - zA - Z0 - 9\ - ]) + \.) + ([a - zA - Z0 - 9]{2, 4})$ / ;
   if ( ! EmailRegExp.test(emailAdd.Trim()))
   {
      return false;
   }
   else
   {
      return true;
   }
};

function IsValidEMail(psEmail)
{
   var EmailRegExp =  / ^ ([a - zA - Z0 - 9_\.\ - ]) + \@(([a - zA - Z0 - 9\ - ]) + \.) + ([a - zA - Z0 - 9]{2, 4})$ / ;
   var poControl = window.document.getElementById(psEmail);
   sEmailAddressToValidate = poControl.value;
   if ( ! EmailRegExp.test(sEmailAddressToValidate.Trim()))
   {
      return false;
   }
   else
   {
      return true;
   }
};

/// This function is addded by mehul
function IsValidEmailAddress( psEmail ,psMessage, psMsgControlName)
{
    var EmailRegExp =  /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})$/;
    var poControl = window.document.getElementById(psEmail);
    sEmailAddressToValidate = poControl.value;
   if (! EmailRegExp.test(sEmailAddressToValidate.trim()) )
   {
     var psMsgControl = window.document.getElementById(psMsgControlName);
     AddErrorMessage(psMsgControl, psMessage);
     if ( focusControl == null && poControl != undefined)
    {
        focusControl =poControl;
        if(focusControl.style.visibility == "visible" || focusControl.style.visibility == "") 
            {
				focusControl.select();
				focusControl.focus();
			}
    }
     return false;
   }
   else
     return true;
}




function ValidateEmailAddress(psEmailControl, psMessage, psMsgControlName)
{

   var EmailRegExp =  / ^ ([a - zA - Z0 - 9_\.\ - ]) + \@(([a - zA - Z0 - 9\ - ]) + \.) + ([a - zA - Z0 - 9]{2, 4})$ / ;
   var emailID = window.document.getElementById(psEmailControl);
   var strAddrs = emailID.value;
   var j = strAddrs.indexOf(",");
   if(strAddrs.indexOf(",") == - 1)
   {
      if ( ! EmailRegExp.test(emailID.value.trim()) )
      {
         var psMsgControl = window.document.getElementById(psMsgControlName);
         AddErrorMessage(psMsgControl, psMessage);
         if ( focusControl == null && emailID != undefined)
         {
            focusControl = emailID;
            if(focusControl.style.visibility == "visible" || focusControl.style.visibility == "")
            {
               focusControl.select();
               focusControl.focus();
            }
         }
         return false;
      }
   }
   else
   {
      strAddrs = strAddrs + ",";
      var validatedEmailAddrs = "";
      var strAddr;
      var i = 0;
      while(j >= 0)
      {
         strAddr = strAddrs.substr(i, j);
         strAddr = strAddr.trim();

         if(strAddr.length > 0)
         {
            if ( ! EmailRegExp.test(strAddr) )
            {
               var psMsgControl = window.document.getElementById(psMsgControlName);
               AddErrorMessage(psMsgControl, psMessage);
               if ( focusControl == null && emailID != undefined)
               {
                  focusControl = emailID;
                  if(focusControl.style.visibility == "visible" || focusControl.style.visibility == "")
                  {
                     focusControl.select();
                     focusControl.focus();
                  }
               }
               return false;
            }
            if(validatedEmailAddrs.length > 0)validatedEmailAddrs = validatedEmailAddrs + ", ";
            validatedEmailAddrs = validatedEmailAddrs + strAddr;
         }
         strAddrs = strAddrs.substr(j + 1, strAddrs.length);
         j = strAddrs.indexOf(",");
      }
      emailID.value = validatedEmailAddrs;
   }
};

function CheckDuplicateString(commaSeperatedString)
{
   var stringArray = commaSeperatedString.split(',');
   for(var i = 0; i < stringArray.length; i ++ )
   {
      for(var j = i + 1; j < stringArray.length;
      j ++ )
      {
         if(stringArray[i].trim() == stringArray[j].trim())
         {
            return false;
         }
      }
   }
   return true;
};

function CheckUSZipCode(psPostalCode, psMessage, psMsgControlName)
{
   var txtZipControl = document.getElementById(psPostalCode).value;
   var psMsgControl = window.document.getElementById(psMsgControlName);
   if(txtZipControl != "")
   {

      if(txtZipControl.length == 5)
      {

         if(isNaN(txtZipControl))
         {
            AddErrorMessage(psMsgControl, psMessage);
            return false;
         }
         else
         {
            return true;
         }
      }
      else
      {
         AddErrorMessage(psMsgControl, psMessage);
         return false;
      }
   }
};
function CheckPostalCode(psCountry, psPostalCode, psMessage, psMsgControlName)
{
   var zipControl = document.getElementById(psPostalCode);
   var txtZipControl = zipControl.value;
   var psMsgControl = window.document.getElementById(psMsgControlName);
   if(txtZipControl != "")
   {
      var drpCtryControl = window.document.getElementById(psCountry).value;
      if(drpCtryControl == '117')
      {
         if(txtZipControl.length == 5)
         {
            if(isNaN(txtZipControl))
            {
               AddErrorMessage(psMsgControl, psMessage);
               if ( focusControl == null && zipControl != undefined)
               {
                  focusControl = zipControl;
                  if(focusControl.style.visibility == "visible" || focusControl.style.visibility == "")
                  {
                     focusControl.select();
                     focusControl.focus();
                  }
               }
               return false;
            }
            else
            {
               return true;
            }
         }
         else
         {
            AddErrorMessage(psMsgControl, psMessage);
            if ( focusControl == null && zipControl != undefined)
            {
               focusControl = zipControl;
               if(focusControl.style.visibility == "visible" || focusControl.style.visibility == "")
               {
                  focusControl.select();
                  focusControl.focus();
               }
            }
            return false;
         }
      }
      else if(drpCtryControl == '17')
      {
         if(txtZipControl.length == 7)
         {
            if(txtZipControl.indexOf(' ') != 3)
            {
               AddErrorMessage(psMsgControl, psMessage);
               if ( focusControl == null && zipControl != undefined)
               {
                  focusControl = zipControl;
                  if(focusControl.style.visibility == "visible" || focusControl.style.visibility == "")
                  {
                     focusControl.select();
                     focusControl.focus();
                  }
               }
               return false;
            }
            else
            {
               return true;
            }
         }
         else
         {
            AddErrorMessage(psMsgControl, psMessage);
            if ( focusControl == null && zipControl != undefined)
            {
               focusControl = zipControl;
               if(focusControl.style.visibility == "visible" || focusControl.style.visibility == "")
               {
                  focusControl.select();
                  focusControl.focus();
               }
            }
            return false;
         }
      }
      else
      {
         return true;
      }
   }
};

// Declaring valid date character, minimum year and maximum year
var dtCh = "/";
var minYear = 1900;
var maxYear = 2100;

function isInteger(s)
{
   var i;
   for (i = 0; i < s.length; i ++ )
   {
      // Check that current character is number.
      var c = s.charAt(i);
      if (((c < "0") || (c > "9"))) return false;
   }
   // All characters are numbers.
   return true;
};

function stripCharsInBag(s, bag)
{
   var i;
   var returnString = "";
   // Search through string's characters one by one.
   // If character is not in bag, append to returnString.
   for (i = 0; i < s.length; i ++ )
   {
      var c = s.charAt(i);
      if (bag.indexOf(c) == - 1) returnString += c;
   }
   return returnString;
};

function daysInFebruary (year)
{
   // February has 29 days in any year evenly divisible by four,
   // EXCEPT for centurial years which are not also divisible by 400.
   return (((year % 4 == 0) && ( ( ! (year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
};
function DaysArray(n)
{
   for (var i = 1; i <= n; i ++ )
   {
      this[i] = 31;
      if (i == 4 || i == 6 || i == 9 || i == 11)
      {
         this[i] = 30;
      }
      if (i == 2)
      {
         this[i] = 29;
      }
   }
   return this;
};

function isDate(dtStr)
{
   var daysInMonth = DaysArray(12);
   var pos1 = dtStr.indexOf(dtCh);
   var pos2 = dtStr.indexOf(dtCh, pos1 + 1);
   var strMonth = dtStr.substring(0, pos1);
   var strDay = dtStr.substring(pos1 + 1, pos2);
   var strYear = dtStr.substring(pos2 + 1);
   strYr = strYear;
   if (strDay.charAt(0) == "0" && strDay.length > 1) strDay = strDay.substring(1);
   if (strMonth.charAt(0) == "0" && strMonth.length > 1) strMonth = strMonth.substring(1);
   for (var i = 1; i <= 3; i ++ )
   {
      if (strYr.charAt(0) == "0" && strYr.length > 1) strYr = strYr.substring(1);
   }
   month = parseInt(strMonth);
   day = parseInt(strDay);
   year = parseInt(strYr);
   if (pos1 == - 1 || pos2 == - 1)
   {
      // alert("The date format should be : mm/dd/yyyy")
      return false;
   }
   if (strMonth.length < 1 || month < 1 || month > 12)
   {
      // alert("Please enter a valid month")
      return false;
   }
   if (strDay.length < 1 || day < 1 || day > 31 || (month == 2 && day > daysInFebruary(year)) || day > daysInMonth[month])
   {
      // alert("Please enter a valid day")
      return false;
   }
   if (strYear.length != 4 || year == 0 || year < minYear || year > maxYear)
   {
      // alert("Please enter a valid 4 digit year between " + minYear + " and " + maxYear)
      return false;
   }
   if (dtStr.indexOf(dtCh, pos2 + 1) != - 1 || isInteger(stripCharsInBag(dtStr, dtCh)) == false)
   {
      // alert("Please enter a valid date")
      return false;
   }
   return true;
};

function ValidateForm()
{
   var dt = document.frmSample.txtDate;
   if (isDate(dt.value) == false)
   {
      dt.focus();
      return false;
   }
   return true;
};

function ValidateHTML(poControlName)
{
   var ctrl = window.document.getElementById(poControlName);
   var ctrlValue = ctrl.value;
   if ((ctrlValue.indexOf('<') > - 1) || (ctrlValue.indexOf('>') > - 1))
   {
      return false;
   }
   else
   {
      return true;
   }
};

//// Dipti
// Validate whether control contains HTML Tag or not. Only for Textbox or Text Area by passing its Id.
function ValidateHtmlTags(poControlName, psMessage, psMsgControlName, AddError)
{
   var ctrl = window.document.getElementById(poControlName);
   var ctrlValue = ctrl.value;


   // if((ctrlValue.indexOf(''') > -1))
   // {
   //  	 var psMsgControl = window.document.getElementById(psMsgControlName);
   //  	 AddErrorMessage(psMsgControl, psMessage);
   //  	 return false;
   // }

   // if ((ctrlValue.indexOf('<') > - 1) || (ctrlValue.indexOf('>') > - 1) || (ctrlValue.indexOf('&') > - 1))
   if ((ctrlValue.indexOf('<') > - 1) || (ctrlValue.indexOf('>') > - 1) || (ctrlValue.indexOf('&') > - 1))
   {
      var psMsgControl = window.document.getElementById(psMsgControlName);
      if (AddError == undefined || (AddError) )
      {
         // AddErrorMessage(psMsgControl, psMessage);

         // 		      isControlValidated = false;
         // 		        psMsgControl.innerHTML += '<li>' + psMessage + '</li>'; // psMessage + "<br>";
         // 		        psMsgControl.style.display = '';
         AddErrorMessage(psMsgControl, psMessage);
         if ( focusControl == null && ctrl != undefined)
         {
            focusControl = ctrl;
            if(focusControl.style.visibility == "visible" || focusControl.style.visibility == "")
            {
               focusControl.select();
               focusControl.focus();
            }
         }
         checkHTML = false;
      }
      return false;
   }
   return true;
};

///
// Validate whether control contains HTML Tags and Single colon or not.
function ValidateHtmlAndSingleColonTags(poControlName, psMessage, psMsgControlName)
{
   var ctrl = window.document.getElementById(poControlName);
   var ctrlValue = ctrl.value;
   if ((ctrlValue.indexOf('<') > - 1) || (ctrlValue.indexOf('>') > - 1) || (ctrlValue.indexOf('&') > - 1) || ctrlValue.indexOf("'") > -1 || ctrlValue.indexOf('"') > -1)
   {
      var psMsgControl = window.document.getElementById(psMsgControlName);
      AddErrorMessage(psMsgControl, psMessage);
      if ( focusControl == null && ctrl != undefined)
      {
         focusControl = ctrl;
         if(focusControl.style.visibility == "visible" || focusControl.style.visibility == "")
         {
            focusControl.select();
            focusControl.focus();
         }
      }
      return false;
   }
   return true;
};

// Validate whether control contains Special Chars (restricted - ! @#$ % ^ & * () += - []\\\';,./{}|\":<>?)
function ValidateSpecialChar(poControlName, psMessage, psMsgControlName, excludeChar)
{
   var ctrl = window.document.getElementById(poControlName);
   var ctrlValue = ctrl.value;

   var iChars = "~`!@#$%^&*()+=[]\\\';,/{}|\":<>? ";

   if(excludeChar)
   {
      for (var i = 0; i < excludeChar.length; i ++ )
      {
         iChars = iChars.replace(excludeChar.charAt(i), '');
      }
   }
   for (var i = 0; i < ctrlValue.length; i ++ )
   {
      if (iChars.indexOf(ctrlValue.charAt(i)) != - 1)
      {
         var psMsgControl = window.document.getElementById(psMsgControlName);
         AddErrorMessage(psMsgControl, psMessage);
         if ( focusControl == null && ctrl != undefined)
         {
            focusControl = ctrl;
            if(focusControl.style.visibility == "visible" || focusControl.style.visibility == "")
            {
               focusControl.select();
               focusControl.focus();
            }
         }
         return false;
      }
   }
   return true;
};

function ValidateSpecialCharCommnet(poControlName, psMessage, psMsgControlName, excludeChar)
{
   var ctrl = window.document.getElementById(poControlName);
   var ctrlValue = ctrl.value;

   var iChars = "@#$%^&*()+=-[]\\\{}| ";

   if(excludeChar)
   {
      for (var i = 0; i < excludeChar.length; i ++ )
      {
         iChars = iChars.replace(excludeChar.charAt(i), '');
      }
   }
   for (var i = 0; i < ctrlValue.length; i ++ )
   {
      if (iChars.indexOf(ctrlValue.charAt(i)) != - 1)
      {
         var psMsgControl = window.document.getElementById(psMsgControlName);
         AddErrorMessage(psMsgControl, psMessage);
         if ( focusControl == null && ctrl != undefined)
         {
            focusControl = ctrl;
            if(focusControl.style.visibility == "visible" || focusControl.style.visibility == "")
            {
               focusControl.select();
               focusControl.focus();
            }
         }
         return false;
      }
   }
   return true;
};

function ValidateTitle(poControlName, psMessage, psMsgControlName, excludeChar)
{
   var ctrl = window.document.getElementById(poControlName);
   var ctrlValue = ctrl.value;

   var iChars = "~`$%^&;{}|<>";			 

   if(excludeChar)
   {
      for (var i = 0; i < excludeChar.length; i ++ )
      {
         iChars = iChars.replace(excludeChar.charAt(i), '');
      }
   }
   for (var i = 0; i < ctrlValue.length; i ++ )
   {
      if (iChars.indexOf(ctrlValue.charAt(i)) != - 1)
      {
         var psMsgControl = window.document.getElementById(psMsgControlName);
         AddErrorMessage(psMsgControl, psMessage);
         if ( focusControl == null && ctrl != undefined)
         {
            focusControl = ctrl;
            if(focusControl.style.visibility == "visible" || focusControl.style.visibility == "")
            {
               focusControl.select();
               focusControl.focus();
            }
         }
         return false;
      }
   }
   return true;
};

function ValidateTabName(poControlName, psMessage, psMsgControlName, excludeChar)
{
   var ctrl = window.document.getElementById(poControlName);
   var ctrlValue = ctrl.value;

   var iChars = "~`$%^&;{}|<>@#*()_-=+|\{}[]:;?/!,.";			 

   if(excludeChar)
   {
      for (var i = 0; i < excludeChar.length; i ++ )
      {
         iChars = iChars.replace(excludeChar.charAt(i), '');
      }
   }
   for (var i = 0; i < ctrlValue.length; i ++ )
   {
      if (iChars.indexOf(ctrlValue.charAt(i)) != - 1)
      {
         var psMsgControl = window.document.getElementById(psMsgControlName);
         AddErrorMessage(psMsgControl, psMessage);
         if ( focusControl == null && ctrl != undefined)
         {
            focusControl = ctrl;
            if(focusControl.style.visibility == "visible" || focusControl.style.visibility == "")
            {
               focusControl.select();
               focusControl.focus();
            }
         }
         return false;
      }
   }
   return true;
};

// Validates Phone No - 0123456789 - + ()
function ValidatePhoneNumber(poControlName, psMessage, psMsgControlName)
{
   var ctrl = window.document.getElementById(poControlName);
   var ctrlValue = ctrl.value;

   var validChars = "0123456789-+()";

   for(var i = 0; i < ctrlValue.length ; i ++ )
   {
      if(validChars.indexOf(ctrlValue.substring(i, i + 1)) < 0)
      {
         var psMsgControl = window.document.getElementById(psMsgControlName);
         AddErrorMessage(psMsgControl, psMessage);
         if ( focusControl == null && ctrl != undefined)
         {
            focusControl = ctrl;
            if(focusControl.style.visibility == "visible" || focusControl.style.visibility == "")
            {
               focusControl.select();
               focusControl.focus();
            }
         }
         return false;
      }
   }
   return true;
};
// Validates only alpha numric data
function ValidateAlphaNumricData(poControlName, psMessage, psMsgControlName)
{

   var ctrl = window.document.getElementById(poControlName);

   var ctrlValue = ctrl.value;
   // var validChars = "[a-z,A-Z,0-9]*";
   var validChars = "1234567890qwertyuioplkjhgfdsazxvbcnmZXCVBNMLKJHGFDSAQWERTYUIOP";

   for(var i = 0; i < ctrlValue.length ; i ++ )
   {
      if(validChars.indexOf(ctrlValue.substring(i, i + 1)) < 0)
      {
         var psMsgControl = window.document.getElementById(psMsgControlName);
         AddErrorMessage(psMsgControl, psMessage);
         if ( focusControl == null && ctrl != undefined)
         {
            focusControl = ctrl;
            if(focusControl.style.visibility == "visible" || focusControl.style.visibility == "")
            {
               focusControl.select();
               focusControl.focus();
            }
         }
         return false;
      }
   }
   return true;
};
function validatespecialcharacter(poControlName, excludeChar)
{
   var ctrl = window.document.getElementById(poControlName);
   var ctrlValue = ctrl.value;
   var iChars = "~`!@#$%^&*()+=-[]\\\';,./{}|\":<>?";
   if(excludeChar)
   {
      for (var i = 0; i < excludeChar.length; i ++ )
      {
         iChars = iChars.replace(excludeChar.charAt(i), '');
      }
   }
   for (var i = 0; i < ctrlValue.length; i ++ )
   {
      if (iChars.indexOf(ctrlValue.charAt(i)) != - 1)
      {
         return false;
      }
   }

   return true;
};

function ValidateSpecialCharacters(poControlName, psMessage, psMsgControlName, excludeChar)
{
   var ctrl = window.document.getElementById(poControlName);
   var ctrlValue = ctrl.value;

   var iChars = "~`!@#$%^&*()+=-[]\\\';,./{}|\":<>?";

   if(excludeChar)
   {
      for (var i = 0; i < excludeChar.length; i ++ )
      {
         iChars = iChars.replace(excludeChar.charAt(i), '');
      }
   }
   for (var i = 0; i < ctrlValue.length; i ++ )
   {
      if (iChars.indexOf(ctrlValue.charAt(i)) != - 1)
      {
         var psMsgControl = window.document.getElementById(psMsgControlName);
         AddErrorMessage(psMsgControl, psMessage);
         if ( focusControl == null && ctrl != undefined)
         {
            focusControl = ctrl;
            if(focusControl.style.visibility == "visible" || focusControl.style.visibility == "")
            {
               focusControl.select();
               focusControl.focus();
            }
         }
         return false;
      }
   }
   return true;
};

function validatetitle(poControlName, excludeChar)
{
   var ctrl = window.document.getElementById(poControlName);
   var ctrlValue = ctrl.value;

   var iChars = "~`$%^&;{}|<>";

   if(excludeChar)
   {
      for (var i = 0; i < excludeChar.length; i ++ )
      {
         iChars = iChars.replace(excludeChar.charAt(i), '');
      }
   }
   for (var i = 0; i < ctrlValue.length; i ++ )
   {
      if (iChars.indexOf(ctrlValue.charAt(i)) != - 1)
      {
         if ( focusControl == null && ctrl != undefined)
         {
            focusControl = ctrl;
            if(focusControl.style.visibility == "visible" || focusControl.style.visibility == "")
            {
               focusControl.select();
               focusControl.focus();
            }
         }
         return false;
      }
   }
   return true;
};

// Dipti end
