/**
 * Javascript library F3
 * This files contains all javascript functions except the naw help
 * Version 1.0
 */

/**
 * Show element (disclaimer, address form)
 */

function showElementById(id) {

	if (document.getElementById(id)) {

		document.getElementById(id).style.display = 'block';

	}

}

/**
 * Hide element by Id
 */

function hideElementById(id) {

	document.getElementById(id).style.display = 'none';

	/**
	 * Uncheck hidden elements in div with id
	 */

	uncheckElementsDiv(id);

}

/**
 * Uncheck elements
 * 
 * @param div
 * @return
 */

function uncheckElementsDiv(div) {
	if (!div) {
		return;
	}

	div = typeof div === "string" ? document.getElementById(div) : div;

	var elms = div.getElementsByTagName("*");

	for ( var i = 0, maxI = elms.length; i < maxI; ++i) {
		var elm = elms[i];

		// alert("Type: " + elm.type + "\nName: " + elm.name + "\nId: "+
		// elm.id);

		switch (elm.type) {

		case "text":
			document.getElementById(elm.id).value = null;
		case "radio":
			document.getElementById(elm.id).checked = false;
		case "checkbox":
			document.getElementById(elm.id).checked = false;
		case "select-one":
			document.getElementById(elm.id).selectedIndex = 0;

		}
	}
}

/**
 * Show Help Shows user help in helpDiv
 */

function showHelp(strDivId) {
	document.getElementById('helpDiv').innerHTML = eval('strHelp_' + strDivId);
}

/**
 * Hide Help Hides user help in helpDiv
 */

function hideHelp() {

	document.getElementById('helpDiv').innerHTML = '';

	showHelp('default');

}

/**
 * Taf popup function
 */

var win = null;

function newWindow(mypage, myname, w, h, pos, infocus) {
	if (pos == "random") {
		myleft = (screen.width) ? Math
				.floor(Math.random() * (screen.width - w)) : 100;
		mytop = (screen.height) ? Math.floor(Math.random()
				* ((screen.height - h) - 75)) : 100;
	}

	if (pos == "center") {
		myleft = (screen.width) ? (screen.width - w) / 2 : 100;
		mytop = (screen.height) ? (screen.height - h) / 2 : 100;
	} else if ((pos != 'center' && pos != "random") || pos == null) {
		myleft = 0;
		mytop = 20
	}

	settings = "width="
			+ w
			+ ",height="
			+ h
			+ ",top="
			+ mytop
			+ ",left="
			+ myleft
			+ ",scrollbars=yes,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no";

	win = window.open(mypage, myname, settings);

	win.focus();

}

/**
 * function disableFieldsNl
 */

function disableFieldsNl() {

	document.getElementById('pst').disabled = true;
	document.getElementById('phn1').disabled = true;
	document.getElementById('phn2').disabled = true;
	document.getElementById('pc1').disabled = true;
	document.getElementById('pc2').disabled = true;
	document.getElementById('pct').disabled = true;
	document.getElementById('ph1').disabled = true;
	document.getElementById('ph2').disabled = true;

	document.getElementById('pst').style.backgroundColor = '#CECECE';
	document.getElementById('phn1').style.backgroundColor = '#CECECE';
	document.getElementById('phn2').style.backgroundColor = '#CECECE';
	document.getElementById('pc1').style.backgroundColor = '#CECECE';
	document.getElementById('pc2').style.backgroundColor = '#CECECE';
	document.getElementById('pct').style.backgroundColor = '#CECECE';
	document.getElementById('ph1').style.backgroundColor = '#CECECE';
	document.getElementById('ph2').style.backgroundColor = '#CECECE';

}

/**
 * function enableFieldsNl
 */

function enableFieldsNl() {

	document.getElementById('pst').disabled = false;
	document.getElementById('phn1').disabled = false;
	document.getElementById('phn2').disabled = false;
	document.getElementById('pc1').disabled = false;
	document.getElementById('pc2').disabled = false;
	document.getElementById('pct').disabled = false;
	document.getElementById('ph1').disabled = false;
	document.getElementById('ph2').disabled = false;

	document.getElementById('pst').style.backgroundColor = '';
	document.getElementById('phn1').style.backgroundColor = '';
	document.getElementById('phn2').style.backgroundColor = '';
	document.getElementById('pc1').style.backgroundColor = '';
	document.getElementById('pc2').style.backgroundColor = '';
	document.getElementById('pct').style.backgroundColor = '';
	document.getElementById('ph1').style.backgroundColor = '';
	document.getElementById('ph2').style.backgroundColor = '';

}

