var $submit = {

	data : {},
	
	init : function( f )
	{
		if( this.d )
		{
			return;
		}
		
		var self = this;
		
		this.d = true;
		
		$select.init();
		
		$( f ).submit( function() 
		{
			return self.update.call( self );
		});
		
		$( '#sumbit_b' ).click( function()
		{
			$( f ).submit();
			return false;
		});
		
		for( var i in this.data )
		{
			$( '#' + i ).bind( 'keyup blur', function() 
			{ 
				$( '#sumbit_b' ).attr( 'class', ( self.test.call( self ) ? 'submit' : 'submit disable' ) ); 
			});
		}
		
		// no spam !
		$( '#nospam' ).val( 'JbcmllCkd' + ( 3 * 9 ) );
		
		this.test();
	},
	
	_e : [],
	_d : false,
	
	enable_test_class : 'enable',
	
	update : function()
	{
		var val, indeficator = [], error = [];
		
		// load info
		for( var i in this.data )
		{
			if( indeficator[ this.data[i][1] ] == 'undefined' )
			{
				indeficator[ this.data[i][1] ] = -1;
			}
			
			val = this._T( $( '#' + i ).val() );
		
			if ( val == '' ) continue;
			
			if ( this.fn[ this.data[i][0] ].call( this, val, this.data[i][2] ) ) indeficator[ this.data[i][1] ] = 1;
			else
			{
				indeficator[ this.data[i][1] ] = 0;
				error[ this.data[i][1] ] = i;
			}
		}
		
		for( i in this.data )
		{
			switch( indeficator[ this.data[i][1] ] )
			{
				case 1 :
					continue;
					break;
				
				case 0 :
					val = error[ this.data[i][1] ];
					this.alertMessage( this.data[val][4] );
					return false;
					break;
				
				case -1 :
				default : 
					this.alertMessage( this.data[i][3] );
					return false;
					break;
			}
		}
		
		return true;
	},
	
	test : function()
	{
		var val, cl, bad = [], good = [];
		
		// load info
		for( var i in this.data )
		{
			val = this._T( $( '#' + i ).val() );
			
			if ( val == '' || ! this.fn[ this.data[i][0] ].call( this, val, this.data[i][2] ) )
			{
				// bad
				if( good[ this.data[i][1] ] == true )
				{
					$( '.chbhox[rel=' + i + '] img' ).attr('class','hidden');
					continue;
				}
				
				cl = 'disable';
				if( bad[ this.data[i][1] ] == undefined )
				{
					bad[ this.data[i][1] ] = [];
				}
				else
				{
					cl = 'hidden';
				}
				
				bad[ this.data[i][1] ].push( i );
				$( '.chbhox[rel=' + i + '] img' ).attr('class',cl);
			}
			else
			{
				// good
				if( good[ this.data[i][1] ] )
				{
					$( '.chbhox[rel=' + i + '] img' ).attr('class','enable');
					continue;
				}
				
				if( typeof( bad[ this.data[i][1] ] ) == 'object' )
				{
					for( var x in bad[ this.data[i][1] ] )
					{
						$( '.chbhox[rel=' + bad[ this.data[i][1] ][ x ] + '] img' ).attr('class','hidden');
					}
					bad[ this.data[i][1] ] = false;
				}
				
				good[ this.data[i][1] ] = true;
				$( '.chbhox[rel=' + i + '] img' ).attr('class','enable');
			}
		}
		
		for( var i in bad )
		{
			if( good[i] != true ) return false;
		}
		
		return true;
	},
	
	fn : {
	
		min : function( str, min ) 
		{
			return str.length > parseInt( min ) ? true : false;
		},
		
		max : function( str, max ) 
		{
			return str.length < parseInt( max ) ? true : false;
		},
		
		regexp : function( str, rexp )
		{
			return ( rexp.test( str ) );
		},
		
		email : function( str, is_multiply )
		{
			if( is_multiply && str.indexOf(',') > 5 )
			{
				var a = str.split( ',' ), i;
				
				for ( i in a )
				{
					if( ! this.fn.email( this._T( a[i] ), false ) ) return false;
				}
				
				return true;
			}
			
			return this.fn._r[0].test( str );
		},
		
		site : function( str )
		{
			return this.fn._r[1].test( str );
		},
		
		_r : [
			/^[\.\-_A-Za-z0-9]+@[A-Za-z0-9][\.\-_A-Za-z0-9]*[A-Za-z0-9]\.[A-Za-z0-9]{2,6}$/,
			/^(?:www\.)?((?:[A-Za-z0-9](?:[\-A-Za-z0-9]*[A-Za-z0-9])?\.){0,3})([A-Za-z0-9][\-A-Za-z0-9]*[A-Za-z0-9])\.([A-Za-z0-9]{2,6})$/
			]
		
	},
	
	_T : function( str )
	{
		if( ! str  )
		{
			return '';
		}
		
		return str.replace( /^\s+|\s+$/g, '' );
	},
	
	alertMessage : function( str )
	{
		$('#alert_box, #alert_overlay').css('display', 'block');
		$('#alert_box .message').text(str);
		$('#alert_box .close, #alert_overlay')
			.one('click', function() 
			{ 
				$('#alert_box, #alert_overlay').css('display', 'none');
				$('#alert_box .close, #alert_overlay').unbind('click');
			});
	}
}
