protected function __toJS($strCustomJs = "", $arrInitArguments = array(), $blnRawJs = false)
    {
        $strReturn = "";
        $strJs = "";

        // *** Loop through all form elements and get their javascript code.
        foreach ($this->__elements as $element) {
            $strJs .= $element->toJS();
        }

        // *** Form Events.
        foreach ($this->__jsevents as $event => $method) {
            $strJs .= "\tobjForm.addEvent(\"{$event}\", {$method});\n";
        }

        // Indent javascript
        $strJs = str_replace("\n", "\n\t", $strJs);

        if (! $blnRawJs) {
            $strReturn .= "<script type=\"text/javascript\">\n";
            $strReturn .= "// <![CDATA[\n";
        }

        $strName = str_replace("-", "_", $this->__name);
        $strReturn .= "function {$strName}_init() {\n";

        $strCalledClass = static::getStrippedClassName(get_called_class());
        $strArguments = "\"{$this->__name}\", \"{$this->__mainalert}\"";
        if (count($arrInitArguments) > 0) {
            $strArguments = "\"{$this->__name}\", \"{$this->__mainalert}\", " . json_encode($arrInitArguments);
        }

        /**
         * If the ValidForm class is extended, try to initialize a custom javascript class with the same name as well
         * If that javascript class is not available / does not exist, continue initializing ValidForm as usual.
         */
        if ($strCalledClass !== "ValidForm") {
            $strReturn .= "\tvar objForm = (typeof {$strCalledClass} !== \"undefined\") ? ";
            $strReturn .= "new {$strCalledClass}({$strArguments}) : ";
            $strReturn .= "new ValidForm(\"{$this->__name}\", \"{$this->__mainalert}\");\n";
        } else {
            $strReturn .= "\tvar objForm = new ValidForm(\"{$this->__name}\", \"{$this->__mainalert}\");\n";
        }

        $strReturn .= $strJs;
        if (! empty($strCustomJs)) {
            $strReturn .= $strCustomJs;
        }

        $strReturn .= "\tobjForm.initialize();\n";
        $strReturn .= "\t$(\"#{$this->__name}\").data(\"vf__formElement\", objForm);";
        $strReturn .= "};\n";
        $strReturn .= "\n";
        $strReturn .= "try {\n";
        $strReturn .= "\tjQuery(function(){\n";
        $strReturn .= "\t\t{$this->__name}_init();\n";
        $strReturn .= "\t});\n";
        $strReturn .= "} catch (e) {\n";
        $strReturn .= "\talert('Exception caught while initiating ValidForm:\\n\\n' + e.message);\n";
        $strReturn .= "}\n";

        if (! $blnRawJs) {
            $strReturn .= "// ]]>\n";
            $strReturn .= "</script>\n";
        }

        return $strReturn;
    }