$package("js.util");

js.util.Utils = function (){
}

js.util.Utils.prototype.correctAllPNG = function (){
   for(var i=0; i<document.images.length; i++)
   {
	  js.util.Utils.prototype.correctPNG(document.images[i]);
   }
}

js.util.Utils.prototype.correctPNG = function (img, w, h){
    var arVersion = navigator.appVersion.split("MSIE");
	var version = parseFloat(arVersion[1]);      
	if ((version >= 5.5) && (version < 7) && (document.body.filters)){
		var imgName = img.src;
		var imgNameUp = imgName.toUpperCase();
		var ext = null;
		var pos = imgNameUp.lastIndexOf(".");
		if(pos!=-1)
			ext = imgNameUp.substring(pos+1, pos+4);
		if (ext == "PNG"){
			var LW = img.width;
			var LH = img.height;
			if(!LW) LW = img.offsetWidth;
			if(!LH) LH = img.offsetHeight;
			if(w) LW = w;
			if(h) LH = h;
			img.onload = null;
			img.src = imagePath + "/common/transparent.gif";
			img.__filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+imgName+"', sizingmethod=scale);";
			img.style.filter += img.__filter;
			img.__src = imgName;
			img.style.width = LW;
			img.style.height = LH;
		}
		else{
			if(img.__filter)
				img.style.filter = img.style.filter.replace(img.__filter, "");
		}
	} 
}

js.util.Utils.prototype.correctPNGBackground = function (obj, url){
	var bg;
	if(!url){
		bg = obj.style.backgroundImage;
		bg = bg.replace(/url\(/g, '');
		bg = bg.replace(/\)/g, '');
	}
	else{
		bg = url+"?";
	}
	var imgName = bg.toUpperCase();
	var ext = null;
	var pos = imgName.lastIndexOf(".");
	if(pos!=-1)
		ext = imgName.substring(pos+1, pos+4);
	if (ext == "PNG"){
       obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+bg+"', sizingmethod=scale);" ;
	   if(!url)
			obj.style.background = "none";
	}
}
js.util.Utils.prototype.setImage = function(img, src, w, h){
	if(w) img.style.width = w;
	if(h) img.style.height = h;
	if(browser.isIE){
		if(browser.isIE55_6){
			img.onload = function(){
				js.util.Utils.prototype.correctPNG(this, w, h);
			}
		}
//		if(src)
//			if(src.indexOf("?")==-1)
//				src += "?";
		img.src = src;
	}
	else{
		img.src = src;
	}
}

