﻿/*
 * utils.js 杂项函数
 *
 * Licensed under the terms of the GNU Lesser General Public License:
 * 		http://www.opensource.org/licenses/lgpl-license.php
 *
 * File Authors:
 * 		梁晨(lcpcsky@163.com) & http://ostermiller.org/calc/encode.html
 */

function formatContextTool(str) {
	str = str.replace('/\r/g', '');
	str = str.replace('/\n/g', '');
	str = str.replace(new RegExp(' ',"gm"), '');
	str = str.replace(new RegExp('　',"gm"), '');
	strArray = str.split('\n');
	var retStr = '';
	for (var i = 0; i < strArray.length; i++) {
		retStr += '<p align="left">' + strArray[i] + '</p>';
	}
	return retStr;
}

function formatContext(str) {
	return str;
	strArray = str.split('\n');
	var retStr = '';
	for (var i = 0; i < strArray.length; i++) {
		retStr += '<p align="left">' + strArray[i] + '</p>';
	}
	retStr.replace('/\r/g', '');
	retStr.replace('/\n/g', '');
	return retStr;
}

function getElementHeight(Elem) {
	if(document.getElementById) {
		var elem = document.getElementById(Elem);
	} else if (document.all){
		var elem = document.all[Elem];
	}
	xPos = elem.offsetHeight;
	return xPos;
}
function getElementWidth(Elem) {
	if(document.getElementById) {
		var elem = document.getElementById(Elem);
	} else if (document.all){
		var elem = document.all[Elem];
	}
	xPos = elem.offsetWidth;
	return xPos;
}
function getElementLeft(Elem) {
	var elem;
	if(document.getElementById) {
		var elem = document.getElementById(Elem);
	} else if (document.all){
		var elem = document.all[Elem];
	}
	xPos = elem.offsetLeft;
	tempEl = elem.offsetParent;
	while (tempEl != null) {
		xPos += tempEl.offsetLeft;
  		tempEl = tempEl.offsetParent;
	}
	return xPos;
}
function getElementTop(Elem) {
	if(document.getElementById) {	
		var elem = document.getElementById(Elem);
	} else if (document.all) {
		var elem = document.all[Elem];
	}
	yPos = elem.offsetTop;
	tempEl = elem.offsetParent;
	while (tempEl != null) {
		yPos += tempEl.offsetTop;
  		tempEl = tempEl.offsetParent;
	}
	return yPos;
}

/**************************************************
 * dom-drag.js
 * 09.25.2001
 * www.youngpup.net
 * Script featured on Dynamic Drive (http://www.dynamicdrive.com) 12.08.2005
 * 11.23.2006
 * 梁晨增强版 www.lcsky.org
 **************************************************
 * 11.23.2006
 * 增加改变大小功能
 * 10.28.2001 - fixEd minor bug where events
 * sometimes fired off the handle, not the root.
 **************************************************/
