// JavaScript Document

// Date Validation Javascript
// copyright 30th October 2004, by Stephen Chapman
// http://javascript.about.com

// You have permission to copy and use this javascript provided that
// the content of the script is not changed in any way.

function valDateFmt(datefmt, ivlNo) {
	myOption = -1;
	for (i=0; i<datefmt.length; i++) {if (datefmt[i].checked) {myOption = i;}}
	if (myOption == -1) {
		//alert("You must select a date format");
		alert(altMsgs[ivlNo]);
		return ' ';
		}
	return datefmt[myOption].value;
}
function valDateRng(daterng, ivlNo) {
	myOption = -1;
	for (i=0; i<daterng.length; i++) {if (daterng[i].checked) {myOption = i;}}
	if (myOption == -1) {
		//alert("You must select a date range");
		alert(altMsgs[ivlNo]);
		return ' ';
	}
	return daterng[myOption].value;
}
function stripBlanks(fld) {
	var c = "";
	var result = "";for (i=0; i<fld.length; i++) {
	if (fld.charAt(i) != " " || c > 0) {result += fld.charAt(i);
	if (fld.charAt(i) != " ") c = result.length;}}return result.substr(0,c);
}
var numb = '0123456789';
function isValid(parm,val) {
	if (parm == "") return true;
	for (i=0; i<parm.length; i++) {if (val.indexOf(parm.charAt(i),0) == -1)
	return false;
	}
	return true;
}
function isNum(parm) {
	return isValid(parm,numb);
}
var mth = new Array(' ','january','february','march','april','may','june','july','august','september','october','november','december');
var day = new Array(31,28,31,30,31,30,31,31,30,31,30,31);

//fld - field value, fmt-Format,rng-Range
/*
	fmt 
	U - M D Y
	W - D M Y
	J - Y M D
*/
/*
	rng
	P - Past
	A - Any
	F - Future
*/

function ValidateDate(fld,fmt,rng) {
	var dd, mm, yy;
	var today = new Date;
	var t = new Date;
	fld = stripBlanks(fld);
	if (fld == '')return false;
	fld.value = FormatDate("mm/dd/yyyy", fld.value)
	var d1 = fld.split('\/');
	if (d1.length != 3) d1 = fld.split(' ');
	if (d1.length != 3)	return false;
	if (fmt == 'u' || fmt == 'U') {
		  dd = d1[1]; mm = d1[0]; yy = d1[2];}
		else if (fmt == 'j' || fmt == 'J') {
		  dd = d1[2]; mm = d1[1]; yy = d1[0];}
		else if (fmt == 'w' || fmt == 'W'){
		  dd = d1[0]; mm = d1[1]; yy = d1[2];}
		else return false;
	var n = dd.lastIndexOf('st');
	if (n > -1) dd = dd.substr(0,n);
	n = dd.lastIndexOf('nd');
	if (n > -1) dd = dd.substr(0,n);
	n = dd.lastIndexOf('rd');
	if (n > -1) dd = dd.substr(0,n);
	n = dd.lastIndexOf('th');
	if (n > -1) dd = dd.substr(0,n);
	n = dd.lastIndexOf(',');
	if (n > -1) dd = dd.substr(0,n);
	n = mm.lastIndexOf(',');
	if (n > -1) mm = mm.substr(0,n);
	if (!isNum(dd)) return false;
	if (!isNum(yy)) return false;
	if (!isNum(mm)) {
  		var nn = mm.toLowerCase();
  		for (var i=1; i < 13; i++) {
    	if (nn == mth[i] ||
        nn == mth[i].substr(0,3)) {mm = i; i = 13;
			}
  		}
	}
	if (!isNum(mm)) return false;
	dd = parseFloat(dd); mm = parseFloat(mm); yy = parseFloat(yy);
	if (isNaN(dd)) return false;
	if (isNaN(mm)) return false;
	if (isNaN(yy)) return false;
	if (yy < 100) yy += 2000;
	if (yy < 1582 || yy > 4881) return false;
	if (mm == 2 && (yy%400 == 0 || (yy%4 == 0 && yy%100 != 0))) day[mm-1]++;
	if (mm < 1 || mm > 12) return false;
	if (dd < 1 || dd > day[mm-1]) return false;
	t.setDate(dd); t.setMonth(mm-1); t.setFullYear(yy);
	if (rng == 'p' || rng == 'P') 
	{
		if (t > today) 
		return false;
	}
	else if (rng == 'f' || rng == 'F') 
	{
		if (t < today) return false;
	}
	else if (rng != 'a' && rng != 'A') return false;
		return true;
}

