/*
 * Form Validation Check Script
 * 
 * Creator     : bugzzang ( http://bugzzang.com , bugzzang@gmail.com )
 * Create Date : 2008-05-16
 * License     : GPL
 *
 * ----------------------------------------------------------------
 * 
 * 1. script include
 *   - <script src="./bucheck.js"></script>
 * 2. form element ³»¿¡ attribute Çü½ÄÀ¸·Î »ç¿ë
 *   - <input type="text" name="userid" buRequire="true">
 *
 * # attribute
 * - buRequire    : ÇÊ¼ö ÀÔ·Â Ç×¸ñÀÏ¶§ »ç¿ë ( buRequire="true" )
 * - buValueType  : value ÀÇ type À» ÁöÁ¤ ( buValueType="number" , [number, english, korean, numberenglish, numberenglishunderbar, email )
 * - buLabel      : alert Ã¢¿¡ Ç¥½ÃÇÒ Ç×¸ñ¸íÀ» Àû¾îÁÝ´Ï´Ù. ( buLabel="¾ÆÀÌµð" [default : name ¼Ó¼º] )
 * - buMinLangth  : ÃÖ¼Ò ±ÛÀÚ¼ö ( buRequire="6" )
 * - buCheckCount : checkbox ÀÇ ÃÖ¼Ò Ã¼Å© ¼ö ( buRequire="3" [default : 1] )
 * - buDropFirst  : select ÀÇ Ã¹¹øÂ° ¿É¼ÇÀº seleced ·Î º¸Áö ¾ÊÀ½ ( buDropFirst="true" )
 *
 * -----------------------------------------------------------------
 *
 */


var BuCheck = function(){};

BuCheck.prototype.returnValue = {

    sMessage : '',
    oElement : null,
    bFlag    : true
}

BuCheck.prototype.setReturnValue = function( sMessage, oElement ){

    BuCheck.returnValue.sMessage = sMessage;
    BuCheck.returnValue.oElement = oElement;
}

BuCheck.prototype.trim = function( sText ){

    var pattern = /(^\s*)|(\s*$)/g;
    var sReturn = sText.replace(pattern, "");
    return sReturn;
}

