function validator(id)
{
	this.browser = navigator.userAgent.toLowerCase();
	this.is_ie = ((this.browser.indexOf("msie") != -1) && (this.browser.indexOf("opera") == -1));
	this.start();
}
validator.prototype.start = function()
{
	var instance = this;
	var el = document.getElementsByTagName('input');
	for(var i=0;i<el.length;i++)
	{
		if(el[i].type.toLowerCase() == 'text' && el[i].getAttribute("accept") != null)
		{
			var type = el[i].getAttribute("accept").toLowerCase();
			el[i].onchange = function (){ instance.check(this);};
			el[i].onkeypress = function (event){ return instance.only(event,this);}; 
			this.set(type,el[i]);
		}
	}
}
validator.prototype.charcode = function (evt)
{
	if(this.is_ie)
	{
		var charCode = window.event.keyCode
	}
	else
	{
		var charCode = evt.which
	}
	return charCode;
}
validator.prototype.set = function (type,field)
{
	if(type == 'phone')
	{
		field.maxLength = 12;
	}
	if(type == 'date')
	{
		field.maxLength = 10;
	}
}
validator.prototype.removeclass = function (field)
{
	field.className = field.className.replace(' Correct','');
	field.className = field.className.replace(' Wrong','');
}
validator.prototype.setclass = function (field,condition)
{
	if(field.value.length > 0)
	{
		this.removeclass(field);
		field.className += (condition)?' Correct':' Wrong';
	}
	else
	{
		this.removeclass(field);
	}
}
validator.prototype.check = function (field)
{
	var type = field.getAttribute('accept');
	switch(type)
	{
		case 'phone':
			this.phonecheck(field);
		break;
		case 'date':
			this.datecheck(field);
		break;
		case 'email':
			this.emailcheck(field);
		break;
		case 'number':
			this.numbercheck(field);
		break;
		case 'alpha':
			this.alphacheck(field);
		break;
		case 'alphanum':
			this.alphanumcheck(field);
		break;
		case 'float':
			this.floatcheck(field);
		break;
		default:
		break;
	}
}
validator.prototype.only = function (event,field)
{
	var type = field.getAttribute('accept');
	if(type == 'number')
	{
		return this.numberonly(event);
	}
	else if(type == 'phone')
	{
		return this.phoneonly(event,field);
	}
	else if(type == 'date')
	{
		return this.dateonly(event,field);
	}
	else if(type == 'alpha')
	{
		return this.alphaonly(event);
	}
	else if(type == 'alphanum')
	{
		return this.alphanumonly(event);
	}
	else if(type == 'float')
	{
		return this.floatonly(event,field);
	}
	else
	{
		return true;
	}
}
validator.prototype.trim = function (field)
{
	field.value = field.value.replace(/(^\s{1,})|(\s{1,}$)/,'');
}
validator.prototype.numbercheck = function (field)
{
	this.trim(field);
	var isValid = !field.value.match(/[^\d]/g,'');
	this.setclass(field,isValid);
}
validator.prototype.numberonly = function (evt)
{
	var charCode = this.charcode(evt);
	if(charCode == 8||charCode == 9||charCode == 13)
	{
		return true;
	}
	if(charCode > 31 && (charCode < 48 || charCode > 57))
	{
		return false;
	}
	return true;
}
validator.prototype.alphacheck = function (field)
{
	this.trim(field);
	var isValid = field.value.match(/^[a-zA-Z\s]+$/g);
	this.setclass(field,isValid);
}
validator.prototype.alphaonly = function (evt)
{
	var charCode = this.charcode(evt);
	if(charCode == 8||charCode == 9||charCode == 13||charCode == 0)
	{
		return true;
	}
	if((charCode > 96 && charCode < 123) || (charCode > 64 && charCode < 91) || charCode == 32)
	{
		return true;
	}
	return false;
}
validator.prototype.alphanumcheck = function (field)
{
	this.trim(field);
	var isValid = field.value.match(/^[a-zA-Z0-9\s]+$/g);
	this.setclass(field,isValid);
}
validator.prototype.alphanumonly = function (evt)
{
	return (this.alphaonly(evt)||this.numberonly(evt));
}
validator.prototype.floatcheck = function (field)
{
	this.trim(field);
	var isValid = field.value.match(/^[-+]?[0-9]*\.?[0-9]+$/g);
	this.setclass(field,isValid);
}
validator.prototype.floatonly = function (evt,that)
{
	var charCode = this.charcode(evt);
	if(charCode == 8||charCode == 9||charCode == 13)
	{
		return true;
	}
	if(that.value.length < 1)
	{
		if(charCode == 43 || charCode == 45)
		{
			return true;
		}
	}
	else
	{
		if(charCode == 46 && !that.value.match(/\.+/))
		{
			return true;
		}
	}
	return this.numberonly(evt);
}
validator.prototype.phonecheck = function (field)
{
	this.trim(field);
	var num = field.value.replace(/[^\d]/g,'');
	var isValid = (num.length == 10);
	this.setclass(field,isValid);
}
validator.prototype.phoneonly = function (evt,that)
{
	var charCode = this.charcode(evt);
	if(charCode != 8)
	{
		if(that.value.length == '3' || that.value.length == '7')
		{
			that.value += '-';
		}
	}
	return this.numberonly(evt);
}
validator.prototype.datecheck = function (field)
{
	this.trim(field);
	var num = field.value.replace(/[^\d]/g,'');
	var isValid = true;
	var mm = num.substr(0,2);
	var dd = num.substr(2,2);
	var yy = num.substr(4,8);
	var isleap = ((yy % 4 == 0 && yy % 100 != 0) || yy % 400 == 0);
	if(num.length != 8)
	{
		isValid = false;
	}
	if(yy < 1600 || yy > 9999 && isValid)
	{
		isValid = false;
	}
	if(mm < 1 || mm > 12 && isValid)
	{
		isValid = false;
	}
	if(isleap && isValid)
	{
		if(mm == 2 && dd > 29)
		{
			isValid = false;
		}
	}
	else
	{
		if(mm == 2 && dd > 28 && isValid)
		{
			isValid = false;
		}
		else
		{
			if((mm == 4 || mm == 6 || mm == 9 || mm == 11) && (dd < 1 || dd > 30) && isValid)
			{
				isValid = false;
			}
			else if(dd < 1 || dd > 31 && isValid)
			{
				isValid = false;
			}
		}
	}
	this.setclass(field,isValid);
}
validator.prototype.dateonly = function (evt,that)
{
	var charCode = this.charcode(evt);
	if(charCode != 8)
	{
		if(that.value.length == '2' || that.value.length == '5')
		{
			that.value += '-';
		}
	}
	return this.numberonly(evt);
}
validator.prototype.emailcheck = function (field)
{
	this.trim(field);
	var email = field.value;
	var isValid = true;
	if (!email.match(/^[^@]{1,64}@[^@]{1,255}$/i)) 
	{
		isValid = false;
	}
	if(isValid == true)
	{
		var email_array = email.split("@");
		var local_array = email_array[0].split(".");
		for (i = 0; i < local_array.length; i++)
		{
			if (!local_array[i].match(/^(([A-Za-z0-9!#$%&\'*+/=?^_`{|}~-][A-Za-z0-9!#$%&\'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$/))
			{
				isValid = false;
			}
		}
		if(isValid == true)
		{
			if (!email_array[1].match(/^\[?[0-9\.]+\]?$/))
			{
				var domain_array = email_array[1].split(".");
				if (domain_array.length < 2)
				{
					isValid = false;
				}
				if(isValid == true)
				{
					for (i = 0; i < domain_array.length; i++)
					{
						if (!domain_array[i].match(/^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$/))
						{
							isValid = false;
						}
					}
				}
			}
		}
	}
	this.setclass(field,isValid);
}


function Validate()
{
	var validate = new validator();
}
document.write('<script type="text/javascript">AttachEvent(window,"onload",Validate);</script>');
