function Validator() {
	this.verURL = f_validator_URL;
	this.verMAIL = f_validator_MAIL;
	this.verEmpty = f_validator_Empty;
	this.doGetDNS = f_validator_DNS;
	this.doGetCol = f_validator_Col;
	this.verChar = f_validator_CHAR;
	this.trimChar = f_validator_TRIM;
	this.trimCharR = f_validator_TRIM_R;
	this.trimCharL = f_validator_TRIM_L;
	this.verFolder = f_validator_FOLDER;
	this.verPath = f_validator_PATH;
	this.str = "";
}

function f_validator_CHAR(str) {
	var val = false;
	for(var i=0; i<str.length; i++) {
		var ch=str.charCodeAt(i);
		if(ch>32&&ch<147) {
			
		} else {
			val = true;
		}
	}
	return val;
}

function f_validator_FOLDER(str) {
	var val = false;
	if(/\?/.test(str)) {
		var temp = "";
		var temp1 = "";
		for(var i = 0; i<str.length; i++) {
			var ch=str.charCodeAt(i);
			if(ch==63) {
				temp = str.substr(0,i);
				temp1 = str.substr(i,str.length-i);
			}
		}
	} else {
		var temp = str;
	}
	if(/\/\/|\\|<|>|\^|\||\"|:|\.\./.test(temp)) {
		val = true;
	}
	if(/\/|\\|<|>|\^|\||\"|:|;|,| |#|\!|\[|\]|\{|\}|\(|\)|`|\+|\'/.test(temp1)) {
		val = true;
	}
	return val;
}

function f_validator_Empty(str) {
	var val = true;
	if(str != "undefined" && typeof str != "undefined") {
		if(str.length>0) val = false;
	}
	return val;
}

function f_validator_TRIM_L(str) {
	while(str.charCodeAt(0)==32) {
		   str=str.substr(1,str.length-1);
	}
	return str;
}

function f_validator_TRIM_R(str) {
	while(str.charCodeAt(str.length-1)==32) {
		   str=str.substr(0,str.length-1);
	}
	return str;
}

function f_validator_TRIM(str, type) {
	try {
		var cutType = parseInt(type);
		isNaN(cutType) && (cutType = 0);
		if(cutType>2 || cutType<0) {
			cutType = 0;
		}
	} catch(e) {
		var cutType = 0;
	}
	switch (cutType) {
		case 0:
			str = this.trimCharR(str);
			str = this.trimCharL(str);
			break;
		case 1:
			str = this.trimCharR(str);
			break;
		case 2:
			str = this.trimCharL(str);
			break;
	}
	return str;
}

function f_validator_DNS(str) {
	var strTemp = "";
	var DNS = str.match(/^[a-zA-Z0-9\.\-]+/);
	if(DNS) {
		strTemp = DNS[0];
	}
	return strTemp;
}

function f_validator_Col(str) {
	var val = 0;
	var arTemp = new Array();
	arTemp = str.split(".");
	if(arTemp.length>1) {
		var test = true;
		for(var i=0; i<arTemp.length; i++) {
			if(arTemp[i]=="") {
				test = false;
			} else if(arTemp[i].substr(0,1) == "-" || arTemp[i].substr(arTemp[i].length-1,1) == "-") {
				test = false;
			}
		}
		if(test) {
			val = arTemp.length;
		}
	}
	return val;
}

