/*!
  Creates a \<a\> link tag of the given \a text using the given \a url.
  This is an overloaded function.
  The \a jsCondition argument serves for for creating Javascript confirm
  alerts where if you pass 'confirm' => 'Are you sure?', the link will be
  guarded with a Javascript popup asking that question. If the user accepts,
  the link is processed, otherwise not.
*/
QString TViewHelper::linkTo(const QString &text, const QUrl &url, Tf::HttpMethod method, const QString &jsCondition, const THtmlAttribute &attributes) const
{
    QString string("<a href=\"");
    string.append(url.toString(QUrl::FullyEncoded)).append("\"");

    if (method == Tf::Post) {
        string.append(" onclick=\"");
        if (!jsCondition.isEmpty()) {
            string.append("if (").append(jsCondition).append(") { ");
        }

        string += "var f = document.createElement('form'); document.body.appendChild(f); f.method = 'post'; f.action = this.href;";

        // Authenticity token
        QString token = actionView()->authenticityToken();
        if (!token.isEmpty()) {
            string += " var i = document.createElement('input'); f.appendChild(i); i.type = 'hidden'; i.name = 'authenticity_token'; i.value = '";
            string += token;
            string += "';";
        }
        string += " f.submit();";

        if (!jsCondition.isEmpty()) {
            string += " }";
        }
        string += " return false;\"";
    } else {
        if (!jsCondition.isEmpty()) {
            string.append(" onclick=\"return ").append(jsCondition).append(";\"");
        }
    }

    string.append(attributes.toString()).append(">").append(text).append("</a>");
    return string;
}
/*!
  Creates a self closing tag of \a name with the given HTML attributes
  \a attributes.
*/
QString TViewHelper::selfClosingTag(const QString &name, const THtmlAttribute &attributes) const
{
    QString string = "<";
    string += name;
    string += attributes.toString();
    string += QLatin1String(" />");
    return string;
}
/*!
  Creates a start-tag of \a name with the given HTML attributes \a attributes.
*/
QString TViewHelper::tag(const QString &name, const THtmlAttribute &attributes)
{
    QString string = "<";
    string += name;
    string += attributes.toString();
    string += QLatin1Char('>');
    endTags << endTag(name);
    return string;
}
QString TPrototypeAjaxHelper::linkToPeriodicalUpdate(const QString &text, const QUrl &url, const QString &id, UpdateBehavior behavior, const TOption &options, bool evalScripts, int frequency, int decay, const QString &jsCondition, const THtmlAttribute &attributes) const
{
   QString string;
    string += QLatin1String("<a href=\"#\" onclick=\"");
    string += periodicalUpdateFunction(url, id, behavior, options, evalScripts, frequency, decay, jsCondition);
    string += QLatin1String(" return false;\"");
    string += attributes.toString();
    string += QLatin1Char('>');
    string += text;
    string += QLatin1String("</a>");
    return string;
}
QString TPrototypeAjaxHelper::linkToRequest(const QString &text, const QUrl &url, const TOption &options, const QString &jsCondition, const THtmlAttribute &attributes) const
{
    QString string;
    string += QLatin1String("<a href=\"#\" onclick=\"");
    string += requestFunction(url, options, jsCondition);
    string += QLatin1String(" return false;\"");
    string += attributes.toString();
    string += QLatin1Char('>');
    string += text;
    string += QLatin1String("</a>");
    return string;
}
/*!
  Creates a \<a\> link tag whose onclick handler triggers the passed JavaScript.
*/
QString TViewHelper::linkToFunction(const QString &text, const QString &function,
                                    const THtmlAttribute &attributes) const
{
    QString string("<a href=\"#\" onclick=\"");
    QString func = function.trimmed();
    if (!func.isEmpty() && !func.endsWith(";")) {
        func += QLatin1Char(';');
    }
    string += func;
    string += QLatin1String(" return false;\"");
    string += attributes.toString();
    string += QLatin1Char('>');
    string += text;
    string += QLatin1String("</a>");
    return string;
}
/*!
  Creates a form tag that points to an \a url. If \a multipart is true,
  this function creates a form tag of multipart/form-data.
*/
QString TViewHelper::formTag(const QUrl &url, Tf::HttpMethod method, bool multipart,
                             const THtmlAttribute &attributes)
{
    QString string;
    string.append("<form action=\"").append(url.toString()).append("\"");

    if (multipart) {
        string += " enctype=\"multipart/form-data\"";
    }

    string += " method=";
    string += (method == Tf::Post) ? "\"post\"" : "\"get\"";

    string.append(attributes.toString()).append(">").append(inputAuthenticityTag());
    endTags << endTag("form");
    return string;
}
/*!
  Creates a \<a\> link tag of the given \a text using a given URL
  \a url in a popup window  with the name \a windowName.
*/
QString TViewHelper::linkToPopup(const QString &text, const QUrl &url, const QString &windowName,
                                 const QSize &size, const QPoint &topLeft, const QString &windowStyle,
                                 const QString &jsCondition, const THtmlAttribute &attributes) const
{
    QString string("<a href=\"");
    string.append(url.toString()).append("\"");

    string += " onclick=\"";
    if (!jsCondition.isEmpty()) {
        string.append("if (").append(jsCondition).append(") { ");
    }

    string += "window.open(this.href";
    if (!windowName.isEmpty()) {
        string.append(", '").append(windowName).append("'");
    }

    string += ", '";
    if (size.isValid()) {
        string.append("width=").append(QString::number(size.width())).append(",height=").append(QString::number(size.height()));
    }

    if (!topLeft.isNull()) {
        if (string.right(1) != "'")
            string += ",";
        string.append("top=").append(QString::number(topLeft.x())).append(",left=").append(QString::number(topLeft.y()));
    }

    if (!windowStyle.isEmpty()) {
        if (string.right(1) != "'")
            string += ",";
        string += windowStyle;
    }
    string += "');";

    if (!jsCondition.isEmpty()) {
        string += " }";
    }
    string += " return false;\"";

    string.append(attributes.toString()).append(">").append(text).append("</a>");
    return string;
}
/*!
  Outputs a escaped string of the HTML attribute \a attr to
  a view template.
*/
QString TActionView::eh(const THtmlAttribute &attr)
{
    return echo(THttpUtility::htmlEscape(attr.toString().trimmed()));
}
/*!
  Outputs the string of the HTML attribute \a attr to a view
  template.
*/
QString TActionView::echo(const THtmlAttribute &attr)
{
    responsebody += attr.toString().trimmed();
    return QString();
}