
function getObj(id,d)
{
	var i,x;  if(!d) d=document; 
	if(!(x=d[id])&&d.all) x=d.all[id]; 
	for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][id];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=getObj(id,d.layers[i].document);
	if(!x && document.getElementById) x=document.getElementById(id); 
	return x;
};


function showHideObject(obj, show){
	// alert(obj);
	// var keepVisible = false;

	// if (getObj(obj + "KeepVisible") != null && getObj(obj + "KeepVisible").value == 1) keepVisible = true;

	if (show == true){
		// getObj(obj).style.display = document.all ? "block" : "table-row";
		// getObj(obj).style.display = "block";
		setTimeout("showObject('" + obj + "');", 305);
	} else {
		// This is because onblur event is hiding context help fast making links in context help unclickable
		setTimeout("hideObject('" + obj + "');", 300);
		// getObj(obj).style.display = "none";
	}
};

function hideObject(obj){
	getObj(obj).style.display = "none";
}

function showObject(obj){
	getObj(obj).style.display = "block";
}

function disableEnableObject(obj, disabled){
	obj = getObj(obj);
	obj.disabled = disabled;
};

function disableEnableObjectItem(obj, itemNameIndex, nextItemIndex, disabled){
	obj = getObj(obj);
	objItem = getObj(itemNameIndex);
	
	objItem.disabled = disabled;
	if (disabled){
		objItem.style.color = "gray";
		obj.onchange = "if (this.selectedIndex || this.options[this.selectedIndex].id == " + itemNameIndex + ") { this.selectedIndex = " + nextItemIndex + " };";
	} else {
		objItem.style.color = "";
		obj.onchange = "";
	}

};

function showHideObjectItem(obj, itemId, show, noForce){
	// obj = getObj(obj);
	objItem = getObj(itemId);

	if (!objItem){	// If item doesn't exist then use cached one
		if (typeof(obj) == "string") obj = getObj(obj);
		if(document.all && !window.opera) { 
			objItem = visibilityObjsContainer[obj.id].children[itemId];
		} else {
			objItem = visibilityObjsContainer[obj.id].options[itemId];
		}
	}

	if (show){
		objItem.style.display = "";
	} else {
		objItem.style.display = "none";
	}

	if (!noForce){	// used to just once per event manage object items visibility
		manageObjectItemsVisibility(obj);
	}
}

var visibilityObjsContainer = Array();				// Copy of objects wich items visibility is dinamic
function manageObjectItemsVisibility(obj){
	if (typeof(obj) == "string") obj = getObj(obj);

	if (!visibilityObjsContainer[obj.id]){
		visibilityObjsContainer[obj.id] = obj.cloneNode(true);
	}

	visibilityObj = visibilityObjsContainer[obj.id];
	for(var optCounter = 1; optCounter < visibilityObj.length; optCounter++){	
		var visibilityOpt = visibilityObj.options[optCounter];

		if (!visibilityOpt) alert(optCounter);
		if(document.all && !window.opera) { 
			var currentOpt = obj.children[visibilityOpt.id];
		} else {
			var currentOpt = obj.options[visibilityOpt.id];
		}
		
		if (currentOpt && visibilityOpt.id == currentOpt.id){	// Ok, item shown
			visibilityObj.options[optCounter].style.display = currentOpt.style.display;
			if (currentOpt.style.display == "none"){
				obj.remove(currentOpt.index);
			}
		} else {	// Item is not shown (there is no such item in list)
			visibilityOptBefore = visibilityObj.options[optCounter - 1];
			if(document.all && !window.opera) {
				var optBefore = obj.children[visibilityOptBefore.id].nextSibling;
			} else {
				var optBefore = obj.options[visibilityOptBefore.id].nextSibling;
			}
			var cloned = visibilityObj.options[optCounter].cloneNode(true);
			obj.insertBefore(cloned, optBefore);
		}
	}
}

