﻿// JScript File

window.undefined = window.undefined;
String.prototype.trimLeft = function(){return this.replace(/^[ ]+/, '');}
String.prototype.trimRight = function(){return this.replace(/[ ]+$/, '');};
String.prototype.trim = function(){return this.trimLeft().trimRight();};
String.prototype.toDateTime = function()
{
}
Date.prototype.toFormatString = function (format)
{
    var f = function(p){return p > 9 ? p : "0" + p};
    var retVal = format || "yyyy-MM-dd hh:mm:ss:fff";

    retVal = retVal.replace(/yyyy/ig, this.getFullYear());
    retVal = retVal.replace(/yy/ig, f(this.getYear() % 100));
    retVal = retVal.replace(/MM/g, f(this.getMonth() + 1));
    retVal = retVal.replace(/M/g, this.getMonth() + 1);
    retVal = retVal.replace(/dd/ig, f(this.getDate()));
    retVal = retVal.replace(/d/ig, this.getDate());
    retVal = retVal.replace(/hh/ig, f(this.getHours()));
    retVal = retVal.replace(/h/ig, this.getHours());
    retVal = retVal.replace(/mm/g, f(this.getMinutes()));
    retVal = retVal.replace(/m/g, this.getMinutes());
    retVal = retVal.replace(/ss/ig, f(this.getSeconds()));
    retVal = retVal.replace(/s/ig, this.getSeconds());
    retVal = retVal.replace(/fff/ig, f(this.getMilliseconds()));
    return retVal;
}


var FCoreTCIT={Element:{},Input:{},PageInfo:{},Search:{},Validate:{},Request:{},Response:{},Function:{},Events:{}};

FCoreTCIT.Function.isSafeType = function (tobj,tkey){if(tkey!=null){return tkey==typeof(tobj);}var blnRetVal=false;switch (typeof(tobj)){case "number": case "string": case "boolean":blnRetVal = true;break;case "object": case "function": case "undefined":break;default:{break;}}return blnRetVal;};
FCoreTCIT.Function.__toArrayHandler = function (){var arr=new Array();for (key in this){if(FCoreTCIT.Function.isSafeType(this[key])){arr.push(this[key])}}return arr;};
FCoreTCIT.Function.getSafeSqlParameterString = function (text){return text.replace(new RegExp(CoreBBS.Page.CallBackSplit, "ig"), '~');};

FCoreTCIT.Tools = {Dictionary:function(){if (FCoreTCIT.PageInfo.IsIE) return new ActiveXObject("Scripting.Dictionary"); return {_key:"prefix_key_",_data:{},exists : function(key) {return this._data[this._key+key] == window.undefined || this._data[this._key+key] == null;},item:function(key) {return this._data[this._key+key];},setValue:function(key, value){this._data[this._key+key] = value;}};}};

FCoreTCIT.PageInfo.IsIE = (navigator.appName=="Microsoft Internet Explorer");
FCoreTCIT.PageInfo.DefaultCultureName = "zh-CN";
FCoreTCIT.PageInfo.CurrentCulture = Sys.CultureInfo.CurrentCulture;
FCoreTCIT.PageInfo.IsDefaultCulture = (FCoreTCIT.PageInfo.CurrentCulture.name.toLowerCase() == FCoreTCIT.PageInfo.DefaultCultureName.toLowerCase());


FCoreTCIT.GetEvent = FCoreTCIT.PageInfo.IsIE? function(base){return window.event;}:function(base){return (base.length>0&&base[0].constructor==Event)?base[0]:null;};
FCoreTCIT.GetEventElement = function(base){var ev=FCoreTCIT.GetEvent(base);if(ev==null)return null; return FCoreTCIT.PageInfo.IsIE?ev.srcElement:ev.target;};
FCoreTCIT.Events.FireMouseEvent = function(object,eventMethod,simpleClick){if(FCoreTCIT.PageInfo.IsIE && (simpleClick!=null && simpleClick==true)){if(typeof(object[eventMethod])=="function"){object[eventMethod]();}}else{var evnt = document.createEvent("MouseEvents");evnt.initEvent("click", true, true);object.dispatchEvent(evnt);}};
FCoreTCIT.Events.RegisterObjectEvent = function(obj,param){if(FCoreTCIT.PageInfo.IsIE){obj.attachEvent("on"+param[0],param[1]);}else{obj.addEventListener(param[0], param[1], false);}};
FCoreTCIT.Events.RemoveObjectEvent = function(obj,param){if(FCoreTCIT.PageInfo.IsIE){obj.detachEvent("on"+param[0],param[1]);}else{obj.removeEventListener(param[0], param[1], false);}};