/**
 * function disableFieldsDld
 */

function disableFieldsDld() {

	document.getElementById('pst').disabled = true;
	document.getElementById('phn1').disabled = true;
	document.getElementById('phn2').disabled = true;
	document.getElementById('pc1').disabled = true;
	document.getElementById('pct').disabled = true;
	document.getElementById('pcn').disabled = true;
	document.getElementById('ph1').disabled = true;
	document.getElementById('ph2').disabled = true;

	document.getElementById('pst').style.backgroundColor = '#CECECE';
	document.getElementById('phn1').style.backgroundColor = '#CECECE';
	document.getElementById('phn2').style.backgroundColor = '#CECECE';
	document.getElementById('pc1').style.backgroundColor = '#CECECE';
	document.getElementById('pct').style.backgroundColor = '#CECECE';
	document.getElementById('pcn').style.backgroundColor = '#CECECE';
	document.getElementById('ph1').style.backgroundColor = '#CECECE';
	document.getElementById('ph2').style.backgroundColor = '#CECECE';

}

/**
 * function enableFieldsDld
 */

function enableFieldsDld() {

	document.getElementById('pst').disabled = false;
	document.getElementById('phn1').disabled = false;
	document.getElementById('phn2').disabled = false;
	document.getElementById('pc1').disabled = false;
	document.getElementById('pct').disabled = false;
	document.getElementById('pcn').disabled = false;
	document.getElementById('ph1').disabled = false;
	document.getElementById('ph2').disabled = false;

	document.getElementById('pst').style.backgroundColor = '';
	document.getElementById('phn1').style.backgroundColor = '';
	document.getElementById('phn2').style.backgroundColor = '';
	document.getElementById('pc1').style.backgroundColor = '';
	document.getElementById('pct').style.backgroundColor = '';
	document.getElementById('pcn').style.backgroundColor = '';
	document.getElementById('ph1').style.backgroundColor = '';
	document.getElementById('ph2').style.backgroundColor = '';

}

/**
 * function blockSubmit
 */

function blockSubmit(Id, Msg, Frm) {
	document.getElementById(Id).value = Msg;
	document.getElementById(Id).disabled = true;
	document.getElementById(Frm).submit();
}

/**
 * function enableField
 */

function enableField(Id) {

	document.getElementById(Id).disabled = false;
	document.getElementById(Id).style.backgroundColor = '';

}

/**
 * function disableField
 */

function disableField(Id) {

	document.getElementById(Id).disabled = true;
	document.getElementById(Id).style.backgroundColor = '#CECECE';

}

/**
 * Sequential enquete action_type 5 nav Next Question
 */

var currentQuestionId = 1;

function showNextQuestion(actionId) {

	var maxQuestionId = document.getElementById('maxQuestionId').value;

	if (currentQuestionId == (maxQuestionId - 1)) {

		/*
		 * Max questions reached, submit form
		 */

		document.form.submit();

	} else {

		/**
		 * Show next hidden page
		 */

		nextQuestionId = document.getElementById('currentQuestionId').value = ++currentQuestionId;

		document.getElementById('div' + nextQuestionId).style.display = 'block';

		/**
		 * Hide previous page
		 */

		hidePage = document.getElementById('currentQuestionId').value = --nextQuestionId;

		document.getElementById('div' + hidePage).style.display = 'none';

		showProgress(actionId);

	}

}

/**
 * Sequential enquete action_type 5 nav Previous Question
 */

function showPreviousQuestion() {

	if (currentQuestionId == 1) {

		// alert('Kan niet verder terug!');

	} else {
		/**
		 * Show hidden div of previous question, hide next question
		 */

		nextQuestionId = document.getElementById('currentQuestionId').value = --currentQuestionId;

		document.getElementById('div' + nextQuestionId).style.display = 'block';

		hidePage = document.getElementById('currentQuestionId').value = ++nextQuestionId;

		document.getElementById('div' + hidePage).style.display = 'none';

		showRegress();

	}
}

/**
 * Post page
 */

function submitPagePost() {

	document.nawForm.submit();

}

/**
 * Show progress - adjust progressbar - plus step_width
 */

