var loaded = false;
var resetting = false;
var regexp_num = /\d/;
var regexp_decsep = /[\.?\,?]/;
var hiddenUndef = "0";
var respUndef = "-";

function setToday() {
	var now = new Date();
	var returning = 0;
	try {
		returning = parent.fraAction.document.frmAction.active.value;
		if (returning == 1) {
			refreshControls();
			parent.fraAction.updateLabels();
			return;
		} else {
			parent.fraAction.document.frmAction.date_2.value = now.getFullYear().toString() + '-' + (now.getMonth()+1).toString() + '-' + now.getDate().toString();
		}
	} catch (e) {}
if (returning == 0) {
		document.frmInput_2.ddlYear.value = now.getFullYear();
		document.frmInput_2.ddlMonth.value = now.getMonth() + 1;
		document.frmInput_2.ddlDay.value = now.getDate();
	}
}

function doSubmit(sender) {
	parent.fraAction.document.frmAction.active.value = "1";
	var caller = sender.form.name;
	if (caller=="frmGeneral") doSubmitGen();
	if (caller=="frmInput_1") doSubmit1();
	if (caller=="frmInput_2") doSubmit2();
}

function doSubmitGen() {
	if (!resetting) {
		try {
			var generalForm = document.frmGeneral;
			// do not set birthdate if no valid date is selected !!!!!!!
			if (generalForm.ddlDay.value==0 || generalForm.ddlMonth.value==0 || generalForm.ddlYear.value==0) {
				// reset all birthdate related data ??
				return;
			}
			if (document.frmInput_2.ddlDay.value!=0 && document.frmInput_2.ddlMonth.value!=0 && document.frmInput_2.ddlYear.value!=0) checkDates(2);
			if (document.frmInput_1.ddlDay.value!=0 && document.frmInput_1.ddlMonth.value!=0 && document.frmInput_1.ddlYear.value!=0) checkDates(1);

			var actionWin = parent.fraAction;
			var actionForm = actionWin.document.frmAction;
			actionForm.birthdate.value = generalForm.ddlYear.value + '-' + generalForm.ddlMonth.value + '-' + generalForm.ddlDay.value;
			actionForm.gender.value = generalForm.gender.value;
			actionForm.reference.value = generalForm.ddlReference.value;
			if (generalForm.ckbShort_pregnancy.checked == true) {
				actionForm.pregnancy.value = "1";
			} else {
				actionForm.pregnancy.value = "0";
			}
			if (generalForm.ckbLight_weight.checked == true) {
				actionForm.light_weight.value = "1";
			} else {
				actionForm.light_weight.value = "0";
			}
			if (generalForm.txtMotherLen.value) actionForm.mother_length.value = generalForm.txtMotherLen.value;
			else actionForm.mother_length.value = hiddenUndef;
			if (generalForm.txtFatherLen.value) actionForm.father_length.value = generalForm.txtFatherLen.value;
			else actionForm.father_length.value = hiddenUndef;

			parent.fraAction.submitGeneral();
		} catch (e) {
			reloadActionFrame();
		}
	}
}

function doSubmit1() {//submits the previous measurement data
	if (!resetting) {
		try {
			var inputForm = document.frmInput_1;
			var actionWin = parent.fraAction.document;
			// do not set previous date if no valid date is selected !!!!!!!
			if (inputForm.ddlDay.value==0 || inputForm.ddlMonth.value==0 || inputForm.ddlYear.value==0) {
				actionWin.frmAction.date_1.value = "0";
				parent.fraAction.submitInput_1();
				return;
			}
		  //  do not set previous date if invalid relative to last date and birth date
		  if (!checkDates(1)) return;
		  else {
				actionWin.frmAction.date_1.value = inputForm.ddlYear.value + '-' + inputForm.ddlMonth.value + '-' + inputForm.ddlDay.value;
				if (inputForm.txtLength.value) actionWin.frmAction.length_1.value = inputForm.txtLength.value;
				else actionWin.frmAction.length_1.value = hiddenUndef;
				if (inputForm.txtWeight.value) actionWin.frmAction.weight_1.value = inputForm.txtWeight.value;
				else actionWin.frmAction.weight_1.value = hiddenUndef;

				parent.fraAction.submitInput_1();
			}
		} catch (e) {
			reloadActionFrame();
		}
	}
}