function form3PopulateContractSigned(contractSigned){
	var regimematr_box = 'regimeMatrBox';
	var regimematr_box1 = 'regimeMatrBox1';
	var regimematr_select_obj = 'regimeMatr';
	var regimematr_1st_option = 'regimeMatr1';
	
	if (contractSigned == true){
		showHideObject(regimematr_box, true);
		getObj(regimematr_select_obj).selectedIndex = 1;
		disableEnableObject(regimematr_select_obj, false);
		showHideObject(regimematr_box1, true);
	} else {
		showHideObject(regimematr_box, false);
		showHideObject(regimematr_box1, false);
	}
	
}

function form3PopulateMariageOutSwiss(outOfSwiss){
	var regimenotary_box = 'regimeNotaryBox';
	var regimematr_other = 'otherRegimeBox';
	var regimenotary_select_obj = 'regimeMatr';
	var regimematr_3rd_option = 'regimeMatr4';

	showHideObjectItem(regimenotary_select_obj, regimematr_3rd_option, outOfSwiss);
	showHideObject(regimenotary_box, !outOfSwiss);
	
	if (outOfSwiss){
		if (getObj(regimenotary_select_obj).options[getObj(regimenotary_select_obj).selectedIndex].id == regimematr_3rd_option) {
			showHideObject(regimematr_other, true);
		}
		else {
			showHideObject(regimematr_other, false);
			getObj(regimematr_other).text = "";
		}
	}else {
		if (getObj(regimenotary_select_obj).options[getObj(regimenotary_select_obj).selectedIndex].style.display == "block"){
			getObj(regimenotary_select_obj).selectedIndex = 0;
			showHideObject(regimematr_other, false);
		}
		getObj(regimematr_other).text = "";
	}
}

function form4PopulateIndexType(indexType){
	var indextype_box = 'indexTypeBox';
	showHideObject(indextype_box, indexType);
}

function form6PopulateProposal(selected){
	var propClasic1_box = "propClasic1Box";
	var propClasic2_box = "propClasic2Box";
	var propClasic3_box = "propClasic3Box";
	
	var exHolVisitFreq_box = "exHolVisitFreqBox";
	var exHolWorkdaysVisitFreq_box = "exHolWorkdaysVisitFreqBox";
	var exHolWeekends_box = "exHolWeekendsBox";
	var exHolWorkdays_box = "exHolWorkdaysBox";
	var hol_box = "holBox";

	var exHolWeekendsFreq = "visitFrequency";
	var exHolWorkdaysFreq = "visitWorkdaysFrequency";

	showHideObject(propClasic1_box, false);
	showHideObject(propClasic2_box, false);
	showHideObject(propClasic3_box, false);
	
	showHideObject(exHolVisitFreq_box, false);
	showHideObject(exHolWorkdaysVisitFreq_box, false);
	showHideObject(exHolWeekends_box, false);
	showHideObject(exHolWorkdays_box, false);
	
	showHideObject(hol_box, false);
	
	if (selected == 1){
		showHideObject(propClasic1_box, true);
		showHideObject(exHolWeekends_box, true);
	} else if (selected == 2){
		showHideObject(propClasic2_box, true);
		showHideObject(exHolWeekends_box, true);
		showHideObject(exHolWorkdays_box, true);
	} else if (selected == 3){
		showHideObject(propClasic3_box, true);
		showHideObject(exHolVisitFreq_box, true);
		showHideObject(exHolWorkdaysVisitFreq_box, true);
		
		showHideObject(exHolWeekends_box, (getObj(exHolWeekendsFreq).selectedIndex != 3));
		showHideObject(exHolWorkdays_box, (getObj(exHolWorkdaysFreq).selectedIndex != 3));
		showHideObject(hol_box, true);
	}
}