FCoreTCIT.Input.TextBox = {
    NewTextBox:function(nameObj,hashAttr,value){var o=document.createElement("input");return o;},
    Validate2DateTime:function(){var element=FCoreTCIT.GetEventElement(arguments);if(element==null)return;alert(element.value);},
    Validate2Number:function(fractionDigits){alert('Validate2Number');}
};

FCoreTCIT.Search.MatchFirstWith = function(parent,matchHandler,param){var l=parent.childNodes.length;if(l>100)return null;for(var i=0;i<l;i++){if(matchHandler(param)){return parent.childNodes[i];}}}
FCoreTCIT.Search.FindNodeByTagName = function(el, tag) {var l=el.childNodes.length; if(l>100){return null;}for(var i=0;i<l;i++){if(el.childNodes[i].tagName!=undefined && el.childNodes[i].tagName!=null && el.childNodes[i].tagName.toLowerCase()==tag.toLowerCase()){return el.childNodes[i];}}};
FCoreTCIT.Search.FindNodeById = function(el, id) {var l=el.childNodes.length; if(l>100){return null;}for(var i=0;i<l;i++){var txtId=el.childNodes[i].getAttribute("id");if(txtId!=null&&el.childNodes[i].getAttribute("id").toLowerCase()==id.toLowerCase()){return el.childNodes[i];}}};


