var bissRegEx = /\b[0-9a-fA-F]{12}\b/;
var initDecryptValue = "No";

function hide_all() {
	var optionButtons = document.getElementById("nav_ca");
	var x, count = 0;
	for (x = 0; x < optionButtons.childNodes.length; x++) {
		if (optionButtons.childNodes[x].nodeType == 1) {
			count++;
			optionButtons.childNodes[x].className = '';
			document.getElementById("ca" + count + "_container").style.display = "none";
		}
	}
}

function checkBiss(value, ca_index) {
	if (value == "BISS1") {
		document.getElementById("ca" + ca_index + "_biss").style.display = "";
		document.getElementById("ca" + ca_index + "_not_biss").style.display = "none";
		document.getElementById("ca" + ca_index + "_enableNameContainer").className = "shortgeneralcell";
	} else {
		document.getElementById("ca" + ca_index + "_biss").style.display = "none";
		document.getElementById("ca" + ca_index + "_not_biss").style.display = "";
		document.getElementById("ca" + ca_index + "_enableNameContainer").className = "generalcell";
	}
}

function updateBissKey(value, key_index) {
	var keyElements = document.getElementsByName("xpath:/configuration/ca_biss_keys/key[@index='" + key_index + "']?create=1");
	var len = keyElements.length;
	while (len--) {
		keyElements[len].value = value;
		if (!bissRegEx.test(keyElements[len].value)) {
			keyElements[len].style.backgroundColor = "#ff0000";
		} else {
			keyElements[len].style.backgroundColor = "#ffffff";
		}
	}	
}

function show_ca(id) {
	hide_all();
	document.getElementById(id).className = 'here';
	document.getElementById(id.replace('nav_ca', 'ca') + "_container").style.display = "";
	checkBiss(document.getElementById(id.replace('nav_ca', 'ca') + "_selector").value, id.replace('nav_ca', ''));
	var len;
	if (document.getElementById("ca1_selector").value == "BISS1" && id.replace('nav_ca', '') != "1") {
		var inputs = document.getElementById(id.replace('nav_ca', 'ca') + "_container").getElementsByTagName("input");
		var selects = document.getElementById(id.replace('nav_ca', 'ca') + "_container").getElementsByTagName("select");
		len = inputs.length;
		while (len--) {
			inputs[len].disabled = true;
		}
		len = selects.length;
		while (len--) {
			selects[len].disabled = true;
		}
	} else if (id.replace('nav_ca', '') == "1") {
		var keys = document.getElementById("ca1_biss").getElementsByTagName("input");
		len = keys.length;
		while (len--) {
			updateBissKey(keys[len].value, keys[len].name.replace(/\?\S*/, '').replace(/\D/g, ''));
		}
	}
}

function init() {
	var decryptNodes = document.getElementsByName("xpath:/configuration/ca[@index='1']/decrypt");
	if (decryptNodes.length > 0) {
		decryptNodes[0].value = initDecryptValue;
	}
	show_ca('nav_ca1');
}

function validateKeys() {
	var keyElements = document.getElementById("ca1_biss").getElementsByTagName("input");
	var len = keyElements.length;
	while (len--) {
		if (!bissRegEx.test(keyElements[len].value)) {
			document.getElementById("serveralert").innerHTML = "Cannot apply invalid keys.";
			document.getElementById("serveralert").style.display = "block";
			keyElements[len].focus();
			return;
		}
	}
	document.applyform.submit();
}