
var RHForm = Class.create({

	// °´Ã¼ ÃÊ±âÈ­ V0.7
	initialize :

		function(oBody,orgForm,submitOfType)
		{
			this.oBody = oBody	;
			
			this.orgForm = orgForm ;

			this.el		= orgForm.elements			// type=image °¡ Á¦¿ÜµÈ elements
			
			if( this.orgForm.method == "" )
				this.orgForm.method = "POST" ;
				
			this.preAction = this.orgForm.action  ;
			this.preTarget = this.orgForm.target ;
			this.preMethod = this.orgForm.method ;

			this.submitOfType = ( submitOfType == undefined ) ? "NORMAL" : submitOfType ; // AJAX
			this.submitOfSuper = null ;

			this.isSubmited = false ;		// Áßº¹ Submit ¹æÁö

			return this ;
		},


	setAction :

		function(url)
		{
			this.preAction = this.orgForm.action ; 
			this.orgForm.action = url ;

			return this ;
		},
		
	setMethod :

		function(str)
		{
			this.preMethod = this.orgForm.method ; 
			this.orgForm.method = str ;

			return this ;
		},
		
	setTarget :

		function(target)
		{
			this.preTarget = this.orgForm.target ; 
			this.orgForm.target = target ;

			return this ;
		},


	setSubmitType :

		function(key)
		{
			this.submitOfSuper = key ;

			return this ;
		},

	getSubmitType : 

		function()
		{
			return this.submitOfSuper ;

			return this ;
		},

	// ¿À¸®Áö³¯ Æû ¸®ÅÏ
	getOrg : 

		function()
		{
			return this.orgForm ;

		},

			

	// Document Object ¹«Á¶°Ç Array ·Î °¡Á®¿À±â V0.7
	getOrgE : 

		function(key)
		{
			var orgElements = WPUtil.safeArray(this.orgForm[key]) ;

			return this.oBody.wrapElements(orgElements) ;
		},

	// Document Object ¹«Á¶°Ç Array ·Î °¡Á®¿À±â V0.7
	getaOrgE : 

		function(key)
		{
			return this.getOrgE(key)[0] ;
		},

	// Æû ¿¤·¹¸ÕÆ® ¸®ÅÏ
	getE :

		function(key)
		{

			var type	= this.getaOrgE(key).getType() ;
			var obj		= null ;

			switch(type)
			{
				case "text" :
					obj = new RHText(this,key) ;
					break ;
				case "password" :
					obj = new RHText(this,key) ;
					break ;
				case "textarea" :
					obj = new RHText(this,key) ;
					break ;
				case "hidden" :
					obj = new RHText(this,key) ;
					break ;
				case "button" :
					obj = new RHText(this,key) ;
					break ;
				case "radio" :
					obj = new RHRadio(this,key) ;
					break ;
				case "checkbox" :
					obj = new RHCheckBox(this,key) ;
					break ;
				case "select-one" :
					obj = new RHSelect(this,key) ;
					break ;
				default :
					obj = this.getOrgE(key) ;

			}

			return obj ;

		},

	// Æû°ªÀ» °¡Á®¿Â´Ù.
	getV : 

		function(key)
		{
			return this.getE(key).getV() ;
		},

	// Æû ÅØ½ºÆ®¸¦ °¡Á®¿Â´Ù.
	getT : 

		function(key)
		{
			return this.getE(key).getT() ;
		},

	// Æû°ú ¿¬°ü¹è¿­À» °¡Áö°í °ªÀ» ¼¼ÆÃ
	setVAssoc : 

		function(assoc)
		{
			for (var key in assoc) 
				if(assoc.hasOwnProperty(key))
					this.setV(key,assoc[key]) ;

			return this ;
		},

	// °ªÀ» ¼¼ÆÃÇÑ´Ù.
	setV : 

		function(key,value,index)
		{
	
			this.getE(key).setV(value,index) ;

			return this ;

		},

	// ¹è¿­ÀÎ °´Ã¼¿¡ °ªÀ» ÀÎµ¦½º·Î ¼¼ÆÃÇÑ´Ù.(For Text ¸¸ Áö¿ø) 
	setVBySpliter : 

		function(key,value,spliter)
		{
			this.getE(key).setVBySpliter(value,spliter) ;

			return this ;
		}, 


	// ÀÏ¹Ý SUBMIT »ç¿ë( POPUP À¸·Î SUBMIT )
	submit : 

		function()
		{
			if( this.validate() == true )	{

				if( this.isSubmited == false )	{

					// rurl À» Àü¼Û
					if( this.oBody.autoCreateRurl  )	{

					}

					this.isSubmited = true ;
					this.orgForm.submit() ;
					this.setTarget(this.preTarget) ;
					return true ;
				}
				else	{
					window.status = "È®ÀÎ¹öÆ°Àº ÇÑ¹øÇÑ ´©¸£¼Å¾ß ÇÕ´Ï´Ù." ;
				}

			}
			else
				return false ;


		},


	// POPUP À¸·Î SUBMIT 
	popSubmit :

		function()
		{
			if( this.validate() == true )	{

					this.orgForm.submit() ;
					this.setTarget(this.preTarget) ;
					return true ;

			}
			else
				return false ;


		}, 

		// override ÇØ¼­ ¹ß¸®µ¥ÀÌÆ® ÇÒ ¼ö ÀÖ´Ù.
		chk : 
			function() { return true ; },

		// Æû µ¥ÀÌÅÍ °ËÁõÃ³¸®ÇÔ - ¿À¹ö¶óÀÌµå Ã³¸®ÇÒ ¼ö ÀÖ½À´Ï´Ù.
		validate : 

			function(except)	{

			var f = this.orgForm	;
			var min = 0			;
			var necessary = "" ;
			var isExcept = false ;
			
			var chk_img = "";
			
			
			except = ( except == undefined ) ? new Array() : except ;

			for(var i = 0 ; i < f.elements.length ; i++ )	{

				for(var j = 0 ; j < except.length ; j++ )	{
					if( f.elements[i].name == except[j] )	{
						isExcept = true ;
						break ;
					}
				}
				if( isExcept == true )	{	
					isExcept = false ;
					continue ;
				}
				
				necessary = f.elements[i].getAttribute("VTYPE")	;
				min = f.elements[i].getAttribute("MIN_LENGTH")	;
				chk_img = f.elements[i].getAttribute("CHK_IMG")	;

				if( necessary != null )	{

					if( f.elements[i].type == "radio" )	{

						if( WPUtil.nullRadio(f[f.elements[i].name]) == false )	{
							try
							{
								f.elements[i].focus()	;
							}
							catch(e){ continue ; }
							alert(f.elements[i].title + "Àº(´Â) ÇÊ¼ö ÀÔ·Â »çÇ×ÀÔ´Ï´Ù.") ;
							return false ;
						}
					}
					else if ( f.elements[i].type == "checkbox") {
							var sChkIndex = i ;
							var bOk = false ;
							while( f.elements[i].type == "checkbox" )	{
								if( f.elements[i].checked == true )	
									bOk = true ;
								i++ ;
							}

							if( bOk == false )	{
								alert(f.elements[sChkIndex].title + "Àº(´Â) ÇÊ¼ö ÀÔ·Â »çÇ×ÀÔ´Ï´Ù.") ;
								return false ;
							}
				
					}
					else	
					{

						if( f.elements[i].value == "" )	{
							if( f.elements[i].disabled )
							{}
							else if( f.elements[i].type == "select-one" )
								f.elements[i].focus() ;
							else if(f.elements[i].readOnly == false )
								f.elements[i].focus() ;
						
							
							alert(f.elements[i].title + "Àº(´Â) ÇÊ¼ö ÀÔ·Â »çÇ×ÀÔ´Ï´Ù.") ;
								
							return false ;
						}
					}
				}

				if( min != null ) {
					if( f.elements[i].value.length < min )	{
						alert(f.elements[i].title + "´Â ÃÖ¼Ò " + min + "ÀÚ ÀÌ»ó ÀÔ´Ï´Ù. ´Ù½Ã ÀÔ·ÂÇÏ¿© ÁÖ½Ê½Ã¿À.") ;
						if( f.elements[i].readOnly == false )
							f.elements[i].focus() ;
						return false ;
					}
				}
				
				if(chk_img != null) {		
				
					var obj = f.elements[i]; 
				
					if(obj.value == '')
						return true;
			
					if(obj.value != '' && !obj.value.toLowerCase().match(/(.jpg|.jpeg|.gif|.png|.bmp)/)) { 
						alert('ÀÌ¹ÌÁö ÆÄÀÏ¸¸ ¾÷·Îµå ÇÒ ¼ö ÀÖ½À´Ï´Ù.');			
						obj.focus();
						//document.selection.clear();
						//document.execCommand('Delete');
						return false ;	
					}		
					
				}



			}
			
			// ¿À¹ö¶óÀÌµå µÈ chk() ÇÔ¼ö È£Ãâ
			if( this.chk() == false )
				return false ; 

			return true ;
		},

	// Æû Submit ÈÄ ´ÙÀ½ Çàµ¿À» ¾î¶»°Ô ÇÒ°ÍÀÎ Áö¸¦ action.xxx ÀÇ Çü½Ä(±Ô°Ý) ¸ÂÃç Æû ¿¤·¹¸ÕÆ® »ý¼º V0.7
	//  nextType 
	//		- ToOpener
	//		- ToSelf
	//		- ToPrepageByRef
	//		- ToOpenerAndClose
	//		- OpenerRefreshAndClose
	//		- Close
	//		- ToPrepage
	nextActionCreate : 

		function(actionSC,nextType,nextUrl,nextMsg)
		{
				this.setMethod("POST")			;
				this.setAction(actionSC)		;
				this.create("rType",nextType)	;
				this.create("rUrl",nextUrl)		;
				this.create("rMsg",nextMsg)		;
		},

	// rurl À» »ý¼º V0.7
	rurlCreate : 

		function(url)
		{
			this.ecreate("rurl",url) ;
		},


	// µ¥ÀÌÅÍ¸¦ ÀÎÄÚµùÇØ¼­ »ý¼º
	ecreate : 

		function(name,value,type)
		{
			this.create(name,WPUtil.bin2hex(value),type) ;
		},


	// 2008.06.21  ¿¬°ü¹è¿­À» È÷µçÀ¸·Î ÀüºÎ º¯È¯
	createAssoc :

		function(assoc)
		{
			for(key in assoc)
				if(assoc.hasOwnProperty(key)){
					this.create(key,assoc[key]) ;
				}
		},

	/* ÇØ´ç FORM ¿¡ INPUT ÅÂ±× »ðÀÔ&¼öÁ¤ */
	create : 

		function(name,value,type)
		{
				var obj ;
				var preObj ;
		
				preObj = document.getElementsByName(name)[0] ;
				
				if( preObj == undefined )	{
		
		
					var input = document.createElement("INPUT");
		
					input.type		= "hidden"	;
					input.name		= name		;
					input.id		= name		;
					input.value		= value		;
		
					type = ( type == undefined ) ? "afterBegin" : type ;

					obj = this.orgForm.appendChild(input);

				}
				else
				{
		
					preObj.value = value ;
					obj = preObj ;
				}
			
				return obj ;
		
		}

}) ;



Object.extend(RHForm.prototype,{

	nac : RHForm.prototype.nextActionCreate  

}) ;


// CMD °ªÀ» °¡Áö°í ¼­ºê¹ÌÆ®
Class.overload.apply(RHForm,[ 'submit',[ String ] , 
	function(cmd)
	{
			this.create("cmd",cmd) ;

			this.submit() ;
	} ]
) ;

// ¸®ÅÏ Á¤º¸¸¦ °¡Áö°í ¼­ºê¹ÌÆ®
Class.overload.apply(RHForm,[ 'submit',[ String , String , String , String ] , 
	function(cmd,nextType,nextUrl,nextMsg)
	{
			this.create("cmd",cmd) ;
			this.create("rType",nextType)	;
			this.ecreate("rUrl",nextUrl)	;
			this.create("rMsg",nextMsg)		;

			this.submit() ;
	} ]
) ;