//FCoreTCIT.Validate名字空间下的类型。
FCoreTCIT.Validate.ActionType = {UnKnow:0,Custom:1,DigitalCompare:2,RegularExpression:3,RequiredField:4};
FCoreTCIT.Validate.ObjectType = {UnKnow:0,RadioBox:1,CheckBox:2,Select:25,Hidden:40,TextBox:41,TextArea:43,File:44,Button:66,ImageButton:67,Image:68,FCKEditor:143,RadioBoxList:11,CheckBoxList:12};
FCoreTCIT.Validate.CssText = function(outputText, className) {this.innerText = outputText; this.cssClass = className || "";};
FCoreTCIT.Validate.Resource = function(text,errorMesg,optsimpleerrortext,optfocus,optcorrect){var o=new FCoreTCIT.Validate.CssText(""); this.defaultText = text || o; this.errorMessage = errorMesg || this.defaultText || o; this.simpleErrorText = optsimpleerrortext || new FCoreTCIT.Validate.CssText("*"); this.whenFocus = optfocus || this.defaultText || o; this.whenCorrect = optcorrect || this.defaultText || o;}
//静态成员、方法。
FCoreTCIT.Validate._altext = "_SPAN_ERROR_ALT_FLAG";
FCoreTCIT.Validate._getType = function(tagObj) {if (tagObj==null||!FCoreTCIT.Function.isSafeType(tagObj,"object")||FCoreTCIT.Function.isSafeType(tagObj.constructor,"function"))return FCoreTCIT.Validate.ObjectType.UnKnow;switch (tagObj.tagName.toUpperCase()){case "INPUT": switch (tagObj.getAttribute("type").toUpperCase()) {case "RESET": case "SUBMIT": case "BUTTON": return FCoreTCIT.Validate.ObjectType.Button; break;  case "IMAGE": return FCoreTCIT.Validate.ObjectType.ImageButton; break;  case "CHECKBOX": return FCoreTCIT.Validate.ObjectType.CheckBox; break;  case "RADIOBOX": return FCoreTCIT.Validate.ObjectType.RadioBox; break;  case "TEXT": case "PASSWORD": return FCoreTCIT.Validate.ObjectType.TextBox; break;  case "HIDDEN": return FCoreTCIT.Validate.ObjectType.Hidden; break;  case "FILE": return FCoreTCIT.Validate.ObjectType.File; break;} break; case "IMG" : return FCoreTCIT.Validate.ObjectType.Image; break; case "BUTTON": return FCoreTCIT.Validate.ObjectType.Button; break; case "SELECT": return FCoreTCIT.Validate.ObjectType.Select; break; case "TEXTAREA": return FCoreTCIT.Validate.ObjectType.TextArea; break;} return FCoreTCIT.Validate.ObjectType.UnKnow};
FCoreTCIT.Validate.IsInputType = function(objType){return FCoreTCIT.Function.isSafeType(objType,"number") && objType > 0 && (objType % 10) <= 5;}
FCoreTCIT.Validate.IsButtonType = function(objType){return FCoreTCIT.Function.isSafeType(objType,"number") && objType > 0 && (objType % 10) > 5;}
FCoreTCIT.Validate.IsRadioCheckType = function(objType){return FCoreTCIT.Function.isSafeType(objType,"number") && objType > 0 && objType < 10;}
FCoreTCIT.Validate.DoValidate = function (objVi, arg){var isEmpty = objVi.common.target.value.length == 0; if (!objVi.doValidate(arg)) {FCoreTCIT.Validate.SetTrackFocus(objVi.common.target, objVi.common.resource.simpleErrorText, objVi.common.resource.errorMessage.innerText); FCoreTCIT.Validate.UpdateTargetPanel(objVi.common.output, objVi.common.resource.errorMessage); return false;} /**/FCoreTCIT.Validate.ClearTrackFocus(objVi.common.target); FCoreTCIT.Validate.UpdateTargetPanel(objVi.common.output, objVi.common.resource.whenCorrect); return true;};
FCoreTCIT.Validate.SetTrackFocus = function(tagObj, textObj, title){if (tagObj.parentNode == null) return; var siblingList = tagObj.parentNode.childNodes; for (var i=0; i<siblingList.length; i++) {if (siblingList[i].nodeType == 1 && siblingList[i].tagName.toUpperCase() == "SPAN" && siblingList[i].getAttribute("alt") == FCoreTCIT.Validate._altext) return;} var span = document.createElement("SPAN"); span.setAttribute("alt", FCoreTCIT.Validate._altext); span.setAttribute("title", title); span.innerHTML = textObj.innerText; if (textObj.cssClass.length == 0) {span.style.color = "Red";} else {span.className = textObj.cssClass;} if (tagObj.nextSibling != null && FCoreTCIT.Validate._getType(tagObj) != FCoreTCIT.Validate.ObjectType.Hidden) {tagObj.parentNode.insertBefore(span, tagObj.nextSibling);} else {tagObj.parentNode.appendChild(span);}};
FCoreTCIT.Validate.ClearTrackFocus = function(tagObj) {if (tagObj.parentNode == null)  return;var siblingList = tagObj.parentNode.childNodes; for (var i=0; i<siblingList.length; i++) {if (siblingList[i].nodeType == 1 && siblingList[i].tagName.toUpperCase() == "SPAN" && siblingList[i].getAttribute("alt") == FCoreTCIT.Validate._altext){tagObj.parentNode.removeChild(siblingList[i]);/*return;*/}}};
FCoreTCIT.Validate.UpdateTargetPanel = function(dispArea, cssText) {if (dispArea == null) return; dispArea.className = cssText.cssClass; dispArea.innerHTML = cssText.innerText;};
//内联实体。js中的"类"不能被继承。
//-------------------------------------------------------------------------
//名  称： FCoreTCIT.Validate._baseValidator
//功  能：	抽象"类"
//返回值：	void
//参  数：	tagElem:    需验证的目标对象
//参  数：	outElem:    结果的目标输出区域
//参  数：	res:        输出资源
//参  数：	required:   是否必填，默认为true;
//
//补  充： 定义验证类型时，前三行需要复制(因为无法继承)。
//         必须重写(override) this.doValidate(arg) 方法。
//-------------------------------------------------------------------------
FCoreTCIT.Validate._baseValidator = function(tagElem, outElem, res, required) {
    this.target = tagElem;
    this.output = outElem;
    this.resource = res;
    this.inputRequired = (required == window.undefined || required == true);
    this.actionType = FCoreTCIT.Validate.ActionType.UnKnow;//Me
    this.doValidate = function (arg) {return false;};
    this.setObjectType = function(type) {this.objectType = type; this.target.objectType = type;}

    this.setObjectType(FCoreTCIT.Validate._getType(this.target));
};
//用户自定义验证控件
FCoreTCIT.Validate.CustomValidator = function (tagElem, outElem, res, required) {
    var _myself = this; this.__doValidate = function(arg) {FCoreTCIT.Validate.DoValidate(_myself, arg);}; this.__clearTrackFocus = function() {FCoreTCIT.Validate.ClearTrackFocus(_myself.common.target);};
    this.common = new FCoreTCIT.Validate._baseValidator(tagElem, outElem, res, required);
    this.common.actionType = FCoreTCIT.Validate.ActionType.Custom;
    this.handler = null;
    this.doValidate = function (arg)
    {
        if (!this.common.inputRequired && this.common.target.value.length == 0) return true;
        if (this.handler == null) return true;
        return this.handler(_myself, arg);
    };
};
//必填项验证控件
FCoreTCIT.Validate.RequiredFieldValidator = function (tagElem, outElem, res, required) {
    var _myself = this; this.__doValidate = function(arg) {FCoreTCIT.Validate.DoValidate(_myself, arg);}; this.__clearTrackFocus = function() {FCoreTCIT.Validate.ClearTrackFocus(_myself.common.target);};
    this.common = new FCoreTCIT.Validate._baseValidator(tagElem, outElem, res, required);
    this.common.actionType = FCoreTCIT.Validate.ActionType.RequiredField;
    this.doValidate = function (arg)
    {
        if (!this.common.inputRequired && this.common.target.value.length == 0) return true;
        return this.common.target.value.trim().length > 0
    };
};
//数字比较类型验证控件
FCoreTCIT.Validate.DigitalCompareValidator = function (tagElem, outElem, res, required) {
    var _myself = this; this.__doValidate = function(arg) {FCoreTCIT.Validate.DoValidate(_myself, arg);}; this.__clearTrackFocus = function() {FCoreTCIT.Validate.ClearTrackFocus(_myself.common.target);};
    this.common = new FCoreTCIT.Validate._baseValidator(tagElem, outElem, res, required);
    this.common.actionType = FCoreTCIT.Validate.ActionType.DigitalCompare;
    this.max = Number.MAX_VALUE;
    this.min = Number.MIN_VALUE;
    this.fractionDigits = -1;
    this.doValidate = function (arg)
    {
        if (!this.common.inputRequired && this.common.target.value.length == 0) return true;
        var val = parseFloat(this.common.target.value);
        if (isNaN(val)) {/*if (this.common.inputRequired) this.common.target.value="";*/ return false;}
        val = CoreBBS.Functions.ParseFloat(val, this.fractionDigits);
        this.common.target.value = val;
        if (this.max != Number.MAX_VALUE && this.max < val) return false;
        if (this.min != Number.MIN_VALUE && this.min > val) return false;
        return true;
    };
};
FCoreTCIT.Validate.DateTimeValidator = function (tagElem, outElem, res, required) {
    var _myself = this; this.__doValidate = function(arg) {FCoreTCIT.Validate.DoValidate(_myself, arg);}; this.__clearTrackFocus = function() {FCoreTCIT.Validate.ClearTrackFocus(_myself.common.target);};
    this.common = new FCoreTCIT.Validate._baseValidator(tagElem, outElem, res, required);
    this.common.actionType = FCoreTCIT.Validate.ActionType.DigitalCompare;
    this.doValidate = function (arg)
    {
        if (!this.common.inputRequired && this.common.target.value.length == 0) return true;
        return arg == window.undefined || arg == null;
    };
};
FCoreTCIT.Validate.RegularExpressionValidator = function (tagElem, outElem, res, required) {
    var _myself = this; this.__doValidate = function(arg) {FCoreTCIT.Validate.DoValidate(_myself, arg);}; this.__clearTrackFocus = function() {FCoreTCIT.Validate.ClearTrackFocus(_myself.common.target);};
    this.common = new FCoreTCIT.Validate._baseValidator(tagElem, outElem, res, required);
    this.common.actionType = FCoreTCIT.Validate.ActionType.RegularExpression;
    this.doValidate = function (arg)
    {
        if (!this.common.inputRequired && this.common.target.value.length == 0) return true;
        return arg == window.undefined || arg == null;
    };
};