function form6PopulateWorkdaysPicker(whatIsChanged){
	var startDay_SelObj = getObj("exHolWorkdaysStartDay");
	var startHour_SelObj = getObj("exHolWorkdaysStartHour");
	var endDay_SelObj = getObj("exHolWorkdaysEndDay");
	var endHour_SelObj = getObj("exHolWorkdaysEndHour");
	
	startDay_SelId = startDay_SelObj.options[startDay_SelObj.selectedIndex].id;
	startHour_SelId = startHour_SelObj.options[startHour_SelObj.selectedIndex].id;
	endDay_SelId = endDay_SelObj.options[endDay_SelObj.selectedIndex].id;
	endHour_SelId = endHour_SelObj.options[endHour_SelObj.selectedIndex].id;

	manageObjectItemsVisibility(endDay_SelObj);		// Just to make sure they are cached
	manageObjectItemsVisibility(startHour_SelObj);
	manageObjectItemsVisibility(endHour_SelObj);	// Just to make sure they are cached

	if (startDay_SelId == "") {	// If start day is not selected, then allow full selection of end day
		// for(EDCounter = 1; EDCounter <= endDay_SelObj.options.length; EDCounter++){
		for(EDCounter = 1; EDCounter <= visibilityObjsContainer[endDay_SelObj.id].options.length; EDCounter++){
			// EDOption = endDay_SelObj.options[EDCounter];
			EDOption = visibilityObjsContainer[endDay_SelObj.id].options[EDCounter];
			showHideObjectItem(endDay_SelObj.id, EDOption.id, true, true);
		}
	} else if(startDay_SelObj.id == whatIsChanged) { // Otherwise allow only selection of that day and that day+1 in end day dropdown box only in case user changes start day
		// for(EDCounter = 1; EDCounter < (endDay_SelObj.options.length); EDCounter ++){
		for(EDCounter = 1; EDCounter <= visibilityObjsContainer[endDay_SelObj.id].options.length - 1; EDCounter++){
			// EDOption = endDay_SelObj.options[EDCounter];
			// alert(visibilityObjsContainer[endDay_SelObj.id].options.length);
			EDOption = visibilityObjsContainer[endDay_SelObj.id].options[EDCounter];
			if (EDCounter != startDay_SelObj.selectedIndex && EDCounter != (startDay_SelObj.selectedIndex + 1)){
				showHideObjectItem(endDay_SelObj.id, EDOption.id, false, true);
			} else {
				showHideObjectItem(endDay_SelObj.id, EDOption.id, true, true);
			}
		}
		endDay_SelObj.selectedIndex = 0;
	}

	manageObjectItemsVisibility(endDay_SelObj);

	if (startHour_SelId == "" || startDay_SelId == ""){ // If start hour or start day and end day is not selected, then allow full selection of end hour
		// for(EHCounter = 1; EHCounter <= endHour_SelObj.options.length; EHCounter++){;
		for(EHCounter = 1; EHCounter <= visibilityObjsContainer[endHour_SelObj.id].options.length; EHCounter++){;
			// EHOption = endHour_SelObj.options[EHCounter];
			EHOption = visibilityObjsContainer[endHour_SelObj.id].options[EHCounter];
			try{
				showHideObjectItem(endHour_SelObj.id, EHOption.id, true, true);
			} 
			catch(ex){}
		}
		showHideObjectItem(startHour_SelObj.id, startHour_SelObj.id + "20", true, true);	// allow 20:00 in start hour
	} else if (endHour_SelObj.id != whatIsChanged) { // Otherwise allow only specific scenarios only in case user don't changes end hour now
		if (endDay_SelObj.options[endDay_SelObj.selectedIndex].value == startDay_SelObj.options[startDay_SelObj.selectedIndex].value){	// If selection is within period of one day
			// Don't allow selection of 20:00 for a start hour bicouse selecting period 20h-20h makes no sense
			showHideObjectItem(startHour_SelObj.id, startHour_SelObj.id + "20", false, true);
			for(EHCounter = 1; EHCounter <= visibilityObjsContainer[endHour_SelObj.id].options.length - 1; EHCounter++){
				EHOption = visibilityObjsContainer[endHour_SelObj.id].options[EHCounter];
				showHideObjectItem(endHour_SelObj.id, EHOption.id, true, true);
			}
		} else if (endDay_SelObj.options[endDay_SelObj.selectedIndex].value == ((startDay_SelObj.options[startDay_SelObj.selectedIndex].value * 1) + 1)){	// If selection is within period of two days
			showHideObjectItem(startHour_SelObj.id, startHour_SelObj.id + "20", true, true);	// allow 20:00 in start hour
			// Don't allow selection of hours in end hour wich are greather then one selected in start hour
			// for(EHCounter = 1; EHCounter <= endHour_SelObj.options.length - 1; EHCounter++){
			for(EHCounter = 1; EHCounter <= visibilityObjsContainer[endHour_SelObj.id].options.length - 1; EHCounter++){
				// EHOption = endHour_SelObj.options[EHCounter];
				EHOption = visibilityObjsContainer[endHour_SelObj.id].options[EHCounter];
				if (EHCounter > startHour_SelObj.selectedIndex){
					showHideObjectItem(endHour_SelObj.id, EHOption.id, false, true);
				} else {
					showHideObjectItem(endHour_SelObj.id, EHOption.id, true, true);
				}
			}
			endHour_SelObj.selectedIndex = 0;
		} else {
		}
	}
	manageObjectItemsVisibility(startHour_SelObj);
	manageObjectItemsVisibility(endHour_SelObj);
}