function IsValidTime(timeStr, ivlNo1, ivlNo2, ivlNo3, ivlNo4, ivlNo5, ivlNo6, ivlNo7) 
{
	// Checks if time is in HH:MM:SS AM/PM format.
	// The seconds and AM/PM are optional.

	var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;

	var matchArray = timeStr.match(timePat);
	if (matchArray == null) 
	{
		//alert("Time is not in a valid format.");
		alert(altMsgs[ivlNo1]);
		return false;
	}
	hour = matchArray[1];
	minute = matchArray[2];
	second = matchArray[4];
	ampm = matchArray[6];

	if (second=="") { second = null; }
	if (ampm=="") { ampm = null }
	
	if (hour < 0  || hour > 23)
	{
		//alert("Hour must be between 1 and 12 or (0 to 23)");
		alert(altMsgs[ivlNo2]);
		return false;
	}
	if (hour <= 12 && ampm == null) 
	{
//		if (confirm("Please indicate which time format you are using. Press OK = Standard Time 12 Hrs, CANCEL = 24 Hrs Time")) 
		if (confirm(altMsgs[ivlNo7]))
		{
			//alert("You must specify AM or PM.");
			alert(altMsgs[ivlNo3]);
			return false;
		}
	}
	if  (hour > 12 && ampm != null) 
	{
//		alert("You can't specify AM or PM for 24 Hrs format.");
		alert(altMsgs[ivlNo4]);
		return false;
	}
	if (minute<0 || minute > 59) 
	{
//		alert ("Minute must be between 0 and 59.");
		alert(altMsgs[ivlNo5]);
		return false;
	}
	if (second != null && (second < 0 || second > 59)) 
	{
//		alert ("Second must be between 0 and 59.");
		alert(altMsgs[ivlNo6]);
		return false;
	}
	return false;
}

function ValidateStartDate(fld, ivlNo)
{
    var d = new Date();
    var mon = d.getMonth();
	mon = parseInt(mon) + 1;
    var fdate= mon + "/" + d.getDate() + "/" + d.getFullYear();
    //fdate = FormatDate(fdate, "mm/dd/yyyy");	
    if (new Date(fld.value) < new Date(fdate)) 
    {
        //alert('Start should be Greater than current date');
        alert(altMsgs[ivlNo]);
        fld.focus();
        return false;
    }
    return true;
}
//fld1 is Start Date
//fld2 is End Date
function ValidateBetweenDates(fld1, fld2, ivlNo)
{
    if (new Date(fld2.value) < new Date(fld1.value))
    {
        //alert('End Date should be greater than start date');
        alert(altMsgs[ivlNo]);
        return false;
    }
    return true;
}

function ValidateBetweenD(fld1, fld2, ivlNo)
{
    if (new Date(fld2.value) <= new Date(fld1.value))
    {
        //alert('End Date should be greater than start date');
        alert(altMsgs[ivlNo]);
        return false;
    }
    return true;
}