BuCheck.prototype.checkForm = function( oForm ){

    var nLoopCount = oForm.elements.length;

    for(var i=0; i<nLoopCount; i++){

        var oElement = oForm.elements[i];

        var sRequire    = oElement.getAttribute('buRequire');
        var sValueType  = oElement.getAttribute('buValueType');
        var sLabel      = oElement.getAttribute('buLabel');
        var nMinLength  = oElement.getAttribute('buMinLength');
        var nCheckCount = oElement.getAttribute('buCheckCount');
        var sDropFirst  = oElement.getAttribute('buDropFirst');

        if(!sLabel){

            sLabel = oElement.name;
        }

        if(!nCheckCount){

            nCheckCount = 1;
        }

        if( sRequire && sRequire == 'true' ){

            switch( oElement.tagName ){

                case 'SELECT' :
                    var aOption  = oElement.getElementsByTagName('option');
                    var bSelected = false;

                    for(var k=0; k<aOption.length; k++){

                        if( aOption[k].selected == true ){
                            bSelected = true;
                        }
                    }

                    if(sDropFirst == 'true'){

                        if( oElement.selectedIndex == 0 ){
                            bSelected = false;
                        }
                    }

                    if(bSelected == false){
                        BuCheck.setReturnValue( sLabel + ' : ÇÊ¼ö ¼±ÅÃ Ç×¸ñÀÔ´Ï´Ù.' , oElement );
                        return false;
                    }
                    break;

                case 'INPUT' :
                    switch(oElement.type.toUpperCase()){

                        case 'RADIO' :
                            var aRadio   = document.getElementsByName(oElement.name);
                            var bChecked = false;

                            for(var k=0; k<aRadio.length; k++){

                                if( aRadio[k].checked == true ){

                                    bChecked = true;
                                }
                            }

                            if(bChecked == false){

                                BuCheck.setReturnValue( sLabel + ' : ÇÊ¼ö ¼±ÅÃ Ç×¸ñÀÔ´Ï´Ù.' , oElement );
                                return false;
                            }
                            break;

                        case 'CHECKBOX' :
                            var aCheckbox     = document.getElementsByName(oElement.name);
                            var nCheckedCount = 0;

                            for(var k=0; k<aCheckbox.length; k++){

                                if( aCheckbox[k].checked == true ){
                                    nCheckedCount++;
                                }
                            }

                            if(nCheckedCount < nCheckCount){

                                BuCheck.setReturnValue( sLabel + ' : ' + nCheckCount + '°³ ÀÌ»ó ¼±ÅÃÇÏ¼Å¾ß ÇÕ´Ï´Ù.' , oElement );
                                return false;
                            }
                            break;

                        case 'TEXT' :
                            if( !oElement.value || BuCheck.trim(oElement.value) == '' ){

                                BuCheck.setReturnValue( sLabel + ' : ÇÊ¼ö ÀÔ·Â Ç×¸ñÀÔ´Ï´Ù.' , oElement );
                                return false;
                            }
                            break;

                        case 'FILE' :
                            if( !oElement.value || BuCheck.trim(oElement.value) == '' ){

                                BuCheck.setReturnValue( sLabel + ' : ÇÊ¼ö ÀÔ·Â Ç×¸ñÀÔ´Ï´Ù.' , oElement );
                                return false;
                            }
                            break;

                        case 'PASSWORD' :
                            if( !oElement.value || BuCheck.trim(oElement.value) == '' ){

                                BuCheck.setReturnValue( sLabel + ' : ÇÊ¼ö ÀÔ·Â Ç×¸ñÀÔ´Ï´Ù.' , oElement );
                                return false;
                            }
                            break;
                    }
                    break;

                case 'TEXTAREA' :

                default :
                    if( !oElement.value || BuCheck.trim(oElement.value) == '' ){

                        BuCheck.setReturnValue( sLabel + ' : ÇÊ¼ö ÀÔ·Â Ç×¸ñÀÔ´Ï´Ù.' , oElement );
                        return false;
                    }
                    break;
            }
        }

        if( sValueType ){

            switch( sValueType ){

                case 'number' :
                    var sPattern = /^[0-9]*$/g;
                    if( !oElement.value.match( sPattern ) ){
                        BuCheck.setReturnValue( sLabel + ' : ¼ýÀÚ¸¸ ÀÔ·ÂÇØ ÁÖ¼¼¿ä.' , oElement );
                        return false;
                    }
                    break;

                case 'english' :
                    var sPattern = /^[a-zA-Z]*$/g;
                    if( !oElement.value.match( sPattern ) ){
                        BuCheck.setReturnValue( sLabel + ' : ¿µ¹®¸¸ ÀÔ·ÂÇØ ÁÖ¼¼¿ä.' , oElement );
                        return false;
                    }
                    break;

                case 'korean' :
                    var sPattern = /^[°¡-ÆR\x20]*$/g;
                    if( !oElement.value.match( sPattern ) ){
                        BuCheck.setReturnValue( sLabel + ' : ÇÑ±Û¸¸ ÀÔ·ÂÇØ ÁÖ¼¼¿ä.' , oElement );
                        return false;
                    }
                    break;

                case 'numberenglish' :
                    var sPattern = /^[0-9a-zA-Z]*$/g;
                    if( !oElement.value.match( sPattern ) ){
                        BuCheck.setReturnValue( sLabel + ' : ¿µ¹® È¤Àº ¼ýÀÚ¸¸ ÀÔ·ÂÇØ ÁÖ¼¼¿ä.' , oElement );
                        return false;
                    }
                    break;

                case 'numberenglishunderbar' :
                    var sPattern = /^[0-9a-zA-Z_]*$/g;
                    if( !oElement.value.match( sPattern ) ){
                        BuCheck.setReturnValue( sLabel + ' : ¿µ¹®, ¼ýÀÚ, "_" ¸¸ ÀÔ·ÂÇØ ÁÖ¼¼¿ä.' , oElement );
                        return false;
                    }
                    break;

                case 'email' :
                    var sPattern = /([0-9a-zA-Z_-]+)@([0-9a-zA-Z_-]+)\.([0-9a-zA-Z_-]+)/;
                    if( !oElement.value.match( sPattern ) ){
                        BuCheck.setReturnValue( sLabel + ' : ÀÌ¸ÞÀÏ Çü½Ä¿¡ ¸ÂÃç ÀÔ·ÂÇØ ÁÖ¼¼¿ä.' , oElement );
                        return false;
                    }
                    break;

                default :
                    break;
            }
        }

        if( nMinLength ){

            if( oElement.value.length < nMinLength ){

                BuCheck.setReturnValue( sLabel + ' : ' + nMinLength + 'ÀÚ ÀÌ»ó ÀÔ·ÂÇÏ¼¼¿ä' , oElement );
                return false;
            }
        }
    }

    return true;
}

BuCheck.prototype.check = function( sForm ){

    var oForm       = '';
    var nSubmitType = 0;

    if( !sForm || sForm == '' ){
        oForm = document.forms[0];
    }
    else{
        oForm = document.getElementsByName( sForm )[0];
    }

    if(oForm.onsubmit){
        nSubmitType = 1;
    }
    else{
        nSubmitType = 0;
    }

    var bFlag = BuCheck.checkForm( oForm );

    if(bFlag == false){

        window.alert(BuCheck.returnValue.sMessage);
        BuCheck.returnValue.oElement.focus();
        
        if(nSubmitType == 0){
            return;
        }
        else{
            return false;
        }
    }
    else{
        if(nSubmitType == 0){
            oForm.submit();
        }
    }
}

BuCheck = new BuCheck();

function FORM_CHK(obj)
{
	BuCheck.check();
	obj.submit();
}
