// Export a field
 public void ExportField(cField fld)
 {
     if (Horizontal) {
         ExportValue(ref fld);
     } else { // Vertical, export as a row
         FldCnt++;
         string tdcaption = "<td";
         switch (Table.Export) {
             case "html":
             case "email":
             case "word":
             case "excel":
                 tdcaption += " class=\"ewTableHeader\"";
                 break;
             case "pdf":
                 tdcaption += " class=\"ewTablePdfHeader\"";
                 break;
         }
         tdcaption += ">";
         fld.CellCssClass = ((FldCnt % 2) == 1) ? "ewExportTableRow" : "ewTableAltRow";
         string tdvalue = "<td" + ((EW_EXPORT_CSS_STYLES) ? fld.CellStyles : "") + ">";
         Text += "<tr>" + tdcaption + fld.ExportCaption + "</td>" + tdvalue +
             fld.ExportValue(Table.Export, Table.ExportOriginalValue) +
             "</td></tr>";
     }
 }
 // Field value
 public void ExportValue(ref cField fld)
 {
     ExportValueEx(ref fld, fld.ExportValue(Table.Export, Table.ExportOriginalValue));
 }
 // Get User ID subquery
 public string GetUserIDSubquery(cField fld, cField masterfld)
 {
     string sWrk = "";
     SqlDataReader RsUser;
     string sSql = "SELECT " + masterfld.FldExpression + " FROM [dbo].[Usuarios] WHERE " + AddUserIDFilter("");
     if (EW_USE_SUBQUERY_FOR_MASTER_USER_ID)	{	// Use subquery
         sWrk = sSql;
     }	else { // List all values
         RsUser = Conn.GetTempDataReader(sSql);
         try {
             while (RsUser.Read()) {
                 if (ew_NotEmpty(sWrk)) sWrk += ",";
                 sWrk += ew_QuotedValue(RsUser[0], masterfld.FldDataType);
             }
         }	finally {
             Conn.CloseTempDataReader();
         }
     }
     if (ew_NotEmpty(sWrk))
         sWrk = fld.FldExpression + " IN (" + sWrk + ")";
     return sWrk;
 }
 // Field Caption
 public void ExportCaption(cField fld)
 {
     ExportValueEx(ref fld, fld.ExportCaption);
 }
 // Multiple column sort
 public void UpdateSort(cField ofld, bool ctrl)
 {
     string sSortField, sLastSort, sThisSort, sOrderBy;
     if (CurrentOrder == ofld.FldName)	{
         sSortField = ofld.FldExpression;
         sLastSort = ofld.Sort;
         if (CurrentOrderType == "ASC" || CurrentOrderType == "DESC") {
             sThisSort = CurrentOrderType;
         }	else {
             sThisSort = (sLastSort == "ASC") ? "DESC" : "ASC";
         }
         ofld.Sort = sThisSort;
         if (ctrl)	{
             sOrderBy = SessionOrderBy;
             if (sOrderBy.Contains(sSortField + " " + sLastSort)) {
                 sOrderBy = sOrderBy.Replace(sSortField + " " + sLastSort, sSortField + " " + sThisSort);
             }	else {
                 if (ew_NotEmpty(sOrderBy)) sOrderBy = sOrderBy + ", ";
                 sOrderBy = sOrderBy + sSortField + " " + sThisSort;
             }
             SessionOrderBy = sOrderBy; // Save to Session
         }	else {
             SessionOrderBy = sSortField + " " + sThisSort;	// Save to Session
         }
     }	else {
         if (!ctrl)
             ofld.Sort = "";
     }
 }
 // Sort URL
 public string SortUrl(cField fld)
 {
     string OutStr = "";
     if (ew_NotEmpty(CurrentAction) || ew_NotEmpty(Export) || (fld.FldType == 201 || fld.FldType == 203 || fld.FldType == 205 || fld.FldType == 141)) {
         OutStr = "";
     }	else if (fld.Sortable) {
         OutStr = ew_CurrentPage();
         string sUrlParm = UrlParm("order=" + ew_UrlEncode(fld.FldName) + "&amp;ordertype=" + fld.ReverseSort());
         OutStr += "?" + sUrlParm;
     }
     return OutStr;
 }
 // Return multi-value search SQL
 public static string ew_GetMultiSearchSql(ref cField Fld, string FldVal)
 {
     string sSql;
     string sVal;
     string sWrk = "";
     string[] arVal = FldVal.Split(new char[] {','});
     for (int i = 0; i <= arVal.GetUpperBound(0); i++) {
         sVal = arVal[i].Trim();
         if (arVal.GetUpperBound(0) == 0 || EW_SEARCH_MULTI_VALUE_OPTION == 3)	{
             sSql = Fld.FldExpression + " = '" + ew_AdjustSql(sVal) + "' OR " + ew_GetMultiSearchSqlPart(ref Fld, sVal);
         }	else	{
             sSql = ew_GetMultiSearchSqlPart(ref Fld, sVal);
         }
         if (ew_NotEmpty(sWrk)) {
             if (EW_SEARCH_MULTI_VALUE_OPTION == 2)	{
                 sWrk = sWrk + " AND ";
             } else if (EW_SEARCH_MULTI_VALUE_OPTION == 3) {
                 sWrk = sWrk + " OR ";
             }
         }
         sWrk = sWrk + "(" + sSql + ")";
     }
     return sWrk;
 }
 //
 // Build search URL
 //
 public void BuildSearchUrl(ref string Url, cField Fld)
 {
     bool IsValidValue;
     string sWrk = "";
     string FldParm = Fld.FldVar.Substring(2);
     string FldVal = ObjForm.GetValue("x_" + FldParm);
     string FldOpr = ObjForm.GetValue("z_" + FldParm);
     string FldCond = ObjForm.GetValue("v_" + FldParm);
     string FldVal2 = ObjForm.GetValue("y_" + FldParm);
     string FldOpr2 = ObjForm.GetValue("w_" + FldParm);
     int lFldDataType = (Fld.FldIsVirtual) ? EW_DATATYPE_STRING : Fld.FldDataType;
     if (ew_SameText(FldOpr, "BETWEEN"))	{
     IsValidValue = (lFldDataType != EW_DATATYPE_NUMBER) || (lFldDataType == EW_DATATYPE_NUMBER && Information.IsNumeric(FldVal) && Information.IsNumeric(FldVal2));
     if (ew_NotEmpty(FldVal) && ew_NotEmpty(FldVal2) && IsValidValue)	{
         sWrk = "x_" + FldParm + "=" + ew_UrlEncode(FldVal) + "&y_" + FldParm + "=" + ew_UrlEncode(FldVal2) + "&z_" + FldParm + "=" + ew_UrlEncode(FldOpr);
     }
     } else if (ew_SameText(FldOpr, "IS NULL") || ew_SameText(FldOpr, "IS NOT NULL")) {
     sWrk = "x_" + FldParm + "=" + ew_UrlEncode(FldVal) + "&z_" + FldParm + "=" + ew_UrlEncode(FldOpr);
     }	else {
     IsValidValue = (lFldDataType != EW_DATATYPE_NUMBER) || (lFldDataType == EW_DATATYPE_NUMBER && Information.IsNumeric(FldVal));
     if (ew_NotEmpty(FldVal) && IsValidValue && ew_IsValidOpr(FldOpr, lFldDataType)) {
         sWrk = "x_" + FldParm + "=" + ew_UrlEncode(FldVal) + "&z_" + FldParm + "=" + ew_UrlEncode(FldOpr);
     }
     IsValidValue = (lFldDataType != EW_DATATYPE_NUMBER) || (lFldDataType == EW_DATATYPE_NUMBER && Information.IsNumeric(FldVal2));
     if (ew_NotEmpty(FldVal2) && IsValidValue && ew_IsValidOpr(FldOpr2, lFldDataType)) {
         if (ew_NotEmpty(sWrk)) sWrk += "&v_" + FldParm + "=" + FldCond + "&";
         sWrk += "y_" + FldParm + "=" + ew_UrlEncode(FldVal2) + "&w_" + FldParm + "=" + ew_UrlEncode(FldOpr2);
     }
     }
     if (ew_NotEmpty(sWrk)) {
     if (ew_NotEmpty(Url)) Url += "&";
     Url += sWrk;
     }
 }
 //
 // Convert search value
 //
 public string ConvertSearchValue(ref cField Fld, string FldVal)
 {
     if (Fld.FldDataType == EW_DATATYPE_BOOLEAN)	{
     if (ew_NotEmpty(FldVal)) return (FldVal == "1" || ew_SameText(FldVal, "y") || ew_SameText(FldVal, "t")) ? "1" : "0";
     } else if (Fld.FldDataType == EW_DATATYPE_DATE) {
     if (ew_NotEmpty(FldVal)) return ew_UnformatDateTime(FldVal, Fld.FldDateTimeFormat);
     }
     return FldVal;
 }
 //
 // Get search parm
 //
 public void GetSearchParm(cField Fld)
 {
     string FldParm = Fld.FldVar.Substring(2);
     Fld.AdvancedSearch.SearchValue = HistoricoVehiculos.GetAdvancedSearch("x_" + FldParm);
     Fld.AdvancedSearch.SearchOperator = HistoricoVehiculos.GetAdvancedSearch("z_" + FldParm);
     Fld.AdvancedSearch.SearchCondition = HistoricoVehiculos.GetAdvancedSearch("v_" + FldParm);
     Fld.AdvancedSearch.SearchValue2 = HistoricoVehiculos.GetAdvancedSearch("y_" + FldParm);
     Fld.AdvancedSearch.SearchOperator2 = HistoricoVehiculos.GetAdvancedSearch("w_" + FldParm);
 }
        //
        // Build search SQL
        //
        public void BuildSearchSql(ref string Where, cField Fld, bool MultiValue)
        {
            string FldParm = Fld.FldVar.Substring(2);
            string FldVal = Convert.ToString(Fld.AdvancedSearch.SearchValue);
            string FldOpr = Fld.AdvancedSearch.SearchOperator;
            string FldCond = Fld.AdvancedSearch.SearchCondition;
            string FldVal2 = Convert.ToString(Fld.AdvancedSearch.SearchValue2);
            string FldOpr2 = Fld.AdvancedSearch.SearchOperator2;
            string sWrk = "";
            FldOpr = FldOpr.Trim().ToUpper();
            if (ew_Empty(FldOpr)) FldOpr = "=";
            FldOpr2 = FldOpr2.Trim().ToUpper();
            if (ew_Empty(FldOpr2)) FldOpr2 = "=";
            if (EW_SEARCH_MULTI_VALUE_OPTION == 1) MultiValue = false;
            if (FldOpr != "LIKE") MultiValue = false;
            if (FldOpr2 != "LIKE" && ew_NotEmpty(FldVal2)) MultiValue = false;
            if (MultiValue)	{
            string sWrk1;
            string sWrk2;

            // Field value 1
            if (ew_NotEmpty(FldVal))	{
                sWrk1 = ew_GetMultiSearchSql(ref Fld, FldVal);
            }	else	{
                sWrk1 = "";
            }

            // Field value 2
            if (ew_NotEmpty(FldVal2) && ew_NotEmpty(FldCond)) {
                sWrk2 = ew_GetMultiSearchSql(ref Fld, FldVal2);
            }	else	{
                sWrk2 = "";
            }

            // Build final SQL
            sWrk = sWrk1;
            if (ew_NotEmpty(sWrk2)) {
                if (ew_NotEmpty(sWrk)) {
                    sWrk = "(" + sWrk + ") " + FldCond + " (" + sWrk2 + ")";
                }	else	{
                    sWrk = sWrk2;
                }
            }
            }	else {
            FldVal = ConvertSearchValue(ref Fld, FldVal);
            FldVal2 = ConvertSearchValue(ref Fld, FldVal2);
            sWrk = ew_GetSearchSql(ref Fld, FldVal, FldOpr, FldCond, FldVal2, FldOpr2);
            }
            ew_AddFilter(ref Where, sWrk);
        }
 //
 // Build basic search SQL
 //
 public void BuildBasicSearchSQL(ref string Where, cField Fld, string Keyword)
 {
     string sFldExpression;
     string sWrk;
     if (ew_NotEmpty(Fld.FldVirtualExpression))	{
     sFldExpression = Fld.FldVirtualExpression;
     }	else {
     sFldExpression = Fld.FldExpression;
     }
     int lFldDataType = Fld.FldDataType;
     if (Fld.FldIsVirtual)
     lFldDataType = EW_DATATYPE_STRING;
     if (lFldDataType == EW_DATATYPE_NUMBER)	{
     sWrk = sFldExpression + " = " + ew_QuotedValue(Keyword, lFldDataType);
     }	else {
     sWrk = sFldExpression + ew_Like(ew_QuotedValue("%" + Keyword + "%", lFldDataType));
     }
     if (ew_NotEmpty(Where))
     Where += " OR ";
     Where += sWrk;
 }
 //
 // Set search parm
 //
 public void SetSearchParm(cField Fld)
 {
     string FldParm = Fld.FldVar.Substring(2);
     HistoricoVehiculos.SetAdvancedSearch("x_" + FldParm, Fld.AdvancedSearch.SearchValue);
     HistoricoVehiculos.SetAdvancedSearch("z_" + FldParm, Fld.AdvancedSearch.SearchOperator);
     HistoricoVehiculos.SetAdvancedSearch("v_" + FldParm, Fld.AdvancedSearch.SearchCondition);
     HistoricoVehiculos.SetAdvancedSearch("y_" + FldParm, Fld.AdvancedSearch.SearchValue2);
     HistoricoVehiculos.SetAdvancedSearch("w_" + FldParm, Fld.AdvancedSearch.SearchOperator2);
 }
 // Export a value (caption, field value, or aggregate)
 public void ExportValueEx(ref cField fld, string val, bool usestyle)
 {
     switch (Table.Export) {
         case "html":
         case "email":
         case "word":
         case "excel":
             Text += "<td" + ((usestyle && EW_EXPORT_CSS_STYLES) ? fld.CellStyles : "") + ">";
             if (Table.Export == "excel" && fld.FldDataType == EW_DATATYPE_STRING && Information.IsNumeric(val)) {
                 Text += "=\"" + val + "\"";
             } else {
                 Text += val;
             }
             Text += "</td>";
             break;
         case "csv":
             if (ew_NotEmpty(Line))
                 Line += ",";
             Line += "\"" + val.Replace("\"", "\"\"") + "\"";
             break;
         case "pdf":
             Text += "<td" + ((usestyle && EW_EXPORT_CSS_STYLES) ? fld.CellStyles : "") + ">" + val + "</td>\r\n";
             break;
     }
 }
 // Get multi search SQL part
 public static string ew_GetMultiSearchSqlPart(ref cField Fld, string FldVal)
 {
     return Fld.FldExpression + ew_Like("'" + ew_AdjustSql(FldVal) + ",%'") + " OR " +
         Fld.FldExpression + ew_Like("'%," + ew_AdjustSql(FldVal) + ",%'") + " OR " +
         Fld.FldExpression + ew_Like("'%," + ew_AdjustSql(FldVal) + "'");
 }
 // Export a value (caption, field value, or aggregate)
 public void ExportValueEx(ref cField fld, string val)
 {
     ExportValueEx(ref fld, val, true);
 }
 // Get search SQL
 public static string ew_GetSearchSql(ref cField Fld, string FldVal, string FldOpr, string FldCond, string FldVal2, string FldOpr2)
 {
     bool IsValidValue;
     string sSql = "";
     string sFldExpression;
     if (Fld.FldIsVirtual && !Fld.FldForceSelection) {
         sFldExpression = Fld.FldVirtualExpression;
     } else {
         sFldExpression = Fld.FldExpression;
     }
     int FldDataType = Fld.FldDataType;
     if (Fld.FldIsVirtual && !Fld.FldForceSelection)
         FldDataType = EW_DATATYPE_STRING;
     if (FldDataType == EW_DATATYPE_NUMBER) { // Fix wrong operator
         if (FldOpr == "LIKE" || FldOpr == "STARTS WITH") {
             FldOpr = "=";
         } else if (FldOpr == "NOT LIKE") {
             FldOpr = "<>";
         }
         if (FldOpr2 == "LIKE" || FldOpr2 == "STARTS WITH") {
             FldOpr2 = "=";
         } else if (FldOpr2 == "NOT LIKE") {
             FldOpr2 = "<>";
         }
     }
     if (FldOpr == "BETWEEN")	{
         IsValidValue = (FldDataType != EW_DATATYPE_NUMBER) || (FldDataType == EW_DATATYPE_NUMBER && Information.IsNumeric(FldVal) && Information.IsNumeric(FldVal2));
         if (ew_NotEmpty(FldVal) && ew_NotEmpty(FldVal2) && IsValidValue)	{
             sSql = sFldExpression + " BETWEEN " + ew_QuotedValue(FldVal, FldDataType) + " AND " + ew_QuotedValue(FldVal2, FldDataType);
         }
     } else if (FldVal == EW_NULL_VALUE || FldOpr == "IS NULL") {
         sSql = sFldExpression + " IS NULL";
     } else if (FldVal == EW_NOT_NULL_VALUE || FldOpr == "IS NOT NULL") {
         sSql = sFldExpression + " IS NOT NULL";
     }	else {
         IsValidValue = (FldDataType != EW_DATATYPE_NUMBER) || (FldDataType == EW_DATATYPE_NUMBER && Information.IsNumeric(FldVal));
         if (ew_NotEmpty(FldVal) && IsValidValue && ew_IsValidOpr(FldOpr, FldDataType))	{
             sSql = sFldExpression + ew_SearchString(FldOpr, FldVal, FldDataType);
             if (FldDataType == EW_DATATYPE_BOOLEAN && FldVal == "0" && FldOpr == "=")
                 sSql = "(" + sSql + " OR " + sFldExpression + " IS NULL)";
         }
         IsValidValue = (FldDataType != EW_DATATYPE_NUMBER) || (FldDataType == EW_DATATYPE_NUMBER && Information.IsNumeric(FldVal2));
         if (ew_NotEmpty(FldVal2) && IsValidValue && ew_IsValidOpr(FldOpr2, FldDataType))	{
             string sSql2 = sFldExpression + ew_SearchString(FldOpr2, FldVal2, FldDataType);
             if (FldDataType == EW_DATATYPE_BOOLEAN && FldVal2 == "0" && FldOpr2 == "=")
                 sSql2 = "(" + sSql2 + " OR " + sFldExpression + " IS NULL)";
             if (ew_NotEmpty(sSql)) {
                 sSql = "(" + sSql + " " + ((FldCond == "OR") ? "OR" : "AND") + " " + sSql2 + ")";
             } else {
                 sSql = sSql2;
             }
         }
     }
     return sSql;
 }
 // Single column sort
 public void UpdateSort(cField ofld)
 {
     string sLastSort, sSortField, sThisSort;
     if (CurrentOrder == ofld.FldName)	{
         sSortField = ofld.FldExpression;
         sLastSort = ofld.Sort;
         if (CurrentOrderType == "ASC" || CurrentOrderType == "DESC")	{
             sThisSort = CurrentOrderType;
         }	else {
             sThisSort = (sLastSort == "ASC") ? "DESC" : "ASC";
         }
         ofld.Sort = sThisSort;
         SessionOrderBy = sSortField + " " + sThisSort;	// Save to Session
         string sSortFieldList = (ew_NotEmpty(ofld.FldVirtualExpression)) ? ofld.FldVirtualExpression : sSortField;
         SessionOrderByList = sSortFieldList + " " + sThisSort; // Save to Session
     }	else	{
         ofld.Sort = "";
     }
 }
 // Generate Value Separator based on current row index
 // rowidx - zero based row index
 // dispidx - zero based display index
 // fld - field object
 public static string ew_ValueSeparator(int rowidx, int dispidx, cField fld)
 {
     return ", ";
 }
 // Convert value to object for parameter
 public object ParameterValue(ref cField fld, object value)
 {
     return value;
 }
 // Field aggregate
 public void ExportAggregate(cField fld, string type)
 {
     if (Horizontal) {
         string val = "";
         if (ew_SameText(type, "TOTAL") || ew_SameText(type, "COUNT") || ew_SameText(type, "AVERAGE"))
             val = Language.Phrase(type) + ": " + fld.ExportValue(Table.Export, Table.ExportOriginalValue);
         ExportValueEx(ref fld, val);
     }
 }
 // Convert to parameter name for use in SQL
 public string SqlParameter(ref cField fld)
 {
     string sValue = EW_DB_SQLPARAM_SYMBOL;
     if (EW_DB_SQLPARAM_SYMBOL != "?")
         sValue += fld.FldVar;
     return sValue;
 }
 // Convert value to object for parameter
 public object ParameterValue(ref cField fld, object value)
 {
     if (EW_ENCRYPTED_PASSWORD && ew_SameStr(fld.FldName, "Password")) {
         if (EW_CASE_SENSITIVE_PASSWORD) {
             return ew_EncryptPassword(Convert.ToString(value));
         } else {
             return ew_EncryptPassword(Convert.ToString(value).ToLower());
         }
     } else {
         return value;
     }
 }