/**
 * 检查表单(发表文章、市民交流)
 */
function FrontPage_Form1_Validator(theForm)
{
  if (theForm.title.value == "")
  {
    alert("请在 主题 域中输入值。");
    theForm.title.focus();
    return (false);
  }

  if (theForm.title.value.realLength() > 60)
  {
    alert("在 主题 域中，请最多输入 60 个字符 或 30 个中文字。");
    theForm.title.focus();
    return (false);
  }

  if (theForm.ttype.selectedIndex < 0)
  {
    alert("请选择 类别 选项中的一项。");
    theForm.ttype.focus();
    return (false);
  }

  if (theForm.qtype.selectedIndex < 0)
  {
    alert("请选择 问题分类 选项中的一项。");
    theForm.qtype.focus();
    return (false);
  }

  if (theForm.Street.value == "0")
  {
    alert("请选择 所属街道 选项中的一项。");
    theForm.Street.focus();
    return (false);
  }

  if (theForm.content.value == "")
  {
    alert("请在 内容 域中输入值。");
    theForm.content.focus();
    return (false);
  }

  if (theForm.content.value.realLength() >6000)
  {
    var i = 0;
    i = theForm.content.value.length;
    alert("在 内容 域中，您共输入了 " + i + " 个字,已超出了文章三千个中文字的限制，请压缩文章内容。");
    theForm.content.focus();
    return (false);
  }

  if (theForm.file1.value.length > 0)
  {
    var i=0,j=0;
    var strtemp = "";
    strtemp = theForm.file1.value;
    i= strtemp.indexOf(".");
    while(i>0)
    { 
      j = i;
      i= strtemp.indexOf(".", j+1);
    } 
    strtemp = strtemp.substring(j);
    strtemp = strtemp.toLowerCase();
    if (!(strtemp == ".txt" || strtemp == ".doc" || strtemp == ".xls" || strtemp == ".jpg" || strtemp == ".gif" || strtemp == ".bmp" || strtemp == ".htm" || strtemp == ".html"))
    {
      alert("文件类型不对，请重新选择。");
      theForm.file1.select();
      return (false);
    }
  }

  if (theForm.file2.value.length > 0)
  {
    var i=0,j=0;
    var strtemp = "";
    strtemp = theForm.file2.value;
    i= strtemp.indexOf(".");
    while(i>0)
    { 
      j = i;
      i= strtemp.indexOf(".", j+1);
    } 
    strtemp = strtemp.substring(j);
    strtemp = strtemp.toLowerCase();
    if (!(strtemp == ".txt" || strtemp == ".doc" || strtemp == ".xls" || strtemp == ".jpg" || strtemp == ".gif" || strtemp == ".bmp" || strtemp == ".htm" || strtemp == ".html"))
    {
      alert("文件类型不对，请重新选择。");
      theForm.file2.select();
      return (false);
    }
  }

  if (theForm.file3.value.length > 0)
  {
    var i=0,j=0;
    var strtemp = "";
    strtemp = theForm.file3.value;
    i= strtemp.indexOf(".");
    while(i>0)
    { 
      j = i;
      i= strtemp.indexOf(".", j+1);
    } 
    strtemp = strtemp.substring(j);
    strtemp = strtemp.toLowerCase();
    if (!(strtemp == ".txt" || strtemp == ".doc" || strtemp == ".xls" || strtemp == ".jpg" || strtemp == ".gif" || strtemp == ".bmp" || strtemp == ".htm" || strtemp == ".html"))
    {
      alert("文件类型不对，请重新选择。");
      theForm.file3.select();
      return (false);
    }
  }
  return (true);
}

//从左往右去空格
function ltrim(stringObj)
{
  while (stringObj.charCodeAt(0) == 32)
  {
    stringObj = stringObj.substring(1,stringObj.length);
  }
  return stringObj;
}

/**
 * 重置表单(发表文章、市民交流)
 */
function FrontPage_Form1_Reset(theForm)
{

  theForm.reset();
  theForm.title.focus();
  return (false);
}

/**
 * 确定信件类型(发表文章、市民交流)
 */
function FrontPage_Form1_type(theRadio)
{
  if (theRadio.value == "2" || theRadio.value == "3" || theRadio.value == "4")
  {
    alert("根据信访条例，请信访人在用户注册时载明真实姓名、联系电话和住址，如不属实，会造成我们无法办理您的信件。");
    theRadio.focus();
  }
  return (true);
}

/**
 * 检查表单(用户登录)
 */
function FrontPage_Form2_Validator(theForm)
{

  if (theForm.name.value == "")
  {
    alert("请在 用户名 域中输入值。");
    theForm.name.focus();
    return (false);
  }

  if (theForm.name.value.realLength() > 12)
  {
    alert("在 用户名 域中，请最多输入 12 个字符 或 6 个中文字。");
    theForm.name.focus();
    return (false);
  }

  if (theForm.password.value == "")
  {
    alert("请在 密码 域中输入值。");
    theForm.password.focus();
    return (false);
  }

  if (theForm.password.value.length < 6)
  {
    alert("在 密码 域中，请至少输入 6 个字符。");
    theForm.password.focus();
    return (false);
  }

  if (theForm.password.value.realLength() > 12)
  {
    alert("在 密码 域中，请最多输入 12 个字符。");
    theForm.password.focus();
    return (false);
  }
  
  if (theForm.rand.value == "")
  {
    alert("请在 验证码 域中输入值。");
    theForm.rand.focus();
    return (false);
  }

  if (theForm.rand.value.length != 4)
  {
    alert("在 验证码 域中，请输入 4 个字符。");
    theForm.rand.focus();
    return (false);
  }

  var checkOK = "0123456789";
  var checkStr = theForm.rand.value;
  var allValid = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  if (!allValid)
  {
    alert("在 验证码 域中，只能输入 数字 字符。");
    theForm.rand.focus();
    return (false);
  }

  return (true);
}

/**
 * 计算字符串的长度(一个汉字两个字符)
 */
String.prototype.realLength = function()
{
  return this.replace(/[^\x00-\xff]/g,"**").length;
}

function checkEmail(str)
{
    if (str.charAt(0) == "." || str.charAt(0) == "@" || str.indexOf('@', 0) == -1
        || str.indexOf('.', 0) == -1 || str.lastIndexOf("@") == str.length-1 || str.lastIndexOf(".") == str.length-1)
      return false;
    else
      return true;
}
