<?php
// Get language from query parameter, cookie, or default to English
$lang = $_GET['lang'] ?? $_COOKIE['language'] ?? 'en';

// Load translations
$langFile = __DIR__ . '/../../../../lang/' . $lang . '/app/validation_tooltips.php';
if (!file_exists($langFile)) {
    $langFile = __DIR__ . '/../../../../lang/en/app/validation_tooltips.php';
}
$trans = include($langFile);

// Enable caching per language - each language URL gets cached separately
header('Content-Type: application/javascript; charset=UTF-8');
header('Cache-Control: public, max-age=2592000'); // Cache for 30 days
header('Vary: Cookie'); // Cache varies by cookie in case query param is missing
?>

(function($) {
	$.fn.validationEngineLanguage = function() {};
	$.validationEngineLanguage = {
		newLang: function() {
			$.validationEngineLanguage.allRules = 	{
					"required":{
						"regex":"none",
						"alertText":"<?php echo addslashes($trans['required']); ?>",
						"alertTextCheckboxMultiple":"<?php echo addslashes($trans['checkbox_multiple']); ?>",
						"alertTextCheckboxe":"<?php echo addslashes($trans['checkbox_terms']); ?>"},
					"length":{
						"regex":"none",
						"alertText":"<?php echo addslashes($trans['length_between']); ?>",
						"alertText2":" <?php echo addslashes($trans['length_and']); ?> ",
						"alertText3":" <?php echo addslashes($trans['length_chars']); ?>"},
					"maxCheckbox":{
						"regex":"none",
						"alertText":"<?php echo addslashes($trans['max_checkbox']); ?>"},	
					"minCheckbox":{
						"regex":"none",
						"alertText":"<?php echo addslashes($trans['min_checkbox']); ?>",
						"alertText2":" <?php echo addslashes($trans['min_checkbox_options']); ?>"},	
					"confirm":{
						"regex":"none",
						"alertText":"<?php echo addslashes($trans['confirm_mismatch']); ?>"},
					"telephone":{
						"regex":"/^[0-9\-\(\)\ ]+$/",
						"alertText":"<?php echo addslashes($trans['invalid_phone']); ?>"},	
					"email":{
						"regex":"/^[a-zA-Z0-9_\\.\\-]+\\@([a-zA-Z0-9\\-]+\\.)+[a-zA-Z0-9]{2,4}$/",
						"alertText":"<?php echo addslashes($trans['invalid_email']); ?>"},
					"date":{
                         "regex":"/^[0-9]{4}\-\[0-9]{1,2}\-\[0-9]{1,2}$/",
                         "alertText":"<?php echo addslashes($trans['invalid_date']); ?>"},
					"onlyNumber":{
						"regex":"/^[0-9\ ]+$/",
						"alertText":"<?php echo addslashes($trans['only_numbers']); ?>"},	
					"noSpecialCharacters":{
						"regex":"/^[0-9a-zA-Z]*$/",
						"alertText":"<?php echo addslashes($trans['no_special_chars']); ?>"},
					"onlyValidPasswordCharacters":{
						"regex":"/^[a-zA-Z0-9 @!#$%&(){}*+,\-./:;<>=?[\\]\^_|~]*$/",
						"alertText":"<?php echo addslashes($trans['no_special_chars']); ?>"},
					"ajaxUser":{
						"file":"../validateUser.php",
						"alertTextOk":"<?php echo addslashes($trans['username_available']); ?>",	
						"alertTextLoad":"<?php echo addslashes($trans['loading']); ?>",
						"alertText":"<?php echo addslashes($trans['username_taken']); ?>"},	
					"ajaxName":{
						"file":"../validateUser.php",
						"alertText":"<?php echo addslashes($trans['username_taken']); ?>",
						"alertTextOk":"<?php echo addslashes($trans['username_available']); ?>",	
						"alertTextLoad":"<?php echo addslashes($trans['loading']); ?>"},		
					"onlyLetter":{
						"regex":"/^[a-zA-Z\ \']+$/",
						"alertText":"<?php echo addslashes($trans['only_letters']); ?>"}
					}	
		}
	}
})(jQuery);

$(document).ready(function() {	
	$.validationEngineLanguage.newLang()
});