var Drag = {
	obj : null,

	fixE : function(e) {
		if (typeof e == 'undefined') e = window.event;
		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;

		//Internet Explorer and older browsers
		//other browsers provide this, but follow the pageX/Y branch
		if(typeof e.pageX == 'undefined') {
			var xcoord = e.clientX;
			var ycoord = e.clientY;
			if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
				//IE 4, 5 & 6 (in non-standards compliant mode)
				xcoord += document.body.scrollLeft;
				ycoord += document.body.scrollTop;
			} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
				//IE 6 (in standards compliant mode)
				xcoord += document.documentElement.scrollLeft;
				ycoord += document.documentElement.scrollTop;
			}
			e.pageX = xcoord;
			e.pageY = ycoord;
		}
		return e;
	},

	init : function(o, changed, oRoot, minX, maxX, minY, maxY, x, y, w, h, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
	{
		o.changed = changed;
		o.onmousedown	= this.start;
		o.onmousemove	= this.move;

		o.hmode			= bSwapHorzRef ? false : true ;
		o.vmode			= bSwapVertRef ? false : true ;

		o.root = oRoot && oRoot != null ? oRoot : o ;
		o.root.style["left"] = (minX + x) + 'px';
		o.root.style["top"] = (minY + y) + 'px';
		o.root.style["width"] = w + 'px';
		o.root.style["height"] = h + 'px';

		if (o.hmode  && isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
		if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";
		if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";
		if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";

		o.minX	= typeof minX != 'undefined' ? minX : null;
		o.minY	= typeof minY != 'undefined' ? minY : null;
		o.maxX	= typeof maxX != 'undefined' ? maxX : null;
		o.maxY	= typeof maxY != 'undefined' ? maxY : null;

		o.xMapper = fXMapper ? fXMapper : null;
		o.yMapper = fYMapper ? fYMapper : null;

		o.root.onDragStart	= new Function();
		o.root.onDragEnd	= new Function();
		o.root.onDrag		= new Function();
	},

	start : function(e)
	{
		var o = Drag.obj = this;
		e = Drag.fixE(e);
		var ey	= e.pageY;
		var ex	= e.pageX;
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		o.root.onDragStart(x, y);

		o.lastMouseX	= e.pageX;
		o.lastMouseY	= e.pageY;

		if (o.hmode) {
			if (o.minX != null)	o.minMouseX	= e.pageX - x + o.minX;
			if (o.maxX != null)	o.maxMouseX	= o.minMouseX + o.maxX - o.minX;
		} else {
			if (o.minX != null) o.maxMouseX = -o.minX + e.pageX + x;
			if (o.maxX != null) o.minMouseX = -o.maxX + e.pageX + x;
		}

		if (o.vmode) {
			if (o.minY != null)	o.minMouseY	= e.pageY - y + o.minY;
			if (o.maxY != null)	o.maxMouseY	= o.minMouseY + o.maxY - o.minY;
		} else {
			if (o.minY != null) o.maxMouseY = -o.minY + e.pageY + y;
			if (o.maxY != null) o.minMouseY = -o.maxY + e.pageY + y;
		}

		var w = parseInt(o.root.style.width);
		var h = parseInt(o.root.style.height);

		if ((((ex - x) > (w - 10)) || ((ey - y) < 10)) || (((ex - x) < 10) || ((ey - y) > (h - 10)))) {
			o.resizeH = false;
			o.resizeW = false;
			o.resizeTH = false;
			o.resizeLW = false;
			if (((ex - x) > (w - 10)) && ((ey - y) > (h - 10))) {
				o.resizeH = true;
				o.resizeW = true;
			} else if (((ex - x) > (w - 10)) && ((ey - y) < 10)) {
				o.resizeW = true;
				o.resizeTH = true;
			} else if (((ex - x) < 10) && ((ey - y) > (h - 10))) {
				o.resizeH = true;
				o.resizeLW = true;
			} else if (((ex - x) < 10) && ((ey - y) < 10)) {
				o.resizeTH = true;
				o.resizeLW = true;
			} else if ((ex - x) < 10) {
				o.resizeLW = true;
			} else if ((ex - x) > (w - 10)) {
				o.resizeW = true;
			} else if ((ey - y) < 10) {
				o.resizeTH = true;
			} else if ((ey - y) > (h - 10)) {
				o.resizeH = true;
			}
			o.origTop = parseInt(o.root.style.height) + parseInt(o.root.style.top);
			o.origRight = parseInt(o.root.style.width) + parseInt(o.root.style.left);
			document.onmousemove	= Drag.resize;
		} else {
			document.onmousemove	= Drag.drag;
		}

		document.onmouseup		= Drag.end;

		return false;
	},

	drag : function(e)
	{
		var o = Drag.obj;
		e = Drag.fixE(e);

		var ey	= e.pageY;
		var ex	= e.pageX;
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		var w = parseInt(o.root.style.width);
		var h = parseInt(o.root.style.height);
		var nx, ny;

		if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
		if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX - w) : Math.max(ex, o.minMouseX);
		if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
		if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY - h) : Math.max(ey, o.minMouseY);

		nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
		ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));

		if (o.xMapper)		nx = o.xMapper(y)
		else if (o.yMapper)	ny = o.yMapper(x)

		o.root.style[o.hmode ? "left" : "right"] = nx + "px";
		o.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
		o.lastMouseX	= ex;
		o.lastMouseY	= ey;

		o.root.onDrag(nx, ny);
		return false;
	},

	resize : function(e)
	{
		var o = Drag.obj;
		e = Drag.fixE(e);

		var ey	= e.pageY;
		var ex	= e.pageX;
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		var w = parseInt(o.root.style.width);
		var h = parseInt(o.root.style.height);

		if (o.resizeW) {
			if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxX + 5/2*3) : Math.max(ex, o.minMouseX);
		} else {
			if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minX + 4/2) : Math.min(ex, o.maxMouseX);
		}
		if (o.resizeH) {
			if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxY + 5/2*3) : Math.max(ey, o.minMouseY);
		} else {
			if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minY + 4/2) : Math.min(ey, o.maxMouseY);
		}

		/*var nx, ny;

		if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
		if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
		if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
		if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);

		nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
		ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));

		if (o.xMapper)		nx = o.xMapper(y)
		else if (o.yMapper)	ny = o.yMapper(x)*/

		try {
			if (o.resizeW) o.root.style["width"] = (ex - x - 5/2*3) + "px";
			if (o.resizeH) o.root.style["height"] = (ey - y - 5/2*3) + "px";
			if ((o.resizeLW) && (o.origRight - ex + 4/2 > 0)) {
				o.root.style[o.hmode ? "left" : "right"] = (ex - 4/2) + "px";
				o.root.style["width"] = (o.origRight - ex + 4/2) + "px";
			}
			if ((o.resizeTH) && (o.origTop - ey + 4/2 > 0)) {
				o.root.style[o.vmode ? "top" : "bottom"] = (ey - 4/2) + "px";
				o.root.style["height"] = (o.origTop - ey + 4/2) + "px";
			}
		} catch (e) {
		
		}
		/*o.lastMouseX	= ex;
		o.lastMouseY	= ey;

		o.root.onDrag(nx, ny);*/
		return false;
	},

	move : function(e)
	{
		var o = this;
		e = Drag.fixE(e);

		var ey	= e.pageY;
		var ex	= e.pageX;
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		var w = parseInt(o.root.style.width);
		var h = parseInt(o.root.style.height);

		if (((ex - x) > (w - 10)) && ((ey - y) > (h - 10))) {
			o.style.cursor = 'se-resize';
		} else if (((ex - x) > (w - 10)) && ((ey - y) < 10)) {
			o.style.cursor = 'ne-resize';
		} else if (((ex - x) < 10) && ((ey - y) > (h - 10))) {
			o.style.cursor = 'sw-resize';
		} else if (((ex - x) < 10) && ((ey - y) < 10)) {
			o.style.cursor = 'nw-resize';
		} else if ((ex - x) < 10) {
			o.style.cursor = 'w-resize';
		} else if ((ex - x) > (w - 10)) {
			o.style.cursor = 'e-resize';
		} else if ((ey - y) < 10) {
			o.style.cursor = 'n-resize';
		} else if ((ey - y) > (h - 10)) {
			o.style.cursor = 's-resize';
		} else {
			o.style.cursor = 'move';
		}
		return false;
	},

	end : function()
	{
		var o = Drag.obj;
		document.onmousemove = null;
		document.onmouseup   = null;
		o.root.onDragEnd(	parseInt(o.root.style[o.hmode ? "left" : "right"]), 
									parseInt(o.root.style[o.vmode ? "top" : "bottom"]));
		if (typeof(o.changed) != 'undefined') o.changed(o, parseInt(o.root.style.left) - o.minX, parseInt(o.root.style.top) - o.minY, parseInt(o.root.style.width), parseInt(o.root.style.height));
		Drag.obj = null;
	}
};

