/*******************************************************************************
 *                              FormsUtility.js
 *                            --------------------
 *   update      : 2004³â 6¿ù 14ÀÏ ¿ù¿äÀÏ
 *   authors     : ¾È¼¼¿ø
 *   email       : aminoai@aminoai.com
 *   description : Form¿¡ ÇÊ¿äÇÑ ¿©·¯ À¯Æ¿¸®Æ¼. ºí·©Å© Ã¼Å©, ÀÌ¸ÞÀÏÃ¼Å©
 *				   µîÀ» ¼öÇà.
 ******************************************************************************/
function FormsUtility(tar)//Constructor
{
	this.result = true;
	this.form = tar;
	this.formSubmit = formSubmit;
	this.checkBlank = checkBlank;
	this.checkSameValue = checkSameValue;
	this.checkNum = checkNum;
	this.checkURL = checkURL;
	this.checkEmail = checkEmail;
	this.emailCheck = emailCheck;
	this.checkNull = checkNull;
}

function formSubmit()
{
	if(this.result==true){
		eval("document."+this.form+".submit();");
	}
}

function checkBlank(tar, msg, foc)
{
	if ( foc === undefined ){
		foc = true;
	}
	if(this.result==true){
		if(eval("document."+this.form+"."+tar+".value")==""){
			if(msg!=""&&msg!=undefined) alert(msg);
			if ( foc == true ){
				eval("document."+this.form+"."+tar+".focus();");
			}
			this.result = false;
		} else {
			this.result = true;
		}
	} 
}

function checkSameValue()
{
	if(this.result==true){
		var r=true;
		var a=checkSameValue.arguments;
		var msg=checkSameValue.arguments[a.length-1];
		for(i=0;i<(a.length-2);i++){
			if(a[i+1]!=""){
				m=eval("document."+this.form+"."+a[i]+".value");
				n=eval("document."+this.form+"."+a[i+1]+".value");
				if(m!=n)r=false;
			} else if (a[i-1]!=""){
				m=eval("document."+this.form+"."+a[i]+".value");
				n=eval("document."+this.form+"."+a[i-1]+".value");
				if(m!=n)r=false;
			}
		}
		
		if(r==false){
			alert(msg);
			this.result = false;
		} else {
			this.result = true;
		}
	}
}

function checkNum(tar, msg)
{
	if(this.result==true){
		v = eval("document."+this.form+"."+tar+".value");
		num = v.length;
		src = v.substr(0,1);
		if(isNaN(v)||src=="0"){
			if(msg!=""&&msg!=undefined) alert(msg);
			eval("document."+this.form+"."+tar+".focus();");
			this.result = false;
		} else {
			this.result = true;
		}
	} 
}

function checkURL(){
	var tar = checkURL.arguments[0];
	var msg = checkURL.arguments[1];
	var must = checkURL.arguments[2];

	if (must==null) must=true;
	var str = eval("document."+this.form+"."+tar+".value");
	
	if(this.result==true){
		if (must==false) {
			if (this.checkNull(tar)||str=="http://") {
				this.result = true;
				return true;
			}
		}

		str_spl = str.split("//");
		if (str_spl[1]!=null) str_spl2 = str_spl[1].split(":");
		
		if(str_spl[0]=="http:"&&str_spl2[0]!="http"&&str_spl[1]!=""){
			this.result = true;
		} else {
			if(msg!=""&&msg!=undefined) alert(msg);
			eval("document."+this.form+"."+tar).value = "http://";
			eval("document."+this.form+"."+tar+".focus();");
			this.result = false;
		}
	} 
}


function checkEmail()
{
	var tar= checkEmail.arguments[0];
	var msg= checkEmail.arguments[1];
	var must= checkEmail.arguments[2];
	
	if (must==null) must=true;
	var emailStr = eval("document."+this.form+"."+tar+".value");
	
	if(this.result==true){
		if (must==false&&this.checkNull(tar)) {
			this.result = true;
			return true;
		}
		var r = this.emailCheck(emailStr);
		if (r==true){
			this.result = true;
		} else {
			msg = msg + "\n error : " + r;
			alert(msg);
			eval("document."+this.form+"."+tar+".focus();");
			this.result = false;
		}
	}
}

function emailCheck(emailStr) 
{
	
	var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	var msg;
	
	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) {
		msg = "À¯È¿ÇÑ ÀÌ¸ÞÀÏ ÁÖ¼Ò°¡ ¾Æ´Ñ°Í °°½À´Ï´Ù (@ ¿Í . Ã¼Å©)";
		return msg;
	}
	var user=matchArray[1]
	var domain=matchArray[2]
	
	// See if "user" is valid 
	if (user.match(userPat)==null) {
	    // user is not valid
	    msg = "»ç¿ëÀÚ ÀÌ¸§ÀÌ Æ²¸³´Ï´Ù";
	    return msg;
	}
	
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
	    // this is an IP address
	          for (var i=1;i<=4;i++) {
	            if (IPArray[i]>255) {
	                msg = "IP ÁÖ¼Ò°¡ Æ²·È½À´Ï´Ù";
	                return msg;
	            }
	    }
	    return true
	}
	
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
	        msg = "µµ¸ÞÀÎ¸íÀÌ ¸ÂÁö ¾Ê½À´Ï´Ù";
	    return msg
	}
	
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || 
	    domArr[domArr.length-1].length>3) {
	   // the address must end in a two letter or three letter word.
	   msg = "µµ¸ÞÀÎ¸íÀÇ ¸¶Áö¸· ±ÛÀÚ´Â 3±ÛÀÚ°Å³ª 2ÀÚ¸®ÀÇ ±¹°¡ÄÚµå ÀÔ´Ï´Ù";
	   return msg;
	}
	
	// Make sure there's a host name preceding the domain.
	if (len<2) {
	   msg ="ÀÌ ÁÖ¼ÒÀÇ È£½ºÆ®¸íÀÌ ¾ø½À´Ï´Ù.";
	   return msg;
	}
	
	// If we've gotten this far, everything's valid!
	return true;
}

function checkNull(tar)
{
	if (eval("document."+this.form+"."+tar+".value")=="") return true;
	else return false;
}