function FormatDate(DateToFormat,FormatAs)
{
if(DateToFormat==""){return"";}
if(!FormatAs){FormatAs="dd/mm/yyyy";}
var strReturnDate;
FormatAs = FormatAs.toLowerCase();
DateToFormat = DateToFormat.toLowerCase();
var arrDate
var arrMonths = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var strMONTH;
var Separator;

while(DateToFormat.indexOf("st")>-1){
DateToFormat = DateToFormat.replace("st","");
}

while(DateToFormat.indexOf("nd")>-1){
DateToFormat = DateToFormat.replace("nd","");
}

while(DateToFormat.indexOf("rd")>-1){
DateToFormat = DateToFormat.replace("rd","");
}

while(DateToFormat.indexOf("th")>-1){
DateToFormat = DateToFormat.replace("th","");
}

if(DateToFormat.indexOf(".")>-1){
Separator = ".";
}

if(DateToFormat.indexOf("-")>-1){
Separator = "-";
}


if(DateToFormat.indexOf("/")>-1){
Separator = "/";
}

if(DateToFormat.indexOf(" ")>-1){
Separator = " ";
}

arrDate = DateToFormat.split(Separator);
DateToFormat = "";
	for(var iSD = 0;iSD < arrDate.length;iSD++){
		if(arrDate[iSD]!=""){
		DateToFormat += arrDate[iSD] + Separator;
		}
	}
DateToFormat = DateToFormat.substring(0,DateToFormat.length-1);
arrDate = DateToFormat.split(Separator);

if(arrDate.length < 3){
return "";
}

var DAY = arrDate[0];
var MONTH = arrDate[1];
var YEAR = arrDate[2];




if(parseFloat(arrDate[1]) > 12){
DAY = arrDate[1];
MONTH = arrDate[0];
}

if(parseFloat(DAY) && DAY.toString().length==4){
YEAR = arrDate[0];
DAY = arrDate[2];
MONTH = arrDate[1];
}


for(var iSD = 0;iSD < arrMonths.length;iSD++){
var ShortMonth = arrMonths[iSD].substring(0,3).toLowerCase();
var MonthPosition = DateToFormat.indexOf(ShortMonth);
	if(MonthPosition > -1){
	MONTH = iSD + 1;
		if(MonthPosition == 0){
		DAY = arrDate[1];
		YEAR = arrDate[2];
		}
	break;
	}
}

var strTemp = YEAR + "";
if(strTemp.length==2){

	if(parseFloat(YEAR)>40){
	YEAR = "19" + YEAR;
	}
	else{
	YEAR = "20" + YEAR;
	}

}


	if(parseInt(MONTH)< 10 && MONTH.toString().length < 2){
	MONTH = "0" + MONTH;
	}
	if(parseInt(DAY)< 10 && DAY.toString().length < 2){
	DAY = "0" + DAY;
	}
	switch (FormatAs){
	case "dd/mm/yyyy":
	return DAY + "/" + MONTH + "/" + YEAR;
	case "mm/dd/yyyy":
	return MONTH + "/" + DAY + "/" + YEAR;
	case "dd/mmm/yyyy":
	return DAY + " " + arrMonths[MONTH -1].substring(0,3) + " " + YEAR;
	case "mmm/dd/yyyy":
	return arrMonths[MONTH -1].substring(0,3) + " " + DAY + " " + YEAR;
	case "dd/mmmm/yyyy":
	return DAY + " " + arrMonths[MONTH -1] + " " + YEAR;	
	case "mmmm/dd/yyyy":
	return arrMonths[MONTH -1] + " " + DAY + " " + YEAR;
	}

return DAY + "/" + strMONTH + "/" + YEAR;;

} //End Function


    function isValidDate(dateStr) {
        // Checks for the following valid date formats:
        // MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
        // Also separates date into month, day, and year variables

        var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;

        // To require a 4 digit year entry, use this line instead:
        // var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;

        var matchArray = dateStr.match(datePat); // is the format ok?
        if (matchArray == null) {
        //alert("Date is not in a valid format.")
        alert("Date is not in a valid format.");
        return false;
        }
        month = matchArray[1]; // parse date into variables
        day = matchArray[3];
        year = matchArray[4];
	    if (parseInt(year) > 2100)
	    {
	        alert("hi");
	        return false;
	    }

        if (month < 1 || month > 12) { // check month range
        //alert("Month must be between 1 and 12.");
        alert("Month must be between 1 and 12.");
        return false;
        }
        if (day < 1 || day > 31) {
        //alert("Day must be between 1 and 31.");
        alert("Day must be between 1 and 31.");
        return false;
        }
        if ((month==4 || month==6 || month==9 || month==11) && day==31) {
        //alert("Month "+month+" doesn't have 31 days!")
        alert("Month "+month+" doesn't have 31 days!");
        return false
        }
        if (month == 2) { // check for february 29th
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day>29 || (day==29 && !isleap)) 
        {
        //alert("February " + year + " doesn't have " + day + " days!");
        alert("February " + year + " doesn't have " + day + " days!");
        return false;
        }

}
return true;  // date is valid
}

