var theResourceBundle_sk = {
	noCapicom : "Kni\u017enica 'CAPICOM' nie je na Va\u0161om po\u010d\u00edta\u010di nain\u0161talovan\u00e1. Pros\u00edm, nain\u0161talujte ju pod\u013ea pokynov na http://to.be.defined.sk",
	ffSigningErrorUserCancel : "Podpisovanie bolo zru\u0161en\u00e9 na Va\u0161u \u017eiados\u0165.",
	ffSigningErrorNoMatchingCert : "Nedisponujete \u017eiadnym certifik\u00e1tom vhodn\u00fdm na podpisovanie v tejto aplik\u00e1cii.",
	ffSigningErrorInternalError : "Po\u010das podpisovania do\u0161lo k internej chybe. Podpisovanie bolo ne\u00faspe\u0161n\u00e9: podpis nebol vytvoren\u00fd.",
	ieSigningError : "Podpisovanie zlyhalo s nasleduj\u00facou chybou: ",
	signUnsupportedBrowser : "T\u00e1to aplik\u00e1cia nepodporuje elektronick\u00e9 podpisy vo Va\u0161om type prehliada\u010da. Pros\u00edm, pou\u017eite Internet Explorer alebo Firefox."
};

var theResourceBundle_en = {
	noCapicom : "Library 'CAPICOM' is not installed on your computer. Please, install 'CAPICOM' according to instructions on http://to.be.defined.sk",
	ffSigningErrorUserCancel : "The signing has been cancelled by user.",
	ffSigningErrorNoMatchingCert : "There is no matching certificate to be used for signing procedure.",
	ffSigningErrorInternalError : "There was an internal error within the signing procedure. The signing process has failed and no signature has been produced.",
	ieSigningError : "The signing procedure failed: ",
	signUnsupportedBrowser : "This system does not support signing using your browser. Please, use Internet Explorer or Firefox."
};

function getI18nText(aKey) {
	if (top.userLocale == 'sk') {
		return theResourceBundle_sk[aKey];
	} else {
		return theResourceBundle_en[aKey];
	}
}


function submitForm(aForm, anAction, someActionParams) {
	with (aForm) {
		action.value = anAction;
		actionParams.value = someActionParams;
		submit();
	}
	return false;
}

function submitFormIfConfirmed(aForm, anAction, someActionParams, aMessage) {
	if (confirmButton(aMessage)) {
		submitForm(aForm, anAction, someActionParams);
	} 
	return false;
}

function submitFormIfSigned(aForm, anAction, someActionParams, aMessageToSign) {
	if (sign(aMessageToSign)) {
		submitForm(aForm, anAction, someActionParams);
	} 
	return false;
}

function submitFormIfConfirmedAndSigned(aForm, anAction, someActionParams, aMessage, aMessageToSign) {
	if (confirmAndSign(aMessage, aMessageToSign)) {
		submitForm(aForm, anAction, someActionParams);
	} 
	return false;
}

function sign(aMessageToSign) {
	aMessageToSign = unescape(aMessageToSign);
	if (Prototype.Browser.Gecko) {
		return signFF(aMessageToSign);
	} else if (Prototype.Browser.IE) {
		return signIE(aMessageToSign);
	} else {
		alert(getI18nText('signUnsupportedBrowser'));
		return false;
	}
}

function signIE(aMessageToSign) {

    var theSignedData = null;
    var theSigner = null;

    try {
    	theSigner = new ActiveXObject("CAPICOM.Signer");
    	theSignedData = new ActiveXObject("CAPICOM.SignedData");
    } catch (theException) {
        alert(getI18nText('noCapicom'));
        return false;
    }

    var theSignature = null;

    try {
        theSignedData.Content = aMessageToSign;
        theSignature = theSignedData.Sign(theSigner, true, 0);
    } catch (theException) {
    	alert(getI18nText('ieSigningError') + theException.description);
		return false;
    }
	
    document.getElementById('clientSignature').value = theSignature;
    return true;

}

function signFF(aMessageToSign) {
	var theSignature = crypto.signText(aMessageToSign,'ask');
	if (theSignature && theSignature.startsWith('error:')) {
		if (theSignature == "error:userCancel") {
			alert(getI18nText('ffSigningErrorUserCancel'));
		} else if (theSignature == "error:noMatchingCert") {
			alert(getI18nText('ffSigningErrorNoMatchingCert'));
		} else {
			alert(getI18nText('ffSigningErrorInternalError'));
		}
		return false;
	}
	document.getElementById('clientSignature').value= theSignature;
	return true;
}

function confirmButton(aMessage) {
	return confirm(aMessage);
}

function confirmAndSign(aMessage, aMessageToSign) {
	return confirmButton(aMessage) && sign(aMessageToSign);
}

function setDelValue(aDelId, aCheckedFlag) {
	var theDelElement =	document.getElementById(aDelId);
	if (aCheckedFlag) {
		theDelElement.value = 'deleted';
	} else {
		theDelElement.value = '';
	}
}

function _gatherCheckboxes(anElement, aResultArray) {
	if (anElement.tagName && anElement.tagName.toLowerCase() == 'input' && anElement.type == 'checkbox') {
		aResultArray[aResultArray.length] = anElement;
		return;
	}
	for (var i = 0; i < anElement.childNodes.length; i++) {
		_gatherCheckboxes(anElement.childNodes[i], aResultArray);
	}
}

function _getCheckboxes(aBoxId) {
	var theResult = new Array();
	var theBox = document.getElementById(aBoxId);
	if (theBox) {
		var theCheckboxes = new Array();
		_gatherCheckboxes(theBox, theCheckboxes);
		var theCheckboxesSize = theCheckboxes.length;
		for (var i = 0; i < theCheckboxesSize; i++) {
			var eachCheckbox = theCheckboxes[i];
			if (eachCheckbox.name && eachCheckbox.name.match('^boxSelection.*__::__.*')) {
				theResult[theResult.length] = eachCheckbox;
			}
		}
	}
	return theResult;
}

function setAllRowsChecked(aBoxId, aValue) {
	if (aValue == null) {
		aValue = true;
	}
	var theCheckboxes = _getCheckboxes(aBoxId);
	var theCheckboxesSize = theCheckboxes.length;
	for (var i = 0; i < theCheckboxesSize; i++) {
		var eachCheckbox = theCheckboxes[i];
		eachCheckbox.checked = aValue; 
	}
}

function isAnyRowSelected(aBoxId) {
	var theCheckboxes = _getCheckboxes(aBoxId);
	var theCheckboxesSize = theCheckboxes.length;
	for (var i = 0; i < theCheckboxesSize; i++) {
		var eachCheckbox = theCheckboxes[i];
		if (eachCheckbox.checked) {
			return true;
		}
	}
	return false;
}

function redirect(aHref) {
	document.location.href=aHref;
}
