// JavaScript Document
var nana=new nana_news;
function nana_news()
{
    var xmlHttp = false;
    var e;


    this.user_login=function()
    {
        if(this.check_loginusername()&&this.check_loginuserpass())
        {
            return ;
        }
        else
        {
            alert("用户名和密码必须填写");
            return false;
        }
    }
    this.check_loginusername=function()
    {
             return validateValue("username","用户名必须输入,并且只能在2-16位之间","用户名已输入",/^([a-zA-Z]{1})([a-zA-Z0-9]{2,16})$/);

    }
    this.check_loginuserpass=function()
    {
             return validateValue("userpass","密码必须输入,并且只能在6-16位之间","密码已输入",/^([\w]{6,16})$/);

    }

    var bHasUserName="yes";
    this.user_create = function()
    {
        if(bHasUserName=="no"&&this.check_username_client()&&this.check_userpass()&&this.check_userrepass()&&this.check_companyname())
        {
             return true;  
        }
        else
        {
             alert("对不起，请详细检查各种输入，在保证没有任何错误的情况下然后提交此页");
             return false;
        }
    }

    this.check_username_client = function()
    {
         return validateValue("username","用户名格式不正确，应该以字母开头，包括字母、数字、下划线以内的2-16位字符","用户名输入正确",/^([a-zA-Z]{1})([a-zA-Z0-9]{2,16})$/);
    }
    
    this.check_username=function()
    {
        if(validateValue("username","用户名格式不正确，应该以字母开头，包括字母、数字、下划线以内的2-16位字符","用户名输入正确",/^([a-zA-Z]{1})([a-zA-Z0-9]{2,16})$/))
        {   
          
          try
          {
            if( xmlHttp && xmlHttp .readyState != 0 )
            {
                xmlHttp.abort();
            }
            xmlHttp = getXMLHTTPObj();

            if( xmlHttp )
            {
                var tempObj = getObject("username");
                if ( tempObj == null ) return false;
                var tempExObj=getObject("Exusername");
                if(tempExObj==null) return false;
               
                var url = "../aspx/Register.aspx?action=searchnew&username="+tempObj.value;            
                xmlHttp.open("GET", url, true);
                xmlHttp.onreadystatechange = updatePage; 
                xmlHttp.send(null);
            }
            else
            {
                tempExObj.innerHTML="<font color='red'>XMLHTTP对象创建失败</font>";
            }
          }
          catch (e)
          {
             tempExObj.innerHTML="<font color='red'>查询错误</font>";
          }
              return true;
        }
        else
        {
            return false;
        }
    }
    
    this.check_userpass=function()
    {
         return validateValue("userpass","密码格式不正确，应该包括字母、数字、下划线以内的6-16位字符","密码输入正确",/^([\w]){6,16}$/);
    }
    
    this.check_userrepass=function()
    {
         return validateCompure("userpass","userrepass","两次密码不一致","密码输入正确");
    }
    
    this.check_companyname=function()
    {
         return validateValue("companyname","公司名称不符合要求,应该为6-64个字符之间","公司名称输入正确",/^([\s\S]){6,64}$/);
    }
    
    this.check_mainproduct=function() 
    {
        return validateValue("mainproduct","主营产品应该为4-200之间的字符","主营产品输入正确",/^([\s\S]){4,200}$/);
    }
    
    this.check_ms=function()
    {
    //document.forms[0].doing.options[document.forms[0].doing.options.selectedIndex].text 
        return validateValue("ms1","请选择经营范围","已选择经营范围",/^([\w]){0,200}$/);
    }
    
    this.check_telephone=function()
    {

    return validateValue("telephone","联系电话应为000-00000000或0000-0000000的格式","联系电话输入正确",/^0\d{2,3}-\d{5,9}|0\d{2,3}-\d{5,9}$/);
    }
    this.check_email=function()
    {
         return validateValue("email","email格式不正确","email输入正确",/^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/);
    }
    
    this.check_linkuser=function()
    {
            return validateValue("linkuser","联系人应在2-16个字符之间","联系人输入正确",/^([\w]){2,16}$/);
    }

     this.check_newsreply=function()
    {
         return validateValue("NewsReplyContent","回复内容应该在4-2000之间","内容输入正确",/^([\s\S]){4,2000}$/);
    }

this.check_product=function()
{
    alert("fuck");
    return false;
    if(this.check_productclass()&&this.check_producttitle()&&this.check_productcontent())
    {
        return true;
    }
    return false;
}
this.check_productclass=function()
    {
         return validateValue("ddl_producttype","请选择产品类别","",/^([\s\S]){2,64}$/);
    }


this.check_producttitle=function()
    {
         return validateValue("tb_producttitle","请填写产品标题","",/^([\s\S]){2,64}$/);
    }
this.check_productcontent=function()
    {
         return validateValue("tb_productcontent","请填写产品介绍","",/^([\s\S]){2,64}$/);
    }



//end add new product
    var updatePage=function()
    {
        var tempObj = getObject("username");
        if ( tempObj == null ) return false;
        var tempExObj=getObject("Exusername");
        if(tempExObj==null) return false;

        try
        {
            if (xmlHttp.readyState == 1)
            {
                 tempExObj.innerHTML = "正在加载连接对象......";
            }

            if (xmlHttp.readyState == 2)
            {
                tempExObj.innerHTML = "连接对象加载完毕。";
            }

            if (xmlHttp.readyState == 3)
            {
                tempExObj.innerHTML = "数据获取中......";
            }

            if (xmlHttp.readyState == 4)
            {
                var response = xmlHttp.responseText;
                if(response!="False")
                {
                    tempExObj.innerHTML="<font color='red'>×很遗憾！此用户名不可用</font>";
                    bHasUserName="yes";
                }
                else
                {
                    tempExObj.innerHTML="<font color='green'>√恭喜！此用户名可以使用</font>";
                    bHasUserName="no";
                }
            }
        }
        catch (e)
        {
            tempExObj.innerHTML = "回调处理错误:" + e;
        }
    }

    // end 


    //********************************begin***********************************************
    var getXMLHTTPObj=function()
    {
        var C = null;
        try
        {
            C = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(e)
        {
            try
            {
                C = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(sc)
            {
                C = null;
            }
        }

        if( !C && typeof XMLHttpRequest != "undefined" )
        {
            C = new XMLHttpRequest();
        }

        return C;
    }


    var validateBoolen = function(objID,text,value)
    {
        try
        {
            var tempObj = getObject(objID);
            if ( tempObj == null ) return false;

            if ( tempObj.value == "" )
            {
                //getObject(objID).focus();
                isException(text + "不允许为空" + text);
                return false;
            }

            if(tempObj.value!=value)
            {
                isException(text + "不符合规则！请重新选择" + text);
                return false;
            }
            return true;


        }
        catch(e)
        {
            return false;
        }

    }
    

    var validateCompure = function(objIDone,objIDtwo,exceptiontext,righttext)
    {
        try
        {
            var tempObjone = getObject(objIDone);
            var tempObjtwo=getObject(objIDtwo);

            if ( tempObjone == null ) return false;
            if ( tempObjone.value == "" )
            {
                 //getObject(objID).focus();
                 return false;
            }

            if(tempObjone.value!=tempObjtwo.value)
            {
                   isException(objIDtwo,exceptiontext);
                   return false;

            }
            isRight(objIDtwo,righttext);
            return true;
        }
        catch(e)
        {
            return false;
        }

    }

    var validateValue = function( objID, exceptiontext,righttext, ex )
    {
        try
        {
            var tempObj = getObject(objID);
            if ( tempObj == null ) return false;

            if ( tempObj.value == "" )
            {
                // isException(objID,"");
                //getObject(objID).focus();
                return false;
            }

            if(!tempObj.value.match(ex))
            {
                isException(objID,exceptiontext);
                return false;
            }
            isRight(objID,righttext);
            return true;
        }
        catch(e)
        {
             return false;
        }

    }

    //begin getObjectxxx
    var getObject = function ( objectID )
    {
        if (typeof(document) == "object")
        {
            if ( document.getElementById(objectID) )
            {
                return document.getElementById(objectID);
            }
            else
            {
                return false;
            }
        }
    }
   

    
    var isException = function ( objID, str )
    {

        var tempObj = getObject("Ex"+objID);
        if ( tempObj == null ) return false;
             tempObj.innerHTML="<font color='red'>×"+str+"</font>";
    } 

    
    var isRight = function( objID, str)
    {
        var tempObj = getObject("Ex"+objID);
        if ( tempObj == null ) return false;
        tempObj.innerHTML="<font color='green'>√"+str+"</font>";
        return true;
    }
    
    //************************************end***************************************

}//class end

