/**
 * <p>Description: </p>
 * 
 * @author tianyu * Modification&Comments on 2005-7-28
 * 
 * @version 1.50
 * 
 * <p>Copyright: Copyright (c) 2004 2005</p>
 * <p>Company: 清华同方科技有限公司</p>
 */
/*Global Variables used by the ezFramework*/

var LF = '\n';
var NBSP = '';

var RunAt = new Object();
RunAt.SERVER = 0;
RunAt.CLIENT = 1;

var SelectMode = new Object();
SelectMode.NONE = 0;
SelectMode.SINGLE = 1;
SelectMode.MULTIPLE = 2;


/*Opens a PopUp Window which shows a datetime picker.*/
var calendar = null;

function popupCalendar(fieldId, locale, formatMask, width, height, template) {
	var obj    = document.getElementById(fieldId);
	var id     = obj.id;
	var value  = (null != obj.value) ? obj.value : '';
	var target = '';

	if (null == width || '' == width) {
		width = 350;
	}
	if (null == height || '' == height) {
		height = 250;
	}
	
	target += template;
	target += '?datetime=' + value;
	target += '&fieldid=' + id;
	target += '&locale=' + locale.toUpperCase();
	target += '&mask=' + formatMask;

	// Check if the window is already opend
	if (null == calendar || calendar.closed) {
		calendar = window.open(target, '', 'width=' + width + ',height=' + height + ',status=no,toolbar=no,location=no,resizable=no,scrollbars=no,menubar=no');
	} else {
		calendar.focus();
	}
}

/*Opens a PopUp Window which shows a color picker.*/
var colorpicker = null;

function popupCPicker(target, fieldId, locale, palette) {
	var obj    = document.getElementById(fieldId);
	var id     = obj.id;
	var value  = (null != obj.value) ? obj.value : '';

	target += '?fieldid=' + id;
	target += '&locale='  + locale.toUpperCase();
	target += '&value='   + value;
	target += '&palette=' + palette;

	// Check if the window is already opend
	if (null == calendar || calendar.closed) {
		colorpicker = window.open(target, '', 'width=255,height=250,status=no,toolbar=no,location=no,resizable=no,scrollbars=no,menubar=no');
	} else {
		colorpicker.focus();
	}
}


/*Provides some helper functions for a textarea*/
function Textarea(id, maxlength, message) {
	var a = arguments;
	this.id                = id;
	this.obj               = document.getElementById(id);
	this.maxlength         = (a.length >= 1) ? a[1] : null;
	this.limited           = (maxlength != null) ? true : false;
	this.infoLine          = (a.length >= 2) ? a[2] : 'Characters remaining: <b>{0}</b>/{1}';    // Text to display for remaining characters
	this.infoLineNode      = null;                                                               // span which contains the text
	this.textWarningtAt    = 0;                                                                  // default 10 percent remaining
	this.CSS_INFOLINE      = 'ltail';
	
	// add eventhandler
	if (null != this.obj && this.limited) {
		this.setUpHandler();
		this.checkLimit();
	}
}
function Textarea_getId() {
	return this.id;
}
function Textarea_getMaxLength() {
	return this.maxlength;
}
function Textarea_setInfoLine(infoLine) {
	this.infoLine = infoLine;
}
function Textarea_setUpHandler() {
	var _textarea = this;
	
	this.obj.onchange = function() {
		_textarea.checkLimit();
	}
	this.obj.onkeyup = function() {
		_textarea.checkLimit();
	}
	this.obj.onkeypress = function() {
		_textarea.checkLimit();
	}
	this.obj.onpaste = function() {
		_textarea.checkLimit();
	}
}
function Textarea_checkLimit() {
	var remaining = parseInt(this.maxlength) - parseInt(this.obj.value.length);
	
	var warning = (remaining < this.textWarningtAt) ? true : false;
	
	if (null == this.infoLineNode) {
		this.createInfoLine();
	}
	
	this.updateInfoLine(warning, remaining);
}
function Textarea_updateInfoLine(warning, remaining) {
	var out = this.infoLine;
	
	if (this.infoLine.indexOf('{0}') != -1) {
		out = out.replace('{0}', remaining);
	}
	if (this.infoLine.indexOf('{1}') != -1) {
		out = out.replace('{1}', this.maxlength);
	}
	
	if (warning) {
		out = out.fontcolor('red');
	}
	
	this.infoLineNode.innerHTML = '<BR>' + out;
}
function Textarea_createInfoLine() {
	var text = document.createTextNode(this.infoLine);
	var span = document.createElement('Span');
	span.appendChild(text);
	span.className = this.CSS_INFOLINE;
	this.obj.parentNode.appendChild(span);
	
	this.infoLineNode = span;
}
function Textarea_encodeHTML(s) {
	s = s.replace(/\&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/\n/g, "<BR>");

	while (/\s\s/.test(s)) {
		s = s.replace(/\s\s/, "&nbsp; ");
	}
	
	return s.replace(/\s/g, " ");
}
function Textarea_insertTag(textareaId, tag) {
	var ta = document.getElementById(textareaId);
	var tagOpen = '[' + tag.toLowerCase() + ']';
	var tagClose = '[/' + tag.toLowerCase() + ']';
	
	if (ie) {
		var selected = document.selection.createRange().text;

		if (selected) {
            var addSpace = false;
            if (selected.charAt(selected.length-1) == ' ') {
                selected = selected.substring(0, selected.length-1);
                addSpace = true;
            }
            document.selection.createRange().text = tagOpen + selected + tagClose + ((addSpace) ? ' ': '');
        } else {
            ta.value += tagOpen + tagClose;
        }
    } else {
        ta.value += tagOpen + tagClose;
    }
    
    ta.focus();
    return;
}
function Textarea_toString() {
	var out = '';
	out += '******** TextArea ******* ' + LF;
	out += 'Id.........: ' + this.id + LF;
	out += 'MaxLenght..: ' + this.maxlength + LF;
	out += 'Limited....: ' + this.limited + LF;
	out += 'InfoLine...: ' + this.infoLine + LF;
	return out;
}
new Textarea();
Textarea.prototype.setUpHandler   = Textarea_setUpHandler;
Textarea.prototype.checkLimit     = Textarea_checkLimit;
Textarea.prototype.createInfoLine = Textarea_createInfoLine;
Textarea.prototype.updateInfoLine = Textarea_updateInfoLine;
Textarea.encodeHTML               = Textarea_encodeHTML;
Textarea.insertTag                = Textarea_insertTag;
Textarea.prototype.toString       = Textarea_toString;

