var gItemSepChar = '\011';	//	Horizontal TAB octal code equivalent to '\t'
var gLineSepChar = '\013';	//	Vertical TAB octal code, DON'T USE '\v' (incompatible with IE7)

function FormatStr(text,len)
{
	var result = (text == null) ? "" : text;
	var actualLen = result.length;
	for (var i = 0; i < (len - actualLen); i++) result += " ";
	return result;
}
function InitSelect(inItem,inValue)
{
	var ItemFound = 0;
	for (var Item=0; Item<inItem.length; Item++)
		if (inItem[Item].value == inValue) {
			ItemFound = Item;
			break;
		}
	inItem.selectedIndex = ItemFound;
}
function CheckInteger(inObj,inCouldBeEmpty,inMinValue,inMaxValue)
{
	var Str = inObj.value;
	if (Str == "") return inCouldBeEmpty;
	if (isNaN(Str)) return false;
	if (inMinValue != -1 && Str < inMinValue) return false;
	if (inMaxValue != -1 && Str > inMaxValue) return false;
	return true;
}
function CheckIntegerList(inObj,inCouldBeEmpty,inMinValue,inMaxValue)
{
	var Str = inObj.value;
	if (Str == "") return inCouldBeEmpty;
	var list = Str.split(',');
	var listSize = list.length;
	for (var i = 0; i < listSize; i++) {
		if (inMinValue != -1 && eval(list[i]) < inMinValue) return false;
		if (inMaxValue != -1 && eval(list[i]) > inMaxValue) return false;
	}
	return true;
}
function CheckName(inObj,inCouldBeEmpty,withSpace,inMinLength,inMaxLength)
{
	var Str = inObj.value;
	if (Str == "") return inCouldBeEmpty;
	if (Str.length < inMinLength) return false;
	if (Str.length > inMaxLength) return false;
	Str = Str.toUpperCase();
	var regExp = new RegExp("^[0-9A-Z" + ((withSpace) ? " " : "") + "]{" + inMinLength + "," + inMaxLength + "}$");
	return regExp.test(Str);
}
function CheckExtendName(inObj,inCouldBeEmpty,inMinLength,inMaxLength)
{
	var Str = inObj.value;
	if (Str == "") return inCouldBeEmpty;
	if (Str.length < inMinLength) return false;
	if (Str.length > inMaxLength) return false;
	Str = Str.toUpperCase();
	var regExp = new RegExp("^[0-9A-Z&@#$*%=+-_()<> ]{" + inMinLength + "," + inMaxLength + "}$");
	return regExp.test(Str);
}
function CheckString(inObj,inCouldBeEmpty,inMinLength,inMaxLength)
{
	var Str = inObj.value;
	if (Str == "") return inCouldBeEmpty;
	if (Str.length < inMinLength) return false;
	if (Str.length > inMaxLength) return false;
	return true;
}
function CheckPhone(inObj,inCouldBeEmpty)
{
	var Str = inObj.value;
	if (Str == "") return inCouldBeEmpty;
	var regExp = new RegExp("^[0-9#*]*$");
	return regExp.test(Str);
}
function CheckDigit(inObj,inCouldBeEmpty)
{
	var Str = inObj.value;
	if (Str == "") return inCouldBeEmpty;
	var regExp = new RegExp("^[0-9]*$");
	return regExp.test(Str);
}
function CheckHexadecimal(inObj,inCouldBeEmpty,inLength)
{
	var Str = inObj.value;
	if (Str == "") return inCouldBeEmpty;
	if (Str.length != inLength) return false;
	Str = Str.toUpperCase();
	var regExp = new RegExp("^[0-9A-F]{" + inLength + "}$");
	return regExp.test(Str);
}
function CheckDNS(inObj,inCouldBeEmpty)
{
	var Str = inObj.value;
	if (Str == "") return inCouldBeEmpty;
	Str = Str.toUpperCase();
	var regExpDNS = new RegExp("^[0-9a-zA-Z][_0-9a-zA-Z-]*(\\.[0-9a-zA-Z][_0-9a-zA-Z-]*)+$");
	if (regExpDNS.test(Str)) return true;
	var regExpIP = new RegExp("^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$");
	if (!regExpIP.test(Str)) return false;
	var ipArray = Str.split(".");
	for (var i = 0; i < 4; i++) {
		if ((Math.floor(ipArray[i]) < 0) || (Math.floor(ipArray[i]) > 255)) return false;
	}
	return true;
}
function CheckFQDN(inObj,inCouldBeEmpty)
{
	var Str = inObj.value;
	if (Str == "") return inCouldBeEmpty;
	Str = Str.toUpperCase();
	var regExpDNS = new RegExp("^[0-9a-zA-Z][_0-9a-zA-Z-]*(\\.[0-9a-zA-Z][_0-9a-zA-Z-]*)*$");
	if (!regExpDNS.test(Str)) return false;
	return true;
}
function CheckIP(inObj,inCouldBeEmpty)
{
	var Str = inObj.value;
	if (Str == "") return inCouldBeEmpty;
	var regExpIP = new RegExp("^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$");
	if (!regExpIP.test(Str)) return false;
	var ipArray = Str.split(".");
	for (var i = 0; i < 4; i++) {
		if ((Math.floor(ipArray[i]) < 0) || (Math.floor(ipArray[i]) > 255)) return false;
	}
	return true;
}
function CheckMask(inObj,inCouldBeEmpty)
{
	var Str = inObj.value;
	if (Str == "") return inCouldBeEmpty;
	if (!CheckIP(inObj,false)) return false;
	var ipArray = Str.split(".");
	var lastByte = 1;
	for (var i = 0; i < 4; i++) {
		var octet = ipArray[i];
		for (var j = 7; j > 0; j--) {
			if ((octet & (1 << j)) == 0) lastByte = 0;
			else if (lastByte == 0) return false; 
		}
	}
	return true;
}
function CheckMAC(inObj,inCouldBeEmpty)
{
	var Str = inObj.value;
	if (Str == "") return inCouldBeEmpty;
	Str = Str.toUpperCase();
	var regExpMAC = new RegExp("^[0-9A-F]{1,2}:[0-9A-F]{1,2}:[0-9A-F]{1,2}:[0-9A-F]{1,2}:[0-9A-F]{1,2}:[0-9A-F]{1,2}$");
	if (!regExpMAC.test(Str)) return false;
	return true;
}
function IsSimulVar(inStr)
{
	var result = false;
	if (inStr.length >= 2)
		result = ((inStr.charAt(0) == '<') && (inStr.charAt(1) == '!'));
	return result;
}
function NotifyError(errorID)
{
	switch (LANGUAGE) {
		case "fr":
			switch (eval(errorID)) {
				case 0: break;
				//	ConfigLib errors
				case -1: alert("Erreur: le format des données fournies est incorrect."); break;
				case -2: alert("Erreur: fichier de configuration incorrect."); break;
				case -3: alert("Erreur: fichier de syntaxe incorrect."); break;
				case -4: alert("Erreur: mémoire du routeur saturée."); break;
				case -5: alert("Erreur: syntaxe de la propriété incorrecte."); break;
				case -6: alert("Erreur: object de configuration inexistant."); break;
				case -7: alert("Erreur: ajout d'un élément de configuration déjà existant."); break;
				case -8: alert("Erreur: propriété non modifiable."); break;
				case -9: alert("Erreur: propriété obligatoire non renseignée."); break;
				case -10: alert("Erreur: l'exécution d'une commande système a échoué."); break;
				case -11: alert("Erreur: toutes les propriétés obligatoires ne sont pas renseignées."); break;
				case -12: alert("Erreur: propriété non compatible avec la configuration actuelle."); break;
				case -13: alert("Erreur: propriété manquante pour définir l'élément de configuration."); break;
				case -14: alert("Erreur: opération interdite."); break;
				case -15: alert("Erreur: le fichier de configuration est temporairement verrouillé."); break;
				//	Firmware update
				case -128: alert("Erreur: mise à jour du firmware annulée."); break;
				case -129: alert("Erreur: pas mise à jour de firmware en cours."); break;
				case -130: alert("Erreur: fichier du firmware non défini."); break;
				case -132: alert("Erreur: fichier du firmware inexistant."); break;
				case -133: alert("Erreur: fichier du firmware non conforme."); break;
				case -134: alert("Erreur: fichier du firmware tronqué."); break;
				case -135: alert("Erreur: impossible d'effacer la mémoire flash."); break;
				case -136: alert("Erreur: impossible d'écrire dans la mémoire flash."); break;
				case -137: alert("Erreur: une mise à jour du firmware est déjà en cours."); break;
				//	Unknown error
				default: if (!IsSimulVar(errorID)) alert("Erreur: valeur non reconnue (" + errorID + ")."); break;
			}
			break;
		case "us":
			switch (eval(errorID)) {
				case 0: break;
				//	ConfigLib errors
				case -1: alert("Error: invalid datas format."); break;
				case -2: alert("Error: invalid config file."); break;
				case -3: alert("Error: invalid syntax file."); break;
				case -4: alert("Error: not enough memory."); break;
				case -5: alert("Error: invalid property syntax."); break;
				case -6: alert("Error: configuration item not found."); break;
				case -7: alert("Error: item to add already exists."); break;
				case -8: alert("Error: property not writable."); break;
				case -9: alert("Error: required property is empty."); break;
				case -10: alert("Error: system command failed."); break;
				case -11: alert("Error: all required properties are not setted."); break;
				case -12: alert("Error: property not compatible with current configuration."); break;
				case -13: alert("Error: missing property needed to define configuration item."); break;
				case -14: alert("Error: operation not permitted."); break;
				case -15: alert("Error: configuration file is temporary locked."); break;
				//	Firmware update
				case -128: alert("Error: firmware update cancelled."); break;
				case -129: alert("Error: no firmware update in progress."); break;
				case -130: alert("Error: firmware file is invalid or undefined."); break;
				case -132: alert("Error: firmware file does not exist."); break;
				case -133: alert("Error: invalid firmware file."); break;
				case -134: alert("Error: truncated firmware file."); break;
				case -135: alert("Error: cannot erase flash partition."); break;
				case -136: alert("Error: cannot write to flash memory."); break;
				case -137: alert("Error: firmware is already updating."); break;
				//	Unknown error
				default: if (!IsSimulVar(errorID)) alert("Error: unknown (" + errorID + ")."); break;
			}
			break;
		case "es":
		case "it":
			break;
	}
}
