
var rootPath = '';

// 指定dwr的loading显示图片
function useLoadingImage(imageSrc) {
  dwr.engine.setPreHook(function() {
    var disabledImageZone = $('disabledImageZone');
    if (!disabledImageZone) {
      disabledImageZone = document.createElement('div');
      disabledImageZone.setAttribute('id', 'disabledImageZone');
      disabledImageZone.style.cssText = 'position:absolute; top:0px; right:0px; z-index:1000; width:100%; height:100%';
      var imageZone = document.createElement('img');
      imageZone.setAttribute('id','imageZone');
      imageZone.setAttribute('src',imageSrc);
      imageZone.style.cssText = 'position:absolute; top:4px; right:4px; background-color:#fff';
      disabledImageZone.appendChild(imageZone);
      document.body.appendChild(disabledImageZone);
    } else {
      $('imageZone').src = imageSrc;
      disabledImageZone.style.visibility = 'visible';
    }
  });
  dwr.engine.setPostHook(function() {
    $('disabledImageZone').style.visibility = 'hidden';
  });
}
try{ useLoadingImage(rootPath+'/image/loading.gif'); }catch(e){}

/* ============================== 日期时间转换 ============================== */
// 将日期转换成 yyyy-MM-dd 格式的文字
Date.prototype.toDateString = function() {
  var result = '';
  if(this!=null && this!='' && this instanceof Date){
    var m = this.getMonth()+1; if(m<10){m='0'+m;}
    var d = this.getDate(); if(d<10){d='0'+d;}
    result = this.getFullYear() + '-' + m + '-' + d;
  }
  return result;
};
// 将日期转换成 hh:mm:ss 格式的文字
Date.prototype.toTimeString = function() {
  var result = '';
  if(this!=null && this!='' && this instanceof Date){
    var h = this.getHours(); if(h<10){h='0'+h;}
    var m = this.getMinutes(); if(m<10){m='0'+m;}
    var s = this.getSeconds(); if(s<10){s='0'+s;}
    result = h + ':' + m + ':' +s;
  }
  return result;
};
// 将日期转换成 yyyy-MM-dd hh:mm:ss 格式的文字
Date.prototype.toDateTimeString = function() {
  var result = '';
  if( this instanceof Date){
    result = this.toDateString() + ' ' + this.toTimeString();
  }
  return result;
};
// 将字符串转换成日期
String.prototype.toDate = function(separator) {
  var d;
  if(this == null || this == ''){
    d = null;
  } else {
    if(!separator) {
      separator = '-'
    }
    var yyyy = this.substring(0,this.indexOf(separator));
    var MM = this.substring(this.indexOf(separator)+1,this.lastIndexOf(separator));
    var dd = this.substring(this.lastIndexOf(separator)+1);
    d = new Date(yyyy,parseInt(MM,10)-1,dd);
  }
  return d;
}

/* ============================== cookie ============================== */
// 保存cookie
function saveCookie(key,val) {
  document.cookie = key+'='+val+';path=/';
}
// 获取cookie
function getCookie(key) {
  var val = '';
  var cookies = document.cookie.split(';');
  for(var i=0;i<cookies.length;i++) {
    if(cookies[i].indexOf(key)>-1 && cookies[i].indexOf('=')>-1) {
      val = decodeURI(cookies[i].substring(cookies[i].indexOf('=')+1));
      break;
    }
  }
  return val;
}
// 删除cookie
function removeCookie(key) {
    var exp = new Date();
    exp.setTime (exp.getTime() - 1);
    document.cookie = key+'=;expires='+ exp.toGMTString()+';path=/';
}
// 删除所有cookie
function removeCookiesAll() {
  var cookies = document.cookie.split(';');
  for(var i=0;i<cookies.length;i++) {
    removeCookie(cookies[i].substring(cookies[i],cookies[i].indexOf('=')));
  }
}

/* ============================== 格纳库 ============================== */

// 显示日志
function log(message) {
  if(!document.getElementById('divLog')){
    var divLog = document.createElement('div');
    divLog.id = 'divLog';
    document.body.appendChild(divLog);
  }
  document.getElementById('divLog').innerHTML += '<div>'+message+'</div>';
}
    
// 设置元素可见性
function toggleVisibility(element,visibility) {
  show = (visibility!=undefined) ? visibility : (element.style.display=='none');
  element.style.display = show?'':'none';
}

// 判断字符串是否是电子邮件格式
String.prototype.isEmail = function() {
  reg=/^([a-zA-Z0-9]+[_|\-|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\-|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/gi;
  return this!='' && this.replace(reg,'')=='';
};

// 设置文本框只允许输入整数
function setIntegerOnly(textCtrl) {
  if(textCtrl.tagName.toLowerCase() != 'input' &&textCtrl.tagName.toLowerCase() != 'textarea') {
    return;
  }
  textCtrl.onchange = function() {
    var s = "";
    for(var i=0;i<textCtrl.value.length;i++) {
      var chr = textCtrl.value.charAt(i);
      if('1234567890'.indexOf(chr)>-1) {
        s+=chr;
      }
    }
    textCtrl.value=s;
  };
}

// 新开窗口显示代码
function openNewWindow(target,content) {
  target = (target!=null)?target:'_blank';
  var newWindow = window.open('', target, '');
  newWindow.document.open('text/html', 'replace');
  //newWindow.opener = null;
  if(content!=null){
    newWindow.document.write(content);
  }
  newWindow.document.close();
  return newWindow;
}