function trim(str) {
   return str.replace(/^\s*|\s*$/g,"");
}

//在转去的URL后自动补充参数
function FillParameters(link, para) { //Call like this: <a onclick="FillParameters(this, new Array('issue','pageTitle'));" href="?pageNum=1">
	for (var i = 0; i < para.length; i ++) {
		link.href+="&"+para[i]+"="+eval("window."+para[i]);
	}
}

function urlDecode(str){
    str=str.replace(new RegExp('\\+','g'),' ');
    return unescape(str);
}
function urlEncode(str){
    str=escape(str);
    str=str.replace(new RegExp('\\+','g'),'%2B');
    return str.replace(new RegExp('%20','g'),'+');
}

var END_OF_INPUT = -1;

var base64Chars = new Array(
    'A','B','C','D','E','F','G','H',
    'I','J','K','L','M','N','O','P',
    'Q','R','S','T','U','V','W','X',
    'Y','Z','a','b','c','d','e','f',
    'g','h','i','j','k','l','m','n',
    'o','p','q','r','s','t','u','v',
    'w','x','y','z','0','1','2','3',
    '4','5','6','7','8','9','+','/'
);

var reverseBase64Chars = new Array();
for (var i=0; i < base64Chars.length; i++){
    reverseBase64Chars[base64Chars[i]] = i;
}

