var browserName = navigator.userAgent;
function searchCert() {
	printErrors("");
	var path = new String(document.FirmaFacturae.searchCert());
	if (path != '') {
		document.getElementById("certPath").value = path;
		showPass();
	}
	else {
		document.getElementById("certPath").value = '';
		hidePass();
	}
}

function checkJavaVersion() {
	
	var msg = new String(document.FirmaFacturae.isValidJREVersion());
	if (msg != 'true') {
		alertInvalidJRE();
	}
}

function alertInvalidJRE() {
	  MOOdalBox.open(
		INVALID_JRE_URL,
		INVALID_JRE_CAPTION,
	    "650 250"
	);
}

function getUserHome() {
	document.appletForm.userHome.value = new String(document.FirmaFacturae.getUserHome());
}

function hasErrors(msg) {
	
	return (msg.indexOf(NO_ERROR) == -1);
}

function getUnsignedPath() {
	document.appletForm.userHome.value = new String(document.FirmaFacturae.getUnsignedPath());
}

function showSearchFileCert() {
	certSelected = false;
	document.getElementById("certComboDiv").style.display = "none";
	document.getElementById("waiting").style.display = "none";
	document.getElementById("instructions").innerHTML = CERTS_NOT_FOUND_INSTRUCTIONS;
	printErrors("");
	document.getElementById("examinCert").style.display = "block";
	document.getElementById("certPath").value = "";
}

function searchCertificate() {
	certSelected = false;
	document.getElementById("certComboDiv").style.display = "none";
	document.getElementById("waiting").style.display = "none";
	document.getElementById("instructions").innerHTML = SHOW_CERTS_FOUND;
	printErrors("");
	document.getElementById("examinCert").style.display = "block";
	document.getElementById("certPath").value = "";
}

function evaluateCertsFound(msg) {
	if (msg.indexOf(ERROR_NO_PRIVILEGES) != -1) {
		printErrors(msg);
	}
	else {
		document.getElementById("form_firma_uno").style.display = "block";
		document.getElementById("form_firma_dos").style.display = "block";
		try{ document.getElementById("form_firma_tres").style.display = "block";}catch(oException){}
		if (!hasErrors(msg)) {
			showCertsFound();
		}
		else {
			showSearchFileCert();
		}
	}
}

function printErrors(msg) {
	var errors = msg.split("#");
	var result = "";
	var hasError = false;
	for (i = 0; i < errors.length; i++) {
		if (errors[i] != '') {
			hasError = true;
			result += new String(document.FirmaFacturae.getErrorDescription(errors[i]));
			result += "<br/>";
		}
	}
	document.getElementById("spanErrors").innerHTML = result;
	if (hasError) {
		document.getElementById("errorList").style.display = "block";
	}
	else {
		document.getElementById("errorList").style.display = "none";
	}
}


function alertErrors(msg) {
	var errors = msg.split("#");
	var result = "";
	
	for (i = 0; i < errors.length; i++) {
		if (errors[i] != '') {
			result += new String(document.FirmaFacturae.getErrorDescription(errors[i]));
			result += "\n";
		}
	}
	alert(result);
	this.close();
}

function sign() {
	
	combo = document.appletForm.certCombo;
	
	if (certSelected) {
		generateSignature(combo.selectedIndex, "Cert");
	}
	else {
		evaluateCertificate();
	}
}

function generateSignature(index, mode) {
	document.forms[0].disabled = true;
	var msg = new String(document.FirmaFacturae.signDocument(index, mode));
	
	if (!hasErrors(msg)) {
		signOK();
	}
	else {
		document.forms[0].disabled = false;
		if (msg.indexOf(ERROR_CERTIFICATE_EXPIRED) != -1) {
			document.FirmaFacturae.setCertExpired("");
			alertErrors(msg);
		}
		else {
			printErrors(msg);
		}
	}
}

function signOK() {
	document.forms[0].action = URL_OK;
	document.forms[0].disabled = false;
	document.forms[0].submit();
	document.forms[0].disabled = true;
}

function showCertsFound() {
	document.getElementById("instructions").innerHTML = CERTS_FOUND_INSTRUCTION;
	
	document.getElementById("certComboDiv").style.display = "block";
	document.getElementById("waiting").style.display = "none";
	
	document.getElementById("certPassId").style.display = "none";
	document.getElementById("examinCert").style.display = "none";
	
	printErrors("");
	certSelected = true;
}

function evaluateCertificate() {
	var password = document.appletForm.certPassword.value;
	var msg = new String(document.FirmaFacturae.checkCertificate(password, ""));
	if (!hasErrors(msg)) {
		certSelected = true;
		generateSignature(0, "File");
	}
	else {
		printErrors(msg);
		hidePass();
		document.getElementById("certPath").value = '';
	}
}

var certs = new Array();

function addCert(param) {

	param = new String(unescape(param));
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){
		param = Utf8.decode(param);
	}
	certs = param.split("#");
	combo = document.appletForm.certCombo;
	
	for (i = 0; i < certs.length; i++) {
		if (certs[i] != '') {
			combo.options[i] = new Option(certs[i], i);
		}
	}
}

var certSelected = false;
		
function cancel() {
	getUnsignedPath();
	document.forms[0].action = URL_CANCEL;
	document.forms[0].submit();
	document.forms[0].disabled = true;
}

function showPass() {
	document.appletForm.certPassword.value = '';
	document.getElementById("passWordInstructions").innerHTML = GET_PASSWORD;
	document.getElementById("certPassId").style.display = "block";
}

function hidePass() {
	document.appletForm.certPassword.value = '';
	document.getElementById("passWordInstructions").innerHTML = GET_PASSWORD;
	document.getElementById("certPassId").style.display = "none";
}

var Utf8 = {
		 
	// public method for url encoding
	encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// public method for url decoding
	decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}