js.util.Utils.prototype.encodeString = function (text){
    text = text.replace(/%/g, "%25");
    text = text.replace(/ /g, "%20");
    text = text.replace(/\+/g, "%2B");
    text = text.replace(/#/g, "%23");
    text = text.replace(/&/g, "%26");
    text = text.replace(/=/g, "%3D");
    text = text.replace(/\?/g, "%3F");
	return text;
}

js.util.Utils.prototype.getBodyScrollXY = function (){
	var x,y;
	if(document.body.scrollTop){
	  x = document.body.scrollLeft;
	  y = document.body.scrollTop;
	}
	else{
	  x = document.documentElement.scrollLeft;
	  y = document.documentElement.scrollTop;
	}
	return {x:x, y:y};
} 
js.util.Utils.prototype.copyEdit = function(edit) {
	try{
		if(edit.value)
			window.clipboardData.setData ("Text", edit.value); 
		else if(edit.innerHTML)
			window.clipboardData.setData ("Text", edit.innerHTML); 
		browser.messageBox('Copy Sucessfuly!');
	}catch(e){
	   edit.select();
		browser.messageBox('No support clipboard!');
	}
}

js.util.Date = Date;

js.util.Date.prototype.format = function(style) {
}

js.util.Date.prototype.formatDateTime = function(format) { 
  var o = { 
    "M+" : this.getMonth()+1, //month 
    "d+" : this.getDate(),    //day 
    "H+" : this.getHours(),   //hour 
    "m+" : this.getMinutes(), //minute 
    "s+" : this.getSeconds(), //second 
    "q+" : Math.floor((this.getMonth()+3)/3),  //quarter 
    "S" : this.getMilliseconds() //millisecond 
  } 
  if(/(y+)/.test(format)) format=format.replace(RegExp.$1, 
    (this.getFullYear()+"").substr(4 - RegExp.$1.length)); 
  for(var k in o)if(new RegExp("("+ k +")").test(format)) 
    format = format.replace(RegExp.$1, 
      RegExp.$1.length==1 ? o[k] : 
        ("00"+ o[k]).substr((""+ o[k]).length)); 
  return format; 
} 



js.util.Format = function (){
}

js.util.Format.prototype.formatFloat = function (value,mask){
	var action;
	if(browser.isIE)
		action = "formatNumber";
	else
		action = "format-number";
	return js.util.Format.prototype.basicFormat(value, mask, action)
}

js.util.Format.prototype.formatDate = function (varDate, bstrFormat, varDestLocale){
	if(browser.isIE){
		action = "formatDate";
		return js.util.Format.prototype.basicFormat(varDate,bstrFormat,action,varDestLocale);
	}
	else{
		var v = new js.util.Date(varDate);
		v = v.format(bstrFormat);
		return v;
	}
}

js.util.Format.prototype.formatTime = function (varTime, bstrFormat, varDestLocale){
	if(browser.isIE){
		action = "formatTime";
		return js.util.Format.prototype.basicFormat(varTime,bstrFormat,action,varDestLocale);
	}
	else{
		var v = new js.util.Date(varTime);
		v = v.format(bstrFormat);
		return v;
	}
}

js.util.Format.prototype.basicFormat = function (value, mask, action, param){
	var xmlDoc;
	var xslDoc;
	var dom_impl = new js.data.DOMImpl();
	var v='<formats><format><value>'+value+'</value><mask>'+mask+'</mask></format></formats>';
	xmlDoc=dom_impl.parse(v);

	var x; 
	if(browser.isIE)
		x='<xsl:stylesheet xmlns:xsl="uri:xsl">'
	else
		x='<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:math="http://exslt.org/math">';
	x+='<xsl:template match="/">';
	if(isIE) {
		if(action=='formatDate' || action=='formatTime')
			x+='<xsl:eval>'+action+'(new Date("'+value+'").getVarDate(),"'+mask+'"';
		else
			x+='<xsl:eval>'+action+'('+value+',"'+mask+'"';
		if(param)x+=',"'+param+'"';
		x+=')</xsl:eval>';
	}
	else{
		x+='<xsl:value-of select="'+action+'(\''+value+'\',\''+mask+'\')" />';
	}
	x+='</xsl:template></xsl:stylesheet>';
	xslDoc=dom_impl.parse(x);
	var s;
	if(browser.isIE)
		s= xmlDoc.transformNode(xslDoc)
	else{
		//for mozilla/netscape 
        var processor = new XSLTProcessor(); 
		processor.importStylesheet(xslDoc);
		var result = processor.transformToFragment(xmlDoc, xmlDoc);
        var xmls = new XMLSerializer(); 
        s = xmls.serializeToString(result);
	}
	return s;
}

js.util.Format.prototype.formatUnits = function (baseNumber, unitDivisors, unitLabels, singleFractional) {
		var i, unit, unitDivisor, unitLabel;

		if (baseNumber === 0) {
			return "0 " + unitLabels[unitLabels.length - 1];
		}
		
		if (singleFractional) {
			unit = baseNumber;
			unitLabel = unitLabels.length >= unitDivisors.length ? unitLabels[unitDivisors.length - 1] : "";
			for (i = 0; i < unitDivisors.length; i++) {
				if (baseNumber >= unitDivisors[i]) {
					unit = (baseNumber / unitDivisors[i]).toFixed(2);
					unitLabel = unitLabels.length >= i ? " " + unitLabels[i] : "";
					break;
				}
			}
			
			return unit + unitLabel;
		} else {
			var formattedStrings = [];
			var remainder = baseNumber;
			
			for (i = 0; i < unitDivisors.length; i++) {
				unitDivisor = unitDivisors[i];
				unitLabel = unitLabels.length > i ? " " + unitLabels[i] : "";
				
				unit = remainder / unitDivisor;
				if (i < unitDivisors.length -1) {
					unit = Math.floor(unit);
				} else {
					unit = unit.toFixed(2);
				}
				if (unit > 0) {
					remainder = remainder % unitDivisor;
					
					formattedStrings.push(unit + unitLabel);
				}
			}
			
			return formattedStrings.join(" ");
		}
}

js.util.Format.prototype.formatBytes = function (baseNumber) {
	var sizeUnits = [1073741824, 1048576, 1024, 1], sizeUnitLabels = ["GB", "MB", "KB", "bytes"];
	return this.formatUnits(baseNumber, sizeUnits, sizeUnitLabels, true);
	
}

js.util.Format.prototype.formatMinutes = function (minutes){  
    var day = parseInt(Math.floor(minutes / 1440));
    var hour = day >0 
                   ?Math.floor((minutes - day*1440)/60) 
                   :Math.floor(minutes/60);  
    var minute = hour > 0
                      ? Math.floor(minutes -day*1440 - hour*60)
                      :minutes;
    var time="";       
    if (day > 0) time += day + " ";
    if (hour > 0) time += hour + ":";
    if (minute > 0) time += minute + ":";
    return time;
}

js.util.Format.prototype.formatSeconds = function (seconds) {
    if(seconds >0){
        var minutes = Math.floor(seconds/60);
        seconds = seconds - minutes * 60;
        return this.formatMinutes(minutes) + (seconds > 0 ? seconds + "s" : "");
    } 
    return seconds;
}

