var report=new String();


function ISEmpty(s) {
    return ((s == null) || (s.length == 0))
}


function ttest(s){
	if(ISEmpty(s))
	{
	alert("empty");
	return false;
	}
	return true;
}

function ISNumber(inputVal){
	inputStr =inputVal.toString();
	oneDecimal =false;
	for(var i=0;i<inputStr.length;i++){
		var oneChar =inputStr.charAt(i);
		
		if(oneChar=="."&&!oneDecimal){
			oneDecimal =true;
			continue;
		}
		if(oneChar<"0" || oneChar>"9"){
			return false;
		}
	}
	return true;
}

function IsNumeric(theText)
{
    for (var  i=0;i<theText.length;i++)
    {
	var strTemp= theText.charAt(i)
        if (!(strTemp>='0' && strTemp<='9' || strTemp=='.'))
	    return false;
    }
    return true;
}

function IsDate(strYear,strMonth,strDay)
{
        var Month,day;
	
	if (strMonth.substring(0,1)=="0")
	   Month=strMonth.substring(1,2);
	else
	   Month=strMonth;
	 
	if (strDay.substring(0,1)=="0")
	   Day=strDay.substring(1,2);
	else
	   Day=strDay;
	   
	   
	Year=parseInt(strYear);
	Month=parseInt(Month);
	Day=parseInt(Day);

	if(!(IsNumber(strYear) && IsNumber(strMonth) && IsNumber(strDay)))
		return false;
	if(Month<1 || Month>12)
		return false;
	if(Month==2 && (((Year % 4 ==0) && (Year % 100 != 0)) || (Year % 400 == 0)))
	{
		if(Day<1 || Day>Days[Month-1]+1)
			return false;
	}
	else
	{
		if(Day<1 || Day>Days[Month-1])
			return false;
	}
			
	return true;
}

function InterDays(B,E)
{
	var intDays=0;
	
	if(E[0]<B[0])
		return false;
	else
		if(E[0]==B[0])
			if(E[1]<B[1])
				return false;
			else
				if(E[1]==B[1])
					if(E[2]<B[2])
						return false;
	for(var i=B[0];i<E[0];i++)
	{
		if(B[1]<=2 && B[2]<=31)
		{
			if(i % 4 ==0)
				intDays+=366;
			else
				intDays+=365;
		}
		else
		{
			if((i+1) % 4 ==0)
				intDays+=366;
			else
				intDays+=365;
		}
	}
	if(B[1]<=E[1])
	{
		for(i=B[1];i<E[1];i++)
		{
			if(i==2 && E[0] % 4 == 0)
				intDays+=Days[i-1]+1;
			else
				intDays+=Days[i-1];
		}
	}
	else
	{
		for(i=E[1];i<B[1];i++)
		{
			if(i==2 && E[0] % 4 == 0)
				intDays-=Days[i-1]+1;
			else
				intDays-=Days[i-1];
		}
	}
	intDays+=E[2]-B[2]+1;
	return intDays;
}


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)) {
        if (ISWhitespace(s)) {
            report=report+"输入的E-mail地址中不能包含空格符，请重新输入！\n";
            return false;
        }

        var i = 1;
        var len = s.length;

        if (len > 50) {
            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<5)) {
        report=report+"口令不能超过12位也不能少于5位！\n";
        return false;
    }
    return true;
}