function form6PopulateSummerDaysWeeks(selectedSummer){
	var summerDays_box = 'holydaysSummerDaysBox';
	var summerWeeks_box = 'holydaysSummerWeeksBox';

	if (selectedSummer == 5){
		showHideObject(summerDays_box, true);
		showHideObject(summerWeeks_box, false);
	}	else {
			showHideObject(summerDays_box, false);
			if (selectedSummer == 4){
				showHideObject(summerWeeks_box, true);
			}
	}

}


function form7PopulateContPayed(contPayedValue){
	var contPayedBy_box = 'contPayedByBox';
	var contPayed_box = 'contPayedBox';

	if (contPayedValue == 1){
		showHideObject(contPayedBy_box, true);
		showHideObject(contPayed_box, true);
	} else {
		showHideObject(contPayedBy_box, false);
		showHideObject(contPayed_box, false);
	}
}

function form7PopulateModifyByAge(agesVisible){
	var firstAmount_box = 'firstAmountBox';
	var firstAge_box = 'firstAgeBox';
	var amounts_box = 'amountsBox';
	
	var childAgeValue = 'childAge';
	
	var varDivorceChildContribAge1Value = "varDivorceChildContribAge1";
	
	if (agesVisible == true){
		showHideObject(firstAge_box, true);
		showHideObject(amounts_box, true);
		showHideObject(firstAmount_box, ((getObj(childAgeValue).value * 1) <= (getObj(varDivorceChildContribAge1Value).value * 1)));
	} else {
		showHideObject(firstAge_box, false);
		showHideObject(amounts_box, false);
		showHideObject(firstAmount_box, true);
	}
}

function form8PopulateAmountBoxes(){
	var amountDiv = getObj('amountBox');
	var modifiableDiv = getObj('modifiableBox');
	var modifiableControlledDiv = getObj('modifiableControlledBox');
	var degressiveAmountsDiv = getObj('amountsBox');
	var endDateDiv = getObj('endDateBox');
	var modifiableOption = getObj('modifiable');
	
	// If amount is modifiable then show degressive and indexation options
	if (modifiableOption[0].checked){
		showHideObject(modifiableControlledDiv.id, true);
	} else {
		showHideObject(modifiableControlledDiv.id, false);
	}
	
	// If amount is degressive modifiable then show degressive amounts and hide static amount
	if (modifiableOption[0].checked) {
		showHideObject(degressiveAmountsDiv.id, true);
		showHideObject(amountDiv.id, false);
		showHideObject(endDateDiv.id, false);
	} else {
		showHideObject(degressiveAmountsDiv.id, false);
		showHideObject(endDateDiv.id, true);
		showHideObject(amountDiv.id, true);	
	}
}

function form11PopulateCourt(courts){
	courtsObj = courts;

	for(optionsCounter = 1; courtsObj.options.length > optionsCounter; optionsCounter++){
		showHideObject("court[" + courtsObj.options[optionsCounter].value + "]", false);
	}
	if (courts.value != 0){
		showHideObject("court[" + courts.value + "]", true);
	}
}

