/*
 * FormChecker
 * alex 2007-8-16 11:31:34
 */
function FormChecker(form){
	this.f = form;
	this.errNum = 0;
	this.testNull = FC_testNull;
	this.rz = FC_rz;
	this.testSame = FC_testSame;
	this.testRegister = FC_testRegister;
	this.testRegisterAdvanced = FC_testRegisterAdvanced;
	this.testLength = FC_testLength;
	this.testEmail = FC_testEmail;
	this.testIntRage = FC_testIntRage;
	this.testIsInteger = FC_testIsInteger;
	this.sColor = "#FFFFFF";
	this.eColor = "#FFFFCB";
	this.setErr = FC_setErr;
	this.setOk = FC_setOk;
	this.testImgFile = FC_testImageFile;
	this.testUploadFile = FC_testUploadFile;
}
function FC_testUploadFile(fieldName,alertString){
	if(this.errNum==0){
		var fi = this.f[fieldName];
		var value = fi.value;
		if(value.lastIndexOf(".")!=-1)
		 {
		    var fileType = (value.substring(value.lastIndexOf(".")+1,value.length)).toLowerCase();
		    var suppotFile = new Array();
		    suppotFile[0] = "gif";
		    suppotFile[1] = "bmp";
		    suppotFile[2] = "jpg";
			suppotFile[3] = "png";
			suppotFile[4] = "zip";
			suppotFile[5] = "rar";
			suppotFile[6] = "doc";
			suppotFile[7] = "swf";
			suppotFile[8] = "gz";
			suppotFile[9] = "xls";
			suppotFile[10] = "rm";
			suppotFile[11] = "rmvb";
			suppotFile[12] = "mpg";
			suppotFile[13] = "mpeg";
			suppotFile[14] = "avi";
			suppotFile[15] = "wma";
			suppotFile[16] = "flv";
		    suppotFile[17] = "jpeg";
		    for(var i =0;i<suppotFile.length;i++){
		        if(suppotFile[i]==fileType){
		     		this.setOk(fi,alertString);
					return ;
		    	}else{
		  			continue;
		    	}
		  }
		  	this.setErr(fi,alertString);
		 }else
		 {
		 	this.setErr(fi,alertString);
		 }
	}
}
function FC_testImageFile(fieldName,alertString){
	if(this.errNum==0){
		var fi = this.f[fieldName];
		var value = fi.value;
		if(value.lastIndexOf(".")!=-1)
		 {
		    var fileType = (value.substring(value.lastIndexOf(".")+1,value.length)).toLowerCase();
		    var suppotFile = new Array();
		    suppotFile[0] = "gif";
		    suppotFile[1] = "bmp";
			suppotFile[2] = "png";
		    suppotFile[3] = "jpg";
		    suppotFile[4] = "jpeg";
		    for(var i =0;i<suppotFile.length;i++){
		        if(suppotFile[i]==fileType){
		     		this.setOk(fi,alertString);
					return ;
		    	}else{
		  			continue;
		    	}
		  }
		  	this.setErr(fi,alertString);
		 }else
		 {
		 	this.setErr(fi,alertString);
		 }
	}
}

function FC_testNull(fieldName,alertString){
	if(this.errNum==0){
		var fi = this.f[fieldName];
		if(fi.value==""){
			this.setErr(fi,alertString);
		}else{
			this.setOk(fi,alertString);
		}
	}
}
function FC_testSame(fieldName1,fieldName2,alertString){
	if(this.errNum==0){
		var fi = this.f[fieldName1];
		var fi2 = this.f[fieldName2];
		if(fi.value!=fi2.value){
			this.setErr(fi2,alertString);
		}else{
			this.setOk(fi2,alertString);
		}
	}
}
function FC_testLength(fieldName,min,max,alertString){
	if(this.errNum==0){
		var fi = this.f[fieldName];
		if(fi.value.length>max || fi.value.length<min){
			this.setErr(fi,alertString);
		}else{
			this.setOk(fi,alertString);
		}
	}
}
function FC_testRegister(fieldName,alertString){
	if(this.errNum==0){
		var fi = this.f[fieldName];
		var reg = /^[\u4e00-\u9fa5a-zA-Z0-9_]*$/;
		if(!reg.test(fi.value)){
			this.setErr(fi,alertString);
		}else{
			this.setOk(fi,alertString);
		}
	}
}
function FC_testRegisterAdvanced(fieldName,alertString, reg) {
	if(this.errNum==0){
		var fi = this.f[fieldName];
		if(!reg.test(fi.value)){
			this.setErr(fi,alertString);
		}else{
			this.setOk(fi,alertString);
		}
	}
}
function FC_testIsInteger(fieldName,alertString){
	if(this.errNum==0){
		var fi = this.f[fieldName];
		var reg = /\d/;
		if(!reg.test(fi.value)){
			this.setErr(fi,alertString);
		}else{
			this.setOk(fi,alertString);
		}
	}
}
function FC_testEmail(fieldName,alertString){
	if(this.errNum==0){
		var fi = this.f[fieldName];
		var reg = /[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/i;
		if(!reg.test(fi.value)){
			this.setErr(fi,alertString);
		}else{
			this.setOk(fi,alertString);
		}
	}
}
function FC_testIntRage(fieldName,min,max,alertString){
	if(this.errNum==0){
		var fi = this.f[fieldName];
		if(fi.value>max || fi.value<min){
			this.setErr(fi,alertString);
		}else{
			this.setOk(fi,alertString);
		}
	}
}
function FC_setErr(fi,alertString){
	alert(alertString);
	this.errNum++;
	fi.focus();
	fi.style.background = this.eColor;
	fi.title = alertString;
}
function FC_setOk(fi,alertString){
	fi.style.background = this.sColor;
	fi.title = "";
}
function FC_rz(){
	if(this.errNum>0){
		return false;
	}else{
		return true;
	}
}