function doSubmit2() {//submits the last measurement data
	if (!resetting) {
		try {
			var inputForm = document.frmInput_2;
			var actionWin = parent.fraAction.document;
			// do not set date if no valid date is selected !!!!!!!
			if (inputForm.ddlDay.value==0 || inputForm.ddlMonth.value==0 || inputForm.ddlYear.value==0) {
				actionWin.frmAction.date_2.value = "0";
				parent.fraAction.submitInput_2();
				return;
			}
		  //  do not set last date if invalid relative to birth date
		  if (!checkDates(2)) return;
	    else {
				actionWin.frmAction.date_2.value = inputForm.ddlYear.value + '-' + inputForm.ddlMonth.value + '-' + inputForm.ddlDay.value;
				if (inputForm.txtLength.value) actionWin.frmAction.length_2.value = inputForm.txtLength.value;
				else actionWin.frmAction.length_2.value =  hiddenUndef;
				if (inputForm.txtWeight.value) actionWin.frmAction.weight_2.value = inputForm.txtWeight.value;
				else actionWin.frmAction.weight_2.value = hiddenUndef;

				parent.fraAction.submitInput_2();
			}
		} catch (e) {
			reloadActionFrame();
		}
	}
}

function checkNumeric(sender, withdecimals) {
	var content = sender.value;
	var letter;
	var result = "";
	var decsep_found = false;
	for (var i = 0; i < content.length; i++) {
		letter = content.substr(i,1);
		if (regexp_num.test(letter)) result += letter;
		else if (withdecimals) {
			if (!decsep_found && regexp_decsep.test(letter)) {
				// ensure decimal separator is a point, not a comma
				result += '.';
				decsep_found = true;
			}
		}
	}
	sender.value = result;
}

function checkDates(idx) {
	var birth = new Date(document.frmGeneral.ddlYear.value,document.frmGeneral.ddlMonth.value-1,document.frmGeneral.ddlDay.value);
	var datePrevious = new Date(document.frmInput_1.ddlYear.value, document.frmInput_1.ddlMonth.value-1, document.frmInput_1.ddlDay.value);
	var dateLast = new Date(document.frmInput_2.ddlYear.value, document.frmInput_2.ddlMonth.value-1, document.frmInput_2.ddlDay.value);
	// if last measure date is before previous measure date show message and quit
	if (idx==1 || idx==0) {
		if ((dateLast - datePrevious) <= 0) {
			alert("De vorige meet datum is later of gelijk aan de laatste meet datum !\nKies een datum die voor die van de huidige meting valt.");
			return false;
		}
		// if age is negative show message and quit
		if ((datePrevious-birth) < 0) {
			alert("De vorige meet datum ligt voor de geboorte datum !\nKies een datum die minstens 2 weken na de geboorte valt.");
			return false;
		}
		// if age is less than 2 weeks show message and quit
		if ((datePrevious-birth) < (14*24*60*60*1000)) {
			alert("Dit kind was op de vorige meet datum jonger dan 2 weken !\nKies een datum die minstens 2 weken na de geboorte valt.");
			return false;
		}
		return true;
	}
	if (idx==2 || idx==0) {
		// if age is negative show message and quit
		if ((dateLast-birth) < 0) {
			alert("De huidige meet datum ligt voor de geboorte datum !\nKies een datum die minstens 2 weken na de geboorte valt.");
			return false;
		}
		// if age is less than 2 weeks show message and quit
		if ((dateLast-birth) < (14*24*60*60*1000)) {
			alert("Dit kind was op de huidige meet datum jonger dan 2 weken !\nKies een datum die minstens 2 weken na de geboorte valt.");
			return false;
		}
		return true;
	}
}