// wichBudgetForm - can be:
//	"M" - Males after divorce,
//	"F" - Females after divorce or 
//	"" - budget form before divorce
function BudgetCalculate(wichBudgetForm){

	if (wichBudgetForm == undefined) wichBudgetForm = "";
	var bilansBox = wichBudgetForm + "Bilans";
	var bilansSum = 0;
	objBilans = getObj(bilansBox);

	// bilansSum contains balance
	// "+=" means that left side value is sumarised to the right
	// "-=" means that left side value is substracted from the right

	bilansSum += ParseAndPublicateBudgetItem(getObj("in" + wichBudgetForm + "Sallary"));
	bilansSum += ParseAndPublicateBudgetItem(getObj("in" + wichBudgetForm + "Annexes"));
	bilansSum += ParseAndPublicateBudgetItem(getObj("in" + wichBudgetForm + "Other"));
	
	bilansSum -= ParseAndPublicateBudgetItem(getObj("out" + wichBudgetForm + "FixTax"));
	bilansSum -= ParseAndPublicateBudgetItem(getObj("out" + wichBudgetForm + "FixRent"));
	bilansSum -= ParseAndPublicateBudgetItem(getObj("out" + wichBudgetForm + "FixHAssurance"));
	bilansSum -= ParseAndPublicateBudgetItem(getObj("out" + wichBudgetForm + "FixOAssurance"));
	bilansSum -= ParseAndPublicateBudgetItem(getObj("out" + wichBudgetForm + "FixEl"));
	bilansSum -= ParseAndPublicateBudgetItem(getObj("out" + wichBudgetForm + "FixTel"));
	bilansSum -= ParseAndPublicateBudgetItem(getObj("out" + wichBudgetForm + "FixCar"));
	bilansSum -= ParseAndPublicateBudgetItem(getObj("out" + wichBudgetForm + "FixPTrans"));
	bilansSum -= ParseAndPublicateBudgetItem(getObj("out" + wichBudgetForm + "FixDebts"));
	
	bilansSum -= ParseAndPublicateBudgetItem(getObj("out" + wichBudgetForm + "HouseFood"));
	bilansSum -= ParseAndPublicateBudgetItem(getObj("out" + wichBudgetForm + "HouseMaintain"));
	bilansSum -= ParseAndPublicateBudgetItem(getObj("out" + wichBudgetForm + "HouseHolydays"));
	bilansSum -= ParseAndPublicateBudgetItem(getObj("out" + wichBudgetForm + "HouseEquipment"));
	
	bilansSum -= ParseAndPublicateBudgetItem(getObj("out" + wichBudgetForm + "AdultGarderobe"));
	bilansSum -= ParseAndPublicateBudgetItem(getObj("out" + wichBudgetForm + "AdultCosmetics"));
	bilansSum -= ParseAndPublicateBudgetItem(getObj("out" + wichBudgetForm + "AdultDoctor"));
	bilansSum -= ParseAndPublicateBudgetItem(getObj("out" + wichBudgetForm + "AdultSport"));
	bilansSum -= ParseAndPublicateBudgetItem(getObj("out" + wichBudgetForm + "AdultLearning"));

	// if form calculated is budget2 then call function for child expense fields management
	if (wichBudgetForm != ""){
		// Spouses can take some of the expenses for the children
		// Budget2ManageChildFields();
	}

	bilansSum -= ParseAndPublicateBudgetItem(getObj("out" + wichBudgetForm + "ChildBabysitting"));
	bilansSum -= ParseAndPublicateBudgetItem(getObj("out" + wichBudgetForm + "ChildGarderobe"));
	bilansSum -= ParseAndPublicateBudgetItem(getObj("out" + wichBudgetForm + "ChildLearning"));
	bilansSum -= ParseAndPublicateBudgetItem(getObj("out" + wichBudgetForm + "ChildLearningEq"));
	bilansSum -= ParseAndPublicateBudgetItem(getObj("out" + wichBudgetForm + "ChildDoctor"));
	bilansSum -= ParseAndPublicateBudgetItem(getObj("out" + wichBudgetForm + "ChildDentist"));
	
	// if form calculated is budget2 then determine should contibution be payed or recieved
	if (wichBudgetForm != ""){
		var ctrbSpouse = getObj(wichBudgetForm + "ContribSpouse");
		if (ctrbSpouse[0].checked == true){
			bilansSum += ParseAndPublicateBudgetItem(getObj(wichBudgetForm + "ContribSpouseAmount"));
		} else if (ctrbSpouse[1].checked == true){
			bilansSum -= ParseAndPublicateBudgetItem(getObj(wichBudgetForm + "ContribSpouseAmount"));
		}
		
		var ctrbChildren = getObj(wichBudgetForm + "ContribChildren");
		if (ctrbChildren[0].checked == true){
			bilansSum += ParseAndPublicateBudgetItem(getObj(wichBudgetForm + "ContribChildrenAmount"));
		} else if (ctrbChildren[1].checked == true){
			bilansSum -= ParseAndPublicateBudgetItem(getObj(wichBudgetForm + "ContribChildrenAmount"));
		}
	}
	
	bilansSum -= ParseAndPublicateBudgetItem(getObj("out" + wichBudgetForm + "Other"));

	objBilans.value = bilansSum.toFixed(2);	// set bilans field value
	
	// if any form's element is changed since page is last loaded then disable print button
	ignore_fields = "btnCalculate, btnPrint";
	if (isFormModified(getObj("budget"), null, ignore_fields) == true){
		getObj("btnPrint").disabled = true;
	}
}

