﻿/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;
var centuryLastYear=50;

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,10)
	day=parseInt(strDay,10)
	year=parseInt(strYr,10)
	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 ==2)
	{
		if (year>centuryLastYear)
		{
			year = 1900 + year;
		}
		else
		{
			year = 2000 + year;
		}
	}
	if ((strYear.length !=2 && 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 isDateMonth(dtStr){
//alert(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(pos1+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,10)
	//day=parseInt(strDay,10)
	year=parseInt(strYr,10)
	if (pos1==-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 ==2)
	{
		if (year>centuryLastYear)
		{
			year = 1900 + year;
		}
		else
		{
			year = 2000 + year;
		}
	}
	if ((strYear.length !=2 ) || year==0 || year<minYear || year>maxYear){
		//alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos1+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		//alert("Please enter a valid date")
		return false
	}
return true
}
/*
==================================================================
LTrim(string) : Returns a copy of a string without leading spaces.
==================================================================
*/
function LTrim(str)
/*
   PURPOSE: Remove leading blanks from our string.
   IN: str - the string we want to LTrim
*/
{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
      // We have a string with leading blank(s)...

      var j=0, i = s.length;

      // Iterate from the far left of string until we
      // don't have any more whitespace...
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;

      // Get the substring from the first non-whitespace
      // character to the end of the string...
      s = s.substring(j, i);
   }
   return s;
}

/*
==================================================================
RTrim(string) : Returns a copy of a string without trailing spaces.
==================================================================
*/
function RTrim(str)
/*
   PURPOSE: Remove trailing blanks from our string.
   IN: str - the string we want to RTrim

*/
{
   // We don't want to trip JUST spaces, but also tabs,
   // line feeds, etc.  Add anything else you want to
   // "trim" here in Whitespace
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      // We have a string with trailing blank(s)...

      var i = s.length - 1;       // Get length of string

      // Iterate from the far right of string until we
      // don't have any more whitespace...
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;


      // Get the substring from the front of the string to
      // where the last non-whitespace character is...
      s = s.substring(0, i+1);
   }

   return s;
}

/*
=============================================================
Trim(string) : Returns a copy of a string without leading or trailing spaces
=============================================================
*/
function Trim(str)
/*
   PURPOSE: Remove trailing and leading blanks from our string.
   IN: str - the string we want to Trim

   RETVAL: A Trimmed string!
*/
{
   return RTrim(LTrim(str));
}

