/**
 * Date validation with Javascript;
 * Author 	: I Kadek Budi Antara <boeddy1908@yahoo.com>;
 * Feature	: Handling date validation dynamicaly including leap year;
 * Usage	: create 3 selects for holding year, month, date and named it respectively (e.g "cYear","cMonth","cDate");
 *			  call chkDate function via onChange eventHandler within each 3 selects with parameter: 
 *			  document.formName.yearSelectName, document.formName.monthSelectName, document.formName.dateSelectName;
 *			  formName 			refers to your <form> name properties;
 *			  yearSelectName 	refers to your <select> name properties which handle year input;
 *			  monthSelectName 	refers to your <select> name properties which handle month input, (1-12);
 *			  dateSelectName 	refers to your <select> name properties which handle date input, (1-31);
 * --------------------------------------------------------------------------------------------------------------------;
 * Attention : You may use, redistribute, modify this script, with no warranty, if only you include the information above;
 */
function isLeap(yy){
	var year = parseInt(yy);
	if( ((year%4 == 0) && (year%100 != 0)) || (year%400==0) ){
		return true;
	}else{
		return false;
	}
}

function chkDate(y, m, d){
	var yy 	= parseInt(y.options[y.selectedIndex].value);	
	var mm 	= parseInt(m.options[m.selectedIndex].value);
	var dd 	= parseInt(d.options[d.selectedIndex].value);		
	if(mm==2){
		if(isLeap(yy)){
			d.options[30]=null;
			d.options[29]=null;
			if (d.options[28]==null) {d.options[28] = new Option("29","29");}
			if(dd >29){								
				d.options.selectedIndex=28;
			}						
		}else{
			d.options[30]=null;
			d.options[29]=null;
			d.options[28]=null;
			if(dd>28){
				d.options.selectedIndex=27;
			}						
		}
	}else if(mm==4 || mm==6 || mm==9 || mm==11){
		d.options[30]=null;
		if(d.options[28]==null) {d.options[28] = new Option("29","29");}
		if(d.options[29]==null) {d.options[29] = new Option("30","30");}
		if(dd > 30){			
			d.options.selectedIndex=29;
		}		
	}else{		
		if(d.options[28]==null) {d.options[28] = new Option("29","29");}
		if(d.options[29]==null) {d.options[29] = new Option("30","30");}
		if(d.options[30]==null) {d.options[30] = new Option("31","31");}		
	}						
}

function showClientDate(){
	var currDate= new Date();
	var arrD	= new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
	var arrM	= new Array("January","February","March","April","May","June","July","August","September","October","November","December");
	var year 	= currDate.getYear();
	if(year<1000){
		year += 1900;
	}
	return ( arrD[currDate.getDay()]+", "+arrM[currDate.getMonth()]+" "+currDate.getDate()+", "+year);
}