function Utility(){}

Utility.openWindow = function(url, name, feature){
	var win = window.open(url, name, feature);
	win.focus();
	return win;
}

Utility.closeWindow = function(refresh){
	if(refresh == null) refresh = false;
	try
	{
		if(window.opener){ 
			window.opener.focus();
			if(refresh) window.opener.location.reload();
		}
	}
	catch(e){}

	window.close();
}

Utility.checkLoginInvalid = function(login){
	var format = new Array("-", "@","!","#","$","%","^","&","*","(",")","=","+","[","]","{","}","/","","\\","|","<",">","~","`","\"", " ");
	var found = false;
	login = login.toLowerCase();
	var first_char = login.charAt(0);
	if((first_char < "a".charCodeAt()) || (first_char > "z".charCodeAt()))
		return true;
	else
	{
		for(var i=0;i<login.length;i++){
			for(var j=0;j<format.length;j++){
				if(format[j] == login.charAt(i)){
					return true;
					break;
				}
			}
		}
	}

	return false;
}

Utility.checkEmail = function(email){
	if((email.indexOf('@') != -1) && (email.indexOf(' ') == -1)){
		var ext = email.substring(email.indexOf('@'));
		if(ext.indexOf('.') != -1) return true;
	}
	return false;
}

var img_ext = new Array("jpg","jpeg", "gif","png");

 Utility.checkImageExtension = function(fileext){
	return Utility.checkFileExtension(img_ext, fileext);
}

Utility.checkFileExtension = function(acceptlist, filename){
	for(var i = 0;i < acceptlist.length;i++){
		var accept_ext = acceptlist[i];
		var fileext = Utility.getExtension( filename );
		if(fileext != null){
			if(accept_ext.toLowerCase() == fileext.toLowerCase()) return true;
		}
	}

	return false;
}


Utility.getExtension = function( file ){
	if(file.indexOf('.') != -1)
		return file.substring( (file.lastIndexOf('.') + 1) );
	return null;
}


Utility.getLayer = function(name){
	if(document.getElementById(name))
		return document.getElementById(name);
	return null;
}


Utility.isNumber = function(v){
	if(v.indexOf('.') != -1){
		var integer = v.split('.');
		for(var i=0;i<integer.length;i++){
			if(!Utility.isInteger(integer[i])){
				return false;
			}
		}
		return true;
	}
	else
		return Utility.isInteger(v);
}

Utility.isInteger = function(v){
	v = Math.floor(v);
	return !isNaN(v);
}


Utility.compareDate = function( start_date, end_date ){
	var start_y = 0, start_m = 0, start_d = 0, start_h = 0, start_i = 0, start_s = 0;
	var end_y = 0, end_m = 0, end_d = 0, end_h = 0, end_i = 0, end_s = 0;
	var start_ymd = '', start_his = '', end_ymd = '', end_his = '';

	if(start_date.indexOf(' ') != -1){
		var sd = start_date.split(' ');
		start_ymd = sd[0];
		start_his = sd[1].split(':');
		start_h = start_his[0];
		start_i = start_his[1];
		start_s = start_his[2];
	}
	else
		start_ymd = start_date;

	if(end_date.indexOf(' ') != -1){
		var sd = end_date.split(' ');
		end_ymd = sd[0];
		end_his = sd[1].split(':');
		end_h = end_his[0];
		end_i = end_his[1];
		end_s = end_his[2];
	}
	else
		end_ymd = end_date;

	var sd = start_ymd.split('-');
	start_y = parseInt(sd[0]);
	start_m = parseInt(sd[1]);
	start_d = parseInt(sd[2]);

	var ed = end_ymd.split('-');
	end_y = parseInt(ed[0]);
	end_m = parseInt(ed[1]);
	end_d = parseInt(ed[2]);

	var date_start = new Date(start_y, start_m, start_d, start_h, start_i, start_s);
	var date_end = new Date(end_y, end_m, end_d, end_h, end_i, end_s);
//	return 
}

Utility.random = function( len ){
	var charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
	var pwd = '';
	for(var i=0;i<len;i++){
		var n = Math.floor(Math.random() * charset.length);
		pwd += charset.charAt(n);
	}

	return pwd;
}


/********************************************************/
if(!String.prototype.replaceAll){
	String.prototype.replaceAll = function( tag, value ){
		var text = this;
		if(tag != value){ while(text.indexOf( tag ) != -1) text = text.replace( tag , value ); }
		return text;
	}
}

if(!String.prototype.isEmail){
	String.prototype.isEmail = function(){
		var email = this;
		if(email != null && email != ''){
			if((email.indexOf('@') != -1) && (email.indexOf(' ') == -1)){
				var ext = email.substring(email.indexOf('@'));
				if(ext.indexOf('.') != -1) return true;
			}
		}

		return false;
	}
}

if(!String.prototype.toInt){
	String.prototype.toInt = function(default_value){
		if(default_value == null || typeof(default_value) == 'undefined') default_value = 0;
		var text = this;
		if(typeof(text) != 'number') text = parseInt(data);
		if(isNaN(text)) text = default_value;
		return text;
	}
}

if(!String.prototype.toCurrency){
	String.prototype.toCurrency = function(){
		var num = this;
		num = num.replace(/\$|\,/g,'');
		if(isNaN(num)) num = "0";
		sign = (num == (num = Math.abs(num)));
		num = Math.floor(num*100+0.50000000001);
		satang = num%100;
		num = Math.floor(num/100).toString();
		if(satang<10) satang = "0" + satang;
		for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
			num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3));
		return (((sign)?'':'-') + num + '.' + satang);
	}
}

if(!Number.prototype.toCurrency){
	Number.prototype.toCurrency = function() {
		var num = this;
		num = num.toString().replace(/\$|\,/g,'');
		return num.toCurrency();
	}
}