//This function is used to accept the Date and Time only.
function blockNonDateAndTime(obj, e, AllowDate, AllowTime)
{
	var key;
	var isCtrl = false;
	var keychar;
	var reg;
		
	if(window.event) {
		key = e.keyCode;
		isCtrl = window.event.ctrlKey
	}
	else if(e.which) {
		key = e.which;
		isCtrl = e.ctrlKey;
	}
	
	keychar = String.fromCharCode(key);
	
	// check for backspace or delete, or if Ctrl was pressed
	if (key == 8 || isCtrl)
	{
		return true;
	}
	if (AllowDate)
	{
		if (keychar == '/') return true;
	}
	if (AllowTime)
	{
		if (keychar == ':') return true;
		if (keychar == ' ') return true;	
		if (keychar == 'A' || keychar == 'a' ) return true;	
		if (keychar == 'P' || keychar == 'p' ) return true;	
		if (keychar == 'M' || keychar == 'm') return true;	
	}

	if (isNaN(key)) return true;

	reg = /\d/;
	return reg.test(keychar);
}
function FutureDateValidation(field)
{
    if(field.value !="")
            {
                var d=new Date();
		        var da,mon,yr,fdate;
		        mon=d.getMonth();
		        mon++;
                fdate=mon+"/"+d.getDate()+"/"+d.getFullYear();                
                if((Date.parse(field.value))<=(Date.parse(fdate)))
                {
                    alert(altMsgs[44]);
                    return false;
                    
                }
            }
            return true;
}
function CurrentFutureDateValidation(field)
{
    if(field.value !="")
            {
                var d=new Date();
		        var da,mon,yr,fdate;
		        mon=d.getMonth();
		        mon++;
                fdate=mon+"/"+d.getDate()+"/"+d.getFullYear();                
                if((Date.parse(field.value))<(Date.parse(fdate)))
                {
                    alert(altMsgs[44]);
                    return false;
                    
                }
            }
            return true;
}

function PastDateValidation(field)
{
    
    if(field.value !="")
            {
                var d=new Date();
		        var da,mon,yr,fdate;
		        mon=d.getMonth();
		        mon++;
                fdate=mon+"/"+d.getDate()+"/"+d.getFullYear();     
                if((Date.parse(field.value))>(Date.parse(fdate)))
                {
                    alert(altMsgs[45]);
                    return false;
                }
            }
            return true;
}

