if (!window.console) console = {log: function() {}};

var cenAX = {
	version: '5.0.0 alpha 2',
	settingsVersion: '1',
	storagePrefix: 'com.censoft.hte',
	tpxPrefix: 'com.censoft.hte.tpx',
	licenseUser: '',
	codepagesUNIX: [
		"STD 210 PC Greek (437G)",
		"STD 220 PC Spanish (National Spain)",
		"STD 437 MS-DOS Latin US",
		"STD 737 PC Greek",
		"STD 850 MS-DOS Latin 1",
		"STD 852 MS-DOS Latin 2",
		"STD 855 PC Cyrillic",
		"STD 857 PC Modern Turkish",
		"STD 858 MS-DOS Latin 1",
		"STD 860 MS-DOS Portugese",
		"STD 861 PC Icelandic",
		"STD 863 MS-DOS French Canada",
		"STD 865 MS-DOS Nordic",
		"STD 866 PC Cyrillic",
		"STD 1250 Windows Latin 2 (Central Europe)",
		"STD 1251 Windows Cyrillic (Slavic)",
		"STD 1252 Windows Latin 1 (ANSI)",
		"STD 1252 Windows Latin 1 (ANSI) (French)",
		"STD 1252 Windows Latin 1 (ANSI) (German)",
		"STD 1252 Windows Latin 1 (ANSI) United Kingdom",
		"STD 1253 Windows Greek",
		"STD 1254 Windows Turkish",
		"1252 Windows Latin 1 (ANSI) (Polish)",
		"1252 Windows Latin 1 (ANSI) (FONT)",
		"STD DEC ISO Latin NR 1 Right",
		"STD DEC Multinational Left",
		"STD DEC Multinational Right",
		"STD DEC Supplemental",
		"STD DEC Cyrillic",
		"STD DEC Greek 1287",
		"STD DEC Turkish 1288",
		"STD ISO 8859-1 Latin I",
		"STD ISO 8859-2 Latin II",
		"STD ISO-IEC 8859-5 Cyrillic",
		"STD ISO 8859-5 Latin/Cyrillic",
		"STD ISO 8859-7 Greek",
		"STD ISO 8859-9 Turkish",
		"STD ISO 8859-15 Latin 9",
		"STD POLISH CSK",
		"STD POLISH MAZOVIA",
		"STD POLISH POLICE",
		"STD Russian Alternative",
		"STD Russian ANSI",
		"STD Russian KOI-8",
		"HP Roman 8",
		"HP Roman 9",
		"Eastern European 7-bit",
		"Latin-Greek 7-bit",
		"Czech Output 8859-2 Latin II",
		"Polish Output 8859-2 Latin II",
	],
	codepages3270: ["N/A"],
	codepages5250: [
		"1399 (Japanese DBCS)",
		"933 (Korean DBCS)",
		"935 (Simplified Chinese DBCS)",
		"870 (Latin 2 Multilingual)",
		"1153 (Latin 2 Multilingual w/Euro)"
	],
	VT100_NRCS: [
		"AMERICAN",
        "BRITISH",
        "DUTCH",
        "FINNISH",
        "FRENCH",
        "CANADIAN",
        "GERMAN",
        "ITALIAN",
        "DANISH",
        "NORWEGIAN",
        "PORTUGUESE",
        "SPANISH",
        "SWEDISH",
        "SWISS"
	],
	VT200_MULTINATIONAL: [
		"STD DEC Multinational Left",
		"STD DEC Multinational Right",
		"VT200 Multinational Mapping",
		"VT200 Multinational Cyrillic",
		"VT200 Multinational Greek",
		"VT200 Multinational Turkish",
		"VT200 Multinational Czech",
		"VT200 Multinational Latin II",
		"VT200 Multinational Polish"
	],
	dbcsFonts: ["PMingLiu","Arial Unicode MS","Malgun Gothic"],
	
	hteConnection:null,
	params: null,
		
	getParams:function() {

		if(this.params != null) {
			return this.params;
		}
	
		var params = location.search.substr(location.search.indexOf("?")+1);
		var sval = new Object();
		params = params.split("&");
		for (var i=0; i<params.length; i++) {
		     temp = params[i].split("=");
			 sval[temp[0]] = decodeURIComponent(temp[1]);
		}

		this.params = sval;
		return this.params;
	},
	
	baseURL:function() {
		var url = document.URL.split("?");
		return url[0];
	},

	getNavOffset: function () {
		var startX = window.screenLeft;
		var startY = window.screenTop;

		window.moveTo(0, 0);

		var r = {
			x: window.screenLeft,
			y: window.screenTop
		};

		window.moveTo(startX - r.x, startY - r.y);

		return r;
	},

	getWindowPosX:function() {
		if (window.screenLeft !== undefined) {
			return window.screenLeft - this.getNavOffset().x;
		}
		else {
			return window.screenX;
		}
	},

	getWindowPosY:function() {
		if (window.screenTop !== undefined) {
			return window.screenTop - this.getNavOffset().y;
		}
		else {
			return window.screenY;
		}
	},

	getWindowWidth: function () {
		if (window.outerWidth) {
			return window.outerWidth;
		}
		else {
			if ($.browser.msie) {
				return $(window).width() + this.getNavOffset().x + 8;
			}

			return $(window).width();
		}
	},

	getWindowHeight: function () {
		if (window.outerHeight) {
			return window.outerHeight;
		}
		else {
			if ($.browser.msie) {
				return $(window).height() + this.getNavOffset().y + 31;
			}

			return $(window).height();
		}
	},
	
	clone:function(obj, result) {
	    if(obj==null || typeof obj != 'object'){
	        //nothing really to do here - you're going to lose the reference to result if you try an assignment
	    }

        result.constructor = obj.constructor;
        result.constructor();
        
	    if(obj instanceof Array) {
	        for(var i=0; i<obj.length; i++){
	            result.push();
	            this.clone(obj[i], result[i]);
	        }
	    }    
	    if(obj instanceof Object){
	        for(var k in obj){
	            var count=0;
	            if(obj[k]==null || typeof obj[k] != 'object'){
	                result[k] = obj[k];
	            }else if(obj[k] instanceof Array) {
	                result[k] = [];
	                this.clone(obj[k], result[k]);
	            }else if(obj[k] instanceof Object){
	                result[k] = {};
	                for( var attr in obj[k]){
	                    this.clone(obj[k], result[k]);
	                }
	            }
	        }
	    }
	},
	genRandIdent: function () {
		var i;
		var value;
		var str;
		var strId = "";

		for (i = 0; i < 16; i++) {
			value = parseInt((Math.random() * 255).toFixed(0), 10);	

			str = value.toString(16).toUpperCase();

			if (str.length == 1) {
				strId += "0" + str;
			}
			else {
				strId += str;
			}

			if (i == 3 || i == 5 || i == 7 || i == 9)
				strId += "-";
		}

		return strId;
	},	
	isTE3270:function() {
		return false;
	},

	attributeNamesUNIX:function () {
		return [ "Background", "Foreground", "Bold" ];
	},
	
	attributeNames3270:function() {
		var names = [ 	"Protected, HIGHLIGHTED FIELD","Protected, NON-HIGHLIGHTED FIELD",
					"Non-Protected, HIGHLIGHTED FIELD", "Non-Protected, NON-HIGHLIGHTED FIELD" ];
		return names;
	},
	
	attributeNames5250:function() {
		var names = [ 	"GREEN", "GREEN/REVERSE", "WHITE", "WHITE/REVERSE",
					"GREEN/UNDERSCORE", "GREEN/UNDERSCORE/REVERSE",
					"WHITE/UNDERSCORE", "NONDISPLAY", "RED", "RED/REVERSE",
					"RED/BLINK", "RED/REVERSE/BLINK", "RED/UNDERSCORE",
					"RED/UNDERSCORE/REVERSE", "RED/UNDERSCORE/BLINK",
					"NONDISPLAY", "TURQUOISE/COLUMN", "TURQUOISE/COLUMN/REVERSE",
					"YELLOW/COLUMN", "YELLOW/COLUMN/REVERSE", "TURQUOISE/UNDERSCORE",
					"TURQUOISE/UNDERSCORE/REVERSE", "YELLOW/UNDERSCORE",
					"NONDISPLAY", "PINK", "PINK/REVERSE", "BLUE", "BLUE/REVERSE",
					"PINK/UNDERSCORE", "PINK/UNDERSCORE/REVERSE", "BLUE/UNDERSCORE",
					"NONDISPLAY" ];
		return names;
	},

	attributeNames:function() {
		if(this.isTE3270()) {
			return this.attributeNames3270();
		} else {
			return this.attributeNames5250();
		}
	},

	allTPXFiles:function() {
		var key;
		var tpxFiles = new Array();
		for(key in localStorage) {
			if(key.startsWith(this.tpxPrefix)) {
				var newTPX = new cenTPXSettings();
				newTPX.load(key);
				tpxFiles.push(newTPX);
			}
		}
		
		return tpxFiles;
	},
	
	allTPXFilenames:function() {
		var key;
		var tpxFiles = new Array();
		for(key in localStorage) {
			if(key.startsWith(this.tpxPrefix)) {
				tpxFiles.push(key);
			}
		}
		
		return tpxFiles;
	},
	
	allTPXTitles:function() {
		var key;
		var tpxFiles = new Array();
		for(key in localStorage) {
			if(key.startsWith(this.tpxPrefix)) {
				var newTPX = new cenTPXSettings();
				newTPX.load(key);
				tpxFiles.push(newTPX.tpxSettings['title']);
			}
		}
		
		return tpxFiles;
	},
	
	tpxWithFilename:function(filename) {
		var tpx = new cenTPXSettings();
		tpx.load(filename);
		return tpx;
	},
	
	deleteTPXWithFilename:function(filename) {
		console.log("cenAX.deleteTPXWithFilename: removing " + filename + "\n");
		localStorage.removeItem(filename);
	},
	
	uniqueTPXFilename:function() {
		var tpxFilenames = this.allTPXFilenames();
		var i = 0;
		while(true) {
			var keyCheck = this.tpxPrefix + "." + i.toString(10);
			var found = false;
			for(var j=0; j < tpxFilenames.length; j++) {
				if(tpxFilenames[j] == keyCheck) {
					found = true;
					break;
				}
			}
			
			if(found == false) {
				return keyCheck;
			}
			
			i++;
		}
	},

	objectSize:function(obj) {
	    var size = 0, key;
		for (key in obj) {
	        if (obj.hasOwnProperty(key)) size++;
		}
		return size;
	}
}

// For convenience, monkey patch the string class for some common operations

String.prototype.startsWith = function(prefix) {
    return this.indexOf(prefix) === 0;
}

String.prototype.endsWith = function(suffix) {
    return this.match(suffix+"$") == suffix;
};

if (!String.prototype.format) {
	String.prototype.format = function () {
		var args = arguments;

		return this.replace(/{(\d+)}/g, function (match, number) {
			return (args[number] != undefined) ? args[number] : match;
		});
	};
}