﻿// JScript 文件
var CheckType = new ValidateTypeList();
//验证数据类型枚举
function ValidateTypeList()
{
    this.CheckInt = "Int";
    this.CheckEmpty = "Empty";
    this.CheckDate = "Date";
    this.CheckEqual = "Equal";
    this.CheckRegExp = "RegExp";
    this.CheckFloat = "Float";
}
//表单验证控件
//                  当chkType="RegExp"是，参数equId 变为正则表达式对象 
//                  控件ID  验证类型  是否获得焦点  提示信息  判等控件ID  是否Trim  当值为空时，是否跳过
function FormControl(id,    chkType,  canFocus,     msg,      equId,      bTrim,   bEmptyPass)
{
    this.Id = id;
    this.CheckTypeAttribute = chkType;
    this.CanFocus = (canFocus == null) ? false : canFocus ;
    this.Message = (msg == null) ? "" : msg ;
    this.EqualControlId = equId;
    this.Reg = equId;
    this.Trim = (bTrim == null) ? false : bTrim;
    this.EmptyPass = (bEmptyPass == null) ? false : bEmptyPass;
}
//验证控件列表
function CheckControlList(controlList)
{
    var result = true;
    for(var i=0; i < controlList.length; i++)
    {
        var control = controlList[i];
        result = ControlCheck(control);
        if (!result)
        {
            if (control.Message != "")
            {
                alert(control.Message);
            }
            if (control.CanFocus)
            {
                var obj = GetElementById(control.Id)
                obj.focus();
            }
            break;
        }
    }
    return result;
}
//验证单个控件
function ControlCheck(control)
{
    var result = false;
    var obj = GetElementById(control.Id)
    if (obj != null)
    {
        if (control.Trim == true)
        {
            obj.value = obj.value.trim();
        }
        if ((obj.value == "") && (control.EmptyPass == true) && (control.CheckTypeAttribute != CheckType.CheckEmpty))
        {
            result = true;
        }
        else
        {
            switch(control.CheckTypeAttribute)
            {
                case CheckType.CheckInt:
                    result = obj.value.IsInt();
                    break;
                case CheckType.CheckEmpty:
                    result = !(obj.value.IsNullOrEmpty());
                    break;
                case CheckType.CheckDate:
                    result = obj.value.IsDate();
                    break;
                case CheckType.CheckEqual:
                    var equalControl = GetElementById(control.EqualControlId);
                    if (equalControl != null)
                    {
                        if (control.Trim == true)
                        {
                            equalControl.value = equalControl.value.trim();
                        }
                        result = obj.value.Equals(equalControl.value);
                    }
                    break;
                case CheckType.CheckRegExp:
                    result = control.Reg.test(obj.value);
                    break;
                case CheckType.CheckFloat:
                    result = obj.value.IsNumber();
                    break;
            }
        }
    }
    return result;
}
/////////////////////////////////////////////////////////////

function FormSubmit()
{
    document.forms[0].submit();
}

function GetElementById(eleId)
{
    var obj = document.getElementById(eleId);
    if (obj == null)
    {
        alert("未能获取到对象" + eleId);
    }
    return obj;
}

function GetFormValue(formId)
{
    var result = "";
    var obj = GetElementById(formId);
    if (obj != null)
    {
        result = obj.value;
    }
    return result;
}

function GetFormValueAsInt(formId,defaultValue)
{   
    var result = defaultValue;
    var obj = GetElementById(formId);
    if (obj != null)
    {
        if (obj.value.IsInt())
        {
            result = obj.value.trim();
        }
    }
    return result;
}

function GetQueryStringValue(querystringId)   
{   
      var reg = new RegExp("(^|&)" + querystringId + "=([^&]*)(&|$)");   
  //  var r = document.URL.substr(1).match(reg);
      var r = window.location.search.substr(1).match(reg);
      var result = null;
      if (r != null)
      {   
          result = unescape(r[2]);
      }
      return result;
}

