/**
 * 
 */
Map = function() {
	this.map = new Object();
};
Map.prototype = {
	put : function(key, value) {
		this.map[key] = value;
	},
	get : function(key) {
		return this.map[key];
	},
	containsKey : function(key) {
		return key in this.map;
	},
	containsValue : function(value) {
		for ( var prop in this.map) {
			if (this.map[prop] == value)
				return true;
		}
		return false;
	},
	isEmpty : function(key) {
		return (this.length == 0);
	},
	clear : function() {
		for ( var prop in this.map) {
			delete this.map[prop];
		}
	},
	remove : function(key) {
		delete this.map[key];
	},
	keys : function() {
		var keys = new Array();
		for ( var prop in this.map) {
			keys.push(prop);
		}
		return keys;
	},
	values : function() {
		var values = new Array();
		for ( var prop in this.map) {
			values.push(this.map[prop]);
		}
		return values;
	},
	size : function() {
		var count = 0;
		for ( var prop in this.map) {
			count++;
		}
		return count;
	}
};




/**
 * Base64 Encoding & Decoding
 */
var Base64 = new (Class.extend({

	/** Initialize the property */
	keys : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
	
	/** Public method for Base64 encoding */
	encode : function(input)
	{
		var output = "";
		var chr1, chr2, chr3, enc1;
		var enc2, enc3, enc4;
		var i = 0;
		
		input = Base64.utf8_encode(input);
		
		while(i < input.length)
		{
			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);
			
			enc1 = chr1 >> 2;
			enc2 = ((chr1 &  3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;
			
			if(isNaN(chr2))
			{
				enc3 = enc4 = 64;
			}
			else if(isNaN(chr3))
			{
				enc4 = 64;
			}
			
			output += this.keys.charAt(enc1);
			output += this.keys.charAt(enc2);
			output += this.keys.charAt(enc3);
			output += this.keys.charAt(enc4);
		}
		
		return output;
	},
	
	/** Public method for Base64 decoding */
	decode : function(input)
	{
		var output = "";
		var chr1, chr2, chr3, enc1;
		var enc2, enc3, enc4;
		var i = 0;
		
		input = input.replace(/[^A-Za-z0-9+/=]/g, "");
		
		while(i < input.length)
		{
			enc1 = this.keys.indexOf(input.charAt(i++));
			enc2 = this.keys.indexOf(input.charAt(i++));
			enc3 = this.keys.indexOf(input.charAt(i++));
			enc4 = this.keys.indexOf(input.charAt(i++));
			
			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 &  3) << 6) | enc4;
			
			output += String.fromCharCode(chr1);
			
			if(enc3 != 64)
				output += String.fromCharCode(chr2);
			
			if(enc4 != 64)
				output += String.fromCharCode(chr3);
		}
		
		output = Base64.utf8_decode(output);
		
		return output;
	},
	
	/** Public method for UTF-8 encoding */
	utf8_encode : function(input)
	{
		input = input.replace(/\r\n/g, "\n");
		var utftext = "";
		
		for(var n=0; n<input.length; n++)
		{
			var c = input.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 UTF-8 decoding */
	utf8_decode : function(input)
	{
		var string = "";
		var i=0;
		var c=c1=c2=0;
		
		while(i < input.length)
		{
			c = input.charCodeAt(i);
			
			if(c<128){
				string += String.fromCharCode(c);
				i++;
			}else if((c>191)&&(c<224)){
				c2 = input.charCodeAt(i+1);
				string += String.fromCharCode(((c&31)<<6)|(c2&63));
				i += 2;
			}else{
				c2 = input.charCodeAt(i+1);
				c3 = input.charCodeAt(i+2);
				string += String.fromCharCode(((c&15)<<12)|((c2&63)<<6)|(c3&63));
				i += 3;
			}
		}
		
		return string;
	}

	
	
}))();

/**
 * 
 */
$.fn.isEmptyS = function()
{
	var result = (cf_isEmpty($(this).val())) ? true:false;
	
	return result;
}

/**
 * 
 */
$.fn.isEmptyN = function()
{
	var value = cf_number($(this).val());
	var result = (Number(value) == 0) ? true:false;
	
	return result;
}

/**
 * 
 */
$.fn.setChecked = function(value)
{
	$(this).filter("[value='"+value+"']").prop("checked", true);
}

/**
 * 
 */
$.fn.setSelected = function(value)
{
	$(this).find("option[value='"+value+"']").prop("selected", true);
}

/**
 * 
 */
$.fn.setValue = function(value)
{
	$(this).val(value).change();
}

/**
 * 
 */
$.fn.bind.json = function()
{
	var result = {};
	try
	{
		var formvalues = $(this).serializeArray();
		$(formvalues).each(function(idx, item){
			result[item.name] = item.value;
		});
	}
	catch(e)
	{
		console.log("*script exception[fn.bind.json]:"+e.message);
	}
	
	return JSON.stringify(result);
}
