/**
 * 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 validate(frm, nm)
{
	y = frm[nm+"_y"];
	m = frm[nm+"_m"];
	d = frm[nm+"_d"];
	
	if(d==undefined || m==undefined || y==undefined) return;

	var yy 	= parseInt(y.options[y.selectedIndex].value);
	var mm 	= parseInt(m.options[m.selectedIndex].value);
	var dd 	= parseInt(d.options[d.selectedIndex].value);
	
	//alert(yy+"-"+mm+"-"+dd);
	
	if(mm==2){
		if(isLeap(yy)){
			d.options[31]=null;
			d.options[30]=null;
			if (d.options[29]==null) {d.options[29] = new Option("29","29");}
			if(dd >29){
				d.options.selectedIndex=29;
			}
		}else{
			d.options[31]=null;
			d.options[30]=null;
			d.options[29]=null;
			if(dd>28){
				d.options.selectedIndex=28;
			}
		}
	}else if(mm==4 || mm==6 || mm==9 || mm==11){
		d.options[31]=null;
		if(d.options[29]==null) {d.options[29] = new Option("29","29");}
		if(d.options[30]==null) {d.options[30] = new Option("30","30");}
		if(dd > 30){
			d.options.selectedIndex=30;
		}
	}else{
		if(d.options[29]==null) {d.options[29] = new Option("29","29");}
		if(d.options[30]==null) {d.options[30] = new Option("30","30");}
		if(d.options[31]==null) {d.options[31] = new Option("31","31");}
	}
}