function CurrentPastDateValidation(field)
{
    
    if(field.value !="")
    {
        var d=new Date();
        var da,mon,yr,fdate;
        mon=d.getMonth();
        mon++;
        fdate=mon+"/"+d.getDate()+"/"+d.getFullYear();
                        
        if((Date.parse(field.value))>(Date.parse(fdate)))
        {
            alert(altMsgs[45]);
            return false;
        }
    }
    return true;
}


    function blockYearToYear(obj, e)
	{
		var key;
		var isCtrl = false;
		var keychar;
		var reg;
			
		if(window.event) {
			key = e.keyCode;
			isCtrl = window.event.ctrlKey
		}
		else if(e.which) {
			key = e.which;
			isCtrl = e.ctrlKey;
		}
		
		if (isNaN(key)) return true;
		
		keychar = String.fromCharCode(key);
		
		if (keychar == "-") 
		{
			if (obj.value.indexOf('-') > -1)
				return false;
			return true;
		}
		
		// check for backspace or delete, or if Ctrl was pressed
		if (key == 8 || isCtrl)
		{
			return true;
		}
	
		reg = /\d/;
					
		return reg.test(keychar);
	}

	function blockTimeToTime(obj, e)
	{
		var key;
		var isCtrl = false;
		var keychar;
		var reg;
			
		var AllowTime = true;
		if(window.event) {
			key = e.keyCode;
			isCtrl = window.event.ctrlKey
		}
		else if(e.which) {
			key = e.which;
			isCtrl = e.ctrlKey;
		}
		
		keychar = String.fromCharCode(key);
		
		// check for backspace or delete, or if Ctrl was pressed
		if (key == 8 || isCtrl)
		{
			return true;
		}
		if (AllowTime)
		{
			if (keychar == ':') return true;
			if (keychar == ' ') return true;	
			if (keychar == 'A' || keychar == 'a' ) return true;	
			if (keychar == 'P' || keychar == 'p' ) return true;	
			if (keychar == 'M' || keychar == 'm') return true;	
		}
		
		if (keychar == "-") 
		{
			if (obj.value.indexOf('-') > -1)
				return false;
			return true;
		}
		if (isNaN(key)) return true;

		reg = /\d/;
		return reg.test(keychar);
	}	
	
	
