function chkLeapYear(x)
{
        if(x/400 == parseInt(x/400))
                return(true);
        else if(x/100 == parseInt(x/100))
                return(false);
        else if(x/4 == parseInt(x/4))
                return(true);
        else
                return(false);  
}

function chkDate(YYYY, MM, DD)
{
        Err = 0;
	if (YYYY == 0)
	  return false;

        if(MM <= 12 && MM >= 1)
        {
          if(MM==1 || MM==3 || MM==5 || MM==7 || MM==8 || MM==10 || MM==12)
	  {
            if(DD > 31 || DD < 1)
              Err = 1;
       	  }
       	  else if(MM == 2)
          {
            if(chkLeapYear(YYYY))
            {
               if(DD > 29 || DD < 1)
                   Err = 1;
            }
            else
            {
              if(DD > 28 || DD < 1)
                  Err = 1;
            }
          } 
          else
          {
            if(DD > 30 || DD < 1)
               Err = 1;
          }
       }
       else //MM = 0 (User not selected the date)
        Err = 1;
         
       if (Err == 1)
	  return false;
       else
	  return true;
}


function chkDateComparsion(Y1, M1, D1, Y2, M2, D2)
{
	//date1 : ToDate
	if (D1 < 10)
	  D1 = "0"+D1;
	if (D2 < 10)
	  D2 = "0"+D2;
        if (M1 < 10)
	  M1 = "0"+M1;
	if (M2 < 10)
	  M2 = "0"+M2;

	date1 = Y1 + M1 + D1;
	//date2 : FromDate
        date2 = Y2 + M2 + D2;
        if (date1 >= date2)
                return true;
        else
                return false;

}

//------------------------------------------------------------------------
// add a space behind a chinese character.
function appendSpace(str)
{
        var result = "";
        for (i = 0; i < str.length; i++)
        {
                ch = str.charAt(i);
                nextch = str.charAt(i+1);
                chCode = str.charCodeAt(i);
                result += ch;
                if (chCode >= 0 && chCode <= 255);      // within ASCII CODE (0-255)
                else if (nextch != ' ')
                        result += ' ';
        }
        return result;
}  // end function appendSpace(str) 