var base64Str;
var base64Count;
function setBase64Str(str){
    base64Str = str;
    base64Count = 0;
}
function readBase64(){    
    if (!base64Str) return END_OF_INPUT;
    if (base64Count >= base64Str.length) return END_OF_INPUT;
    var c = base64Str.charCodeAt(base64Count) & 0xff;
    base64Count++;
    return c;
}
function encodeBase64(str){
    setBase64Str(str);
    var result = '';
    var inBuffer = new Array(3);
    var lineCount = 0;
    var done = false;
    while (!done && (inBuffer[0] = readBase64()) != END_OF_INPUT){
        inBuffer[1] = readBase64();
        inBuffer[2] = readBase64();
        result += (base64Chars[ inBuffer[0] >> 2 ]);
        if (inBuffer[1] != END_OF_INPUT){
            result += (base64Chars [(( inBuffer[0] << 4 ) & 0x30) | (inBuffer[1] >> 4) ]);
            if (inBuffer[2] != END_OF_INPUT){
                result += (base64Chars [((inBuffer[1] << 2) & 0x3c) | (inBuffer[2] >> 6) ]);
                result += (base64Chars [inBuffer[2] & 0x3F]);
            } else {
                result += (base64Chars [((inBuffer[1] << 2) & 0x3c)]);
                result += ('=');
                done = true;
            }
        } else {
            result += (base64Chars [(( inBuffer[0] << 4 ) & 0x30)]);
            result += ('=');
            result += ('=');
            done = true;
        }
        lineCount += 4;
        if (lineCount >= 76){
            result += ('\n');
            lineCount = 0;
        }
    }
    return result;
}

function readReverseBase64(){   
    if (!base64Str) return END_OF_INPUT;
    while (true){      
        if (base64Count >= base64Str.length) return END_OF_INPUT;
        var nextCharacter = base64Str.charAt(base64Count);
        base64Count++;
        if (reverseBase64Chars[nextCharacter]){
            return reverseBase64Chars[nextCharacter];
        }
        if (nextCharacter == 'A') return 0;
    }
    return END_OF_INPUT;
}

function ntos(n){
    n=n.toString(16);
    if (n.length == 1) n="0"+n;
    n="%"+n;
    return unescape(n);
}

function decodeBase64(str){
    setBase64Str(str);
    var result = "";
    var inBuffer = new Array(4);
    var done = false;
    while (!done && (inBuffer[0] = readReverseBase64()) != END_OF_INPUT
        && (inBuffer[1] = readReverseBase64()) != END_OF_INPUT){
        inBuffer[2] = readReverseBase64();
        inBuffer[3] = readReverseBase64();
        result += ntos((((inBuffer[0] << 2) & 0xff)| inBuffer[1] >> 4));
        if (inBuffer[2] != END_OF_INPUT){
            result +=  ntos((((inBuffer[1] << 4) & 0xff)| inBuffer[2] >> 2));
            if (inBuffer[3] != END_OF_INPUT){
                result +=  ntos((((inBuffer[2] << 6)  & 0xff) | inBuffer[3]));
            } else {
                done = true;
            }
        } else {
            done = true;
        }
    }
    return result;
}