function nextElement(field) {
  var thisForm = field.form;
  for ( var e = 0; e < thisForm.elements.length; e++) {
    if (field == thisForm.elements[e]) {
      break;
    }
  }
  return thisForm.elements[++e % thisForm.elements.length];
}

function gotoNext(evt, field) {
  var keyCode = evt.keyCode;
  if (keyCode == 13 || keyCode == 40) {
    var e = nextElement(field);
    if (e.type == "button") {
      e = nextElement(e);
    }
    if (e.style.display != "none") {
      e.focus();
    }
  }
}

function getRadioValue(field) {
  if (field.length) {
    for ( var i = 0; i < field.length; i++) {
      if (field[i].checked) {
        return field[i].value;
      }
    }
  } else {
    if (field.checked) {
      return field.value;
    }
  }
  return null;
}
var errorStyle = "float:left;border:solid 1px #FF6600;background-color:#FFF2E9;";
var errorImage = " <img src=\"images/index/icon_red.gif\" width=\"14\" height=\"14\" align=\"absmiddle\" /> ";
function showLoading() {
  var loadingPanel = document.getElementById("login_loading");
  if (loadingPanel == null) {
    var el = document.createElement('DIV');
    el.setAttribute("id", "login_loading");
    el.style.cssText = "font-family:Verdana;font-size:11px;border:1px solid #00CC00;background-color:#A4FFA4;padding:1px;position:absolute;right:2px;top:1px;height:14px;z-index:10000";
    el.innerHTML = "Loading...";
    document.body.appendChild(el);
    loadingPanel = el;
  } else {
    loadingPanel.style.display = "";
  }
}

function  hideLoading() {
  var loadingPanel = document.getElementById("login_loading");
  if (loadingPanel) {
    loadingPanel.style.display = "none";
  }
}

function processError(e) {
  var errorDiv = document.getElementById("actionError");
  if (errorDiv) {
    errorDiv.style.cssText = errorStyle;
    errorDiv.innerHTML = errorImage + e;
  }else{// 用于登陆系统后，用户切换时，出错时转向登陆界面
    alert("该用户密码出错或已修改,请重新登陆");
    var url = location.href;
    if(url.indexOf("/main/")!= -1 || url.indexOf("/admin/")!= -1){
      window.location.href="../logout.htm";
    }else{
      window.location.href="logout.htm";
    }
  }
  isSubmitting = false;
  document.getElementById("login_loading").style.display = "none";
}

function showFlashS(focus_width, focus_height, text_height, pics, links, texts) {
  if (focus_width <= 0) {
    focus_width = 558;
  }
  if (focus_height <= 0) {
    focus_height = 190;
  }
  if (text_height <= 0) {
    text_height = 0;
  }
  var swf_height = focus_height + text_height;
  if (pics == "") {
    pics = 'images/login/flash_pic1.jpg|images/login/flash_pic2.jpg|images/login/flash_pic3.jpg|images/login/flash_pic4.jpg';
  }
  document
      .write('<object ID="focus_flash" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="'
          + focus_width + '" height="' + swf_height + '">');
  document
      .write('<param name="allowScriptAccess" value="sameDomain"><param name="movie" value="images/login/pixviewer.swf"><param name="quality" value="high"><param name="bgcolor" value="#FFFFFF">');
  document.write('<param name="menu" value="false"><param name=wmode value="opaque">');
  document.write('<param name="FlashVars" value="pics=' + pics + '&links=' + links + '&texts=' + texts
      + '&borderwidth=' + focus_width + '&borderheight=' + focus_height + '&textheight=' + text_height + '">');
  document
      .write('<embed ID="focus_flash" src="images/login/pixviewer.swf" wmode="opaque" FlashVars="pics='
          + pics
          + '&links='
          + links
          + '&texts='
          + texts
          + '&borderwidth='
          + focus_width
          + '&borderheight='
          + focus_height
          + '&textheight='
          + text_height
          + '" menu="false" bgcolor="#ffffff" quality="high" width="'
          + focus_width
          + '" height="'
          + swf_height
          + '" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />');
  document.write('</object>');
}
function showFlash() {
  showFlashS(0, 0, 0, "", "", "");
}
function ScrollObj(scrollBodyId, scrollBoxId, showHeight, showWidth, lineHeight, stopTime, speed) {
  this.obj = document.getElementById(scrollBodyId);
  this.box = document.getElementById(scrollBoxId);
  this.style = this.obj.style;
  this.defaultHeight = this.obj.offsetHeight;
  this.obj.innerHTML += this.obj.innerHTML;
  this.obj.style.position = "relative";
  this.box.style.height = showHeight;
  this.box.style.overflow = "hidden";
  this.scrollUp = doScrollUp;
  this.stopScroll = false;
  this.curLineHeight = 0;
  this.lineHeight = lineHeight;
  this.curStopTime = 0;
  this.stopTime = stopTime;
  this.speed = speed;
  this.style.top = lineHeight;
  this.object = scrollBodyId + "Object";
  eval(this.object + "=this");
  setInterval(this.object + ".scrollUp()", speed);
  this.obj.onmouseover = new Function(this.object + ".stopScroll=true");
  this.obj.onmouseout = new Function(this.object + ".stopScroll=false");
}
function doScrollUp() {
  if (this.stopScroll == true) {
    return;
  }
  this.curLineHeight += 1;
  if (this.curLineHeight >= this.lineHeight) {
    this.curStopTime += 1;
    if (this.curStopTime >= this.stopTime) {
      this.curLineHeight = 0;
      this.curStopTime = 0;
    }
  } else {
    this.style.top = parseInt(this.style.top) - 1;
    if (-parseInt(this.style.top) >= this.defaultHeight) {
      this.style.top = 0;
    }
  }
}
function loginSystem(obj) {
  // 显示loading条
  showLoading();
  
  var passWord = obj.passWord;
  var scriptLoginURL = obj.passportUrl;
  
  if (obj.loginType == 0) {
    scriptLoginURL += "?action=login";
    scriptLoginURL += "&username=" + obj.userName;
  } else {
    scriptLoginURL += "?action=customLogin";
    scriptLoginURL += "&serviceURL=" + obj.serviceURL;
    scriptLoginURL += "&paramName=" + obj.loginTypeName;
    scriptLoginURL += "&paramValue=" + obj.loginType;
    scriptLoginURL += "&paramValue=" + obj.userName;
  }
  scriptLoginURL += "&server=" + obj.serverId;
  // scriptLoginURL += "&root=" + obj.rootValue;
  scriptLoginURL += "&cookieSaveType=" + obj.saveType;
  if (passWord != "") {
    scriptLoginURL += "&password=" + hex_md5(passWord) + hex_sha1(passWord);
  }
  scriptLoginURL += "&context=" + obj.context + "&multiAccount=1&version=2";
  scriptLoginURL += "&verifyCode=" + obj.verifyCode;
  scriptLoginURL += "&url=" + obj.url ;
  scriptLoginURL += "&v=" + new Date().getTime();

  // 动态创建登录脚本元素, 这样做的目的是因为firefox不支持多次载入某个已经存在的script元素.
  // 如果已经存在登录脚本元素, 则先删除元素
  
  var scriptLogin = document.getElementById("scriptLogin");
  if (scriptLogin) {
      scriptLogin.parentNode.removeChild(scriptLogin);
  }

  // 创建登录脚本元素
  scriptLogin = document.createElement("script");
  scriptLogin.id = "scriptLogin";
  scriptLogin.type = "text/javascript";
  scriptLogin.src = scriptLoginURL;
  document.body.appendChild(scriptLogin);
}

