// Author: lamb100@korea.com
// Last Modified: 2004-06-30

// class ValidateField
ValidateField = function(field, type, msg)
{
	this.field = field;	
	this.type = type;
	this.msg = msg;
}

ValidateField.prototype.alert = function()
{
	alert(this.msg);
	this.field.focus();
}

ValidateField.prototype.GetValue = function()
{
	return this.field.value;
}

ValidateField.prototype.GetField = function()
{
	return this.field;
}

// class ValidateUtil
ValidateUtil = function() 
{
	this.forms = new Array();
}

ValidateUtil.prototype.AddField = function (field, type, msg)
{	
	this.forms.push(new ValidateField(field, type, msg));
}

ValidateUtil.prototype.Run = function ()
{	
	for(var i=0; i < this.forms.length; i++)
	{
		var field = this.forms[i];
		if(field.type == "Number") {		
			if(!this.IsNumber(field.GetValue()))	{
				field.alert(); return false;				
			}		
		} else if( field.type == "English") {
			if(!this.IsEnglish(field.GetValue()))	{
				field.alert();	return false;				
			}		
		} else if( field.type == "NumberEnglishDashUnderbar") {
			if(!this.IsNumberEnglishDashUnderbar(field.GetValue()))	{
				field.alert();	return false;				
			}				
		} else if( field.type == "ASCII") {
			if(!this.IsASCII(field.GetValue()))	{
				field.alert();	return false;				
			}				
		} else if( field.type == "Date") {
			if(!this.IsDate(field.GetValue()))	{
				field.alert();	return false;				
			}				
		} else if( field.type == "Email") {
			if(!this.IsEmail(field.GetValue()))	{
				field.alert();	return false;				
			}				
		} else if( field.type == "ResidentNumber") {
			if(!this.IsResidentNumber(field.GetValue()))	{
				field.alert();	return false;				
			}
		} else if( field.type == "LicenseNumber") {
			if(!this.IsLicenseNumber(field.GetValue()))	{
				field.alert();	return false;				
			}	
		} else if( field.type == "TelNumber") {
			if(!this.IsTelNumber(field.GetValue()))	{
				field.alert();	return false;				
			}	
							
		} else if( field.type == "Content") {
			if(!this.HasContent(field.GetValue()))	{
				field.alert();	return false;				
			}				
		} else if( field.type == "Checked") {
			if(!this.IsChecked(field.GetField()))	{
				field.alert();	return false;				
			}			
		} else if( field.type == "Radio") {
			if(!this.IsRadio(field.GetField()))	{
				field.alert();	return false;				
			}			
			
		}
	}
	return true;
}

// Is Number?
ValidateUtil.prototype.IsNumber = function(s)
{
	if(s == null) return false;
	
	s = s.Trim();
	if(s.length == 0) return false;
	
	for(var i = 0; i < s.length; i++)
	{
		var c = s.charCodeAt(i);
		if(c >= "0".charCodeAt(0) && c <= "9".charCodeAt(0)) continue;
		else return false;
	}
	return true;
}

// Is English?
ValidateUtil.prototype.IsEnglish = function(s)
{
	if(s == null) return false;
	
	s = s.Trim();
	if(s.length == 0) return false;
	
	for(var i = 0; i < s.length; i++)
	{
		var c = s.charCodeAt(i);
		if(c >= 'a'.charCodeAt(0) && c <= 'z'.charCodeAt(0)) continue;
		else if(c >= 'A'.charCodeAt(0) && c <= 'Z'.charCodeAt(0)) continue;
		else return false;
	}
	return true;
}

// Is English, Number, Dash, Underscore 
ValidateUtil.prototype.IsNumEnglishDashUnderbar = function(s)
{
	if(s == null) return false;
		
	s = s.Trim();
	if(s.length == 0 ) return false;
	
	for(var i = 0; i < s.length; i++)
	{
		var c = s.charCodeAt(i);
		if(c >= 'a'.charCodeAt(0) && c <= 'z'.charCodeAt(0)) continue;
		else if(c >= 'A'.charCodeAt(0) && c <= 'Z'.charCodeAt(0)) continue;
		else if(c >= "0".charCodeAt(0) && c <= "9".charCodeAt(0)) continue;
		else if(c == "-".charCodeAt(0) || c == "_".charCodeAt(0)) continue;
		else return false;
	}
	return true;
}

// Is ASCII? code < 127
ValidateUtil.prototype.IsASCII = function(s)
{
	if(s == null) return false;
	
	s = s.Trim();
	if(s.length == 0) return false;
	
	for(i = 0; i < s.length; i++)
	{
		var c = s.charCodeAt(i);
		if(c <= 127) continue;
		else return false;
	}
	return true;
}

