﻿mousePosX=0
mousePosY=0
function getMouseXY() {
    if (document.all) {
        if (!event)
          return
        try{
           mousePosX = event.clientX + document.body.scrollLeft
           mousePosY = event.clientY + document.body.scrollTop
        }catch(e){}
    } else {
        var e = arguments[0] || null
        if (!e)
          return
        try{
           mousePosX = e.pageX
           mousePosY = e.pageY
        }catch(e){}
    }
}

var _hiddenselect_ = {}
function hideSelect(divObj) {
    if (!document.all)
        return
    if (!_hiddenselect_[divObj])
        _hiddenselect_[divObj] = []
    var selects = document.getElementsByTagName("SELECT")
    for (var i=0;i<selects.length;i++){
        var o = selects[i]
        var oLeft = o.offsetLeft
        var oTop = o.offsetTop
        while(o.offsetParent!=null) { 
            oParent = o.offsetParent 
            oLeft += oParent.offsetLeft
            oTop += oParent.offsetTop
            o = oParent
        }

        var divLeft = Number(divObj.style.left.split('px')[0])
        var divTop = Number(divObj.style.top.split('px')[0])
        if (isOverLap(oLeft,divLeft,oTop,divTop,oLeft+selects[i].clientWidth,divLeft+divObj.clientWidth,oTop+selects[i].clientHeight,divTop+divObj.clientHeight)) {
            if (elmIsHidden(selects[i], divObj)) {
                selects[i].style.visibility = 'hidden'
                _hiddenselect_[divObj][_hiddenselect_[divObj].length] = selects[i]
            }
        }
    }
}

function elmIsHidden(selectObj, divObj) {
   var divObj = divObj || new Object()
   var parentObj = selectObj
   while (parentObj) {
      if (parentObj == divObj || (parentObj.style.visibility && parentObj.style.visibility == 'hidden'))
           return false 
      parentObj = parentObj.parentElement
   }
   return true
}

function showSelect(divObj) {
   if (!document.all)
      return
   var selectList = _hiddenselect_[divObj]
   if (!selectList || !selectList.length)
      return
   for (var i=0;i<selectList.length;i++) {
       selectList[i].style.visibility = ''
   }
}

function isOverLap(leftS,leftD,topS,topD,rightS,rightD,bottomS,bottomD) {
   if (leftS > leftD && leftS < rightD)
      if (bottomS > topD && topS < bottomD)
         return true
   if (leftS < leftD && rightS > leftD)
      if (bottomS > topD && topS < bottomD)
         return true
   if (bottomS > topD && topS < bottomD)
      if (leftS > leftD && leftS < rightD)
         return true
   return false
}

function SelectedAllBox(boo) {
    var frm = document.forms[0]
    var elm = frm.chk
    if (!elm)
        return
    if (typeof elm.length == 'undefined') {
        elm.checked = boo
    }else {
        for (var i=0;i<elm.length;i++) {
            elm[i].checked = boo
        }
    }
}

function SelectedSingleBox(boo) {
    var elm = document.getElementById('chkall')
    if (!elm)
        return
    if (!boo)
        elm.checked = boo
    else {
        var frm = document.forms[0]
        var elms = frm.chk
        if (!elms)
            return
        if (typeof elms.length == 'undefined') {
            elm.checked = elms.checked
        }else {
            for (var i=0;i<elms.length;i++) {
                if (!elms[i].checked) {
                    elm.checked = elms[i].checked
                    return
                }
            }
            elm.checked = true;
        }
    }
}

function CheckHasSelected() {
    var frm = document.forms[0]
    var elm = frm.chk
    if (elm) {
        if (typeof elm.length == 'undefined') {
            if (!elm.checked) {
                alert ("您必须至少选择一项。")
                return false
            }else {
                return elm.checked
            }
        }else {
            for (var i=0;i<elm.length;i++) {
                if (elm[i].checked)
                    return true
            }
        }
    }
    alert ("您必须至少选择一项。")
    return false
}

function OpenWindow(url, winname, width, height, center, params) {
    var center = center || true;
    var left = 0;
    var top = 0;
    if (center) {
        top = (screen.height - height) / 2
        left = (screen.width - width) / 2
    }
    var winname = winname || "_win_" + (new Date()).getTime();
    var s = "width=" + width + ",height=" + height + ",top=" + top + ",left=" + left
    var params = params || []
    if (params.indexOf("menubar") >= 0)
        s += ",menubar=yes"
    else
        s += ",menubar=no"
    if (params.indexOf("resizable") >= 0)
        s += ",resizable=yes"
    else
        s += ",resizable=no"
    if (params.indexOf("scrollbars") >= 0)
        s += ",scrollbars=yes"
    else
        s += ",scrollbars=no"
    if (params.indexOf("titlebar") >= 0)
        s += ",titlebar=yes"
    else
        s += ",titlebar=no"
    var w = window.open(url, winname, s)
    w.focus()
    return w
}