//验证控件管理器 有待补充。
//多策略验证红星问题。需要对tag做分组。
FCoreTCIT.Validate.Manager = function() {
    this.validatorItems = [];
    this.controlorItems = [];
    this.errorMessage = [];
    this.popupError = true;
    this._eventInited = false;

    this.doValidate = function (arg)
    {
        while(this.errorMessage.pop()!=null){};
        for (var i=0; i<this.validatorItems.length; i++)
        {
            if (!FCoreTCIT.Validate.DoValidate(this.validatorItems[i]))
            {
                this.errorMessage.push([this.validatorItems[i].common.target, this.validatorItems[i].common.resource.errorMessage.innerText]);
            }
        }
        if (this.errorMessage.length > 0 && this.popupError)
        {
            alert(this.errorMessage.join("\r\n").replace(/\[[\w ]+\]\,/ig, "-- "));//simple alert.
            for (var i=0; i<this.errorMessage.length; i++) {try {if (this.errorMessage[i][0].objectType == FCoreTCIT.Validate.ObjectType.FCKEditor) try{ var _fck = FCKeditorAPI.GetInstance(this.errorMessage[i][0].id); if (_fck.EditMode==0) _fck.EditorDocument.body.setActive(); else _fck.Focus();}catch(e){} else this.errorMessage[i][0].focus();}catch(e){continue;} break;}
            return false;
        }
        return true;
    };
    this.registerEvnetHandler = function()
    {
        if (this._eventInited) return;
        this._eventInited = true;
        for (var i=0; i<this.validatorItems.length; i++)
        {
            if (this.validatorItems[i].common.objectType != FCoreTCIT.Validate.ObjectType.Hidden)
            {
                if (this.validatorItems[i].common.objectType == FCoreTCIT.Validate.ObjectType.Select)
                    FCoreTCIT.Events.RegisterObjectEvent(this.validatorItems[i].common.target, ["change", this.validatorItems[i].__doValidate]);

                FCoreTCIT.Events.RegisterObjectEvent(this.validatorItems[i].common.target, ["blur", this.validatorItems[i].__doValidate]);
                // FCoreTCIT.Events.RegisterObjectEvent(this.validatorItems[i].common.target, ["focus", this.validatorItems[i].__clearTrackFocus]);
            }
        }
    };
    this.removeEvnetHandler = function()
    {
        if (!this._eventInited) return;
        this._eventInited = false;
        for (var i=0; i<this.validatorItems.length; i++)
        {
            if (this.validatorItems[i].common.objectType != FCoreTCIT.Validate.ObjectType.Hidden)
            {
                if (this.validatorItems[i].common.objectType == FCoreTCIT.Validate.ObjectType.Select)
                    FCoreTCIT.Events.RemoveObjectEvent(this.validatorItems[i].common.target, ["change", this.validatorItems[i].__doValidate]);

                FCoreTCIT.Events.RemoveObjectEvent(this.validatorItems[i].common.target, ["blur", this.validatorItems[i].__doValidate]);
                // FCoreTCIT.Events.RemoveObjectEvent(this.validatorItems[i].common.target, ["focus", this.validatorItems[i].__clearTrackFocus]);
            }
        }
    }
    this.undoValidate = function ()
    {
        if (true || this.errorMessage.length <= 0) for (var i=0; i<this.validatorItems.length; i++) FCoreTCIT.Validate.ClearTrackFocus(this.validatorItems[i].common.target);
        else for (var i=0; i<this.errorMessage.length; i++) FCoreTCIT.Validate.ClearTrackFocus(this.errorMessage[i][0]);
    }
};