// Is DateTime?
ValidateUtil.prototype.IsDate = function(s)
{	
	if(s == null) return false;
	
	s = s.Trim();
	// only support "yyyy-MM-dd"
	if(s.length != 10) return false;
	if(s.charCodeAt(4) != "-".charCodeAt(0)) return false;
	if(s.charCodeAt(7) != "-".charCodeAt(0)) return false;
	
	var year = s.substr(0,4);	
	var month = s.substr(5,2) - 1;	
	var date = s.substr(8,2);	
	var calDate = new Date(year, month, date);
	if ( calDate.getFullYear() != year || 
		calDate.getMonth() != month ||
		calDate.getDate() != date) 
	{
		return false;
	} 
	else 
	{
		return true;
	}
}

// IsEmail?
ValidateUtil.prototype.IsEmail = function(s)
{	
	if(s == null) return false;
			
	s = s.Trim();
	if(s.search(/^\s*[\w\~\-\.]+\@[\w\~\-]+(\.[\w\~\-]+)+\s*$/g) >= 0)
		return true;
	else 
		return false;	
}

// is valid resident number
ValidateUtil.prototype.IsResidentNumber = function(s)
{
	if(s == null) return false;
	
	s = s.Trim();
	if(s.length != 13 && s.length != 14) return false;
	
	// if does not have "-", insert "-" 
	if(s.length == 13) 
		s = s.substr(0, 6) + "-" + s.substr(6, 7);	
	
	// Validate form & 7th number
	var fmt = /^\d{6}-[1234]\d{6}$/;
	if (!fmt.test(s)) {
		return false;
	}

	// Validate Date
	birthYear = (s.charAt(7) <= "2") ? "19" : "20";
	birthYear += s.substr(0, 2);
	birthMonth = s.substr(2, 2) - 1;
	birthDate = s.substr(4, 2);
	birth = new Date(birthYear, birthMonth, birthDate);

	if ( birth.getYear() % 100 != s.substr(0, 2) ||
		birth.getMonth() != birthMonth ||
		birth.getDate() != birthDate) {
		return false;
	}

	// Validate Check Sum Code
	buf = new Array(13);
	for (i = 0; i < 6; i++) buf[i] = parseInt(s.charAt(i));
	for (i = 6; i < 13; i++) buf[i] = parseInt(s.charAt(i + 1));

	multipliers = [2,3,4,5,6,7,8,9,2,3,4,5];
	for (i = 0, sum = 0; i < 12; i++) sum += (buf[i] *= multipliers[i]);

	if ((11 - (sum % 11)) % 10 != buf[12]) {
		return false;
	}

	return true;
}

// is valid IsLicenseNumber number
ValidateUtil.prototype.IsLicenseNumber = function(s)
{
	if(s == null) return false;
	
	s = s.Trim();
	if(s.length != 12) return false;
	
	return true;
}

// is valid IsTelNumber number
ValidateUtil.prototype.IsTelNumber = function(s)
{
	if(s == null) return false;
	
	s = s.Trim();
	if(s.length < 11 || s.length > 13) return false;
	
	for(var i = 0; i < s.length; i++)
	{
		var c = s.charCodeAt(i);
		
		
		if(c >= "0".charCodeAt(0) && c <= "9".charCodeAt(0)) continue;
		else if(c == "-".charCodeAt(0)) continue;
		else return false;
	}
	
	
	
	
	return true;
}

// has Content?
ValidateUtil.prototype.HasContent = function(s)
{	
	if(s == null) return false;
	
	s = s.Trim();
	if(s.length != 0) return true;
	else return false;	
}

// select, check box is checked?
ValidateUtil.prototype.IsChecked = function(grp)
{
	if(grp == null) return false;
	
	for(var i=0; i < grp.length ; i++)
	{
		if(grp[i].checked)
			return true;
	}
	return false;
}

ValidateUtil.prototype.IsRadio = function(grp)
{
	if(grp == null) return false;
	
	alert(grp.length);
	
	for(var i=0; i < grp.length ; i++)
	{
		if(grp[i].checked)
			return true;
	}
	return false;
}



// get Byte length of string 
ValidateUtil.prototype.GetByteLength = function(s)
{
	if(s == null) return false;
	
	var count = 0;
	for(i = 0; i < s.length; i++)
	{
		var c = s.charCodeAt(i);
		if(c <= 127) count = count + 1;
		else count = count + 2;
	}
	return count;
}

// Trim Function Implementation from JScavitto
String.prototype.LTrim=new Function("return this.replace(/^\\s+/,'')")
String.prototype.RTrim=new Function("return this.replace(/\\s+$/,'')")
String.prototype.Trim= new Function("return this.replace(/^\\s+|\\s+$/g,'')")	