function resetAll() {
	resetting = true;
	var actionForm = parent.fraAction.document.frmAction;
	var now = new Date();
	// reset hidden values
	actionForm.gender.value = hiddenUndef;
	actionForm.birthdate.value = hiddenUndef;
	actionForm.reference.value = hiddenUndef;
	actionForm.pregnancy.value = hiddenUndef;
	actionForm.light_weight.value = hiddenUndef;
	actionForm.mother_length.value = hiddenUndef;
	actionForm.father_length.value = hiddenUndef;
	actionForm.date_1.value = hiddenUndef;
	actionForm.length_1.value = hiddenUndef;
	actionForm.weight_1.value = hiddenUndef;
	actionForm.date_2.value = now.getFullYear().toString() + '-' + (now.getMonth()+1).toString() + '-' + now.getDate().toString();
	actionForm.length_2.value = hiddenUndef;
	actionForm.weight_2.value = hiddenUndef;

	// reset date inputs
	document.frmGeneral.ddlYear.value = hiddenUndef;
	document.frmGeneral.ddlMonth.value = hiddenUndef;
	document.frmGeneral.ddlDay.value = hiddenUndef;
	document.frmInput_2.ddlYear.value = now.getFullYear();
	document.frmInput_2.ddlMonth.value = now.getMonth()+1;
	document.frmInput_2.ddlDay.value = now.getDate();
	document.frmInput_1.ddlYear.value = hiddenUndef;
	document.frmInput_1.ddlMonth.value = hiddenUndef;
	document.frmInput_1.ddlDay.value = hiddenUndef;
  // reset other inputs
      document.frmGeneral.gender.value = 0;
	document.getElementById("ddlReference").selectedIndex = 0;
	document.getElementById("optGender_0").checked = true;
	document.getElementById("optGender_1").checked = false;
	document.getElementById("txtMotherLen").value = "";
	document.getElementById("ckbShort_pregnancy").checked = false;
	document.getElementById("txtFatherLen").value = "";
	document.getElementById("ckbLight_weight").checked = false;
	document.getElementById("txtLength_2").value = "";
	document.getElementById("txtWeight_2").value = "";
	document.getElementById("txtLength_1").value = "";
	document.getElementById("txtWeight_1").value = "";
  // submit to reset response labels
	parent.fraAction.submitGeneral();
	resetting = false;
}

function reloadActionFrame() {
	parent.fraAction.location = "action.asp";
}

function refreshControls() {
	resetting = true;
	var actionForm = parent.fraAction.document.frmAction;
	var arrDate = actionForm.birthdate.value.split("-");
	// reset date inputs
	document.frmGeneral.ddlYear.value = arrDate[0];
	document.frmGeneral.ddlMonth.value = arrDate[1];
	document.frmGeneral.ddlDay.value = arrDate[2];
	arrDate = actionForm.date_2.value.split("-");
	document.frmInput_2.ddlYear.value = arrDate[0];
	document.frmInput_2.ddlMonth.value = arrDate[1];
	document.frmInput_2.ddlDay.value = arrDate[2];
	arrDate = actionForm.date_1.value.split("-");
	document.frmInput_1.ddlYear.value = arrDate[0];
	document.frmInput_1.ddlMonth.value = arrDate[1];
	document.frmInput_1.ddlDay.value = arrDate[2];
  // reset other inputs
  document.frmGeneral.gender.value = actionForm.gender.value;
	if (actionForm.gender.value==0) {
		document.getElementById("optGender_0").checked = true;
		document.getElementById("optGender_1").checked = false;
	} else {
		document.getElementById("optGender_0").checked = false;
		document.getElementById("optGender_1").checked = true;
	}
	if (actionForm.pregnancy.value == "1") {
		document.getElementById("ckbShort_pregnancy").checked = true;
	} else {
		document.getElementById("ckbShort_pregnancy").checked = false;
	}
	if (actionForm.light_weight.value == "1") {
		document.getElementById("ckbLight_weight").checked = true;
	} else {
		document.getElementById("ckbLight_weight").checked = false;
	}
	document.getElementById("ddlReference").selectedIndex = parseInt(actionForm.reference.value) - 1;
	if (actionForm.mother_length.value!="0")
		document.getElementById("txtMotherLen").value = actionForm.mother_length.value;
	if (actionForm.father_length.value!="0")
		document.getElementById("txtFatherLen").value = actionForm.father_length.value;
	if (actionForm.length_2.value!="0")
		document.getElementById("txtLength_2").value = actionForm.length_2.value;
	if (actionForm.weight_2.value!="0")
		document.getElementById("txtWeight_2").value = actionForm.weight_2.value;
	if (actionForm.length_1.value!="0")
		document.getElementById("txtLength_1").value = actionForm.length_1.value;
	if (actionForm.weight_1.value!="0")
		document.getElementById("txtWeight_1").value = actionForm.weight_1.value;
	resetting = false;
}

function getPWD() {
	window.open("login.html", "login", "left=400,top=300,width=200,height=35,resizable=no,toolbars=no,statusbar=no,scrollbars=no,menubar=no");
}

