
function init_financial_date(myname){
	document[fc][myname] = new financial_date_obj(myname);
}

function financial_date_obj(myname){
	this.id = myname;
	this.type="financial date";
	this.obj_name = fc + '[\"' + this.id + '\"]';
	
	var temp = ['rept_type','year','quarter','start','finish'];
	
	for(var i in temp){
		var x = temp[i];
		this[x] = new getObj(myname + "_" + x);
		this[x + "_row"] = new getObj(myname + "_" + x + "_row");
	}
	this.input = new getObj(this.id);	
	this.update_type = financial_date_update;
	this.onsubmit = financial_date_onsubmit;
	this.refresh = financial_date_onsubmit;
	this.set = financial_date_set;
	
	this.update_type();
	this.refresh();
}

function financial_date_update(){
	var mytype = this['rept_type'].obj.value;
	switch(mytype){
		case "FY":
		case "CY":
			this.year_row.style.display = "";
			this.quarter_row.style.display = "none";
			this.start_row.style.display = "none";
			this.finish_row.style.display = "none";
			break;
		case "Quarterly":
			this.year_row.style.display = "";
			this.quarter_row.style.display = "";
			this.start_row.style.display = "none";
			this.finish_row.style.display = "none";
			break;
		case "Specify Dates":
			this.year_row.style.display = "none";
			this.quarter_row.style.display = "none";
			this.start_row.style.display = "";
			this.finish_row.style.display = "";
			break;
	}
}

function financial_date_set(x){
	var myvals = this.input.obj.value.split(",");
	this.rept_type.obj.value = myvals[0];
	this.year.obj.value = myvals[1];	
	this.quarter.obj.value = myvals[2];	
	this.start.obj.value = myvals[3];	
	this.finish.obj.value = myvals[4];	
}

function financial_date_onsubmit(){
//	alert('onsubmit');
	switch(this.rept_type.obj.value){
		case "FY":
			this.start.obj.value = (this.year.obj.value - 1) + "-07-01";
			this.finish.obj.value = this.year.obj.value + "-06-30";
			break;
		case "CY":
			this.start.obj.value = this.year.obj.value + "-01-01";
			this.finish.obj.value = this.year.obj.value + "-12-31";
			break;
		case "Quarterly":
			var myyear = this.year.obj.value - (this.quarter.obj.value < 3 ? 1 : 0);
//			alert("myyear" + "-" + this.quarter.obj.value);
			switch(this.quarter.obj.value){
				case "1":
					this.start.obj.value = myyear + "-07-01";
					this.finish.obj.value = myyear + "-09-30";
					break;					
				case "2":
					this.start.obj.value = myyear + "-10-01";
					this.finish.obj.value = myyear + "-12-31";
					break;					
				case "3":
					this.start.obj.value = myyear + "-01-01";
					this.finish.obj.value = myyear + "-03-31";
					break;					
				case "4":
					this.start.obj.value = myyear + "-04-01";
					this.finish.obj.value = myyear + "-06-30";
					break;					
			}
			break;
		default:
			break;
	}
	this.input.obj.value = this.rept_type.obj.value + "," + this.year.obj.value + "," + this.quarter.obj.value + "," + this.start.obj.value + "," + this.finish.obj.value;
}