// Extend Array object
if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function(o){
        for(var i=0;i<this.length;i++)
            if(this[i]==o)return i;
        return-1;
    }
}

if (!Array.prototype.removeAt) {
    Array.prototype.removeAt = function(i){
        return this.slice(0,i).concat(this.slice(i+1,this.length))
    }
}

if (!Array.prototype.remove) {
    Array.prototype.remove = function(o){
        var i=this.indexOf(o);
        if(i!= -1) return this.removeAt(i)
        return this
    }
}

// Extend String object
String.prototype.Ltrim = function()
{
    var regEx = /^\s+/g;
    return this.replace(regEx,''); 
} 

String.prototype.Rtrim = function()
{
    var regEx = /\s+$/g;
    return this.replace(regEx,''); 
}

String.prototype.Trim = function()
{
    var regEx = /(^\s+)|(\s+$)/g;
    return this.replace(regEx,''); 
}
String.prototype.HTMLEncode = function()
{
    var objThis = this;
    var oRegExp = new Array(/\x26/g,/\x3c/g,/\x3e/g,/\x3e/g,/\x27/g);
    var oTarget = new Array('&','<','>','"','\'');
    for (var i=0; i<5; i++)
    {
        objThis = objThis.replace(oRegExp[i], oTarget[i]);
    }
    return objThis;
}
String.prototype.encodeStr = function()
{
    var objThis = this;
    objThis = objThis.HTMLEncode();
    objThis = objThis.replace(/\n/g, '<br>');
    objThis = objThis.replace(/\t/g, ' ');
    objThis = objThis.replace(/\s/g, ' ');
    return objThis; 
}

// Extend Number object
Number.prototype.LengthWithZero = function(oCount)
{ 
    var strText = this.toString();
    while (strText.length<oCount)
    {
        strText = '0' + strText; 
    }
    return strText;
}

var Validator = {
    isEmpty: function(s) {
        return (s.Trim() == "")
    },
    isURL: function(s) {
        var reg = /(:\/\/)|(http:\/\/)|(https:\/\/)|([A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*)/
        return reg.test(s)
    },
    isEmail: function(s) {
        var reg = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/
        return reg.test(s)
    },
    isImage: function(file) {
        var s = file.value
        if (s.Trim() == "")
            return true
        s = s.toLowerCase()
        var reg = /.{1,}\.((jpg)|(gif)|(png)|(bmp))$/
        return reg.test(s)
    }
}

function ShowValidatorMsg(id) {
    var validator = document.getElementById(id)
    if (!validator)
        return;
    validator.style.display = ""
    Page_BlockSubmit = true;
}

function HideValidatorMsg(id) {
    var validator = document.getElementById(id)
    if (!validator)
        return;
    validator.style.display = "none"
    Page_BlockSubmit = false;
}

function AjaxValidator(func, s, img, validator) {
    ShowProcessingImg(img)
    var response = Ajax[func](s)
    validator.IsValid = response.value
    if (response.value){
        ShowSuccessImg(img)
    }else {
        ShowFaultImg(img)
    }
}

function ShowProcessingImg(id) {
    var img = document.getElementById(id)
    if (!img)
        return
    img.style.marginLeft = "3px"
    img.src = ThemeRoot + "images/throbber.gif";
    img.style.display = "";
}

function ShowSuccessImg(id) {
    var img = document.getElementById(id)
    if (!img)
        return
    img.src = ThemeRoot + "images/watchdog-ok.gif";
    img.style.display = "";
}

function ShowFaultImg(id) {
    var img = document.getElementById(id)
    if (!img)
        return
    img.src = ThemeRoot + "images/watchdog-error.gif";
    img.style.display = "";
}

function ChangeContentSize(size, elm){ 
    var elm = elm || "content"
    var obj = document.getElementById(elm);
    obj.style.fontSize = size + "px"; 
    obj.style.lineHeight = "150%";
} 

function PreviewImg(imgFile, destid) 
{
    try{
        var img = document.getElementById(destid);
        img.style.width = "350px"
        img.style.height = "200px"
        img.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgFile.value;
    }catch(e){}
} 