function validatetime(fld)
 {
  var strval = fld.value;
  var strval1;
    
    //minimum lenght is 6. example 1:2 AM 
    if(strval.length <= 0)
    {
        return false;
    }
    
    if(strval.length < 6)
    {
        alert("Invalid time. Time format should be HH:MM AM/PM. 03:30 AM");
        return false;
    }
  
  //Maximum length is 8. example 10:45 AM
  if(strval.length > 8)
  {
   alert("Invalid time. Time format should be HH:MM AM/PM. 04:30 AM");
   return false;
  }
  
  //Removing all space
  strval = trimAllSpace(strval); 
  
  //Checking AM/PM
  if(strval.charAt(strval.length - 1) != "M" && strval.charAt(strval.length - 1) != "m")
  {
   alert("Invalid time. Time should end with AM or PM.");
   return false;
   
  }
  else if(strval.charAt(strval.length - 2) != 'A' && strval.charAt(strval.length - 2) != 'a' && strval.charAt(strval.length - 2) != 'p' && strval.charAt(strval.length - 2) != 'P')
  {
   alert("Invalid time. Time should end with AM or PM.");
   return false;
   
  }
  
  //Give one space before AM/PM
  
  strval1 =  strval.substring(0,strval.length - 2);
  strval1 = strval1 + ' ' + strval.substring(strval.length - 2,strval.length)
  
  strval = strval1;
      
  var pos1 = strval.indexOf(':');
  fld.value = strval;
  
  if(pos1 < 0 )
  {
   alert("Invlalid time. A colon(:) is missing between hour and minute.");
   return false;
  }
  else if(pos1 > 2 || pos1 < 1)
  {
   alert("Invalid time. Time format should be HH:MM AM/PM.");
   return false;
  }
  
  //Checking hours
  var horval =  trimString(strval.substring(0,pos1));
   
  if(horval == -100)
  {
   alert("Invalid time. Hour should contain only integer value (0-11).");
   return false;
  }
      
  if(horval > 12)
  {
   alert("Invalid time. Hour can not be greater that 12.");
   return false;
  }
  else if(horval < 0)
  {
   alert("Invalid time. Hour can not be hours less than 0.");
   return false;
  }
  //Completes checking hours.
  
  //Checking minutes.
  var minval =  trimString(strval.substring(pos1+1,pos1 + 3));
  
  if(minval == -100)
  {
   alert("Invalid time. Minute should have only integer value (0-59).");
   return false;
  }
    
  if(minval > 59)
  {
     alert("Invalid time. Minute can not be more than 59.");
     return false;
  }   
  else if(minval < 0)
  {
   alert("Invalid time. Minute can not be less than 0.");
   return false;
  }
   
  //Checking minutes completed.  
  
  //Checking one space after the mintues 
  minpos = pos1 + minval.length + 1;
  if(strval.charAt(minpos) != ' ')
  {
   alert("Invalid time. Space missing after minute. Time should have HH:MM AM/PM format.");
   return false;
  }

  return true;
 }
 
 function validateTimeValue(fld)
 {
  var strval = fld;
  var strval1;
    
  //minimum lenght is 6. example 1:2 AM
  if(strval.length < 6)
  {
   alert("Invalid time. Time format should be HH:MM AM/PM. 03:30AM");
   return false;
  }
  
  //Maximum length is 8. example 10:45 AM
  if(strval.length > 8)
  {
   alert("Invalid time. Time format should be HH:MM AM/PM. 04:30AM");
   return false;
  }
  
  //Removing all space
  strval = trimAllSpace(strval); 
  
  //Checking AM/PM
  if(strval.charAt(strval.length - 1) != "M" && strval.charAt(strval.length - 1) != "m")
  {
   alert("Invalid time. Time should end with AM or PM.");
   return false;
   
  }
  else if(strval.charAt(strval.length - 2) != 'A' && strval.charAt(strval.length - 2) != 'a' && strval.charAt(strval.length - 2) != 'p' && strval.charAt(strval.length - 2) != 'P')
  {
   alert("Invalid time. Time should end with AM or PM.");
   return false;
   
  }
  
  //Give one space before AM/PM
  
  strval1 =  strval.substring(0,strval.length - 2);
  strval1 = strval1 + ' ' + strval.substring(strval.length - 2,strval.length)
  
  strval = strval1;
      
  var pos1 = strval.indexOf(':');
  fld.value = strval;
  
  if(pos1 < 0 )
  {
   alert("Invlalid time. A colon(:) is missing between hour and minute.");
   return false;
  }
  else if(pos1 > 2 || pos1 < 1)
  {
   alert("Invalid time. Time format should be HH:MM AM/PM.");
   return false;
  }
  
  //Checking hours
  var horval =  trimString(strval.substring(0,pos1));
   
  if(horval == -100)
  {
   alert("Invalid time. Hour should contain only integer value (0-11).");
   return false;
  }
      
  if(horval > 12)
  {
   alert("Invalid time. Hour can not be greater that 12.");
   return false;
  }
  else if(horval < 0)
  {
   alert("Invalid time. Hour can not be hours less than 0.");
   return false;
  }
  //Completes checking hours.
  
  //Checking minutes.
  var minval =  trimString(strval.substring(pos1+1,pos1 + 3));
  
  if(minval == -100)
  {
   alert("Invalid time. Minute should have only integer value (0-59).");
   return false;
  }
    
  if(minval > 59)
  {
     alert("Invalid time. Minute can not be more than 59.");
     return false;
  }   
  else if(minval < 0)
  {
   alert("Invalid time. Minute can not be less than 0.");
   return false;
  }
   
  //Checking minutes completed.  
  
  //Checking one space after the mintues 
  minpos = pos1 + minval.length + 1;
  if(strval.charAt(minpos) != ' ')
  {
   alert("Invalid time. Space missing after minute. Time should have HH:MM AM/PM format.");
   return false;
  }

  return true;
 }
 
 
 function trimAllSpace(str) { var str1 = ''; var i = 0; while(i != str.length) { if(str.charAt(i) != ' ') str1 = str1 + str.charAt(i); i ++; } return str1; }
 function trimString(str) { var str1 = ''; var i = 0; while ( i != str.length) { if(str.charAt(i) != ' ') str1 = str1 + str.charAt(i); i++; } var retval = IsNumeric(str1); if(retval == false) return -100; else return str1; }
 
 function IsNumeric(strString) { 
 var strValidChars = "0123456789"; 
 var strChar; 
 var blnResult = true; 
 //var strSequence = document.frmQuestionDetail.txtSequence.value; 
 // test strString consists of valid characters listed above 
 if (strString.length == 0) return false; for (i = 0; i < strString.length && blnResult == true; i++) { strChar = strString.charAt(i); if (strValidChars.indexOf(strChar) == -1) { blnResult = false; } } return blnResult; }