FCoreTCIT.Response.ReturnCode = {Success:"200",SuccessButError:"201",BadRequest:"400",AccessDenied:"403",ServerError:"500"};
FCoreTCIT.Response.ResponseProcessor = function(){this.MemberList=new Array();for (var i=1; i<arguments.length; i++){this.MemberList.push(arguments[i]);}this.MemberList.push(arguments[0]);};
FCoreTCIT.Response.ResponseProcessor.prototype.doProcess = function(){if(this.MemberList.length<2)return; this.MemberList.pop()(this.MemberList,arguments);};
FCoreTCIT.Request.QueryString = function(key, url) {return (new RegExp("[\\?\\&\\#]?"+key+"\\=([^\\s\\?\\&\\#]+)","i")).exec((url==window.undefined||url==null) ? window.location.search : url) == null ? "" : RegExp.$1;};
FCoreTCIT.Request.ASynRequestServer = function(actiontype, serverParam, content)
{
    var sendata;
    if (FCoreTCIT.Function.isSafeType(serverParam)){if (serverParam.toString().length > 0) sendata = actiontype + CoreBBS.Page.CallBackSplit + serverParam;}
    else if (serverParam != null && serverParam.join != window.undefined){sendata = actiontype + CoreBBS.Page.CallBackSplit + serverParam.join(CoreBBS.Page.CallBackSplit);}
    else{sendata = actiontype;}
    CallServer(sendata, FCoreTCIT.Function.isSafeType(content) ? actiontype + CoreBBS.Page.CallBackSplit + content : content);
};