function DisplayBlock(id,value)
{
    var obj = GetElementById(id);
    if (obj != null)
    {
        obj.style.display = value;
    }
}

function ChangeDisplay(id)
{
    var obj = GetElementById(id);
    if (obj != null)
    {
        if (obj.style.display == "none")
        {
            obj.style.display = "block";
        }
        else
        {
            obj.style.display = "none";
        }
    }
}

function SetStyleClass(id,className)
{
    var obj = GetElementById(id);
    obj.setAttribute("class",className);
    obj.setAttribute("className",className);
}

function SetCheckedByValue(id, value)
{
    var obj = GetElementById(id);
    if (value == 0)
    {
        obj.checked = false;
    }
    else
    {
        obj.checked = true;
    }
}

function SetCheckedByValueInGroup(name,value)
{
    var objList = document.getElementsByName(name);
    var length = objList.length;
    value = value.trim().toLowerCase();
    for (var i = 0;i < length; i++)
    {
        if (value == objList[i].value.toLowerCase())
        {
            objList[i].checked = true;
        }
    }
}

function GetCheckedValueInGroup(name)
{
    var objList = document.getElementsByName(name);
    var value = null;
    for (var i = 0;i < objList.length; i++)
    {
        if (objList[i].checked == true)
        {
            value = objList[i].value;
            break;
        }
    }
    return value;
}

function SetSelectedByValue(id, value)
{
    var obj = GetElementById(id);
    if (obj != null)
    {
        for (var i = 0; i < obj.options.length; i++)
        {
            if (obj.options[i].value == value)
            {
                obj.options[i].selected = true;
                break;
            }        
        }
    }
}

function GetSelectedValue(id)
{
    var itemlist = GetElementById(id);
    var option = itemlist.options[itemlist.selectedIndex];
    return option.value;
}

function CreateElement(tagname,id,value,className,style,type)
{
    var ele = document.createElement(tagname);
    if (id != null)
    {
        ele.setAttribute("id",id);
    }
    if (value != null)
    {
        ele.setAttribute("value",value);
    }
    if (className != null)
    {
        ele.setAttribute("class",className);
        ele.setAttribute("className",className);
    }
    if (style != null)
    {
        ele.setAttribute("style",style);
        ele.style.cssText = style;
    }
    if (type != null)
    {
        ele.setAttribute("type",type);
    }
    return ele;
}

function ClearObjectChildren(id)
{
    var obj = GetElementById(id);
    if(obj != null)
    {
        while(obj.childNodes.length > 0)
        {
            obj.removeChild(obj.childNodes[0]);
        }
    }
}

function SelectAllByIdWidthRegex(re,value)
{
    var nodeList = document.getElementsByTagName("input");
    for (var i = 0; i < nodeList.length; i++)
    {
        var obj = nodeList[i];
        if (re.test(obj.id))
        {
            obj.checked = value;
        }
    }
}

function GetInnerHtmlById(id)
{
    var obj = GetElementById(id);
    return obj.innerHTML;
}

function GetElementsByPreId(preId)
{
    var objElements = new Array();
    var nodeList = document.forms[0].elements;
    var length = nodeList.length;
    var leftlength = preId.length;
    var count = 0;
    for (var i = 0; i < length; i ++)
    {
        var obj = nodeList[i];
        var id = obj.id;
        if (preId == id.substr(0,leftlength))
        {
            objElements[count] = obj;
            count ++;
        }
    }
    return objElements;
}

function RedirectUrl(url)
{
    document.URL = url;
}

function GetBrowserPositionX(obj)
{
    var res = 0;
    while(obj != null)
    {
        res += obj.offsetLeft;
        obj = obj.offsetParent;
    }
    return res;
}

function GetBrowserPositionY(obj)
{
    var res=0;
    while(obj != null)
    {
        res += obj.offsetTop;
        obj = obj.offsetParent;
    }
    return res;
}