﻿var dateType;

function CheckDate(source,args)
{
    args.IsValid = false;
    if(document.getElementById(source.id + "_tb"))
        dateType = parseInt(document.getElementById(source.id + "_tb").DateType);
    else
        dateType = parseInt(document.getElementById("ctl00_cntContent_" + source.id + "_tb").DateType);
    var d = ValDate(args.Value)
    if (d != '') 
    {
        document.getElementById(source.id + "_tb").value = d;
        args.IsValid = true;
    }
}

function ICheckDate(source,args)
{
    args.IsValid = false;
    if(document.getElementById(source.id + "_tb"))
        dateType = parseInt(document.getElementById(source.id + "_tb").DateType);
    else
        dateType = parseInt(document.getElementById("ctl00_cntContent_" + source.id + "_tb").DateType);
    var d = IValDate(args.Value)
    if (d != '') 
    {
        document.getElementById(source.id + "_tb").value = d;
        args.IsValid = true;
    }
}

function  IValDate(Dt)
{
	var i;
	var yyyy=mm=dd='';
	var y,m,d;
	var err;
	y=m=d=0;
	if ((Dt.length > 10) || (Dt.length < 6) )
		return '';
	i=0;
	err=false;
	while ((i<(Dt).length) && (Dt.charAt(i)!='/') && (i<2 ) )
	{
		dd += Dt.charAt(i);
		i++;
	}
	i++;
	while ((i<(Dt).length) && (Dt.charAt(i)!='/') )
	{
		mm += Dt.charAt(i);
		i++;
	}
	i++;
	while ((i<(Dt).length) && (Dt.charAt(i)!='/'))
	{
		yyyy += Dt.charAt(i);
		i++;
	}
	try
	{
		y = parseInt(yyyy,10);
		m = parseInt(mm,10);
		d = parseInt(dd,10);
	}
	catch(myerr)
	{
		err = true;
	}
	if (err)
		return '';
	else
	{
		if (yyyy.length == 2)
		    if(dateType==0)
			    y += 1300;
			else if(dateType==1)
			    y+=2000;
			else if(dateType==2)
			    y+=1400;
		if (((y<1000) || (y>2500 )) || ((m<1) || (m>12)) || ((d<1) || (d>31)))
			return '';
		else
		{
			Dt = '' ;
			
			if (dd.length ==1)
				Dt = '0' + dd;
			else
				Dt = dd;

			if (mm.length == 1)
				Dt = Dt+ '/0' + mm;
			else
				Dt = Dt + '/' + mm;

			if (yyyy.length == 2)
			{
			    if(dateType==0)
				    Dt = Dt + '/13' + yyyy;
				else if (dateType==1)
				    Dt = Dt + '/20' + yyyy;
				else if (dateType==2)
				    Dt = Dt + '/14' + yyyy;
    		}
    		else
				Dt = Dt + '/' + yyyy;

			return Dt;
		}
	}
}

function ValDate(Dt)
{
	var i;
	var yyyy,mm,dd;
	var y,m,d;
	var err;
	y=m=d=0;
	if ((Dt.Length > 10) || (Dt.length <6) )
		return '';
	i=0;
	yyyy='';
	mm='';
	dd='';
	err=false;
	while ((i<Dt.length) && (i<4 ) && (Dt.charAt(i)!= '/'))
	{
		yyyy += Dt.charAt(i);
		i++;
	}
	i++;
	while ((i<(Dt).length) && (Dt.charAt(i)!='/') )
	{
		mm += Dt.charAt(i);
		i++;
	}
	i++;
	while ((i<(Dt).length) && (Dt.charAt(i)!='/') )
	{
		dd += Dt.charAt(i);
		i++;
	}
	try
	{
		y = parseInt(yyyy,10);
		m = parseInt(mm,10);
		d = parseInt(dd,10);
	}
	catch(myerr)
	{
		err = true;
	}

	if (err)
		return '';
	else
	{
		if (yyyy.length == 2)
		    if(dateType==0)
			    y += 1300;
			else if(dateType==1)
			    y+=2000;
			else if(dateType==2)
			    y+=1400;
		if (((y<1000) || (y>2500 )) || ((m<1) || (m>12)) || ((d<1) || (d>31)))
			return '';
		else
		{
			Dt = '' ;
			if (yyyy.length == 2)
			{
			    if(dateType==0)
    				Dt = '13'+ yyyy;
				else if (dateType==1)
    				Dt = '20'+ yyyy;
				else if (dateType==2)
    				Dt = '14'+ yyyy;
    		}
			else
				Dt = yyyy;

			if (mm.length == 1)
				Dt = Dt+'/0' + mm;
			else
				Dt = Dt+'/' + mm;

			if (dd.length ==1)
				Dt = Dt+'/0' + dd;
			else
				Dt = Dt+'/' + dd;
			return Dt;
		}
	}
}