function ISPasswordVerify(s,t) {
    if (s != t) {
	report="两个密码不同，请重新输入密码";
	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;
    }

    if (bCompare == "=") {
        if (s.length != len) {
            report=report+"您输入的" + item + s +"是无效的" + item + "，必须等于" + len + "位！\n";
            return false;
        }
    }
    else if (bCompare == "<") {
        if (s.length >= len) {
            report=report+"您输入的" + item + s +"是无效的" + item + "，必须小于" + len + "位！\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 '';
}


function AddText(NewCode) {
document.frmInput.txtDescription.value+=NewCode;
}

function showsize(size) {
	var txt;
	var AddTxt;
	txt=prompt("设置字体大小为 "+size,"输入需要设置字体大小的文本"); 
	if (txt!=null) {
		AddTxt="[size="+size+"]"+txt+"[/size="+size+"]";
		AddText(AddTxt);
	}
}

function bold() {
 	var txt;
	var AddTxt;
	txt=prompt("加粗显示.","输入想要加粗显示的文本");
	if (txt!=null) {
		AddTxt="[b]"+txt+"[/b]";
		AddText(AddTxt);
	}

}

function italicize() {
	var txt;
	var AddTxt;
	txt=prompt("设置为斜体","输入需要设置为斜体的文本");
	if (txt!=null) {
		AddTxt="[i]"+txt+"[/i]";
		AddText(AddTxt);
	}

}

function showcolor(color) {
	var txt;
	var AddTxt;
     	txt=prompt("文字颜色设置成 "+color,"输入指定颜色的文本");
	if(txt!=null) {
		AddTxt="["+color+"]"+txt+"[/"+color+"]";
		AddText(AddTxt);
	} 

}

function underline() {
	var txt;
	var AddTxt;
	txt=prompt("输入需加上下划线的文本.","文本");
	if (txt!=null) {
		AddTxt="[u]"+txt+"[/u]";
		AddText(AddTxt);
	}

}

function showfont(font) {
	var txt;
	var AddTxt;
	txt=prompt("文本的字体为 "+font,"输入需设置字体的文本");
	if (txt!=null) {
		AddTxt="[font="+font+"]"+txt+"[/font="+font+"]";
		AddText(AddTxt);
	}

}



function add_position(fileName,position) { //得到ubb代码
	if (position == null){
        position = "left";
	}
	if (position == "center")
		 AddPic = "[div][image="+position+"]" + "../../ASP/newsimage/"+fileName+"[/image][/div]";
	else AddPic = "[image="+position+"]"+ "../../ASP/newsimage/"+fileName+"[/image]";	
	AddText(AddPic);
}

function add_positionEdit(fileName,position) { //得到ubb代码（修改）
	if (position == null){
        position = "left";
	}
	if (position == "center")
		 AddPic = "[image=center]"+ "../../ASP/newsimage/"+fileName+"[/image=center]";
	else AddPic = "[image="+position+"]"+ "../../ASP/newsimage/"+fileName+"[/image]";	
	AddTextEdit(AddPic);
}

function AddText(NewCode) {  //向内容中添加ubb代码
	top.window.document.frmInput.txtDetail.value+=NewCode;
}


function AddLink() 
{  //添加超连接

	if(ISEmpty(document.frmInput.txtInfo.value))		
	{
		alert("Please input Hyperlink Detail");
		return false;
	}		
	if(! ISURL(document.frmInput.txtLink.value))		
	{
		alert("Please input Hyperlink Address");
		return false;
		
	}	
	newText="[url=\""+document.frmInput.txtLink.value+"\"]"+document.frmInput.txtInfo.value+"[/url]";
	document.frmInput.txtDetail.value+=newText;
	return true;
}



function AddTextEdit(NewCode) {  //向内容中添加ubb代码（修改）
	top.window.document.frmInput.txtDetail.value+=NewCode;
}

function delete_position(fileName,position) { //得到ubb代码
	while (true){
		contentText =  top.window.document.frmInput.txtDetail.value;
		pos = contentText.indexOf(fileName);
		if (pos == -1) return;
		beforeText = contentText.substring(0,pos);
		afterText  = contentText.substring(pos+fileName.length);
		deleteText(beforeText+afterText);
	}
}

function delete_positionEdit(fileName,position) { //得到ubb代码(修改)
	fileName="../../ASP/newsimage/"+fileName;	
	while (true){

		contentText =  top.window.document.frmInput.txtDetail.value;
		pos        = contentText.indexOf(fileName);
		if (pos == -1) return;
		beforeText = contentText.substring(0,pos);
		afterText  = contentText.substring(pos+fileName.length);
		deleteTextEdit(beforeText+afterText);
	}
}

function deleteText(NewCode) {      //从内容中删除ubb代码
    NewCode = deleteUBBLeft(NewCode);
    NewCode = deleteUBBCenter(NewCode);
    NewCode = deleteUBBRight(NewCode);
	top.window.document.frmInput.txtDetail.value = NewCode;
}

function deleteTextEdit(NewCode) {  //从内容中删除ubb代码(修改)
    NewCode = deleteUBBLeft(NewCode);
    NewCode = deleteUBBCenter(NewCode);
    NewCode = deleteUBBRight(NewCode);
	top.window.document.frmInput.txtDetail.value = NewCode;
}

function deleteUBBLeft(NewCode) {     //删除左边的ubb
	strLeft  = "[image=left][/image]";
	while (true){
		posLeft = NewCode.indexOf(strLeft);
		if (posLeft == -1) return NewCode;
		beforeText = NewCode.substring(0,posLeft);
		afterText  = NewCode.substring(posLeft+strLeft.length);
		NewCode    = beforeText+afterText;
	}
	return NewCode;
}

function deleteUBBCenter(NewCode) {     //删除中间的ubb
	strCenter = "[image=center][/image=center]";
	while (true){
		posCenter = NewCode.indexOf(strCenter);
		if (posCenter == -1) return NewCode;
		beforeText = NewCode.substring(0,posCenter);
		afterText  = NewCode.substring(posCenter+strCenter.length);
		NewCode    = beforeText+afterText;
	}
	return NewCode;
}

function deleteUBBRight(NewCode) {   //删除右边的ubb
	strRight = "[image=right][/image]";
	while (true){
		posRight = NewCode.indexOf(strRight);
		if (posRight == -1) return NewCode;
		beforeText = NewCode.substring(0,posRight);
		afterText  = NewCode.substring(posRight+strRight.length);
		NewCode    = beforeText+afterText;
	}
	return NewCode;
}
