var report=new String()
function isEmpty(s) {
    return ((s == null) || (s.length == 0))
}

function isWhitespace (s) {
    var whitespace = " \t\n\r";
    var i;
    for (i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if (whitespace.indexOf(c) >= 0) {
            return true;
        }
    }
    return false;
}

function isCharsInBagEx (s, bag) {
    var i,c;
     for (i = 0; i < s.length; i++) {
        c = s.charAt(i);
        if (bag.indexOf(c) > -1)
            return c;
    }
    return "";
}

function BadCharsInBag(s,bag) {
    var i,c;
    for (i = 0; i < s.length; i++) {
        c = s.charAt(i);
        if (bag.indexOf(c) > -1)
            return false;
    }
    return true;
}

function isCharsInBag (s, bag) {
    var i;
    for (i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1)
            return false;
    }
  return true;
}

function isUserName(s) {
    var errorChar;
    var badChar = "><,[]{}?/+=|\\'\":;~!#$%()`";
    if (isEmpty(s)) {
        report=report+"请输入用户名！\n";
        return false;
    }
    if ( isWhitespace(s) ) {
        report=report+"输入的用户名中不能包含空格符，请重新输入！\n";
        return false;
    }
    errorChar = isCharsInBagEx( s, badChar)
    if (errorChar != "" ) {
        report=report+"您输入的用户名" + s+"是无效的用户名,请不要在用户名中输入字符" + errorChar + "!\n请重新输入合法的用户名！\n";
        return false;
    }

    return true;
}

function isEnglish(s) {
    if (isEmpty(s)) {
        report=report+"用户名不能为空！\n";
        return false;
    }
    for(i=0;i<s.length;i++) {
        var c=s.charAt(i);
        if((c>='A'&&c<='Z')||(c>='a'&&c<='z')||(c>='0'&&c<='9')||c=='_') {
            continue;
        }
        else {
            report=report+"用户名必须为英文、下划线或数字！\n";
            return false;
        }
    }
    return true;
}

function isEmail (s) {
   /* if (isEmpty(s))
    {
        report=report+"输入的E-mail地址不能为空，请输入！\n";
        return false;
    }*/
    if(!isEmpty(s)) {
        if (isWhitespace(s)) {
            report=report+"输入的E-mail地址中不能包含空格符，请重新输入！\n";
            return false;
        }

        var i = 1;
        var len = s.length;

        if (len > 30) {
            report=report+"email地址长度不能超过30位!\n";
            return false;
        }

        pos1 = s.indexOf("@");
        pos2 = s.indexOf(".");
        pos3 = s.lastIndexOf("@");
        pos4 = s.lastIndexOf(".");
        if ((pos1 <= 0)||(pos1 == len)||(pos2 <= 0)||(pos2 == len)) {
            report=report+"请输入有效的E-mail地址！\n";
            return false;
        }
        else {
            if( (pos1 == pos2 - 1) || (pos1 == pos2 + 1)
              || ( pos1 != pos3 )  //find two @
              || ( pos4 < pos3 ) ) //. should behind the '@'
            {
                report=report+"请输入有效的E-mail地址！\n";
                return false;
            }
        }

        if ( !isCharsInBag( s, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.-_@")) {
            report=report+"email地址中只能包含字符ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.-_@\n请重新输入！\n" ;
            return false;
        }
    }

    return true;
}

function isPassword (s) {
    if (isEmpty(s)) {
        report=report+"密码不能为空，请输入！\n";
        return false;
    }
    if ( isWhitespace(s) ) {
        report=report+"密码中不能包含空格符，请重新输入！\n";
        return false;
    }

    if ((s.length>12)||(s.length<3)) {
        report=report+"口令不能超过12位也不能少于3位！\n";
        return false;
    }
    return true;
}

function iscid(s) {
    if (isEmpty(s)) {
        report=report+"学号不能为空，请输入！\n";
        return false;
    }

    var validChar = "0123456789";
    if (!isCharsInBag(s, validChar)) {
        report=report+"您输入的学号" + s +"是无效的学号，请输入合法的学号！\n";
        return false;
    }
 
   
   if (s.length>10) {
        report=report+"学号不能超过10位！\n";
        return false;
    }
    return true;
}


function isInt(s, item) {
    if (isEmpty(s)) {
        report=report+item + "不能为空，请输入！\n";
        return false;
    }

    var validChar = "0123456789";
    if (!isCharsInBag(s, validChar)) {
        report=report+"您输入的" + item + s +"是无效的" + item + "，请输入合法的" + item + "！\n";
        return false;
    }

    return true;
}

function isIntEx(s, item, len, bCompare) {
    if (isEmpty(s)) {
        report=report+item + "不能为空，请输入！\n";
        return false;
    }

    var validChar = "0123456789";
    if (!isCharsInBag(s, validChar)) {
        report=report+"您输入的" + item + s +"是无效的" + item + "，请输入合法的" + item + "！\n";
        return false;
    }

    return true;
}

function isValidString(s, des) {
    var errorChar;
    var badChar = "><,[]{}?/+=|\\'\":;~!@#$%^&()`";
    if (isEmpty(s)) {
        report=report+"请输入"+ des +"！\n";
        return false;
    }
    errorChar = isCharsInBagEx( s, badChar)
    if (errorChar != "" ) {
        report=report+"您输入的" + des +"是无效的"+des +",请不要在"+des+"中输入字符" + errorChar + "!请重新输入合法的"+des+"！\n" ;
        return false;
    }

    return true;
 }

function JudgePassword(s1,s2) {
    if (s1==s2)
        return true;
    else {
        report=report+"密码输入不一致！请重新输入！\n";
        return false;
    }
}

function LTrim(s) {
    for(var i=0;i<s.length;i++)
        if(s.charAt(i)!=' ')
            return s.substring(i,s.length);
    return "";
}

function RTrim(s){
    for(var i=s.length-1;i>=0;i--)
        if(s.charAt(i)!=' ')
            return s.substring(0,i+1);
    return "";
}

function Trim(s) {
    return RTrim(LTrim(s));
}

// Add by Songouyang 
function isURL(strURL)
{
	strURL=Trim(strURL.toUpperCase());	

	if(strURL.indexOf("HTTP://")!=0 || strURL.length<8)
	{

		return false;
	}		
	else
		return true;		 
}

//By chenkun
function getFileExtensionName(strFileFullName)
{
	for(var i=strFileFullName.length-1;i>=0;i--)
		if(strFileFullName.charAt(i)=='.')
			return strFileFullName.substring(i+1,strFileFullName.length);
	return '';
}

//member login check
/*function frmLogin_onsubmit()
{
  if (frmLogin.txtUser_id.value=="")
     {alert("用户名不能为空！");
      return false;
     }
  if (frmLogin.txtPasswd.value=="")
     {alert("密码不能为空！");
      return false;
     }
  return true;
}  */


/*function form_onsubmit()
    {
    var username,password,confirmpassword,email;
    username=Trim(document.register_1 .username.value)
    password=Trim(document.register_1.password.value)
    confirmpassword=Trim(document.register_1.confirmpassword.value)
    email=Trim(document.register_1.email.value)
    document.register_1.username.value=username
    document.register_1.password.value=password
    document.register_1.confirmpassword.value=confirmpassword
    document.register_1.email.value=email
    if(isUserName(username)&&isEnglish(username)&&isPassword(password)&&JudgePassword(password,confirmpassword)&&isEmail(email))
        return true;
    else{
        if(report==""||report==null)
            report="您的输入有误，请重新输入！\n";
        alert(report);
        report="";
        return false;
        }
    return true;
    }*/