/* Validity Of Range Of Date - Update from Previous Version - Rizwan Ahmed*/
function isValidRange(txtFromDate, txtToDate) {
	
	var from = Trim(txtFromDate.value);
	var to = Trim(txtToDate.value);

	if(from=='') {
		alert('From Date is missing.');
		txtFromDate.focus();
		return false;
	}

	if(to=='') {
		alert('To Date is missing.');
		txtToDate.focus();
		return false;
	}

	var fromDate;
	var toDate;
	
	var fromMonth;
	var fromDay;
	var fromYear;
	
	var toMonth;
	var toDay;
	var toYear;
	
	var str;
	var i;
	var count;
	i=0;

	// From Date String Parsing to Get day, month and year	
	count=0;
	str="";
	for (i=0;i<from.length;i++)
	{
		if (from.charAt(i)=='/' || i==from.length-1) 
		 {
		  if (count==0)
			fromMonth = str
		  else if (count==1)   
		  	fromDay = str
		  else if (i==from.length-1)   
		  	fromYear = str + "" + from.charAt(i)  	
		  count = count + 1
		  str = ""
		 }
		else
			str = str + "" + from.charAt(i)
	}
	
	//to date parsing to get month, day and year from string
	count = 0;
	str = "";
	for (i=0;i<to.length;i++)
	{
		if (to.charAt(i)=='/' || i==to.length-1) 
		 {
		  if (count==0)
			toMonth = str
		  else if (count==1)   
		  	toDay = str
		  else if (i==to.length-1)   
		  	toYear = str + "" + to.charAt(i)  	
		  count = count + 1
		  str = ""
		 }
		else
			str = str + "" + to.charAt(i)
	}
	
	// radix = 10 for values in base 10, reason: when number is 8 or greater than it parse int returns 0
	fromDay = parseInt(fromDay,10)
	fromMonth= parseInt(fromMonth,10)
	toDay = parseInt(toDay,10)
	toMonth= parseInt(toMonth,10)
	
	

	if (fromDay < 10)
	 fromDay = "0" + "" + fromDay
 	
	if (fromMonth < 10)
	 fromMonth = "0" + "" + fromMonth 
	
	if (toDay < 10)
	 toDay= "0" + "" +  toDay
 	
	if (toMonth < 10)
	 toMonth = "0" + "" + toMonth 
			
		
	fromDate = fromYear + fromMonth + fromDay;
	toDate = toYear + toMonth + toDay;
	
		
	if(fromDate > toDate) {
		alert("'From' date is greater than 'To' date.");
		txtFromDate.focus();
		txtFromDate.select();
		return false;
	}
	return true;
} // end of function date range

 
 
 function CompareDates(txtFromDate, txtToDate) {
	
	var from = Trim(txtFromDate);
	var to = Trim(txtToDate);
	//alert(from);
	//alert(to);

	var fromDate;
	var toDate;
	
	var fromMonth;
	var fromDay;
	var fromYear;
	
	var toMonth;
	var toDay;
	var toYear;
	
	var str;
	var i;
	var count;
	i=0;

	// From Date String Parsing to Get day, month and year	
	count=0;
	str="";
	for (i=0;i<from.length;i++)
	{
		if (from.charAt(i)=='/' || i==from.length-1) 
		 {
		  if (count==0)
			fromMonth = str
		  else if (count==1)   
		  	fromDay = str
		  else if (i==from.length-1)   
		  	fromYear = str + "" + from.charAt(i)  	
		  count = count + 1
		  str = ""
		 }
		else
			str = str + "" + from.charAt(i)
	}
	
	//to date parsing to get month, day and year from string
	count = 0;
	str = "";
	for (i=0;i<to.length;i++)
	{
		if (to.charAt(i)=='/' || i==to.length-1) 
		 {
		  if (count==0)
			toMonth = str
		  else if (count==1)   
		  	toDay = str
		  else if (i==to.length-1)   
		  	toYear = str + "" + to.charAt(i)  	
		  count = count + 1
		  str = ""
		 }
		else
			str = str + "" + to.charAt(i)
	}
	
	//alert(fromMonth);
	//alert(fromMonth);	
	fromDay = new Number(fromDay)
	fromMonth= new Number(fromMonth)
	fromYear= new Number(fromYear)
	toDay = new Number(toDay)
	toMonth= new Number(toMonth)
	toYear= new Number(toYear)

	/*if (fromDay < 10)
	 fromDay = "0" + fromDay
 	
	if (fromMonth < 10)
	 fromMonth = "0" + fromMonth 
	
	if (toDay < 10)
	 toDay= "0" + toDay
 	
	if (toMonth < 10)
	 toMonth = "0" + toMonth 
			
	fromDate = fromYear + fromMonth + fromDay;
	toDate = toYear + toMonth + toDay;*/
	
	//alert(fromYear + ' ' + fromMonth + ' ' + fromDay);
	//alert(toYear + ' ' + toMonth + ' ' + toDay);
	
	if (fromYear<100)
	{
		if (fromYear>centuryLastYear)
		{
			fromYear = 1900 + fromYear;
		}
		else 
		{
			fromYear = 2000 + fromYear;
		}
	}
	if (toYear<100)
	{
		if (toYear>centuryLastYear)
		{
			toYear = 1900 + toYear;
		}
		else 
		{
			toYear = 2000 + toYear;
		}
	}
	//alert(fromYear + ' ' + toYear);
		
	if (fromYear > toYear)	{
		
		return 1
	}
	else if (fromYear < toYear)	{
		return -1
	}
	else {
		if (fromMonth > toMonth) {
			return 1
		}
		else if (fromMonth < toMonth)	{
			return -1
		}
		else {
			if (fromDay > toDay) {
				return 1
			}
			else if (fromDay < toDay)	{
				return -1
			}
			else {
				return 0
			}
		}
	}
} // end of function date range




/**
 * DHTML phone number validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

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++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
	
}

/*function ValidateForm(){
	var Phone=document.frmSample.txtPhone
	
	if ((Phone.value==null)||(Phone.value=="")){
		alert("Please Enter your Phone Number")
		Phone.focus()
		return false
	}
	if (checkInternationalPhone(Phone.value)==false){
		alert("Please Enter a Valid Phone Number")
		Phone.value=""
		Phone.focus()
		return false
	}
	return true
 }*/

/*
----------------------------------------------------------
Author:			Tariq Yousaf
Date:			March 8, 2006
Description:	Calculates the age from  date of birth based on an absolute year (usually today.Year). 
	
*/
function getAgeInYears(strDateOfBirth, strAbsoluteYear)
{
	var birthYear;
	var absoluteYear;
	
	if(!isDate(strDateOfBirth))
		return "";
	if(!isInteger(strAbsoluteYear))
		return "";
			
	//else case
	
	var pos1=strDateOfBirth.indexOf(dtCh)
	var pos2=strDateOfBirth.indexOf(dtCh,pos1+1)
	var strYear=strDateOfBirth.substring(pos2+1)
	birthYear = parseInt(strYear,10);
	absoluteYear = parseInt(strAbsoluteYear,10);
	
	if(birthYear>absoluteYear || absoluteYear>2100 || absoluteYear<1900)
		return "";
	
	//else case
	return (absoluteYear - birthYear);
}


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,10)
	day=parseInt(strDay,10)
	year=parseInt(strYr,10)
	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 ==2)
	{
		if (year>centuryLastYear)
		{
			year = 1900 + year;
		}
		else
		{
			year = 2000 + year;
		}
	}
	if ((strYear.length !=2 && 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 DisableButton() {
if (Page_ClientValidate()==true){
				document.forms[0].submit();
				window.setTimeout("disableButton('" + window.event.srcElement.id + "')", 0);
				}
			}

			function disableButton(buttonID) {
			document.getElementById(buttonID).disabled=true;
			}