function showProgress() {

	var actionId = document.getElementById("an").value;
	var progressDone = document.getElementById("progressDone");
	var progressWidth = document.getElementById("progressWidth").value;

	var curr_width = parseInt(progressDone.style.width); // removes the "px"
	// at the end
	var step_width = parseInt(progressWidth); // convert to integer

	var new_width = curr_width + step_width;

	progressDone.style.width = (new_width) + "px";

	if (currentQuestionId == 1) {

		document.getElementById('progressArrowContainerPrevious').innerHTML = '<img src="images/' + actionId + '/stop.gif" />';
		document.getElementById('mapLeft').innerHTML = '';

	} else {

		document.getElementById('progressArrowContainerPrevious').innerHTML = '<img src="images/' + actionId + '/arrow_left.jpg" onclick="javascript:showPreviousQuestion();" style="cursor: pointer;" />';
		document.getElementById('mapLeft').innerHTML = '<img src="images/' + actionId + '/flip_left.gif" usemap="#mapLeft" border="0" alt="" /><area shape="poly" coords="0,0,59,0,59,53,0,53" href="javascript:showPreviousQuestion();" alt="" />';

	}

}

/**
 * Show regress - adjust progressbar - minus step_width
 */

function showRegress() {

	var actionId = document.getElementById("an").value;
	var progressDone = document.getElementById("progressDone");
	var progressWidth = document.getElementById("progressWidth").value;

	var curr_width = parseInt(progressDone.style.width); // removes the "px"
	// at the end
	var step_width = parseInt(progressWidth); // convert to integer

	var new_width = curr_width - step_width;

	progressDone.style.width = (new_width) + "px";

	if (currentQuestionId == 1) {

		document.getElementById('progressArrowContainerPrevious').innerHTML = '<img src="images/' + actionId + '/stop.gif" />';
		document.getElementById('mapLeft').innerHTML = '';

	} else {

		document.getElementById('progressArrowContainerPrevious').innerHTML = '<img src="images/' + actionId + '/arrow_left.jpg" onclick="javascript:showPreviousQuestion();" style="cursor: pointer;" />';
		document.getElementById('mapLeft').innerHTML = '<img src="images/' + actionId + '/flip_left.gif" usemap="#mapLeft" border="0" alt="" /><area shape="poly" coords="0,0,59,0,59,53,0,53" href="javascript:showPreviousQuestion();" alt="" />';

	}

}

/**
 * On show handler
 */

function showElementProfVal(qid) {
	var phoneJsValue = document.getElementById('phoneJsValue').value;
	if (phoneJsValue.length < 10) {
		document.body.style.overflow = 'hidden';
		document.getElementById('sbm').disabled = true;
		document.getElementById('validate-' + qid).style.display = 'none';
		document.getElementById('pf-overlay-' + qid).style.display = 'block';
		document.getElementById('pf-overlay-' + qid).style.position = 'fixed';
		document.getElementById('pf-container-' + qid).style.display = 'block';
	}
	}

	/**
 * Handles phone value on submit
	 */

function submitProfVal(qid, id, lang) {
	var elem = document.getElementById(id);
	var error = validateProfVal(elem, lang);
	if (error == '') {
		document.getElementById('phoneJsValue').value = qid + '_' + elem.value;
		closeProfVal(qid, null);
	} else {
		document.getElementById('validate-' + qid).style.display = 'block';
		document.getElementById('validate-' + qid).innerHTML = error;
}
}

/**
 * Returns error message on validation
 */

function validateProfVal(elem, lang) {
	var error = '';
	if (elem.value.length != 10 || !elem.value.match(/^\d+$/gi)) {
		if (lang == 'dld') {
			error = 'Uw telefoonnummer moet uit 10 cijfers bestaan.';
		} else {
			error = 'Uw telefoonnummer moet uit 10 cijfers bestaan.';
		}

	}

	if (elem.value == '') {
	if (lang == 'dld') {
			error = 'Vul hieronder uw telefoonnummer in.';
		} else {
			error = 'Vul hieronder uw telefoonnummer in.';
		}
		}

	return error;
	}

/**
 * On close handler
 */

function closeProfVal(qid, aid) {
	if (aid !== null) {
		document.getElementById('ID_' + qid + '_' + aid).checked = false;
	}

	document.body.style.overflow = 'auto';
	document.getElementById('pf-overlay-' + qid).style.display = 'none';
	document.getElementById('pf-container-' + qid).style.display = 'none';
	document.getElementById('sbm').disabled = false;
}