// Parses value from the budget's item (text field),
// checks for invalid characters (if there is any set value to zero('0')) and
// sets back into budget's item parsed value with two decimal digits
function ParseAndPublicateBudgetItem(itemObj){
	itemValue = parseFloat(isNaN(itemObj.value) ? 0:itemObj.value);
	itemValue = parseFloat(itemObj.disabled ? 0:itemValue);
	itemObj.value = itemValue.toFixed(2);
	return itemValue;
}

// manages child fields for budget2 form
function Budget2ManageChildFields(){
	if (getObj("outMChildLivewith").checked) {
		enabledMChild = true;
	} else {
		enabledMChild = false;
	}
	
	if (getObj("outFChildLivewith").checked){
		enabledFChild = true;
	} else {
		enabledFChild = false;
	}
	
	disableEnableObject("outMChildBabysitting", !enabledMChild);
	disableEnableObject("outMChildGarderobe", !enabledMChild);
	disableEnableObject("outMChildLearning", !enabledMChild);
	disableEnableObject("outMChildLearningEq", !enabledMChild);
	disableEnableObject("outMChildDoctor", !enabledMChild);
	disableEnableObject("outMChildDentist", !enabledMChild);
	
	disableEnableObject("outFChildBabysitting", !enabledFChild);
	disableEnableObject("outFChildGarderobe", !enabledFChild);
	disableEnableObject("outFChildLearning", !enabledFChild);
	disableEnableObject("outFChildLearningEq", !enabledFChild);
	disableEnableObject("outFChildDoctor", !enabledFChild);
	disableEnableObject("outFChildDentist", !enabledFChild);	
}

function showhide(layer_ref,show) {
	showHideObject(layer_ref,show);
}
function testmineur() {
	var dateActuelle = new Date();
	var birthDateday = document.getElementById("birthDateday").value;
	var birthDatemonth = document.getElementById("birthDatemonth").value;
	var birthDateyear = document.getElementById("birthDateyear").value;
	//alert ( birthDateyear + " " + (dateActuelle.getFullYear() - 18) );
	
	if(birthDateyear > (dateActuelle.getFullYear() - 18)) {
		//alert ('mineur');
		document.getElementById("parentalAuthorityBox").style.display = "block";
	} else {
		document.getElementById("parentalAuthorityBox").style.display = "none";
	}
}