    var flashMovie;
    
    var _pres_pink1 = {bg: "FFD5DF", title: "7A7A7A", text: "444444", field: "ffffff", status: "7A7A7A"};
    var _pres_pink2 = {bg: "FF2AFF", title: "FFFFFF", text: "000000", field: "FFD5EA", status: "FFD5DF"};
    var _pres_wood = {bg: "CCBCB2", title: "805F48", text: "80766F", field: "FFEBDD", status: "805F48"};
    var _pres_ice = {bg: "B6C2CC", title: "4B6880", text: "444444", field: "E3F2FF", status: "727A80"};
    var _pres_sun = {bg: "FFC337", title: "806D42", text: "444444", field: "FFF1D2", status: "80611B"};
    var _pres_coldgreen = {bg: "99CCB8", title: "608073", text: "608073", field: "DDFFF1", status: "398063"};
    var _pres_camouflage = {bg: "AECC90", title: "6D805A", text: "608073", field: "E9F1E0", status: "598033"};
    var _pres_sea = {bg: "53B0FF", title: "506A80", text: "295880", field: "D9EEFF", status: "295880"};
    var _pres_gray = {bg: "eaeaea", title: "7a7a7a", text: "444444", field: "ffffff", status: "777777"};
    var _pres_olive = {bg: "B4AF91", title: "C03000", text: "444444", field: "ffffff", status: "C03000"};
    var _pres_blublak = {bg: "556A99", title: "CCCCCC", text: "FFFFFF", field: "3F3F3F", status: "FFFFFF"};

    function widgetReady(){
        flashMovie = $('dgclient');
        init();
        if($('nextcontrol') != null){ $('nick').focus(); }
    }

    function init(){
        changeTitle($('frmCreator').title.value);
        changeNick($('frmCreator').nick.value);
        loadInit();
    }
    
    function loadInit(){
        debugIt("Running loadInit!");
        
        // CHANGE CUSTOM BOXES
        $('bgcolorbox').style.backgroundColor = "#" + $('frmCreator').bgcolor.value;
        $('titletextbox').style.backgroundColor = "#" + $('frmCreator').titletext.value;
        $('textbox').style.backgroundColor = "#" + $('frmCreator').text.value;
        $('fieldbox').style.backgroundColor = "#" + $('frmCreator').field.value;
        $('statustextbox').style.backgroundColor = "#" + $('frmCreator').statustext.value;
        
        changeSize($('frmCreator').width.value, $('frmCreator').height.value);
        
        // CHANGE FLASH COLORS
        flashMovie.setColor("_bg_color", $('frmCreator').bgcolor.value);
        flashMovie.setColor("_titletext_color", $('frmCreator').titletext.value);
        flashMovie.setColor("_text_color", $('frmCreator').text.value);
        flashMovie.setColor("_field_color", $('frmCreator').field.value);
        flashMovie.setColor("_status_color", $('frmCreator').statustext.value);
        
        debugIt($('bgcolorbox').style.backgroundColor + " - " + $('frmCreator').bgcolor.value);
    }

    function loadPreset(param_name){
        debugIt("Running loadPreset!");
        clearError($('color'));
        var tmp_pres_obj = this["_pres_" + param_name];
        
        flashMovie.setColor("_bg_color", tmp_pres_obj.bg);
        flashMovie.setColor("_titletext_color", tmp_pres_obj.title);
        flashMovie.setColor("_text_color", tmp_pres_obj.text);
        flashMovie.setColor("_field_color", tmp_pres_obj.field);
        flashMovie.setColor("_status_color", tmp_pres_obj.status);
        
        // CHANGE CUSTOM BOXES
        $('frmCreator').bgcolor.value = tmp_pres_obj.bg;
        $('bgcolorbox').style.backgroundColor = "#" + tmp_pres_obj.bg;
        
        $('frmCreator').titletext.value = tmp_pres_obj.title;
        $('titletextbox').style.backgroundColor = "#" + tmp_pres_obj.title;
        
        $('frmCreator').text.value = tmp_pres_obj.text;
        $('textbox').style.backgroundColor = "#" + tmp_pres_obj.text;
        
        $('frmCreator').field.value = tmp_pres_obj.field;
        $('fieldbox').style.backgroundColor = "#" + tmp_pres_obj.field;
        
        $('frmCreator').statustext.value = tmp_pres_obj.status;
        $('statustextbox').style.backgroundColor = "#" + tmp_pres_obj.status;
    }

    function nextStep(){
        if($('nextcontrol') != null){
            generateCode();
        } else {
            forUpdate();
        }
    }

    function changeCustomSize(param_width, param_height){
        if($('frmCreator').size[2].checked){
            changeSize(param_width, param_height);
        }
    }

    function changeSize(param_width, param_height){
        param_width = parseInt(param_width, 10);
        param_height = parseInt(param_height, 10);
        var actual_width = param_width;
        var actual_height = param_height;
        
        if($('frmCreator').size[2].checked && parseInt($('frmCreator').custom_width.value,10) != param_width || param_width < 170){
            $('frmCreator').custom_width.style.borderColor = "red";
        } else {
            $('frmCreator').custom_width.style.borderColor = "#B6B6B6";
        }
        
        if($('frmCreator').size[2].checked && parseInt($('frmCreator').custom_height.value,10) != param_height || param_height < 250){
            $('frmCreator').custom_height.style.borderColor = "red";
        } else {
            $('frmCreator').custom_height.style.borderColor = "#B6B6B6";
        }
        
        if(param_width > 325){
            $('dgclient').width = 325;
        } else if(param_width < 170){
            $('dgclient').width = actual_width = 170;
        } else {
            $('dgclient').width = param_width;
        }
        
        if(param_height > 395){
            $('dgclient').height = 395;
        } else if(param_height < 250) {
            $('dgclient').height = actual_height = 250;
        } else {
            $('dgclient').height = param_height;
        }
        
        $('frmCreator').width.value = actual_width;
        $('frmCreator').height.value = actual_height;
    }

    function clearError(what){
        try { what.style.borderColor = ''; } catch(e){}
        try { $(what.id+'err').innerHTML = '&nbsp;'; } catch(e) {}
    }

    function select_all(what){ what.focus(); what.select(); }

    function displayError(what,msg,pre){
        var msg = (msg == null || msg == '') ? "Error" : msg;
        var arrows = (pre == true) ? '&nbsp;<< ' : '';
        try { $(what.getAttribute('name')+'err').innerHTML = arrows+msg; } catch(e){}
        try { what.style.borderColor = "red"; } catch(e){}
    }

    function goBack(){
        clearAllErrors();
        goToStep("create");
    }

    function goToStep(step){
        var step = (step == null) ? "create" : step;
        switch(step){
            case "log_reg":
                $('flashcontainer').style.display = $('create').style.display = $('nextcontrol').style.display = 'none';
                $('log_reg').style.display = $('submitcontrol').style.display = '';
                break;
            case "create":
                $('flashcontainer').style.display = $('create').style.display = $('nextcontrol').style.display = '';
                $('log_reg').style.display = $('submitcontrol').style.display = 'none';
                setTimeout("postInit()",10);
                break;
        }
    }

    function toggleColorThemePicker(which){
        var which = (which == null) ? "theme" : which;
        switch(which){
            case "picker":
                $('theme').style.display = 'none';
                $('thead').className = $('picker').style.display = '';
                $('phead').className = 'minihdr';
                break;
            default:
                $('theme').style.display = $('phead').className = '';
                $('thead').className = 'minihdr';
                $('picker').style.display = 'none';
                break;
        }
    }

    function generateCode(){
        debugIt("Running generateCode!");
        clearAllErrors();
        // Required minimum for height/width
        if($('frmCreator').size[2].checked){
            $('frmCreator').width.value = ($('frmCreator').custom_width.value > 150 ? $('frmCreator').custom_width.value : $('frmCreator').width.value);
            $('frmCreator').height.value = ($('frmCreator').custom_height.value > 225 ? $('frmCreator').custom_height.value : $('frmCreator').height.value);
        }
        if(checkVars() && checkColors()){
            debugColors();
            goToStep("log_reg");
            postInit();
        }
    }

    function checkColors(){
        debugIt("Running checkColors!");
        if(
            regex($('frmCreator').bgcolor.value,"hex6") == null ||
            regex($('frmCreator').titletext.value,"hex6") == null ||
            regex($('frmCreator').text.value,"hex6") == null ||
            regex($('frmCreator').field.value,"hex6") == null ||
            regex($('frmCreator').statustext.value,"hex6") == null
        ){
            loadPreset('gray');
            displayError($('color'),"Error: Theme has been reset, please re-select colors");
            debugIt("{colors} [returning bad]");
            return false;
        } else { debugIt("[returning ok]"); return true; }
    }

    function debugColors(){
        debugIt("Running debugColors!");
        debugIt($('frmCreator').bgcolor.name + ": " + $('frmCreator').bgcolor.value);
        debugIt($('frmCreator').titletext.name + ": " + $('frmCreator').titletext.value);
        debugIt($('frmCreator').text.name + ": " + $('frmCreator').text.value);
        debugIt($('frmCreator').field.name + ": " + $('frmCreator').field.value);
        debugIt($('frmCreator').statustext.name + ": " + $('frmCreator').statustext.value);
    }

    function forUpdate(){
        clearAllErrors();
        if(checkVars() && checkColors()){
            $('frmCreator').submit();
        }
        debugColors();
    }

    function checkVars(){
        debugIt("Running checkVars!");
        var err = 0;
        if($('frmCreator').title.value.length < 1 || checkSpaces($('frmCreator').title.value)){
            changeTitle($('frmCreator').title.value = 'Digsby Widget');
        } else {
            if(regex($('frmCreator').title.value,"title") == null){
                displayError($('frmCreator').title,"Invalid: can only consist of letters, numbers, spaces, ', _, - and .");
                debugIt("{title}");
                err++;
            }
        }
        if($('frmCreator').nick.textLength < 1 || checkSpaces($('frmCreator').nick.value)){
            displayError($('frmCreator').nick,"Please Enter a Nickname");
            debugIt("{nick}");
            err++;
        } else {
            if(regex($('frmCreator').nick.value,"title") == null){
                displayError($('frmCreator').nick,"Invalid: can only consist of letters, numbers, spaces, ', _, - and .");
                debugIt("{title (bad chars)}");
                err++;
            }
        }
        debugIt("[returning "+(err == 0)+"]");
        return (err == 0);
    }

    function checkReg(){
        debugIt("Running checkReg!");
        var err = 0;
        if(regex($('frmCreator').nname.value,"username") == null){
            displayError($('frmCreator').nname,"Name must be 3-18 letters, numbers or _");
            err++;
        }
        if(regex($('frmCreator').email.value,"email") == null){
            displayError($('frmCreator').email,"The email format appears to be invalid");
            err++;
        }
        if($('frmCreator').pw1.value != $('frmCreator').pw2.value){
            displayError($('frmCreator').pw1,"The passwords do not match");
            displayError($('frmCreator').pw2);
        }
        if(($('frmCreator').pw1.value.length < 5 || $('frmCreator').pw1.value.length > 48) ||
           ($('frmCreator').pw2.value.length < 5 || $('frmCreator').pw2.value.length > 48))
        {
            displayError($('frmCreator').pw1,"Password must be 5-48 characters long");
            displayError($('frmCreator').pw2);
            err++;
        }
        if($('tos').checked == false){
            displayError($('tos'),"You must agree in order to register");
            err++;
        }
        debugIt("[returning "+(err == 0)+"]");
        return (err == 0);
    }

    function checkLogin(){
        debugIt("Running checkLogin!");
        var err = 0;
        if(regex($('frmCreator').uname.value,"username") == null){
            displayError($('frmCreator').uname,"Invalid username length");  err++;
            debugIt("{bad uname}");
        }
        if($('frmCreator').pw.value.length < 5 || $('frmCreator').pw.value.length > 48){
            displayError($('frmCreator').pw,"Must be 5-48 characters long");  err++;
            debugIt("{bad pw}");
        }
        if($('frmCreator').uname.value.length == 0){
            displayError($('frmCreator').uname,"Please Enter a Username"); err++;
            debugIt("{empty uname}");
        }
        if($('frmCreator').pw.value.length == 0){
            displayError($('frmCreator').uname,"Please Enter a Password"); err++;
            debugIt("{empty pw}");
        }
        debugIt("[returning "+(err == 0)+"]");
        return (err == 0);
    }

    function regex(what,type){
        var RegEx;
        switch(type){
            case "hex6":
                var RegEx = /^[a-fA-F0-9]{3,6}$/;
                break;
            case "hex16":
                var RegEx = /^[a-fA-F0-9]{16}$/;
                break;
            case "number":
                var RegEx = /^[0-9]{1,4}$/;
            case "title":
                var RegEx = /^[a-zA-Z0-9._\-\' ]{1,50}$/;
                break;
            case "username":
                var RegEx = /^[a-zA-Z0-9_]{3,18}$/;
                break;
            case "email":
                var RegEx = /.+\@.+\./;
                break;
            case "default":
                return null;
        }
        return RegEx.exec(what);
    }

    function submitReady(){
        debugIt("Running SubmitReady()!");
        clearAllErrors();
        var reg_all = ($('nname').value.length > 0 && $('pw1').value.length > 0 && $('pw2').value.length > 0 && $('email').value.length > 0);
        var reg = ($('nname').value.length > 0 || $('pw1').value.length > 0 || $('pw2').value.length > 0 || $('email').value.length > 0);
        var log_all = ($('uname').value.length > 0 && $('pw').value.length > 0);
        var log = ($('uname').value.length > 0 || $('pw').value.length > 0);
        if(checkVars()){
            if(reg && !log || reg_all && !log_all){
                if(checkReg()){ debugColors(); $('frmCreator').submit(); }
            } else {
                if(checkLogin()){ debugColors(); $('frmCreator').submit(); }
            }
        } else {
            debugColors();
            goToStep("create");
        }
    }

    function submitReg(){
        debugIt("Running SubmitReg()!");
        $('uname').value = $('pw').value = '';
        submitReady();
    }

    function submitLogin(){
        debugIt("Running SubmitLogin()!");
        $('nname').value = $('pw1').value = $('pw2').value = $('email').value = '';
        submitReady();
    }

    function clearAllErrors(){
        var fields = new Array('nname','uname','email','pw','tos','title','nick');
        for(var i=0; i < fields.length; i++ ){
            clearError($(fields[i]));
        }
        clearPWError(); 
    }

    function clearPWError(){
        clearError($('pw1'));
        clearError($('pw2'));
    }

    function changeTitle(param_title){
        if(flashMovie){
            if(param_title.length < 1 || checkSpaces(param_title)){
                flashMovie.setTitle("Digsby Widget");
            } else {
                flashMovie.setTitle(param_title);
            }
        }
    }

    function changeNick(param_nick){
        if(flashMovie){
            if(param_nick.length < 1 || checkSpaces(param_nick)){
                flashMovie.setNick('my.nickname');
            } else {
                flashMovie.setNick(param_nick);
            }
        }
    }

    function postInit(){
        if(flashMovie.setColor){
            changeNick($('frmCreator').nick.value);
            flashMovie.setColor("_bg_color", $('frmCreator').bgcolor.value);
            flashMovie.setColor("_titletext_color", $('frmCreator').titletext.value);
            flashMovie.setColor("_text_color", $('frmCreator').text.value);
            flashMovie.setColor("_field_color", $('frmCreator').field.value);
            flashMovie.setColor("_status_color", $('frmCreator').statustext.value);
        } else {
            setTimeout("postInit()",1);
        }
    }


    function checkSpaces(param){
        for(var i = 0; i < param.length; i++){
            if(param.substr(i, 1) != " "){
                return false;
            }
        }
        return true;
    }

    function changeColorBox(param_id, param_color){
        $(param_id).style.backgroundColor = param_color;
        changeFlashColor(param_id, param_color.toCSShex());
    }

    function changeFlashColor(param_id, param_color){
        if(flashMovie){
            switch(param_id){
                case "bgcolorbox":      param_id = "_bg_color";         break;
                case "titletextbox":    param_id = "_titletext_color";  break;
                case "textbox":         param_id = "_text_color";       break;
                case "fieldbox":        param_id = "_field_color";      break;
                case "statustextbox":   param_id = "_status_color";     break;
            }
            try{ flashMovie.setColor(param_id, param_color.substring(1)); }catch(e){}
        }
    }

    function keyEvent(elm,evt,match,doit,passelm){
        var kc = (evt.charCode) ? evt.charCode : ((evt.which) ? evt.which : evt.keyCode);
        if(kc === match){ if(passelm){ doit(elm); } else { doit(); } }
        if(kc == 13 || kc == 3){ return false; } else { return true; }
    }

    function submitMe(elm){ submitReady(); }
    
    function decreaseMe(elm){ if(parseInt(elm.value,10) == elm.value){ elm.value--; } }
    
    function increaseMe(elm){ if(parseInt(elm.value,10) == elm.value){ elm.value++; } }
    
    String.prototype.toCSShex = function(){
        var out, hx;
        /* in rgb() form */
        if(this.indexOf('r')+1){
            hx = this.match(/\d+/g);
            out = '#'+
            (hx[0]*1).toString(16)+
            (hx[1]*1).toString(16)+
            (hx[2]*1).toString(16);
        } else {
            /* add #, if missing */
            out = this.replace(/^#?/,"#");
            /* expand #ABC form to #AABBCC */
            if(out.length==4){
                out = out.replace(/[0-9a-f]/gi,"$&$&");
            }
        }
        return out.toUpperCase();
    };
    
    function debugIt(append){
        //$('mainerr').innerHTML += " | " + append;
        // alert(append);
    }
