function nullToEmpty(obj) {
	return obj == null ? "" : obj;
}

String.prototype.trim = function () {
	return this.replace(/^\s+|\s+$/g, "");
}

function setValue(ctlName, value) {
	var ctlObj = document.getElementById(ctlName);
	var ctlType = ctlObj.type;
	if(ctlType == "text") {
		ctlObj.value = nullToEmpty(value);
	} else if(ctlType == "textarea") {
		ctlObj.value = nullToEmpty(value);
	} else if(ctlType == "select-one") {
		setOptionValue(ctlName, value);
	}
}

function setOptionValue(ctlName, value) {
	if(value == null || value == "") {
		if(document.getElementById(ctlName).length > 0) {
			document.getElementById(ctlName).selectedIndex = 0;
		}
	} else {
		document.getElementById(ctlName).value = value;
	}
}

function setRadioValue(ctlName, value) {
	if(value != null && value != "") {
		var ctls = document.getElementsByName(ctlName);
		if(ctls != null) {
			for(var i = 0; i < ctls.length; i++) {
				if(ctls[i].value == value) {
					ctls[i].checked = true;
					break;
				}
			}
		}
	}
}

function setSelected(ctlName, selected) {
	var ctls = document.getElementsByName(ctlName);
	if(ctls != null) {
		for(var i = 0; i < ctls.length; i++) {
			ctls[i].checked = selected;
		}
	}
}

function getSelectedCount(ctlName) {
	var count = 0;
	var ctls = document.getElementsByName(ctlName);
	if(ctls != null) {
		for(var i = 0; i < ctls.length; i++) {
			if(ctls[i].checked) {
				count++;
			}
		}
	}
	return count;
}

function getFirstSelectedValue(ctlName) {
	var ctls = document.getElementsByName(ctlName);
	if(ctls != null) {
		for(var i = 0; i < ctls.length; i++) {
			if(ctls[i].checked) {
				return ctls[i].value;
			}
		}
	}
	return "";
}

function getDictItem(parentId, targetCtlName, defaultValue, initOptionText, initOptionValue) {
	if(parentId == "0" || parentId == "") {
		var select = document.getElementById(targetCtlName);
		select.length = 0;
		if(initOptionText != null && initOptionValue != null) {
			select.options[0] = new Option(initOptionText, initOptionValue);
		}
	} else {
		var url = "enterprise/getDictItem";
		if(typeof(_urlPrefix) != 'undefined' && _urlPrefix) url = _urlPrefix + url;
		var params = {
				parentId:parentId
			};
		jQuery.ajax({
			type: "post",
			url: url,
			data: params,
			dataType: "json",
			beforeSend: function(XMLHttpRequest){
				//ShowLoading();
			},
			success: function(data, textStatus){
				if(targetCtlName != null) {
					var select = document.getElementById(targetCtlName);
					select.length = 0;
					if(initOptionText != null && initOptionValue != null) {
						select.options[0] = new Option(initOptionText, initOptionValue);
					}
					for(var i = 0; i < data.dictItems.length; i++) {
						select.options[select.length] = new Option(data.dictItems[i].NAME, data.dictItems[i].ID);
					}

					if(defaultValue != null && defaultValue != "") {
						document.getElementById(targetCtlName).value = defaultValue;
					}
				}
			},
			complete: function(XMLHttpRequest, textStatus){
				//HideLoading();
			},
			error: function(){
				//ÇëÇó³ö´í´¦Àí
			}
		});
	}
}

function toggleToPart1(refresh)
{
	document.all["pPart1"].style.display = "block";
	document.all["pPart2"].style.display = "none";
	document.all["AddEditIframe"].src = "include/nullshow.html";
	if(refresh) {
		if(typeof(doRefresh) == "function") {
			doRefresh();
		}
	}
}

function toggleToPart2(sUrl)
{
	document.all["pPart1"].style.display = "none";
	document.all["pPart2"].style.display = "block";
	document.all["AddEditIframe"].src = sUrl;
}

String.prototype.getBytesLength = function () {
    return this.length + (escape(this).split("%u").length - 1);
};

String.prototype.removeCharAt = function (index) {
    var retStr = this;
    if (index <= this.length) {
        if (index === 0) {
            retStr = this.substring(1);
        } else {
            if (index == this.length - 1) {
                retStr = this.substring(0, this.length - 1);
            } else {
                retStr = this.substring(0, index) + this.substring(index + 1);
            }
        }
    }
    return retStr;
};

String.prototype.intercept = function (length) {
    var s = this;
    var len = s.getBytesLength();
    if(len <= length) {
    	return s;
    } else {
        var inLen = len + 2;
    	while (inLen > length) {
            s = s.removeCharAt(s.length - 1);
            inLen = s.getBytesLength() + 2;
        }
        s = s + "...";
        return s;
    }
};