/*================================================================

	common.js

			copyright (c) groovesync, inc  All Rights Reserved. 

================================================================*/

document.write('<script type="text/javascript" src="'+src+'jquery.js"></script>');
document.write('<script type="text/javascript" src="'+src+'gsSugar.js"></script>');

window.onload = function(){
	
	now  = new Date();
	year = now.getFullYear();
	addYearOptions('byear', year - 130, year);

	if(document.getElementById('bday')){
		checkDateOptions();
	}
	$("#byear").change(function(){
		checkDateOptions();
	});
	$("#bmonth").change(function(){
		checkDateOptions();
	});



	birthday = document.getElementById('birthday');
	if(birthday){
		var birthday_split = birthday.value.split("-");
		$("#byear").val(birthday_split[0]);
		$("#bmonth").val(birthday_split[1]);
		$("#bday").val(birthday_split[2]);
	}

}

function addYearOptions(year_id, start, end){

	var target = document.getElementById(year_id);
	if(target && target.type != 'hidden'){
		j = target.length;

		for(i = end; i > start; i--){
			var elem = document.createElement("option");
			elem.text = i;
			elem.value = i;
			target.options[j] = elem;
			j++;
		}
	}

}

function checkDateOptions(){
	year = $("#byear").val();
	month = $("#bmonth").val();

	if(month == 2){
		if(year%4 == 0 && year%100 != 0){
			fixDayOptions(29);
		}
		else{
			fixDayOptions(28);
		}

	}
	else if(month == 4 || month == 6 || month == 9 || month == 11){
		fixDayOptions(30);
	}
	else{
		fixDayOptions(31);
	}
	return;

}

function fixDayOptions(max_day){
	count = document.getElementById('bday').length;


        if(document.getElementById('bday').options[0].value == ''){
            offset = 1;
        }
	else{
	    offset = 0;
	}

	if(max_day < count){
		for(i = count; i > max_day; i--){
			document.getElementById('bday').options[i + offset - 1] = null;
		}
	}
	else if(max_day > count){

		for(i = count; i <= max_day; i++){
			var elem = document.createElement("option");
			elem.text = i;
			elem.value = i;
			document.getElementById('bday').options[i + offset - 1] = elem;
		}

	}

	return;
}