function f_validator_URL(str, type, pref, prefArray) {
	str = this.trimChar(str);
	if(prefArray=="undefined"||typeof prefArray=="undefined") {
		var prefArray = new Array();
	}
	type = parseInt(type);
	if(type<6&&type>(-1)) {
	} else {
		type = 0;
	}
	var val = false;

	switch (type) {
		case 0:
			if(!this.verEmpty(str)) {
				val = doURL0(str, pref, prefArray);
			}
			break;
		case 1:
			if(!this.verEmpty(str)) {
				val = doURL1(str, pref, prefArray);
			}
			break;
		case 2:
			if(!this.verEmpty(str)) {
				val = doURL2(str, pref, prefArray);
			}
			break;
		case 3:
			if(!this.verEmpty(str)) {
				val = doURL3(str, pref, prefArray);
			}
			break;
		case 4:
			if(!this.verEmpty(str)) {
				val = doURL4(str, pref, prefArray);
			}
			break;
		case 5:
			if(!this.verEmpty(str)) {
				val = doURL5(str, pref, prefArray);
			}
			break;
	}
	this.str = str;
	return val;

	function doPref(str, pref, prefArray) {
		val = false;
		if(pref) {
			var test = false;
			if(prefArray.length>0) {
				for(var i=0; i<prefArray.length; i++) {
					try{
						if(str.substr(0,prefArray[i].length).toLowerCase()==prefArray[i].toLowerCase()) {
							test = true;
						}
					} catch(e) {
					}
				}
			}
			if (str.substr(0,7).toLowerCase()=="http://" || 
				str.substr(0,8).toLowerCase()=="https://" || (test && prefArray.length>0)) {
					val = true;
				}
		} else {
			val = true;
		}
		return val;
	}

	function doClearPref(str, prefArray) {
		str = str.replace(/^http:\/\//i,"");
		str = str.replace(/^https:\/\//i,"");
		if(prefArray.length>0) {
			for(var i=0; i<prefArray.length; i++) {
				try{
					if(str.substr(0,prefArray[i].length).toLowerCase()==prefArray[i].toLowerCase()) {
						str = str.substr(prefArray[i].length,str.length-prefArray[i].length);
					}
				} catch(e) {
				}
			}
		}
		return str;
	}

	function doGetPort(str) {
		var val = "";
		var port = str.match(/^:[0-9]+/);
		if(port) {
			if(port[0].length>1&&port[0].length<6) {
				val = port[0];
			}
		}
		return val;
	}

	function doURL0(str, pref, prefArray) {
		var val = false;
		if(doPref(str, pref, prefArray)) {
			str = doClearPref(str, prefArray);
			var dns = f_validator_DNS(str);
			var col = f_validator_Col(dns);
			if(col != 0) {
				var temp = str.replace(dns,"");
				if((temp.length==1&&temp=="/") || (temp=="")) {
					val = true;
				}
			}
		}
		return val;
	}

	function doURL1(str, pref, prefArray) {
		var val = false;
		if(doPref(str, pref, prefArray)) {
			str = doClearPref(str, prefArray);
			var dns = f_validator_DNS(str);
			var col = f_validator_Col(dns);
			if(col != 0) {
				var temp = str.replace(dns,"");
				var port = doGetPort(temp);
				temp = temp.replace(port,"");
				if((temp.length==1&&temp=="/") || (temp=="")) {
					val = true;
				}
			}
		}
		return val;
	}

	function doURL2(str, pref, prefArray) {
		var val = false;
		if(doPref(str, pref, prefArray)) {
			str = doClearPref(str, prefArray);
			var dns = f_validator_DNS(str);
			var col = f_validator_Col(dns);
			if(col != 0) {
				var temp = str.replace(dns,"");
				if(temp.length>0) {
					if(temp.substr(0,1)=="/") {
						val = true;
					}
					if(temp.substr(0,2)=="//") {
						val = false;
					}
					if(f_validator_FOLDER(temp)) {
						val = false;
					}
					if(f_validator_CHAR(temp)) {
						val = false;
					}
				}
			}
		}
		return val;
	}

	function doURL3(str, pref, prefArray) {
		var val = false;
		if(doPref(str, pref, prefArray)) {
			str = doClearPref(str, prefArray);
			var dns = f_validator_DNS(str);
			var col = f_validator_Col(dns);
			if(col != 0) {
				var temp = str.replace(dns,"");
				var port = doGetPort(temp);
				temp = temp.replace(port,"");
				if(temp.length>0) {
					if(temp.substr(0,1)=="/") {
						val = true;
					}
					if(f_validator_FOLDER(temp)) {
						val = false;
					}
					if(temp.substr(0,2)=="//") {
						val = false;
					}
					if(f_validator_CHAR(temp)) {
						val = false;
					}
				} else {
					val = true;
				}
			}
		}
		return val;
	}

	function doURL4(str, pref, prefArray) {
		var val = false;
		if(doPref(str, pref, prefArray)) {
			str = doClearPref(str, prefArray);
			var dns = f_validator_DNS(str);
			var col = f_validator_Col(dns);
			if(col != 0) {
				var temp = str.replace(dns,"");
				var port = doGetPort(temp);
				if(port != "") {
					temp = temp.replace(port,"");
					if((temp.length==1&&temp=="/") || (temp=="")) {
						val = true;
					}
				}
			}
		}
		return val;
	}

	function doURL5(str, pref, prefArray) {
		var val = false;
		if(doPref(str, pref, prefArray)) {
			str = doClearPref(str, prefArray);
			var dns = f_validator_DNS(str);
			var col = f_validator_Col(dns);
			if(col != 0) {
				var temp = str.replace(dns,"");
				var port = doGetPort(temp);
				if(port != "") {
					temp = temp.replace(port,"");
					if(temp.length>0) {
						if(temp.substr(0,1)=="/") {
							val = true;
						}
						if(temp.substr(0,2)=="//") {
							val = false;
						}
						if(f_validator_FOLDER(temp)) {
							val = false;
						}
						if(f_validator_CHAR(temp)) {
							val = false;
						}
					} else {
						val = true;
					}
				}
			}
		}
		return val;
	}
}

function f_validator_MAIL(str, dnsArray) {
	var val = false;
	str = this.trimChar(str);
	if(dnsArray=="undefined"||typeof dnsArray=="undefined") {
		var dnsArray = new Array();
	}
	if(!this.verEmpty(str)) {
		val = doMail(str, dnsArray);
	}
	this.str = str;
	return val;

	function doGetName(str) {
		var val = "";
		var name = str.match(/^[a-zA-Z0-9\.\-_]+@/);
		if(name) {
			if(name[0].substr(0,1) !="." && name[0].substr(name[0].length-2,2) !=".@") {
				val = name[0];
			}
		}
		return val;
	}

	function doMail(str, dnsArray) {
		var val = false;
		var temp = str;
		var name = doGetName(str);
		temp = str.replace(name,"");
		var dns = f_validator_DNS(temp);
		temp = temp.replace(dns,"");
		var col = f_validator_Col(dns);
		if(dnsArray.length>0) {
			for(var i = 0; i<dnsArray.length; i++) {
				if(dnsArray[i].toLowerCase()==dns.toLowerCase() && col !=0 && temp == "" && name !="") {
					val = true;
				}
			}
		} else {
			if(col !=0 && temp == "" && name !="") {
				val = true;
			}
		}
		return val;
	}
}


function f_validator_PATH(str) {
	str = this.trimChar(str);
	var val = false;
	if(!this.verEmpty(str)) {
		while(/\\/.test(str)) {
			str=str.replace("\\","/");
		} 

		if ( str.charCodeAt(str.length-1) != 47 ) {
			str+='/';
		}
		val = doPath(str);
		this.str = str;
	}

	return val;

	function doPath(str) {
		var val = true;
		arTemp = str.split("/");
		for(var i=0; i<arTemp.length-1; i++) {
			if (i==0) {
				if ( arTemp[i] == '' || (/^[a-z]:/i.test(arTemp[i]) && arTemp[i].length == 2) ) {
				} else {
					val=false;
				}
			} else {
				if ( !/^[a-z0-9]/i.test(arTemp[i]) ) {
					val=false;
				}
				if ( f_validator_FOLDER(arTemp[i]) || f_validator_CHAR(arTemp[i]) || arTemp[i] == '' ) {
					val=false;
				}
			}
		}
		if ( arTemp.length < 3 ) {
			val=false;
		}
		return val;
	}
}

