)>}]
شركة التطبيقات المتكاملة لتصميم وبرمجة البرمجيات الخاصة ش.ش.و.
Integrated Applications Programming Company
Home » Code Library » Default (Ia.Cl.Model)

Public general use code classes and xml files that we've compiled and used over the years:

General use static class of common functions used by most applications.

    1: using Microsoft.AspNetCore.Http;
    2: using System;
    3: using System.Collections;
    4: using System.Collections.Generic;
    5: using System.ComponentModel;
    6: using System.Configuration;
    7: using System.Data;
    8: using System.Globalization;
    9: using System.IO;
   10: using System.Linq;
   11: using System.Net;
   12: using System.Reflection;
   13: using System.Text;
   14: using System.Text.RegularExpressions;
   15: //#if WFA
   16: //#else
   17: using System.Xml;
   18: using System.Xml.Linq;
   19: using static System.Net.WebRequestMethods;
   20: //#endif
   21:  
   22: namespace Ia.Cl.Model
   23: {
   24:     ////////////////////////////////////////////////////////////////////////////
   25:  
   26:     /// <summary publish="true">
   27:     /// General use static class of common functions used by most applications.
   28:     /// 
   29:     /// </summary>
   30:     /// <remarks> 
   31:     /// Copyright © 2001-2019 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
   32:     ///
   33:     /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
   34:     /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
   35:     ///
   36:     /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
   37:     /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
   38:     /// 
   39:     /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
   40:     /// 
   41:     /// Copyright notice: This notice may not be removed or altered from any source distribution.
   42:     /// </remarks> 
   43:     public static class Default
   44:     {
   45:         static Random random = new Random();
   46:         // very important that you declare random here then use random.Next() elsewhere, otherwise you will have duplicate results
   47:  
   48:         ////////////////////////////////////////////////////////////////////////////
   49:  
   50:         /// <summary>
   51:         /// Splits CSV file lines
   52:         /// </summary>
   53:         public static IEnumerable<string> CsvLineSplitter(string line)
   54:         {
   55:             int fieldStart = 0;
   56:  
   57:             for (int i = 0; i < line.Length; i++)
   58:             {
   59:                 if (line[i] == ',')
   60:                 {
   61:                     yield return line.Substring(fieldStart, i - fieldStart);
   62:  
   63:                     fieldStart = i + 1;
   64:                 }
   65:  
   66:                 if (line[i] == '"') for (i++; line[i] != '"'; i++) { }
   67:             }
   68:         }
   69:  
   70:         ////////////////////////////////////////////////////////////////////////////
   71:  
   72:         /// <summary>
   73:         ///
   74:         /// </summary>
   75:         public static string Substring(string str, int len)
   76:         {
   77:             if (str.Length >= len - 3) str = str.Substring(0, len - 3) + "... ";
   78:             else if (str.Length > 0) str = str.Substring(0, str.Length);
   79:             else str = "";
   80:  
   81:             return str;
   82:         }
   83:  
   84:         ////////////////////////////////////////////////////////////////////////////
   85:  
   86:         /// <summary>
   87:         ///
   88:         /// </summary>
   89:         public static string Url(string text, string url)
   90:         {
   91:             return "<a href=" + url + ">" + text + "</a>";
   92:         }
   93:  
   94:         ////////////////////////////////////////////////////////////////////////////
   95:  
   96:         /// <summary>
   97:         ///
   98:         /// </summary>
   99:         public static string YesNo(bool checkValue)
  100:         {
  101:             string text;
  102:  
  103:             if (checkValue) text = "<span class=\"yes\">Yes</span>";
  104:             else text = "<span class=\"no\">No</span>";
  105:  
  106:             return text;
  107:         }
  108:  
  109:         ////////////////////////////////////////////////////////////////////////////
  110:  
  111:         /// <summary>
  112:         ///
  113:         /// </summary>
  114:         public static string YesNo(bool? checkValue)
  115:         {
  116:             string text;
  117:  
  118:             if (checkValue == null) text = "<span class=\"na\">NA</span>";
  119:             else if (checkValue == true) text = "<span class=\"yes\">Yes</span>";
  120:             else text = "<span class=\"no\">No</span>";
  121:  
  122:             return text;
  123:         }
  124:  
  125:         ////////////////////////////////////////////////////////////////////////////
  126:  
  127:         /// <summary>
  128:         ///
  129:         /// </summary>
  130:         public static string YesNoText(bool checkValue)
  131:         {
  132:             string text;
  133:  
  134:             if (checkValue) text = "Yes";
  135:             else text = "No";
  136:  
  137:             return text;
  138:         }
  139:  
  140:         ////////////////////////////////////////////////////////////////////////////
  141:  
  142:         /// <summary>
  143:         ///
  144:         /// </summary>
  145:         public static string YesNoInArabic(bool checkValue)
  146:         {
  147:             string text;
  148:  
  149:             if (checkValue) text = "<span class=\"yes\">نعم</span>";
  150:             else text = "<span class=\"no\">لا</span>";
  151:  
  152:             return text;
  153:         }
  154:  
  155:         ////////////////////////////////////////////////////////////////////////////
  156:  
  157:         /// <summary>
  158:         ///
  159:         /// </summary>
  160:         public static string YesNoInArabic(bool? checkValue)
  161:         {
  162:             string text;
  163:  
  164:             if (checkValue == null) text = "<span class=\"na\">لا ينطبق</span>";
  165:             else if (checkValue == true) text = "<span class=\"yes\">نعم</span>";
  166:             else text = "<span class=\"no\">لا</span>";
  167:  
  168:             return text;
  169:         }
  170:  
  171:         ////////////////////////////////////////////////////////////////////////////
  172:  
  173:         /// <summary>
  174:         ///
  175:         /// </summary>
  176:         public static string ActiveIdle(object o)
  177:         {
  178:             bool b;
  179:             string s;
  180:  
  181:             if (o != null && o.ToString().Length > 0)
  182:             {
  183:                 b = (bool)o;
  184:  
  185:                 if (b) s = "<span style=\"color:Green\">Active</span>";
  186:                 else s = "<span style=\"color:DarkGoldenRod\">Idle</span>";
  187:             }
  188:             else s = "";
  189:  
  190:             return s;
  191:         }
  192:  
  193:         ////////////////////////////////////////////////////////////////////////////
  194:  
  195:         /// <summary>
  196:         ///
  197:         /// </summary>
  198:         public static string ActiveIdleInArabic(object o)
  199:         {
  200:             bool b;
  201:             string s;
  202:  
  203:             if (o != null && o.ToString().Length > 0)
  204:             {
  205:                 b = (bool)o;
  206:  
  207:                 if (b) s = "<span style=\"color:Green\">فعال</span>";
  208:                 else s = "<span style=\"color:DarkGoldenRod\">مطفأ</span>";
  209:             }
  210:             else s = "";
  211:  
  212:             return s;
  213:         }
  214:  
  215:         ////////////////////////////////////////////////////////////////////////////
  216:         ////////////////////////////////////////////////////////////////////////////
  217:  
  218:         /// <summary>
  219:         /// Return a random number n where maxValue > n >= 0
  220:         /// <param name="maxValue">integer</param>
  221:         /// <returns>int where maxValue > n >= 0</returns>
  222:         /// </summary>
  223:         public static int Random(int maxValue)
  224:         {
  225:             return random.Next(maxValue);
  226:         }
  227:  
  228:         ////////////////////////////////////////////////////////////////////////////
  229:  
  230:         /// <summary>
  231:         /// Return a random number n, with seed, where ra > num >= 0
  232:         /// <param name="seed">integer</param>
  233:         /// <param name="ra">integer</param>
  234:         /// <returns>int where ra > num >=0</returns>
  235:         /// </summary>
  236:         public static int RandomWithSeed(int seed, int ra)
  237:         {
  238:             // return a random number num: ra > num >= 0
  239:             int num;
  240:  
  241:             num = random.Next(ra);
  242:  
  243:             return num;
  244:         }
  245:  
  246:         ////////////////////////////////////////////////////////////////////////////
  247:  
  248:         /// <summary>
  249:         /// Return a random number n where r1 > num >= r0
  250:         /// <param name="r1">integer max</param>
  251:         /// <param name="r0">integer min</param>
  252:         /// <returns>int where r1 >= num >= r0</returns>
  253:         /// </summary>
  254:         public static int Random(int r0, int r1)
  255:         {
  256:             // return a random number num: r1 >= num >= r0
  257:             int num;
  258:  
  259:             num = random.Next(r1) + r0;
  260:  
  261:             return num;
  262:         }
  263:  
  264:         ////////////////////////////////////////////////////////////////////////////
  265:  
  266:         /// <summary>
  267:         /// Return a pseudo-random item from list
  268:         /// </summary>
  269:         public static string Random(string s)
  270:         {
  271:             // take a list of comma seperated values in s and return one pseudo-random value
  272:             int n;
  273:             string[] sp;
  274:  
  275:             sp = s.Split(',');
  276:  
  277:             n = Random(sp.Length);
  278:  
  279:             return sp[n].ToString();
  280:         }
  281:  
  282:         ////////////////////////////////////////////////////////////////////////////
  283:  
  284:         /// <summary>
  285:         ///
  286:         /// </summary>
  287:         public static bool RandomBool
  288:         {
  289:             get
  290:             {
  291:                 bool b;
  292:                 int n;
  293:  
  294:                 n = random.Next(2);
  295:  
  296:                 if (n == 1) b = true;
  297:                 else b = false;
  298:  
  299:                 return b;
  300:             }
  301:         }
  302:  
  303:         ////////////////////////////////////////////////////////////////////////////
  304:  
  305:         /// <summary>
  306:         /// Return a random weighted bool value between 0 and 100
  307:         /// </summary>
  308:         public static bool RandomPercentWeightedBool(int weight)
  309:         {
  310:             bool b;
  311:  
  312:             weight = weight > 100 ? 100 : weight;
  313:             weight = weight < 0 ? 0 : weight;
  314:  
  315:             b = (random.Next(0, 100) < weight);
  316:  
  317:             return b;
  318:         }
  319:  
  320:         ////////////////////////////////////////////////////////////////////////////
  321:  
  322:         /// <summary>
  323:         /// Return a random percent value between 0 and 100
  324:         /// </summary>
  325:         public static int RandomPercent()
  326:         {
  327:             return random.Next(0, 100);
  328:         }
  329:  
  330:         ////////////////////////////////////////////////////////////////////////////
  331:  
  332:         /// <summary>
  333:         ///
  334:         /// </summary>
  335:         public static string RandomAlphanumeric(int length)
  336:         {
  337:             const string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  338:  
  339:             return new string(Enumerable.Repeat(chars, length).Select(s => s[random.Next(s.Length)]).ToArray());
  340:         }
  341:  
  342:         ////////////////////////////////////////////////////////////////////////////
  343:  
  344:         /// <summary>
  345:         ///
  346:         /// </summary>
  347:         public static string RandomNumber(int length)
  348:         {
  349:             const string chars = "0123456789";
  350:  
  351:             return new string(Enumerable.Repeat(chars, length).Select(s => s[random.Next(s.Length)]).ToArray());
  352:         }
  353:  
  354:         ////////////////////////////////////////////////////////////////////////////
  355:         ////////////////////////////////////////////////////////////////////////////
  356:  
  357:         /// <summary>
  358:         ///
  359:         /// </summary>
  360:         public static List<int> RandomList(int start, int end, int count)
  361:         {
  362:             int n;
  363:             List<int> list;
  364:  
  365:             list = new List<int>();
  366:  
  367:             while (list.Count < count)
  368:             {
  369:                 n = random.Next(start, end);
  370:  
  371:                 list.Add(n);
  372:             }
  373:  
  374:             return list;
  375:         }
  376:  
  377:         ////////////////////////////////////////////////////////////////////////////
  378:  
  379:         /// <summary>
  380:         /// Return at tick + count long
  381:         /// </summary>
  382:         public static long TickAndCountId(int count)
  383:         {
  384:             return DateTime.UtcNow.AddHours(3).Ticks + count;
  385:         }
  386:  
  387:         ////////////////////////////////////////////////////////////////////////////
  388:  
  389:         /// <summary>
  390:         /// Return at second + count long
  391:         /// </summary>
  392:         public static long TickSecondAndCountId(int count)
  393:         {
  394:             return DateTimeSecond() + count;
  395:         }
  396:  
  397:         ////////////////////////////////////////////////////////////////////////////
  398:  
  399:         /// <summary>
  400:         ///
  401:         /// </summary>
  402:         public static string TickDateTime(string tick)
  403:         {
  404:             // reads a tick count and returns the date time as string
  405:             string line;
  406:  
  407:             try
  408:             {
  409:                 DateTime t = new DateTime(long.Parse(tick));
  410:                 line = t.ToString("dd/MM/yyyy HH:mm");
  411:             }
  412:             catch (Exception)
  413:             {
  414:                 line = "";
  415:             }
  416:  
  417:             return line;
  418:         }
  419:  
  420:         ////////////////////////////////////////////////////////////////////////////
  421:  
  422:         /// <summary>
  423:         ///
  424:         /// </summary>
  425:         public static long DateTimeSecond(DateTime dt)
  426:         {
  427:             // return the number of seconds (total) in datetime
  428:             long l;
  429:  
  430:             // A single tick represents one hundred nanoseconds or one ten-millionth of a second
  431:  
  432:             l = dt.Ticks / 10000000;
  433:  
  434:             return l;
  435:         }
  436:  
  437:         ////////////////////////////////////////////////////////////////////////////
  438:  
  439:         /// <summary>
  440:         /// Return the number of seconds (total) in datetime. A single tick represents one hundred nanoseconds or one ten-millionth of a second.
  441:         /// </summary>
  442:         public static int DateTimeSecond()
  443:         {
  444:             return (int)(DateTime.UtcNow.AddHours(3).Ticks / 10000000);
  445:         }
  446:  
  447: #if WFA
  448: #else
  449:         /*
  450:         ////////////////////////////////////////////////////////////////////////////
  451: 
  452:         /// <summary>
  453:         ///
  454:         /// </summary>
  455:         public static void InitializeDropDownList(DropDownList ddl, DataTable source_dt, string text_field, string value_field, int selected_index)
  456:         {
  457:             int index;
  458: 
  459:             if (selected_index == -1) index = ddl.SelectedIndex;
  460:             else index = selected_index;
  461: 
  462:             ddl.Items.Clear();
  463:             //ddl.Items.Add(new ListItem("XXX","0"));
  464:             ddl.DataSource = source_dt;
  465:             ddl.DataTextField = text_field;
  466:             ddl.DataValueField = value_field;
  467:             ddl.DataBind();
  468:             ddl.SelectedIndex = index;
  469:         }
  470:         */
  471: #endif
  472:         ////////////////////////////////////////////////////////////////////////////
  473:  
  474:         /// <summary>
  475:         ///
  476:         /// </summary>
  477:         public static bool IsFloat(string line)
  478:         {
  479:             // this check if line is floating point number. 
  480:             bool ni = false;
  481:  
  482:             try { float x = float.Parse(line); ni = true; }
  483:             catch (Exception) { ni = false; }
  484:  
  485:             return ni;
  486:         }
  487:  
  488:         ////////////////////////////////////////////////////////////////////////////
  489:  
  490:         /// <summary>
  491:         ///
  492:         /// </summary>
  493:         public static bool IsDecimal(string line)
  494:         {
  495:             // this check if line is a decimal number. 
  496:             bool ni = false;
  497:  
  498:             try { decimal x = decimal.Parse(line); ni = true; }
  499:             catch (Exception) { ni = false; }
  500:  
  501:             return ni;
  502:         }
  503:  
  504:         ////////////////////////////////////////////////////////////////////////////
  505:  
  506:         /// <summary>
  507:         ///
  508:         /// </summary>
  509:         public static bool IsInt(string line)
  510:         {
  511:             // this check if line is an integer
  512:             bool ni = false;
  513:  
  514:             try { int x = int.Parse(line); ni = true; }
  515:             catch (Exception) { ni = false; }
  516:  
  517:             return ni;
  518:         }
  519:  
  520:         ////////////////////////////////////////////////////////////////////////////
  521:  
  522:         /// <summary>
  523:         ///
  524:         /// </summary>
  525:         public static string ByteToHex(byte[] bytes)
  526:         {
  527:             StringBuilder sb = new StringBuilder(2500);
  528:  
  529:             UTF8Encoding utf8 = new UTF8Encoding();
  530:             foreach (byte b in bytes) sb.Append(b.ToString("x2"));
  531:  
  532:             return sb.ToString();
  533:         }
  534:  
  535:         ////////////////////////////////////////////////////////////////////////////
  536:  
  537:         /// <summary>
  538:         ///
  539:         /// </summary>
  540:         public static byte[] HexToByte(string hex_str)
  541:         {
  542:             int i, n;
  543:             byte[] bytes;
  544:             char[] chars;
  545:             string c_str = "";
  546:             UTF8Encoding utf8 = new UTF8Encoding();
  547:  
  548:             chars = hex_str.ToCharArray();
  549:  
  550:             bytes = new byte[chars.Length / 2];  // since hex_str has two chars for every byte
  551:  
  552:             n = 0;
  553:             for (i = 0; i < chars.Length; i += 2)
  554:             {
  555:                 c_str = chars[i].ToString() + chars[i + 1].ToString();
  556:                 bytes[n++] = (byte)Convert.ToInt32(c_str, 16);
  557:             }
  558:  
  559:             return bytes;
  560:         }
  561:  
  562:         ////////////////////////////////////////////////////////////////////////////
  563:  
  564:         /// <summary>
  565:         ///
  566:         /// </summary>
  567:         public static string DecToHex(int dec)
  568:         {
  569:             uint uiDecimal = 0;
  570:             string hex;
  571:  
  572:             try
  573:             {
  574:                 // convert text string to unsigned integer
  575:                 uiDecimal = checked((uint)System.Convert.ToUInt32(dec));
  576:  
  577:                 hex = String.Format("{0:x2}", uiDecimal);
  578:             }
  579:             catch (System.OverflowException)
  580:             {
  581:                 // Show overflow message and return
  582:                 hex = "";
  583:             }
  584:  
  585:             return hex;
  586:         }
  587:  
  588:         ////////////////////////////////////////////////////////////////////////////
  589:  
  590:         /// <summary>
  591:         ///
  592:         /// </summary>
  593:         public static int HexToDec(string hex)
  594:         {
  595:             // To hold our converted unsigned integer32 value
  596:             uint dec;
  597:  
  598:             try
  599:             {
  600:                 // Convert hex string to unsigned integer
  601:                 dec = System.Convert.ToUInt32(hex, 16);
  602:             }
  603:             catch (System.OverflowException)
  604:             {
  605:                 // Show overflow message and return
  606:                 return 0;
  607:             }
  608:  
  609:             return (int)dec;
  610:         }
  611:  
  612:         ////////////////////////////////////////////////////////////////////////////
  613:  
  614:         /// <summary>
  615:         /// http://stackoverflow.com/questions/3702216/how-to-convert-integer-to-binary-string-in-c
  616:         /// </summary>
  617:         public static string IntegerToBinaryString(int x)
  618:         {
  619:             return Convert.ToString(x, 2);
  620:         }
  621:  
  622:         ////////////////////////////////////////////////////////////////////////////
  623:  
  624:         /// <summary>
  625:         /// pos 0 is least significant bit, pos 7 is most
  626:         /// http://stackoverflow.com/questions/2431732/checking-if-a-bit-is-set-or-not
  627:         /// </summary>
  628:         public static bool IsBitSet(byte b, int pos)
  629:         {
  630:             return (b & (1 << pos)) != 0;
  631:         }
  632:  
  633:         ////////////////////////////////////////////////////////////////////////////
  634:  
  635:         /// <summary>
  636:         ///
  637:         /// </summary>
  638:         public static DataTable GenerateEmptyDataTable(DataTable dt)
  639:         {
  640:             // this function is used to produce a single empty DataTable line to make the GridView look nicer.
  641:             // this will simply clone the in_dt and create a completly empty line
  642:             DataRow dr;
  643:  
  644:             if (dt.Rows.Count == 0)
  645:             {
  646:  
  647:                 try
  648:                 {
  649:                     dr = dt.NewRow();
  650:  
  651:                     foreach (DataColumn dc in dt.Columns)
  652:                     {
  653:                         dr[dc.ColumnName] = DBNull.Value;
  654:                     }
  655:  
  656:                     dt.Rows.Add(dr);
  657:                 }
  658:                 catch (Exception)
  659:                 {
  660:                     return null;
  661:                 }
  662:             }
  663:  
  664:             return dt;
  665:         }
  666:  
  667: #if WFA
  668: #else
  669:         /*
  670:         ////////////////////////////////////////////////////////////////////////////
  671: 
  672:         /// <summary>
  673:         /// 
  674:         /// </summary>
  675:         public static string GetPostBackControl(Page page)
  676:         {
  677:             // return the name of control that fired the postback
  678:             // this is strange, sometimes it fills the ID with real name of control and sometimes the TEXT
  679: 
  680:             string s = "";
  681:             Control c = null;
  682: 
  683:             string ctrlname = page.Request.Params.Get("__EVENTTARGET");
  684:             if (ctrlname != null && ctrlname != string.Empty)
  685:             {
  686:                 c = page.FindControl(ctrlname);
  687:             }
  688:             else
  689:             {
  690:                 foreach (string ctl in page.Request.Form)
  691:                 {
  692:                     Control ci = page.FindControl(ctl);
  693:                     if (ci is System.Web.UI.WebControls.Button)
  694:                     {
  695:                         c = ci;
  696:                         break;
  697:                     }
  698:                 }
  699:             }
  700: 
  701:             if (c != null)
  702:             {
  703:                 if (c.GetType().ToString() == "System.Web.UI.WebControls.Button")
  704:                 {
  705:                     s = ((System.Web.UI.WebControls.Button)c).ID;
  706:                 }
  707:                 else if (c.GetType().ToString() == "System.Web.UI.WebControls.DropDownList")
  708:                 {
  709:                     s = ((System.Web.UI.WebControls.DropDownList)c).ID;
  710:                 }
  711:                 else s = "";
  712:             }
  713:             else s = "";
  714: 
  715:             return s;
  716:         }
  717:         */
  718: #endif
  719:  
  720:         ////////////////////////////////////////////////////////////////////////////
  721:  
  722:         /// <summary>
  723:         ///
  724:         /// </summary>
  725:         public static string Color(int count, int index)
  726:         {
  727:             string s;
  728:  
  729:             // rainbow:
  730:             string[] color = { "#f00", "#f10", "#f20", "#f30", "#f40", "#f50", "#f60", "#f70", "#f80", "#f90", "#fa0", "#fb0", "#fc0", "#fd0", "#fe0", "#ff0", "#ef0", "#df0", "#cf0", "#bf0", "#af0", "#9f0", "#8f0", "#7f0", "#6f0", "#5f0", "#4f0", "#3f0", "#2f0", "#1f0", "#0f0", "#0f1", "#0f2", "#0f3", "#0f4", "#0f5", "#0f6", "#0f7", "#0f8", "#0f9", "#0fa", "#0fb", "#0fc", "#0fd", "#0fe", "#0ff", "#0ef", "#0df", "#0cf", "#0bf", "#0af", "#09f", "#08f", "#07f", "#06f", "#05f", "#04f", "#03f", "#02f", "#01f", "#00f", "#10f", "#20f", "#30f", "#40f", "#50f", "#60f", "#70f", "#80f", "#90f", "#a0f", "#b0f", "#c0f", "#d0f", "#e0f" };
  731:  
  732:             // random clear
  733:             //string[] color = {"Black","Blue","BlueViolet","Brown","CadetBlue","CornFlowerBlue","Crimson","DarkBlue","DarkCyan","DarkGoldenRod","DarkGreen","DarkMagenta","DarkOliveGreen","DarkOrange","DarkOrchid","DarkRed","DarkSlateBlue","DarkSlateGray","DarkViolet","Firebrick","ForestGreen","Green","IndianRed","Indigo","LightGray","LightSeaGreen","LightSkyBlue","LightSlateGray","Maroon","MediumBlue","MediumOrchid","MediumPurple","MediumSeaGreen","MediumSlateBlue","MediumVioletRed","MidnightBlue","Navy","Olive","OliveDrab"," Orange","OrangeRed","Orchid","Purple","Red","RosyBrown","RoyalBlue","SaddleBrown","SlateBlue","SlateGray","Teal","Tomato","Transparent" };
  734:  
  735:             if (index > 0) index--;
  736:  
  737:             s = color[color.Length / count * index];
  738:  
  739:             return s;
  740:         }
  741:  
  742:         ////////////////////////////////////////////////////////////////////////////
  743:  
  744:         /// <summary>
  745:         ///
  746:         /// </summary>
  747:         public static List<string> PaintColorList(int count, int index)
  748:         {
  749:             var s = new List<string>() { "000000", "7F7F7F", "880015", "ED1C24", "FF7F27", "FFF200", "22B14C", "00A2E8", "3F48CC", "A349A4", "FFFFFF", "C3C3C3", "B97A57", "FFAEC9", "FFC90E", "EFE4B0", "B5E61D", "99D9EA", "7092BE", "C8BFE7" };
  750:  
  751:             return s;
  752:         }
  753:  
  754:         ////////////////////////////////////////////////////////////////////////////
  755:  
  756:         /// <summary>
  757:         ///
  758:         /// </summary>
  759:         public static string FormatHtml(string text)
  760:         {
  761:             text = Regex.Replace(text, @"[\n|\r]+", "</p><p>");
  762:             text = "<p>" + text + "</p>";
  763:  
  764:             return text;
  765:         }
  766:  
  767:         ////////////////////////////////////////////////////////////////////////////
  768:         ////////////////////////////////////////////////////////////////////////////
  769:  
  770:         /// <summary>
  771:         ///
  772:         /// <see href="http://stackoverflow.com/questions/11412956/what-is-the-best-way-of-validating-an-ip-address"/>
  773:         /// </summary>
  774:         public static bool IsValidIpv4(string ipString)
  775:         {
  776:             bool isValid;
  777:             byte tempForParsing;
  778:             string[] splitValues;
  779:  
  780:             if (!string.IsNullOrWhiteSpace(ipString) && !string.IsNullOrEmpty(ipString))
  781:             {
  782:                 splitValues = ipString.Split('.');
  783:  
  784:                 if (splitValues.Length != 4) isValid = false;
  785:                 else
  786:                 {
  787:                     isValid = splitValues.All(r => byte.TryParse(r, out tempForParsing));
  788:                 }
  789:             }
  790:             else isValid = false;
  791:  
  792:             return isValid;
  793:         }
  794:  
  795:         ////////////////////////////////////////////////////////////////////////////
  796:  
  797:         /// <summary>
  798:         ///
  799:         /// <see href="https://stackoverflow.com/questions/7578857/how-to-check-whether-a-string-is-a-valid-http-url"/>
  800:         /// </summary>
  801:         public static bool IsValidUrl(string uriName)
  802:         {
  803:             var isValid = Uri.TryCreate(uriName, UriKind.Absolute, out Uri uriResult) && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);
  804:  
  805:             return isValid;
  806:         }
  807:  
  808:         ////////////////////////////////////////////////////////////////////////////
  809:  
  810:         /// <summary>
  811:         ///
  812:         /// </summary>
  813:         public static uint IpToUint(string ip)
  814:         {
  815:             uint uintAddress;
  816:  
  817:             IPAddress address = IPAddress.Parse(ip);
  818:             byte[] bytes = address.GetAddressBytes();
  819:             Array.Reverse(bytes); // flip big-endian(network order) to little-endian
  820:             uintAddress = BitConverter.ToUInt32(bytes, 0);
  821:  
  822:             return uintAddress;
  823:  
  824:             /*
  825:             string delimStr = ".";
  826:             char[] delimiter = delimStr.ToCharArray();
  827:             string[] ip_addr = null;
  828:             long ip_num = 0;
  829: 
  830:             try
  831:             {
  832:                 ip_addr = ip.Split(delimiter, 4);
  833:                 ip_num = (long.Parse(ip_addr[0]) * 16777216) + (long.Parse(ip_addr[1]) * 65536) + (long.Parse(ip_addr[2]) * 256) + (long.Parse(ip_addr[3]));
  834:             }
  835:             catch (Exception)
  836:             {
  837:             }
  838: 
  839:             return ip_num;
  840:             */
  841:  
  842:             /*
  843:             long l;
  844:             string r;
  845:             Match m;
  846: 
  847:             m = Regex.Match(ip, @"(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})");
  848: 
  849:             if (m.Success)
  850:             {
  851:                 r = m.Groups[1].Captures[0].Value.PadLeft(3, '0') + m.Groups[2].Captures[0].Value.PadLeft(3, '0') + m.Groups[3].Captures[0].Value.PadLeft(3, '0') + m.Groups[4].Captures[0].Value.PadLeft(3, '0');
  852:                 l = long.Parse(r);
  853:             }
  854:             else l = 0;
  855: 
  856:             return l;
  857:             */
  858:         }
  859:  
  860:         ////////////////////////////////////////////////////////////////////////////
  861:  
  862:         /// <summary>
  863:         ///
  864:         /// </summary>
  865:         public static string UintToIp(uint ui)
  866:         {
  867:             string ipAddress;
  868:  
  869:             byte[] bytes = BitConverter.GetBytes(ui);
  870:             Array.Reverse(bytes); // flip little-endian to big-endian(network order)
  871:             ipAddress = new IPAddress(bytes).ToString();
  872:  
  873:             return ipAddress;
  874:  
  875:             /*
  876:             string s, s1, s2, s3, s4;
  877: 
  878:             s = l.ToString();
  879:             s = s.PadLeft(12, '0');
  880: 
  881:             s1 = s.Substring(0, 3);
  882:             s2 = s.Substring(3, 3);
  883:             s3 = s.Substring(6, 3);
  884:             s4 = s.Substring(9, 3);
  885: 
  886:             s = s1 + "." + s2 + "." + s3 + "." + s4;
  887: 
  888:             s = Regex.Replace(s, @"\.0+", ".");
  889:             s = Regex.Replace(s, @"^0+", "");
  890:             if (s[s.Length - 1] == '.') s = s + "0";
  891: 
  892:             return s;
  893:             */
  894:         }
  895:  
  896:         ////////////////////////////////////////////////////////////////////////////
  897:  
  898:         /// <summary>
  899:         ///
  900:         /// </summary>
  901:         public static string IpToHex(string ip)
  902:         {
  903:             string h;
  904:             Match m;
  905:  
  906:             h = "";
  907:  
  908:             m = Regex.Match(ip, @"(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})");
  909:  
  910:             if (m.Success)
  911:             {
  912:                 h = DecToHex(int.Parse(m.Groups[1].Captures[0].Value)) + DecToHex(int.Parse(m.Groups[2].Captures[0].Value)) + DecToHex(int.Parse(m.Groups[3].Captures[0].Value)) + DecToHex(int.Parse(m.Groups[4].Captures[0].Value));
  913:             }
  914:             else h = "";
  915:  
  916:             return h;
  917:         }
  918:  
  919:         ////////////////////////////////////////////////////////////////////////////
  920:  
  921:         /// <summary>
  922:         ///
  923:         /// </summary>
  924:         public static string HexToIp(string h)
  925:         {
  926:             string s, s1, s2, s3, s4;
  927:  
  928:             h = h.PadLeft(8, '0');
  929:  
  930:             s1 = h.Substring(0, 2);
  931:             s2 = h.Substring(2, 2);
  932:             s3 = h.Substring(4, 2);
  933:             s4 = h.Substring(6, 2);
  934:  
  935:             s = HexToDec(s1) + "." + HexToDec(s2) + "." + HexToDec(s3) + "." + HexToDec(s4);
  936:  
  937:             return s;
  938:         }
  939:  
  940:         ////////////////////////////////////////////////////////////////////////////
  941:  
  942:         /// <summary>
  943:         ///
  944:         /// </summary>
  945:         public static string DecToIp(int i)
  946:         {
  947:             return HexToIp(DecToHex(i));
  948:         }
  949:  
  950:         ////////////////////////////////////////////////////////////////////////////
  951:  
  952:         /// <summary>
  953:         ///
  954:         /// </summary>
  955:         public static int IpToDec(string s)
  956:         {
  957:             return HexToDec(IpToHex(s));
  958:         }
  959:  
  960:         ////////////////////////////////////////////////////////////////////////////
  961:  
  962:         /// <summary>
  963:         /// Check a string to see if all characters are hexadecimal values
  964:         /// <see href="https://stackoverflow.com/questions/223832/check-a-string-to-see-if-all-characters-are-hexadecimal-values"/>
  965:         /// </summary>
  966:         public static bool IsHex(string term)
  967:         {
  968:             // For C-style hex notation (0xFF) you can use @"\A\b(0[xX])?[0-9a-fA-F]+\b\Z"
  969:             return System.Text.RegularExpressions.Regex.IsMatch(term, @"\A\b[0-9a-fA-F]+\b\Z");
  970:         }
  971:  
  972:         ////////////////////////////////////////////////////////////////////////////
  973:  
  974:         /// <summary>
  975:         ///
  976:         /// </summary>
  977:         public static string NetworkNumberFromIp(string ip)
  978:         {
  979:             string[] ipp;
  980:             string networkNumber;
  981:  
  982:             networkNumber = "";
  983:  
  984:             if (IsValidIpv4(ip))
  985:             {
  986:                 ipp = ip.Split('.');
  987:  
  988:                 networkNumber = (ipp[0] + "." + ipp[1] + "." + ipp[2] + ".0");
  989:             }
  990:  
  991:             return networkNumber;
  992:         }
  993:  
  994:         ////////////////////////////////////////////////////////////////////////////
  995:  
  996:         /// <summary>
  997:         ///
  998:         /// </summary>
  999:         public static List<string> NetworkNumberStrippedList(string startIp, string endIp)
 1000:         {
 1001:             int startIpI, endIpI;
 1002:             string s;
 1003:             List<long> li;
 1004:             List<string> list;
 1005:  
 1006:             li = new List<long>();
 1007:             list = new List<string>();
 1008:  
 1009:             // change network number to a real IP
 1010:             if (Regex.IsMatch(startIp, @"^.+\.0$")) startIp = Regex.Replace(startIp, @"^(.+)\.0$", "$1.1");
 1011:             if (Regex.IsMatch(endIp, @"^.+\.0$")) endIp = Regex.Replace(endIp, @"^(.+)\.0$", "$1.1");
 1012:  
 1013:             startIpI = IpToDec(startIp);
 1014:             endIpI = IpToDec(endIp);
 1015:  
 1016:             for (int i = startIpI; i <= endIpI; i += 256)
 1017:             {
 1018:                 s = DecToIp(i);
 1019:                 s = NetworkNumberFromIp(s);
 1020:  
 1021:                 s = Regex.Replace(s, @"^(.+)\.0$", "$1");
 1022:  
 1023:                 list.Add(s);
 1024:             }
 1025:  
 1026:             return list;
 1027:         }
 1028:  
 1029:         ////////////////////////////////////////////////////////////////////////////
 1030:  
 1031:         /// <summary>
 1032:         /// Check if the passed IP address belongs to Kuwait
 1033:         /// </summary>
 1034:         public static bool IsKuwaitIp(string ip)
 1035:         {
 1036:             bool b;
 1037:             uint ui;
 1038:  
 1039:             ui = Ia.Cl.Models.Default.IpToUint(ip);
 1040:  
 1041:             if (
 1042:             (ui >= 1044742144 && ui <= 1044750335)
 1043:             || (ui >= 1050017792 && ui <= 1050083327)
 1044:             || (ui >= 1054277632 && ui <= 1054343167)
 1045:             || (ui >= 1075513152 && ui <= 1075513183)
 1046:             || (ui >= 1125110400 && ui <= 1125110463)
 1047:             || (ui >= 1161630912 && ui <= 1161630919)
 1048:             || (ui >= 1161641888 && ui <= 1161641911)
 1049:             || (ui >= 1163558016 && ui <= 1163558028)
 1050:             || (ui >= 1308094464 && ui <= 1308096511)
 1051:             || (ui >= 1314455552 && ui <= 1314521087)
 1052:             || (ui >= 1318764544 && ui <= 1318780927)
 1053:             || (ui >= 1319084032 && ui <= 1319092223)
 1054:             || (ui >= 1347219456 && ui <= 1347223551)
 1055:             || (ui >= 1347294424 && ui <= 1347294431)
 1056:             || (ui >= 1347321856 && ui <= 1347323775)
 1057:             || (ui >= 1347323904 && ui <= 1347325183)
 1058:             || (ui >= 1347325440 && ui <= 1347325951)
 1059:             || (ui >= 1354235904 && ui <= 1354301439)
 1060:             || (ui >= 1361035904 && ui <= 1361035907)
 1061:             || (ui >= 1361036764 && ui <= 1361036767)
 1062:             || (ui >= 1361036792 && ui <= 1361036795)
 1063:             || (ui >= 1365220792 && ui <= 1365220799)
 1064:             || (ui >= 1383273984 && ui <= 1383276543)
 1065:             || (ui >= 1383367168 && ui <= 1383367679)
 1066:             || (ui >= 1383368848 && ui <= 1383368895)
 1067:             || (ui >= 1383369120 && ui <= 1383369231)
 1068:             || (ui >= 1383369248 && ui <= 1383369535)
 1069:             || (ui >= 1383369600 && ui <= 1383370751)
 1070:             || (ui >= 1383371776 && ui <= 1383374591)
 1071:             || (ui >= 1385283584 && ui <= 1385291775)
 1072:             || (ui >= 1397006336 && ui <= 1397014527)
 1073:             || (ui >= 1398800384 && ui <= 1398833151)
 1074:             || (ui >= 1403658528 && ui <= 1403658559)
 1075:             || (ui >= 1410013696 && ui <= 1410013727)
 1076:             || (ui >= 1410013920 && ui <= 1410013951)
 1077:             || (ui >= 1410014016 && ui <= 1410014047)
 1078:             || (ui >= 1410014464 && ui <= 1410014495)
 1079:             || (ui >= 1410027008 && ui <= 1410027263)
 1080:             || (ui >= 1410035200 && ui <= 1410035231)
 1081:             || (ui >= 1410035264 && ui <= 1410035295)
 1082:             || (ui >= 1425426432 && ui <= 1425428479)
 1083:             || (ui >= 1441726464 && ui <= 1441729023)
 1084:             || (ui >= 1441729536 && ui <= 1441734655)
 1085:             || (ui >= 1475115008 && ui <= 1475117055)
 1086:             || (ui >= 1500186624 && ui <= 1500188671)
 1087:             || (ui >= 1506476032 && ui <= 1506508799)
 1088:             || (ui >= 1509642240 && ui <= 1509644351)
 1089:             || (ui >= 1509644384 && ui <= 1509646335)
 1090:             || (ui >= 1533419520 && ui <= 1533419775)
 1091:             || (ui >= 1533420032 && ui <= 1533420287)
 1092:             || (ui >= 1533420544 && ui <= 1533421567)
 1093:             || (ui >= 1533448192 && ui <= 1533450239)
 1094:             || (ui >= 1533470720 && ui <= 1533472767)
 1095:             || (ui >= 1533513728 && ui <= 1533515775)
 1096:             || (ui >= 1535934464 && ui <= 1535967231)
 1097:             || (ui >= 1536660016 && ui <= 1536660019)
 1098:             || (ui >= 1536663424 && ui <= 1536663551)
 1099:             || (ui >= 1539227648 && ui <= 1539229695)
 1100:             || (ui >= 1539466752 && ui <= 1539467263)
 1101:             || (ui >= 1539473920 && ui <= 1539474431)
 1102:             || (ui >= 1539737088 && ui <= 1539737343)
 1103:             || (ui >= 1540410112 && ui <= 1540410367)
 1104:             || (ui >= 1540467712 && ui <= 1540467967)
 1105:             || (ui >= 1540622336 && ui <= 1540622591)
 1106:             || (ui >= 1540628480 && ui <= 1540628735)
 1107:             || (ui >= 1540790272 && ui <= 1540791295)
 1108:             || (ui >= 1572814848 && ui <= 1572816895)
 1109:             || (ui >= 1578991616 && ui <= 1579024383)
 1110:             || (ui >= 1585446912 && ui <= 1585577983)
 1111:             || (ui >= 1589346304 && ui <= 1589379071)
 1112:             || (ui >= 1598160896 && ui <= 1598193663)
 1113:             || (ui >= 1603137536 && ui <= 1603141631)
 1114:             || (ui >= 1605320704 && ui <= 1605328895)
 1115:             || (ui >= 1925638656 && ui <= 1925638911)
 1116:             || (ui >= 1925639680 && ui <= 1925639935)
 1117:             || (ui >= 2341273600 && ui <= 2341339135)
 1118:             || (ui >= 2717646848 && ui <= 2717712383)
 1119:             || (ui >= 2830827520 && ui <= 2830893055)
 1120:             || (ui >= 3169255424 && ui <= 3169271807)
 1121:             || (ui >= 3169275904 && ui <= 3169278991)
 1122:             || (ui >= 3169279008 && ui <= 3169279231)
 1123:             || (ui >= 3169279256 && ui <= 3169279263)
 1124:             || (ui >= 3169279296 && ui <= 3169279303)
 1125:             || (ui >= 3169279320 && ui <= 3169279743)
 1126:             || (ui >= 3169279760 && ui <= 3169281023)
 1127:             || (ui >= 3169281280 && ui <= 3169288191)
 1128:             || (ui >= 3239285760 && ui <= 3239286783)
 1129:             || (ui >= 3239488512 && ui <= 3239488767)
 1130:             || (ui >= 3240222720 && ui <= 3240223231)
 1131:             || (ui >= 3240812288 && ui <= 3240812543)
 1132:             || (ui >= 3245088256 && ui <= 3245088511)
 1133:             || (ui >= 3249111552 && ui <= 3249112063)
 1134:             || (ui >= 3250335744 && ui <= 3250339839)
 1135:             || (ui >= 3250359808 && ui <= 3250362879)
 1136:             || (ui >= 3250364416 && ui <= 3250372607)
 1137:             || (ui >= 3251120128 && ui <= 3251120639)
 1138:             || (ui >= 3252483072 && ui <= 3252483583)
 1139:             || (ui >= 3252484096 && ui <= 3252486143)
 1140:             || (ui >= 3258368000 && ui <= 3258384383)
 1141:             || (ui >= 3262477220 && ui <= 3262477223)
 1142:             || (ui >= 3262478601 && ui <= 3262478601)
 1143:             || (ui >= 3263045632 && ui <= 3263046847)
 1144:             || (ui >= 3263046912 && ui <= 3263047935)
 1145:             || (ui >= 3263048192 && ui <= 3263053823)
 1146:             || (ui >= 3266341888 && ui <= 3266342143)
 1147:             || (ui >= 3274145792 && ui <= 3274162175)
 1148:             || (ui >= 3276114944 && ui <= 3276115967)
 1149:             || (ui >= 3276687872 && ui <= 3276688383)
 1150:             || (ui >= 3276858112 && ui <= 3276858367)
 1151:             || (ui >= 3277381120 && ui <= 3277381631)
 1152:             || (ui >= 3280580096 && ui <= 3280580351)
 1153:             || (ui >= 3285922816 && ui <= 3285923327)
 1154:             || (ui >= 3286425600 && ui <= 3286433791)
 1155:             || (ui >= 3286566656 && ui <= 3286567423)
 1156:             || (ui >= 3286568192 && ui <= 3286568703)
 1157:             || (ui >= 3286571008 && ui <= 3286571775)
 1158:             || (ui >= 3288417536 && ui <= 3288418047)
 1159:             || (ui >= 3340584704 && ui <= 3340584959)
 1160:             || (ui >= 3350042880 && ui <= 3350043135)
 1161:             || (ui >= 3453376848 && ui <= 3453376887)
 1162:             || (ui >= 3487969792 && ui <= 3487970047)
 1163:             || (ui >= 3509522432 && ui <= 3509522687)
 1164:             || (ui >= 3509559040 && ui <= 3509559295)
 1165:             || (ui >= 3512891232 && ui <= 3512891263)
 1166:             || (ui >= 3517438880 && ui <= 3517438911)
 1167:             || (ui >= 3518894096 && ui <= 3518894103)
 1168:             || (ui >= 3523593216 && ui <= 3523593231)
 1169:             || (ui >= 3559587840 && ui <= 3559596031)
 1170:             || (ui >= 3561742336 && ui <= 3561750527)
 1171:             || (ui >= 3575824384 && ui <= 3575832575)
 1172:             || (ui >= 3582255104 && ui <= 3582263295)
 1173:             || (ui >= 3585949696 && ui <= 3585957887)
 1174:             || (ui >= 3628153088 && ui <= 3628153343)
 1175:             || (ui >= 3630097664 && ui <= 3630098175)
 1176:             || (ui >= 3630100224 && ui <= 3630100479)
 1177:             || (ui >= 3632481760 && ui <= 3632481767)
 1178:             || (ui >= 3645222912 && ui <= 3645227007)
 1179:             ) b = true;
 1180:             else b = false;
 1181:  
 1182:             return b;
 1183:         }
 1184:  
 1185:         ////////////////////////////////////////////////////////////////////////////
 1186:  
 1187:         /// <summary>
 1188:         ///
 1189:         /// </summary>
 1190:         public static bool IsPrivateIp(string userHostAddress)
 1191:         {
 1192:             bool isPrivate;
 1193:             int[] ipParts;
 1194:  
 1195:             ipParts = userHostAddress.Split(new String[] { "." }, StringSplitOptions.RemoveEmptyEntries).Select(s => int.Parse(s)).ToArray();
 1196:  
 1197:             if (ipParts[0] == 10 || (ipParts[0] == 192 && ipParts[1] == 168) || (ipParts[0] == 172 && (ipParts[1] >= 16 && ipParts[1] <= 31)))
 1198:             {
 1199:                 isPrivate = true;
 1200:             }
 1201:             else
 1202:             {
 1203:                 // IP address is probably public. This doesn't catch some VPN ranges like OpenVPN and Hamachi.
 1204:                 isPrivate = false;
 1205:             }
 1206:  
 1207:             return isPrivate;
 1208:         }
 1209:  
 1210:         ////////////////////////////////////////////////////////////////////////////
 1211:  
 1212:         /// <summary>
 1213:         /// Check if the this.Request.UserHostAddress from the webpage is from the development machine or IP.
 1214:         /// <param name="userHostAddress">The this.Request.UserHostAddress from the webpage</param>
 1215:         /// </summary>
 1216:         public static bool IsIaHostAddress(string userHostAddress)
 1217:         {
 1218:             bool b;
 1219:             string hostAddress;
 1220:  
 1221:             if (ConfigurationManager.AppSettings["iaHostAddress"] != null)
 1222:             {
 1223:                 hostAddress = ConfigurationManager.AppSettings["iaHostAddress"].ToString();
 1224:  
 1225:                 if (userHostAddress == hostAddress || userHostAddress == "::1") b = true;
 1226:                 else b = false;
 1227:             }
 1228:             else b = false;
 1229:  
 1230:             return b;
 1231:         }
 1232:  
 1233:         /*
 1234:         ////////////////////////////////////////////////////////////////////////////
 1235: 
 1236:         /// <summary>
 1237:         ///
 1238:         /// </summary>
 1239:         public static string RequestUserIP()
 1240:         {
 1241:             string ip, ipList;
 1242: 
 1243:             ipList = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
 1244: 
 1245:             if (!string.IsNullOrEmpty(ipList))
 1246:             {
 1247:                 ip = ipList.Split(',')[0];
 1248:             }
 1249:             else ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
 1250: 
 1251:             return ip;
 1252:         }
 1253:         */
 1254:  
 1255:         ////////////////////////////////////////////////////////////////////////////
 1256:         ////////////////////////////////////////////////////////////////////////////
 1257:  
 1258:         /// <summary>
 1259:         ///
 1260:         /// </summary>
 1261:         public static Hashtable DataTableToHashtable(DataTable dt)
 1262:         {
 1263:             // put the datatable first row value into a hashtable key, and second as value. if the table has only one column we will add it only to keys with 0 value
 1264:             Hashtable ht;
 1265:  
 1266:             if (dt != null)
 1267:             {
 1268:                 if (dt.Rows.Count > 0)
 1269:                 {
 1270:  
 1271:                     ht = new Hashtable(dt.Rows.Count);
 1272:  
 1273:                     if (dt.Columns.Count == 1) foreach (DataRow r in dt.Rows) ht[r[0].ToString()] = "0";
 1274:                     else if (dt.Columns.Count > 1) foreach (DataRow r in dt.Rows) ht[r[0].ToString()] = r[1].ToString();
 1275:                 }
 1276:                 else ht = new Hashtable(1);
 1277:             }
 1278:             else ht = null;
 1279:  
 1280:             return ht;
 1281:         }
 1282:  
 1283:         ////////////////////////////////////////////////////////////////////////////
 1284:  
 1285:         /// <summary>
 1286:         ///
 1287:         /// </summary>
 1288:         public static string DataTableToString(DataTable dataTable)
 1289:         {
 1290:             var output = new StringBuilder();
 1291:  
 1292:             // write Column titles
 1293:             for (int i = 0; i < dataTable.Columns.Count; i++)
 1294:             {
 1295:                 var text = dataTable.Columns[i].ColumnName;
 1296:                 output.Append("\t" + text);
 1297:             }
 1298:  
 1299:             output.Append("|\n" + new string('=', output.Length) + "\n");
 1300:  
 1301:             // write rows
 1302:             foreach (DataRow row in dataTable.Rows)
 1303:             {
 1304:                 for (int i = 0; i < dataTable.Columns.Count; i++)
 1305:                 {
 1306:                     var text = row[i].ToString();
 1307:                     output.Append("\t" + text);
 1308:                 }
 1309:  
 1310:                 output.Append("|\n");
 1311:             }
 1312:  
 1313:             return output.ToString();
 1314:         }
 1315:  
 1316:         /*
 1317:         ////////////////////////////////////////////////////////////////////////////
 1318: 
 1319:         /// <summary>
 1320:         ///
 1321:         /// </summary>
 1322:         public static ArrayList ArrayList_Limit_Randomize(ArrayList in_al, int max)
 1323:         {
 1324:             // 
 1325:             // parameter: ArrayList with any number of entries, an integer value indicating the max possible number of returned values
 1326:             // procedure: randomly select upto max values from al and return max >= num >= 0
 1327: 
 1328:             int n, o;
 1329:             ArrayList al;
 1330:             Hashtable ht;
 1331: 
 1332:             if (max > 0)
 1333:             {
 1334:                 al = new ArrayList(max);
 1335:                 ht = new Hashtable(max);
 1336: 
 1337:                 o = 0;
 1338: 
 1339:                 while (o < in_al.Count - 1 && o < max)
 1340:                 {
 1341:                     foreach (string s in in_al)
 1342:                     {
 1343:                         n = r.Next(max);
 1344: 
 1345:                         if (!ht.ContainsKey(n))
 1346:                         {
 1347:                             al.Add(s);
 1348:                             ht[n] = 1;
 1349:                             o++;
 1350:                         }
 1351:                     }
 1352:                 }
 1353: 
 1354:             }
 1355:             else al = null;
 1356: 
 1357:             return al;
 1358:         }
 1359:         */
 1360:  
 1361:         ////////////////////////////////////////////////////////////////////////////
 1362:  
 1363:         /// <summary>
 1364:         ///
 1365:         /// </summary>
 1366:         public static ArrayList SublistArrayList(ArrayList in_al, int n)
 1367:         {
 1368:             // return the first n values from all
 1369:             ArrayList al;
 1370:  
 1371:             if (n > 0)
 1372:             {
 1373:                 al = new ArrayList(n);
 1374:  
 1375:                 for (int i = 0; i < in_al.Count - 1 && i < n; i++)
 1376:                 {
 1377:                     al.Add(in_al[i]);
 1378:                 }
 1379:             }
 1380:             else al = null;
 1381:  
 1382:             return al;
 1383:         }
 1384:  
 1385:         ////////////////////////////////////////////////////////////////////////////
 1386:  
 1387:         /// <summary>
 1388:         ///
 1389:         /// </summary>
 1390:         public static ArrayList ShuffleAndSublistArrayList(ArrayList in_al, int n)
 1391:         {
 1392:             // 
 1393:  
 1394:             ShuffleArrayList(in_al);
 1395:  
 1396:             return SublistArrayList(in_al, n);
 1397:         }
 1398:  
 1399:         ////////////////////////////////////////////////////////////////////////////
 1400:  
 1401:         /// <summary>
 1402:         ///
 1403:         /// </summary>
 1404:         public static ArrayList KeyArrayHashtableToList(Hashtable ht)
 1405:         {
 1406:             // 
 1407:             ArrayList al;
 1408:  
 1409:             if (ht != null)
 1410:             {
 1411:                 if (ht.Count > 0)
 1412:                 {
 1413:                     al = new ArrayList(ht.Count);
 1414:  
 1415:                     foreach (string s in ht.Keys) al.Add(s);
 1416:                 }
 1417:                 else al = new ArrayList(1);
 1418:             }
 1419:             else al = null;
 1420:  
 1421:             al.Sort();
 1422:  
 1423:             return al;
 1424:         }
 1425:  
 1426:         ////////////////////////////////////////////////////////////////////////////
 1427:  
 1428:         /// <summary>
 1429:         ///
 1430:         /// </summary>
 1431:         public static ArrayList KeyIntegerHashtableToArrayList(Hashtable ht)
 1432:         {
 1433:             // 
 1434:             ArrayList al;
 1435:  
 1436:             if (ht != null)
 1437:             {
 1438:                 if (ht.Count > 0)
 1439:                 {
 1440:                     al = new ArrayList(ht.Count);
 1441:  
 1442:                     foreach (int i in ht.Keys) al.Add(i);
 1443:                 }
 1444:                 else al = new ArrayList(1);
 1445:             }
 1446:             else al = null;
 1447:  
 1448:             al.Sort();
 1449:  
 1450:             return al;
 1451:         }
 1452:  
 1453:         ////////////////////////////////////////////////////////////////////////////
 1454:  
 1455:         /// <summary>
 1456:         ///
 1457:         /// </summary>
 1458:         public static Hashtable ReverseKeyValueInHashtable(Hashtable in_ht)
 1459:         {
 1460:             // 
 1461:             Hashtable ht;
 1462:  
 1463:             if (in_ht != null)
 1464:             {
 1465:                 if (in_ht.Count > 0)
 1466:                 {
 1467:                     ht = new Hashtable(in_ht.Count);
 1468:  
 1469:                     foreach (string s in in_ht.Keys) ht[in_ht[s].ToString()] = s;
 1470:                 }
 1471:                 else ht = new Hashtable(1);
 1472:             }
 1473:             else ht = null;
 1474:  
 1475:             return ht;
 1476:         }
 1477:  
 1478:         ////////////////////////////////////////////////////////////////////////////
 1479:  
 1480:         /// <summary>
 1481:         ///
 1482:         /// </summary>
 1483:         public static ArrayList HashtableValueToArrayList(Hashtable ht)
 1484:         {
 1485:             // 
 1486:             ArrayList al;
 1487:  
 1488:             if (ht != null)
 1489:             {
 1490:                 if (ht.Count > 0)
 1491:                 {
 1492:                     al = new ArrayList(ht.Count);
 1493:  
 1494:                     foreach (string s in ht.Values) al.Add(s);
 1495:                 }
 1496:                 else al = new ArrayList(1);
 1497:             }
 1498:             else al = null;
 1499:  
 1500:             al.Sort();
 1501:  
 1502:             return al;
 1503:         }
 1504:  
 1505:         ////////////////////////////////////////////////////////////////////////////
 1506:  
 1507:         /// <summary>
 1508:         ///
 1509:         /// </summary>
 1510:         public static string HashtableKeyString(Hashtable ht)
 1511:         {
 1512:             // 
 1513:             string si;
 1514:             StringBuilder sb;
 1515:  
 1516:             if (ht != null)
 1517:             {
 1518:                 if (ht.Count > 0)
 1519:                 {
 1520:                     sb = new StringBuilder(ht.Count);
 1521:                     sb.Length = 0;
 1522:  
 1523:                     foreach (string s in ht.Keys) sb.Append(s + "|");
 1524:                 }
 1525:                 else sb = new StringBuilder(1);
 1526:             }
 1527:             else sb = null;
 1528:  
 1529:             si = sb.ToString();
 1530:             si = si.Remove(si.Length - 1, 1);
 1531:  
 1532:             return si;
 1533:         }
 1534:  
 1535:         ////////////////////////////////////////////////////////////////////////////
 1536:  
 1537:         /// <summary>
 1538:         ///
 1539:         /// </summary>
 1540:         public static Hashtable SortHashtableKey(Hashtable in_ht)
 1541:         {
 1542:             // sort the hashtable keys alphabetically
 1543:  
 1544:             ArrayList al;
 1545:             Hashtable ht;
 1546:  
 1547:             if (in_ht != null)
 1548:             {
 1549:                 if (in_ht.Count > 0)
 1550:                 {
 1551:                     al = new ArrayList(in_ht.Count + 1);
 1552:                     ht = new Hashtable(in_ht.Count + 1);
 1553:  
 1554:                     al.Clear();
 1555:                     foreach (string s in in_ht.Keys) al.Add(s);
 1556:                     al.Sort();
 1557:                     foreach (string s in al) ht.Add(s, in_ht[s].ToString());
 1558:                 }
 1559:                 else ht = in_ht;
 1560:             }
 1561:             else ht = in_ht;
 1562:  
 1563:             return ht;
 1564:         }
 1565:  
 1566:         ////////////////////////////////////////////////////////////////////////////
 1567:  
 1568:         /// <summary>
 1569:         ///
 1570:         /// </summary>
 1571:         public static string HashtableValueString(Hashtable ht)
 1572:         {
 1573:             // 
 1574:             string si;
 1575:             StringBuilder sb;
 1576:  
 1577:             if (ht != null)
 1578:             {
 1579:                 if (ht.Count > 0)
 1580:                 {
 1581:                     sb = new StringBuilder(ht.Count);
 1582:                     sb.Length = 0;
 1583:  
 1584:                     foreach (string s in ht.Keys) sb.Append(ht[s].ToString() + "|");
 1585:                 }
 1586:                 else sb = new StringBuilder(1);
 1587:             }
 1588:             else sb = null;
 1589:  
 1590:             si = sb.ToString();
 1591:             si = si.Remove(si.Length - 1, 1);
 1592:  
 1593:             return si;
 1594:         }
 1595:  
 1596:         ////////////////////////////////////////////////////////////////////////////
 1597:  
 1598:         /// <summary>
 1599:         /// 
 1600:         /// </summary>
 1601:         public static string Match(string text, string regex)
 1602:         {
 1603:             string matchedText;
 1604:             Match m;
 1605:  
 1606:             m = Regex.Match(text, regex);
 1607:  
 1608:             if (m.Groups[1].Success) matchedText = m.Groups[1].Captures[0].Value;
 1609:             else matchedText = null;
 1610:  
 1611:             return matchedText;
 1612:         }
 1613:  
 1614:         ////////////////////////////////////////////////////////////////////////////
 1615:  
 1616:         /// <summary>
 1617:         /// 
 1618:         /// </summary>
 1619:         public static string MatchToLower(string s, string regex)
 1620:         {
 1621:             string t;
 1622:             Match m;
 1623:  
 1624:             m = Regex.Match(s, regex);
 1625:             if (m.Groups[1].Success) t = m.Groups[1].Captures[0].Value.ToLower();
 1626:             else t = null;
 1627:  
 1628:             return t;
 1629:         }
 1630:  
 1631:         ////////////////////////////////////////////////////////////////////////////
 1632:  
 1633:         /// <summary>
 1634:         ///
 1635:         /// </summary>
 1636:         public static bool IsRegexPatternValid(string pattern)
 1637:         {
 1638:             bool b;
 1639:  
 1640:             try
 1641:             {
 1642:                 new Regex(pattern);
 1643:  
 1644:                 b = true;
 1645:             }
 1646:             catch
 1647:             {
 1648:                 b = false;
 1649:             }
 1650:  
 1651:             return b;
 1652:         }
 1653:  
 1654: #if WFA
 1655: #else
 1656:         /*
 1657:         ////////////////////////////////////////////////////////////////////////////
 1658: 
 1659:         /// <summary>
 1660:         /// Store the current time in a cache variable
 1661:         /// </summary>
 1662:         public static void SetTimeSpan()
 1663:         {
 1664:             HttpRuntime httpRT = new HttpRuntime();
 1665:             Cache cache = HttpRuntime.Cache;
 1666: 
 1667:             cache["TimeSpan_Set"] = DateTime.UtcNow.AddHours(3).Ticks;
 1668:         }
 1669:         */
 1670:  
 1671:         /*
 1672:         ////////////////////////////////////////////////////////////////////////////
 1673: 
 1674:         /// <summary>
 1675:         /// Check if sec seconds had passed since timespan_set
 1676:         /// </summary>
 1677:         public static bool CheckTimeSpan(int sec)
 1678:         {
 1679:             bool b;
 1680:             long l;
 1681:             HttpRuntime httpRT = new HttpRuntime();
 1682:             Cache cache = HttpRuntime.Cache;
 1683: 
 1684:             if (cache["TimeSpan_Set"] == null) b = true;
 1685:             else
 1686:             {
 1687:                 l = (long)cache["TimeSpan_Set"];
 1688: 
 1689:                 if (DateTime.UtcNow.AddHours(3).AddSeconds(-sec).Ticks > l) b = true;
 1690:                 else b = false;
 1691:             }
 1692: 
 1693:             return b;
 1694:         }
 1695:         */
 1696:  
 1697:         /*
 1698:         ////////////////////////////////////////////////////////////////////////////
 1699: 
 1700:         /// <summary>
 1701:         /// Check if 1 sec seconds had passed since timespan_set
 1702:         /// </summary>
 1703:         public static bool CheckTimeSpan()
 1704:         {
 1705:             return CheckTimeSpan(1);
 1706:         }
 1707:         */
 1708:  
 1709: #endif
 1710:         ////////////////////////////////////////////////////////////////////////////
 1711:  
 1712:         /// <summary>
 1713:         /// Return the absolute path
 1714:         /// </summary>
 1715:         public static string AbsolutePath()
 1716:         {
 1717:             return AbsolutePath(false);
 1718:         }
 1719:  
 1720:         ////////////////////////////////////////////////////////////////////////////
 1721:  
 1722:         /// <summary>
 1723:         /// Return the absolute path to temp folder
 1724:         /// </summary>
 1725:         public static string AbsoluteTempPath()
 1726:         {
 1727:             return AbsolutePath(true);
 1728:         }
 1729:  
 1730:         ////////////////////////////////////////////////////////////////////////////
 1731:  
 1732:         /// <summary>
 1733:         /// Return the absolute path
 1734:         /// </summary>
 1735:         public static string AbsolutePath(bool temp_folder)
 1736:         {
 1737:             string path;
 1738:  
 1739: #if WFA
 1740:             if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed) path = System.Deployment.Application.ApplicationDeployment.CurrentDeployment.DataDirectory + @"\";
 1741:             else path = AppDomain.CurrentDomain.BaseDirectory;
 1742: #else
 1743:             if (temp_folder) path = AppDomain.CurrentDomain.BaseDirectory.ToString() + @"app_data\temp\";
 1744:             else path = AppDomain.CurrentDomain.BaseDirectory.ToString();
 1745: #endif
 1746:  
 1747:             //if (path.IndexOf(@"\bin") >= 0) path = path.Remove(path.IndexOf(@"\bin"), path.Length - path.IndexOf(@"\bin"));
 1748:  
 1749:             return path;
 1750:         }
 1751:  
 1752:         ////////////////////////////////////////////////////////////////////////////
 1753:  
 1754:         /// <summary>
 1755:         /// Return the absolute path parent directory
 1756:         /// </summary>
 1757:         public static string AbsolutePathParent()
 1758:         {
 1759:             string s;
 1760:  
 1761:             s = AbsolutePath(false);
 1762:  
 1763:             s = s.Remove(s.Length - 1, 1);
 1764:  
 1765:             s = s.Substring(0, s.LastIndexOf(@"\")) + @"\";
 1766:  
 1767:             return s;
 1768:         }
 1769:  
 1770:         ////////////////////////////////////////////////////////////////////////////
 1771:  
 1772:         /// <summary>
 1773:         /// Return domain name from page Uri
 1774:         /// </summary>
 1775:         public static string BasicHost(Uri uri)
 1776:         {
 1777:             string url, domain, puny, tld;
 1778:  
 1779:             url = uri.Host;
 1780:             url = url.ToLower();
 1781:  
 1782:             if (uri.Host == "localhost")
 1783:             {
 1784:                 url = uri.Segments[1].Replace("/", "");
 1785:                 url = url.ToLower();
 1786:                 url = url.Replace("http://", "");
 1787:                 url = url.Replace("https://", "");
 1788:                 url = url.Replace("www.", "");
 1789:                 url = url.Replace("m.", "");
 1790:             }
 1791:  
 1792:             if (url.Contains("xn--"))
 1793:             {
 1794:                 // URL is punycode
 1795:                 if (url.Contains(".com")) tld = "com";
 1796:                 else if (url.Contains(".net")) tld = "net";
 1797:                 else if (url.Contains(".org")) tld = "org";
 1798:                 else tld = "?";
 1799:  
 1800:                 url = url.Replace(".com", "");
 1801:                 url = url.Replace(".net", "");
 1802:                 url = url.Replace(".org", "");
 1803:  
 1804:                 domain = Ia.Cl.Models.Punycode.Decode(url) + "." + tld;
 1805:                 puny = url + "." + tld;
 1806:             }
 1807:             else
 1808:             {
 1809:                 // URL is not punycode
 1810:                 domain = url;
 1811:                 puny = url;
 1812:             }
 1813:  
 1814:             return domain;
 1815:         }
 1816:  
 1817:         /*
 1818:         ////////////////////////////////////////////////////////////////////////////
 1819: 
 1820:         /// <summary>
 1821:         /// Return the absolute URL
 1822:         /// </summary>
 1823:         public static string AbsoluteUrl(Page p)
 1824:         {
 1825:             string url;
 1826:             Uri uri;
 1827: 
 1828:             uri = p.Request.Url;
 1829: 
 1830:             if (uri.Host == "localhost")
 1831:             {
 1832:                 url = uri.Authority; // +@"/" + uri.Segments[1].Replace("/", "");
 1833:                 url = url.ToLower();
 1834:                 url = url.Replace("http://", "");
 1835:                 url = url.Replace("www.", "");
 1836:             }
 1837:             else
 1838:             {
 1839:                 url = uri.Host;
 1840:                 url = url.ToLower();
 1841:                 url = url.Replace("http://", "");
 1842:                 url = url.Replace("www.", "");
 1843:             }
 1844: 
 1845:             return url;
 1846:         }
 1847:         */
 1848:  
 1849:         /*
 1850:         ////////////////////////////////////////////////////////////////////////////
 1851: 
 1852:         /// <summary>
 1853:         /// Return the URL of a file from the path
 1854:         /// </summary>
 1855:         public static string AbsolutePathUrl(Page page, string file)
 1856:         {
 1857:             string s, absoluteUrl, absolutePath;
 1858: 
 1859:             absoluteUrl = AbsoluteUrl(page) + "/";
 1860:             absolutePath = AbsolutePath();
 1861: 
 1862:             s = file.Replace(absolutePath, absoluteUrl);
 1863: 
 1864:             s = s.Replace(@"\", @"/");
 1865: 
 1866:             return page.ResolveClientUrl("~/" + s);
 1867:         }
 1868:         */
 1869:  
 1870:         /*
 1871:         ////////////////////////////////////////////////////////////////////////////
 1872:         ////////////////////////////////////////////////////////////////////////////
 1873: 
 1874:         /// <summary>
 1875:         /// Shows a client-side JavaScript alert in the browser.
 1876:         /// </summary>
 1877:         public static void JavasciptSrc(Page p, string relative_url_file)
 1878:         {
 1879:             // cleans the message to allow single quotation marks
 1880:             string script;
 1881: 
 1882:             relative_url_file = relative_url_file.Replace("'", "\\'");
 1883: 
 1884:             script = "<script type=\"text/javascript\" src=\"" + Ia.Cl.Models.Default.AbsoluteUrl(p) + @"/" + relative_url_file + "\"/>";
 1885: 
 1886:             // gets the executing web page
 1887:             Page page = HttpContext.Current.CurrentHandler as Page;
 1888: 
 1889:             // checks if the handler is a Page and that the script isn't allready on the Page
 1890:             if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("alert"))
 1891:             {
 1892:                 page.ClientScript.RegisterClientScriptBlock(typeof(string), "alert", script);
 1893:             }
 1894:         }
 1895:         */
 1896:  
 1897:         ////////////////////////////////////////////////////////////////////////////
 1898:         ////////////////////////////////////////////////////////////////////////////
 1899:  
 1900:         /// <summary>
 1901:         /// Make line in proper title case
 1902:         /// </summary>
 1903:         public static string ToTitleCase(string input)
 1904:         {
 1905:             // Rules for Capitalization in Titles of Articles
 1906:             // http://grammar.yourdictionary.com/capitalization/rules-for-capitalization-in-titles.html#S9rKxG2G8gp88qcx.99http://grammar.yourdictionary.com/capitalization/rules-for-capitalization-in-titles.html
 1907:  
 1908:             // "Capitalize all words in titles of publications and documents, except a, an, the, at, by, for, in, of, on, to, up, and, as, but, or, and nor.
 1909:  
 1910:             // this is a ChatGPT generated code 2024-06
 1911:  
 1912:             string line;
 1913:  
 1914:             if (!string.IsNullOrEmpty(input))
 1915:             {
 1916:                 var textInfo = new CultureInfo("en-US", false).TextInfo;
 1917:  
 1918:                 string[] lowercaseWordList = { "a", "an", "and", "as", "at", "but", "by", "for", "if", "in", "nor", "of", "on", "or", "so", "the", "to", "up", "yet" };
 1919:  
 1920:                 // regex to match ordinals like 1st, 2nd, 3rd, 4th, etc.
 1921:                 var ordinalPattern = @"(\d+)(st|nd|rd|th)";
 1922:  
 1923:                 // regex for identifying Roman numerals
 1924:                 var romanNumeralPattern = @"\b(?=[MDCLXVI]+\b)M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})\b";
 1925:  
 1926:                 // regex pattern for identifying uppercased abbreviations
 1927:                 var upperAbbreviationPattern = @"\b[A-Z]{2,}\b";
 1928:  
 1929:                 line = Regex.Replace(input, @"\b[\w']+\b",
 1930:                     m =>
 1931:                     {
 1932:                         string word = m.Value;
 1933:  
 1934:                         if (Regex.IsMatch(word, romanNumeralPattern) || Regex.IsMatch(word, upperAbbreviationPattern)) line = word;
 1935:                         else if (lowercaseWordList.Contains(word.ToLower()) && m.Index != 0)
 1936:                         {
 1937:                             if (m.Index >= 2 && input[m.Index - 2] == ':') line = textInfo.ToTitleCase(word);
 1938:                             else line = word.ToLower(); // skip first word
 1939:                         }
 1940:                         else if (Regex.IsMatch(word, ordinalPattern)) line = word.ToLower();
 1941:                         else line = textInfo.ToTitleCase(word);
 1942:  
 1943:                         return line;
 1944:                     });
 1945:             }
 1946:             else line = string.Empty;
 1947:  
 1948:             return line;
 1949:         }
 1950:  
 1951:         ////////////////////////////////////////////////////////////////////////////
 1952:  
 1953:         /// <summary>
 1954:         /// Make the first letter of a word an upper letter
 1955:         /// </summary>
 1956:         public static string FirstLetterToUpper(string s)
 1957:         {
 1958:             string u;
 1959:  
 1960:             u = s.Substring(0, 1);
 1961:             return u.ToUpper() + s.Remove(0, 1);
 1962:         }
 1963:  
 1964:         ////////////////////////////////////////////////////////////////////////////
 1965:  
 1966:         /// <summary>
 1967:         /// Make the first letter of all words an upper letter and remove underscores
 1968:         /// </summary>
 1969:         public static string FirstWordLetterToUpperAndRemoveUnderscore(string line)
 1970:         {
 1971:             string u, v;
 1972:  
 1973:             v = "";
 1974:  
 1975:             line = RemoveUnderscore(line);
 1976:  
 1977:             foreach (string s in line.Split(' '))
 1978:             {
 1979:                 u = s.Substring(0, 1);
 1980:                 v += u.ToUpper() + s.Remove(0, 1) + " ";
 1981:             }
 1982:  
 1983:             if (v.Length > 0) v = v.Remove(v.Length - 1, 1);
 1984:  
 1985:             return v;
 1986:         }
 1987:  
 1988:         ////////////////////////////////////////////////////////////////////////////
 1989:  
 1990:         /// <summary>
 1991:         /// Convert the string to Pascal case.
 1992:         /// </summary>
 1993:         /// <remarks>http://csharphelper.com/blog/2014/10/convert-between-pascal-case-camel-case-and-proper-case-in-c/</remarks>
 1994:         public static string ToPascalCase(this string s)
 1995:         {
 1996:             // If there are 0 or 1 characters, just return the string.
 1997:             if (s == null) return s;
 1998:             if (s.Length < 2) return s.ToUpper();
 1999:  
 2000:             // Split the string into words.
 2001:             string[] words = s.Split(
 2002:                 new char[] { },
 2003:                 StringSplitOptions.RemoveEmptyEntries);
 2004:  
 2005:             // Combine the words.
 2006:             string result = "";
 2007:             foreach (string word in words)
 2008:             {
 2009:                 result +=
 2010:                     word.Substring(0, 1).ToUpper() +
 2011:                     word.Substring(1);
 2012:             }
 2013:  
 2014:             return result;
 2015:         }
 2016:  
 2017:         ////////////////////////////////////////////////////////////////////////////
 2018:  
 2019:         /// <summary>
 2020:         /// Convert the string to camel case.
 2021:         /// </summary>
 2022:         /// <remarks>http://csharphelper.com/blog/2014/10/convert-between-pascal-case-camel-case-and-proper-case-in-c/</remarks>
 2023:         public static string ProperToCamelCase(this string s)
 2024:         {
 2025:             // If there are 0 or 1 characters, just return the string.
 2026:             if (s == null || s.Length < 2)
 2027:                 return s;
 2028:  
 2029:             // Split the string into words.
 2030:             string[] words = s.Split(
 2031:                 new char[] { },
 2032:                 StringSplitOptions.RemoveEmptyEntries);
 2033:  
 2034:             // Combine the words.
 2035:             string result = words[0].ToLower();
 2036:             for (int i = 1; i < words.Length; i++)
 2037:             {
 2038:                 result +=
 2039:                     words[i].Substring(0, 1).ToUpper() +
 2040:                     words[i].Substring(1);
 2041:             }
 2042:  
 2043:             return result;
 2044:         }
 2045:  
 2046:         ////////////////////////////////////////////////////////////////////////////
 2047:  
 2048:         /// <summary>
 2049:         /// Capitalize the first character and add a space before each capitalized letter (except the first character).
 2050:         /// </summary>
 2051:         /// <remarks>http://csharphelper.com/blog/2014/10/convert-between-pascal-case-camel-case-and-proper-case-in-c/</remarks>
 2052:         public static string CamelToProperCase(this string s)
 2053:         {
 2054:             // If there are 0 or 1 characters, just return the string.
 2055:             if (s == null) return s;
 2056:             if (s.Length < 2) return s.ToUpper();
 2057:  
 2058:             // Start with the first character.
 2059:             string result = s.Substring(0, 1).ToUpper();
 2060:  
 2061:             // Add the remaining characters.
 2062:             for (int i = 1; i < s.Length; i++)
 2063:             {
 2064:                 if (char.IsUpper(s[i])) result += " ";
 2065:                 result += s[i];
 2066:             }
 2067:  
 2068:             return result;
 2069:         }
 2070:  
 2071:         ////////////////////////////////////////////////////////////////////////////
 2072:  
 2073:         /// <summary>
 2074:         /// Convert caps delimited to underscore, lower case string
 2075:         /// </summary>
 2076:         /// <remarks>http://stackoverflow.com/questions/5796383/insert-spaces-between-words-on-a-camel-cased-token</remarks>
 2077:         public static string CapsDelimitedToUnderscoreLowercase(this string s)
 2078:         {
 2079:             string u;
 2080:  
 2081:             u = Regex.Replace(s, "(\\B[A-Z])", "_$1");
 2082:  
 2083:             return u.ToLower();
 2084:         }
 2085:  
 2086:         ////////////////////////////////////////////////////////////////////////////
 2087:  
 2088:         /// <summary>
 2089:         /// Remove underscores
 2090:         /// </summary>
 2091:         public static string RemoveUnderscore(string line)
 2092:         {
 2093:             string u;
 2094:  
 2095:             u = line.Replace('_', ' ');
 2096:  
 2097:             return u;
 2098:         }
 2099:  
 2100:         ////////////////////////////////////////////////////////////////////////////
 2101:         ////////////////////////////////////////////////////////////////////////////
 2102:  
 2103:         /// <summary>
 2104:         /// Regex that defines email
 2105:         /// </summary>
 2106:         public static string EmailRegex()
 2107:         {
 2108:             // http://www.regular-expressions.info/email.html
 2109:             string s;
 2110:  
 2111:             s = @"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)\b";
 2112:  
 2113:             // Text="Invalid e-mail" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" runat="server" />
 2114:  
 2115:             return s;
 2116:         }
 2117:  
 2118:         ////////////////////////////////////////////////////////////////////////////
 2119:  
 2120:         /// <summary>
 2121:         ///
 2122:         /// </summary>
 2123:         public static bool IsEmail(string s)
 2124:         {
 2125:             // return true if argument is an email
 2126:             bool b;
 2127:  
 2128:             b = false;
 2129:  
 2130:             if (Regex.IsMatch(s, Ia.Cl.Models.Default.EmailRegex(), RegexOptions.IgnoreCase)) b = true;
 2131:  
 2132:             return b;
 2133:         }
 2134:  
 2135:         ////////////////////////////////////////////////////////////////////////////
 2136:  
 2137:         /// <summary>
 2138:         ///
 2139:         /// </summary>
 2140:         public static string Decimal(double d, int dec)
 2141:         {
 2142:             // 
 2143:             string s;
 2144:  
 2145:             if (dec == 3) s = string.Format("{0:0.000}", d);
 2146:             else if (dec == 2) s = string.Format("{0:0.00}", d);
 2147:             else if (dec == 1) s = string.Format("{0:0.0}", d);
 2148:             else s = d.ToString();
 2149:  
 2150:             return s;
 2151:         }
 2152:  
 2153:         ////////////////////////////////////////////////////////////////////////////
 2154:  
 2155:         /// <summary>
 2156:         /// Reverse a string
 2157:         /// </summary>
 2158:         public static string ReverseString(string t)
 2159:         {
 2160:             string s;
 2161:  
 2162:             s = "";
 2163:  
 2164:             foreach (char c in t.ToCharArray()) s = c + " " + s;
 2165:  
 2166:             return s;
 2167:         }
 2168:  
 2169:         ////////////////////////////////////////////////////////////////////////////
 2170:         ////////////////////////////////////////////////////////////////////////////
 2171:  
 2172:         /// <summary>
 2173:         /// Convent the data content of a DataSet to an XmlDocument object for use in API Services.
 2174:         /// <param name="ds">DataSet to convert to XmlDocument</param>
 2175:         /// <returns>XmlDocument</returns>
 2176:         /// </summary>
 2177:  
 2178:         public static XmlDocument DataSetToXmlDocument(DataSet ds)
 2179:         {
 2180:             return DataSetToXmlDocument(ds, null);
 2181:         }
 2182:  
 2183:         ////////////////////////////////////////////////////////////////////////////
 2184:  
 2185:         /// <summary>
 2186:         /// Convent the data content of a DataSet to an XmlDocument object for use in API Services.
 2187:         /// </summary>
 2188:         public static XmlDocument DataSetToXmlDocument(DataSet ds, string item_name)
 2189:         {
 2190:             XmlText xt;
 2191:             XmlElement set_xe, table_xe, row_xe, xe;
 2192:             XmlDeclaration xde;
 2193:             XmlDocument xd;
 2194:  
 2195:             xd = new XmlDocument();
 2196:  
 2197:             if (ds != null)
 2198:             {
 2199:                 xde = xd.CreateXmlDeclaration("1.0", "utf-8", null);
 2200:  
 2201:                 // create root element
 2202:                 if (ds.DataSetName.Length > 0) set_xe = xd.CreateElement(ds.DataSetName);
 2203:                 else set_xe = xd.CreateElement("set");
 2204:  
 2205:                 xd.InsertBefore(xde, xd.DocumentElement);
 2206:                 xd.AppendChild(set_xe);
 2207:  
 2208:                 if (ds.Tables.Count > 0)
 2209:                 {
 2210:                     foreach (DataTable dt in ds.Tables)
 2211:                     {
 2212:                         // create table element
 2213:                         if (dt.TableName.Length > 0) table_xe = xd.CreateElement(dt.TableName);
 2214:                         else table_xe = xd.CreateElement("table");
 2215:  
 2216:                         set_xe.AppendChild(table_xe);
 2217:  
 2218:                         if (dt.Rows.Count > 0)
 2219:                         {
 2220:                             foreach (DataRow r in dt.Rows)
 2221:                             {
 2222:                                 // create a new row and add it to the root node
 2223:                                 if (item_name == null) item_name = "row";
 2224:                                 row_xe = xd.CreateElement(item_name);
 2225:  
 2226:                                 table_xe.AppendChild(row_xe);
 2227:  
 2228:                                 foreach (DataColumn dc in dt.Columns)
 2229:                                 {
 2230:                                     xe = xd.CreateElement(dc.ColumnName);
 2231:  
 2232:                                     xt = xd.CreateTextNode(r[dc.ColumnName].ToString());
 2233:  
 2234:                                     xe.AppendChild(xt);
 2235:  
 2236:                                     row_xe.AppendChild(xe);
 2237:                                 }
 2238:                             }
 2239:                         }
 2240:                     }
 2241:                 }
 2242:                 else
 2243:                 {
 2244:                 }
 2245:             }
 2246:             else
 2247:             {
 2248:             }
 2249:  
 2250:             return xd;
 2251:         }
 2252:  
 2253:         ////////////////////////////////////////////////////////////////////////////
 2254:  
 2255:         /// <summary>
 2256:         /// Convert the data content of a DataTable to an XmlDocument object for use in API Services.
 2257:         /// <param name="dt">DataTable to convert to XmlDocument</param>
 2258:         /// <returns>XmlDocument</returns>
 2259:         /// </summary>
 2260:  
 2261:         public static XmlDocument DataTableToXmlDocument(DataTable dt)
 2262:         {
 2263:             return DataTableToXmlDocument(dt, null);
 2264:         }
 2265:  
 2266:         ////////////////////////////////////////////////////////////////////////////
 2267:  
 2268:         /// <summary>
 2269:         /// Convert the data content of a DataTable to an XmlDocument object for use in API Services.
 2270:         /// </summary>
 2271:         public static XmlDocument DataTableToXmlDocument(DataTable dt, string itemName)
 2272:         {
 2273:             XmlText xt;
 2274:             XmlElement table_xe, row_xe, xe;
 2275:             XmlDeclaration xde;
 2276:             XmlDocument xd;
 2277:  
 2278:             xd = new XmlDocument();
 2279:  
 2280:             if (dt != null)
 2281:             {
 2282:                 xde = xd.CreateXmlDeclaration("1.0", "utf-8", null);
 2283:  
 2284:                 // create root element
 2285:                 if (dt.TableName.Length > 0) table_xe = xd.CreateElement(dt.TableName);
 2286:                 else table_xe = xd.CreateElement("table");
 2287:  
 2288:                 xd.InsertBefore(xde, xd.DocumentElement);
 2289:                 xd.AppendChild(table_xe);
 2290:  
 2291:                 if (dt.Rows.Count > 0)
 2292:                 {
 2293:                     foreach (DataRow r in dt.Rows)
 2294:                     {
 2295:                         // create a new row and add it to the root node
 2296:                         if (itemName == null) itemName = "row";
 2297:                         row_xe = xd.CreateElement(itemName);
 2298:  
 2299:                         table_xe.AppendChild(row_xe);
 2300:  
 2301:                         foreach (DataColumn dc in dt.Columns)
 2302:                         {
 2303:                             xe = xd.CreateElement(dc.ColumnName);
 2304:  
 2305:                             xt = xd.CreateTextNode(r[dc.ColumnName].ToString());
 2306:  
 2307:                             xe.AppendChild(xt);
 2308:  
 2309:                             row_xe.AppendChild(xe);
 2310:                         }
 2311:                     }
 2312:                 }
 2313:             }
 2314:             else
 2315:             {
 2316:             }
 2317:  
 2318:             return xd;
 2319:         }
 2320:  
 2321:         ////////////////////////////////////////////////////////////////////////////
 2322:  
 2323:         /// <summary>
 2324:         /// Convert the data content of a DataTable to an XDocument object
 2325:         /// <param name="dt">DataTable to convert to XDocument</param>
 2326:         /// <returns>XDocument</returns>
 2327:         /// </summary>
 2328:  
 2329:         public static XDocument DataTableToXDocument(DataTable dt)
 2330:         {
 2331:             return DataTableToXDocument(dt, null);
 2332:         }
 2333:  
 2334:         ////////////////////////////////////////////////////////////////////////////
 2335:  
 2336:         /// <summary>
 2337:         /// Convert the data content of a DataTable to an XDocument object.
 2338:         /// </summary>
 2339:         public static XDocument DataTableToXDocument(DataTable dt, string itemName)
 2340:         {
 2341:             XElement tableXElement, rowXElement, xe;
 2342:             XDeclaration xde;
 2343:             XDocument xd;
 2344:  
 2345:             if (dt != null)
 2346:             {
 2347:                 xde = new XDeclaration("1.0", "utf-8", null);
 2348:  
 2349:                 // create root element
 2350:                 if (dt.TableName.Length > 0) tableXElement = new XElement(dt.TableName);
 2351:                 else tableXElement = new XElement("table");
 2352:  
 2353:                 if (dt.Rows.Count > 0)
 2354:                 {
 2355:                     foreach (DataRow r in dt.Rows)
 2356:                     {
 2357:                         // create a new row and add it to the root node
 2358:                         if (itemName == null) itemName = "row";
 2359:                         rowXElement = new XElement(itemName, "");
 2360:  
 2361:                         tableXElement.Add(rowXElement);
 2362:  
 2363:                         foreach (DataColumn dc in dt.Columns)
 2364:                         {
 2365:                             xe = new XElement(dc.ColumnName, r[dc.ColumnName].ToString());
 2366:  
 2367:                             rowXElement.Add(xe);
 2368:                         }
 2369:                     }
 2370:                 }
 2371:  
 2372:                 xd = new XDocument(xde, tableXElement);
 2373:             }
 2374:             else
 2375:             {
 2376:                 xd = null;
 2377:             }
 2378:  
 2379:             return xd;
 2380:         }
 2381:  
 2382:         ////////////////////////////////////////////////////////////////////////////
 2383:         ////////////////////////////////////////////////////////////////////////////
 2384:  
 2385:         /// <summary>
 2386:         /// Takes an XmlNodeList with text and value, and change them into a DataTable usable for databinding with controls
 2387:         /// </summary>
 2388:         public static DataTable XmlNodeListToDataTable(XmlNodeList xnl, string value, string text)
 2389:         {
 2390:             DataTable dt;
 2391:             DataRow dr;
 2392:  
 2393:             dt = new DataTable();
 2394:             dt.Columns.Add(value);
 2395:  
 2396:             if (value != text) dt.Columns.Add(text);
 2397:  
 2398:             foreach (XmlNode n in xnl)
 2399:             {
 2400:                 dr = dt.NewRow();
 2401:                 dr[value] = n.Attributes[value].Value;
 2402:                 if (value != text) dr[text] = n.Attributes[text].Value;
 2403:                 dt.Rows.Add(dr);
 2404:             }
 2405:  
 2406:             return dt;
 2407:         }
 2408:  
 2409:         ////////////////////////////////////////////////////////////////////////////
 2410:  
 2411:         /// <summary>
 2412:         /// Takes an XmlNodeList with text and value, and change them into a DataTable usable for databinding with controls
 2413:         /// </summary>
 2414:         public static DataTable XmlNodeListToDataTable(XmlNodeList xnl, string id, string text, string url)
 2415:         {
 2416:             DataTable dt;
 2417:             DataRow dr;
 2418:  
 2419:             dt = new DataTable();
 2420:             dt.Columns.Add(id);
 2421:             dt.Columns.Add(text);
 2422:             dt.Columns.Add(url);
 2423:  
 2424:             foreach (XmlNode n in xnl)
 2425:             {
 2426:                 dr = dt.NewRow();
 2427:                 dr[id] = n.Attributes[id].Value;
 2428:                 dr[text] = n.Attributes[text].Value;
 2429:                 dr[url] = n.Attributes[url].Value;
 2430:                 dt.Rows.Add(dr);
 2431:             }
 2432:  
 2433:             return dt;
 2434:         }
 2435:  
 2436:         ////////////////////////////////////////////////////////////////////////////
 2437:  
 2438:         /// <summary>
 2439:         /// Takes an XmlNodeList with text and value, and extra initial entries, and change them into a DataTable usable for databinding with controls
 2440:         /// </summary>
 2441:         public static DataTable XmlNodeListToDataTable(XmlNodeList xnl, string value, string text, string init_value, string init_text)
 2442:         {
 2443:             DataTable dt;
 2444:             DataRow dr;
 2445:  
 2446:             dt = new DataTable();
 2447:             dt.Columns.Add(value);
 2448:             dt.Columns.Add(text);
 2449:  
 2450:             dr = dt.NewRow();
 2451:             dr[value] = init_value;
 2452:             dr[text] = init_text;
 2453:             dt.Rows.Add(dr);
 2454:  
 2455:             foreach (XmlNode n in xnl)
 2456:             {
 2457:                 dr = dt.NewRow();
 2458:                 dr[value] = n.Attributes[value].Value;
 2459:                 dr[text] = n.Attributes[text].Value;
 2460:                 dt.Rows.Add(dr);
 2461:             }
 2462:  
 2463:             return dt;
 2464:         }
 2465:  
 2466:         ////////////////////////////////////////////////////////////////////////////
 2467:  
 2468:         /// <summary>
 2469:         /// Convert XmlDocument to a tab indented simple text format suitable for simple text and emails
 2470:         /// </summary>
 2471:         /// <remarks>http://stackoverflow.com/questions/10980237/xml-to-text-convert</remarks>
 2472:         public static string XmlDocumentToTabIndentedText(XmlDocument xmlDocument)
 2473:         {
 2474:             string s, name, text;
 2475:             StringBuilder stringBuilder = new StringBuilder();
 2476:  
 2477:             stringBuilder.AppendLine("===================");
 2478:  
 2479:             name = CamelToProperCase(xmlDocument.DocumentElement.Name);
 2480:  
 2481:             stringBuilder.AppendLine(name);
 2482:  
 2483:             s = XmlDocumentToTabIndentedTextIterator(xmlDocument.DocumentElement, out text);
 2484:  
 2485:             stringBuilder.AppendLine(s);
 2486:  
 2487:             stringBuilder.AppendLine("===================");
 2488:  
 2489:             return stringBuilder.ToString();
 2490:         }
 2491:  
 2492:         private static string XmlDocumentToTabIndentedTextIterator(XmlNode xmlNode, out string text)
 2493:         {
 2494:             string s, name;
 2495:             StringBuilder stringBuilder = new StringBuilder();
 2496:  
 2497:             text = string.Empty;
 2498:  
 2499:             if (xmlNode.NodeType == XmlNodeType.Element)
 2500:             {
 2501:                 foreach (XmlNode node in xmlNode.ChildNodes)
 2502:                 {
 2503:                     s = XmlDocumentToTabIndentedTextIterator(node, out text);
 2504:  
 2505:                     name = CamelToProperCase(node.Name);
 2506:  
 2507:                     if (!string.IsNullOrEmpty(text))
 2508:                     {
 2509:                         stringBuilder.AppendLine("\t" + name + ": " + text);
 2510:                     }
 2511:                     else stringBuilder.AppendLine("\t" + name);
 2512:                 }
 2513:             }
 2514:             else if (xmlNode.NodeType == XmlNodeType.Text)
 2515:             {
 2516:                 text = xmlNode.InnerText.Trim();
 2517:             }
 2518:             else
 2519:             {
 2520:  
 2521:             }
 2522:  
 2523:             return stringBuilder.ToString().TrimEnd();
 2524:         }
 2525:  
 2526:         ////////////////////////////////////////////////////////////////////////////
 2527:         ////////////////////////////////////////////////////////////////////////////
 2528:  
 2529: #if WFA
 2530:         ////////////////////////////////////////////////////////////////////////////
 2531:  
 2532:         /// <summary>
 2533:         /// Return the MAC Address of the computer
 2534:         /// </summary>
 2535:         public static string Get_MAC_Address()
 2536:         {
 2537:             // This will support IPv4 and IPv6.
 2538:  
 2539:             string mac_address;
 2540:             System.Net.NetworkInformation.NetworkInterface[] nics;
 2541:  
 2542:             nics = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
 2543:  
 2544:             mac_address = string.Empty;
 2545:  
 2546:             foreach (System.Net.NetworkInformation.NetworkInterface adapter in nics)
 2547:             {
 2548:                 if (mac_address == string.Empty)// only return MAC Address from first card  
 2549:                 {
 2550:                     System.Net.NetworkInformation.IPInterfaceProperties properties = adapter.GetIPProperties();
 2551:  
 2552:                     mac_address = adapter.GetPhysicalAddress().ToString();
 2553:                 }
 2554:             }
 2555:  
 2556:             return mac_address;
 2557:         }
 2558: #endif
 2559:         ////////////////////////////////////////////////////////////////////////////
 2560:  
 2561:         /// <summary>
 2562:         ///
 2563:         /// </summary>
 2564:         public static bool DownloadFile(string url, string fileName)
 2565:         {
 2566:             // 
 2567:             bool b;
 2568:  
 2569:             try
 2570:             {
 2571:                 using (System.Net.WebClient webClient = new System.Net.WebClient())
 2572:                 {
 2573:                     webClient.DownloadFile(url, fileName);
 2574:                 }
 2575:  
 2576:                 b = true;
 2577:             }
 2578:             catch (Exception)
 2579:             {
 2580:                 b = false;
 2581:             }
 2582:  
 2583:             return b;
 2584:         }
 2585:  
 2586:         ////////////////////////////////////////////////////////////////////////////
 2587:  
 2588:         /// <summary>
 2589:         ///
 2590:         /// </summary>
 2591:         public static bool DownloadFile(string url, string userName, string password, string fileName)
 2592:         {
 2593:             bool b;
 2594:  
 2595:             try
 2596:             {
 2597:                 using (System.Net.WebClient webClient = new System.Net.WebClient())
 2598:                 {
 2599:                     webClient.Credentials = new System.Net.NetworkCredential(userName, password);
 2600:  
 2601:                     webClient.DownloadFile(url, fileName);
 2602:                 }
 2603:  
 2604:                 b = true;
 2605:             }
 2606:             catch (Exception)
 2607:             {
 2608:                 b = false;
 2609:             }
 2610:  
 2611:             return b;
 2612:         }
 2613:  
 2614:         ////////////////////////////////////////////////////////////////////////////
 2615:  
 2616:         /// <summary>
 2617:         /// Regular Expression Guid Match
 2618:         /// </summary>
 2619:         /// <remarks> 
 2620:         /// http://www.geekzilla.co.uk/view8AD536EF-BC0D-427F-9F15-3A1BC663848E.htm
 2621:         /// </remarks>
 2622:         public static bool IsGuid(string s)
 2623:         {
 2624:             bool b = false;
 2625:  
 2626:             if (!string.IsNullOrEmpty(s))
 2627:             {
 2628:                 Regex r = new Regex(@"^(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$");
 2629:  
 2630:                 b = r.IsMatch(s);
 2631:             }
 2632:  
 2633:             return b;
 2634:         }
 2635:  
 2636:         ////////////////////////////////////////////////////////////////////////////
 2637:  
 2638:         /// <summary>
 2639:         /// C# to convert a string to a byte array.
 2640:         /// </summary>
 2641:         private static byte[] ConvertStringToByteArray(string str)
 2642:         {
 2643:             System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
 2644:             return encoding.GetBytes(str);
 2645:  
 2646:             /*
 2647:         // C# to convert a byte array to a string.
 2648:         byte [] dBytes = ...
 2649:         string str;
 2650:         System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
 2651:         str = enc.GetString(dBytes);
 2652:              */
 2653:         }
 2654:  
 2655:         ////////////////////////////////////////////////////////////////////////////
 2656:  
 2657:         /// <summary>
 2658:         /// C# to convert an alphnumeric string to an integer
 2659:         /// </summary>
 2660:         public static int AlphanumericStringToInt(string s)
 2661:         {
 2662:             int i;
 2663:             System.Text.Encoding ascii;
 2664:             Byte[] encodedBytes;
 2665:  
 2666:             i = 0;
 2667:             ascii = System.Text.Encoding.ASCII;
 2668:             encodedBytes = ascii.GetBytes(s);
 2669:  
 2670:             foreach (Byte b in encodedBytes) i += b;
 2671:  
 2672:             return i;
 2673:         }
 2674:  
 2675:         ////////////////////////////////////////////////////////////////////////////
 2676:  
 2677:         /// <summary>
 2678:         ///
 2679:         /// </summary>
 2680:         public static int IncrementArrayListIndexOrRestart(ArrayList list, int currentIndex)
 2681:         {
 2682:             int newIndex;
 2683:  
 2684:             if (currentIndex < list.Count - 1) newIndex = ++currentIndex;
 2685:             else newIndex = 0;
 2686:  
 2687:             return newIndex;
 2688:         }
 2689:  
 2690:         ////////////////////////////////////////////////////////////////////////////
 2691:  
 2692:         /// <summary>
 2693:         ///
 2694:         /// </summary>
 2695:         public static int IncrementListIndexOrRestart<T>(List<T> list, int currentIndex)
 2696:         {
 2697:             int newIndex;
 2698:  
 2699:             if (currentIndex < list.Count - 1) newIndex = ++currentIndex;
 2700:             else newIndex = 0;
 2701:  
 2702:             return newIndex;
 2703:         }
 2704:  
 2705:         ////////////////////////////////////////////////////////////////////////////
 2706:  
 2707:         /// <summary>
 2708:         ///
 2709:         /// </summary>
 2710:         public static void IncrementIndexOrReset(int listCount, ref int currentIndex)
 2711:         {
 2712:             currentIndex = (currentIndex < listCount - 1) ? ++currentIndex : 0;
 2713:         }
 2714:  
 2715:         /*
 2716:         ////////////////////////////////////////////////////////////////////////////
 2717: 
 2718:         /// <summary>
 2719:         ///
 2720:         /// </summary>
 2721:         public static void ShuffleArrayList(ArrayList al)
 2722:         {
 2723:             for (int inx = al.Count - 1; inx > 0; --inx)
 2724:             {
 2725:                 int position = r.Next(inx);
 2726:                 object temp = al[inx];
 2727:                 al[inx] = al[position];
 2728:                 al[position] = temp;
 2729:             }
 2730:         }
 2731:          */
 2732:  
 2733:         ////////////////////////////////////////////////////////////////////////////
 2734:  
 2735:         /// <summary>
 2736:         ///
 2737:         /// </summary>
 2738:         public static ArrayList ShuffleArrayList(ArrayList inputList)
 2739:         {
 2740:             ArrayList randomList = new ArrayList();
 2741:  
 2742:             int randomIndex = 0;
 2743:  
 2744:             while (inputList.Count > 0)
 2745:             {
 2746:                 randomIndex = random.Next(0, inputList.Count); //Choose a random object in the list
 2747:                 randomList.Add(inputList[randomIndex]); //add it to the new, random list
 2748:                 inputList.RemoveAt(randomIndex); //remove to avoid duplicates
 2749:             }
 2750:  
 2751:             return randomList; //return the new random list
 2752:         }
 2753:  
 2754:         ////////////////////////////////////////////////////////////////////////////
 2755:  
 2756:         /// <summary>
 2757:         ///
 2758:         /// </summary>
 2759:         public static ArrayList IntegerListRange(ArrayList a_al)
 2760:         {
 2761:             // this will take an ArrayList of integer, remove duplicates, sort, then construct an ArrayList with ranges defined, if available. For example
 2762:             // a_al = [1,2,4,6,7,15,20,21,22,34,35,36,38]
 2763:             // b_al = [1,2,4,6,7,15,20-22,34-36,38]
 2764:  
 2765:             bool range, range_old;
 2766:             int u, v;
 2767:             int start, end;
 2768:             ArrayList b_al, c_al;
 2769:             Hashtable ht;
 2770:  
 2771:             // a_al = [1,2,4,6,7,15,20,21,22,34,35,36,38]
 2772:             // b_al = [1,2,4,6,7,15,20-22,34-36,38]
 2773:  
 2774:             start = end = 0;
 2775:  
 2776:             b_al = new ArrayList(a_al.Count + 1);
 2777:             c_al = new ArrayList(a_al.Count + 1);
 2778:  
 2779:             if (a_al.Count > 0)
 2780:             {
 2781:                 // remove duplicates
 2782:                 ht = new Hashtable(a_al.Count + 1);
 2783:  
 2784:                 foreach (int i in a_al) ht[i] = 1;
 2785:  
 2786:                 foreach (int i in ht.Keys) b_al.Add(i);
 2787:  
 2788:                 // sort
 2789:                 b_al.Sort();
 2790:  
 2791:                 if (b_al.Count > 0)
 2792:                 {
 2793:                     range = range_old = false;
 2794:                     u = (int)b_al[0];
 2795:  
 2796:                     for (int i = 1; i <= b_al.Count; i++)
 2797:                     {
 2798:                         if (i < b_al.Count) v = (int)b_al[i];
 2799:                         else v = (int)b_al[i - 1];
 2800:  
 2801:                         if (v - u == 1)
 2802:                         {
 2803:                             if (range) end = v;
 2804:                             else
 2805:                             {
 2806:                                 start = u;
 2807:                                 range = true;
 2808:                             }
 2809:                         }
 2810:                         else
 2811:                         {
 2812:                             if (range)
 2813:                             {
 2814:                                 end = u;
 2815:                                 range = false;
 2816:                             }
 2817:                             else c_al.Add(u.ToString());
 2818:                         }
 2819:  
 2820:                         if (range != range_old && range == false)
 2821:                         {
 2822:                             if (end - start == 1)
 2823:                             {
 2824:                                 c_al.Add(start.ToString());
 2825:                                 c_al.Add(end.ToString());
 2826:                             }
 2827:                             else c_al.Add(start + "-" + end);
 2828:                         }
 2829:  
 2830:                         u = v;
 2831:                         range_old = range;
 2832:                     }
 2833:                 }
 2834:                 else
 2835:                 {
 2836:  
 2837:                 }
 2838:             }
 2839:             else
 2840:             {
 2841:             }
 2842:  
 2843:             return c_al;
 2844:         }
 2845:  
 2846:         ////////////////////////////////////////////////////////////////////////////
 2847:  
 2848:         /// <summary>
 2849:         /// Generate a list of start-end count optimized to read around count number of values from passed number string list. 
 2850:         /// </summary>
 2851:         public static List<Tuple<string, string>> OptimizedStartEndRangeTupleList(List<string> list, int count)
 2852:         {
 2853:             bool done;
 2854:             int c;
 2855:             string start, end;
 2856:             Tuple<string, string> tuple;
 2857:             List<Tuple<string, string>> tupleList;
 2858:  
 2859:             tupleList = new List<Tuple<string, string>>();
 2860:  
 2861:             done = false;
 2862:             c = 0;
 2863:             start = string.Empty;
 2864:  
 2865:             if (list.Count > 0 && count > 0)
 2866:             {
 2867:                 foreach (string s in list)
 2868:                 {
 2869:                     if (c == 0)
 2870:                     {
 2871:                         start = s;
 2872:                         done = false;
 2873:                     }
 2874:  
 2875:                     if (c == count - 1)
 2876:                     {
 2877:                         end = s;
 2878:  
 2879:                         tuple = new Tuple<string, string>(start, end);
 2880:  
 2881:                         tupleList.Add(tuple);
 2882:  
 2883:                         done = true;
 2884:                         c = 0;
 2885:                     }
 2886:                     else c++;
 2887:                 }
 2888:  
 2889:                 if (!done)
 2890:                 {
 2891:                     end = list[list.Count - 1];
 2892:  
 2893:                     tuple = new Tuple<string, string>(start, end);
 2894:  
 2895:                     tupleList.Add(tuple);
 2896:                 }
 2897:             }
 2898:             else throw new System.ArgumentException("List empty or range too short. ");
 2899:  
 2900:             tupleList.Sort();
 2901:  
 2902:             return tupleList;
 2903:         }
 2904:  
 2905:         ////////////////////////////////////////////////////////////////////////////
 2906:  
 2907:         /// <summary>
 2908:         /// Generate a list of start-end count optimized to read around count number of values from passed list. 
 2909:         /// </summary>
 2910:         public static List<Tuple<int, int>> OptimizedStartEndRangeTupleList(List<int> list, int count)
 2911:         {
 2912:             bool done;
 2913:             int c, start, end;
 2914:             Tuple<int, int> tuple;
 2915:             List<Tuple<int, int>> tupleList;
 2916:  
 2917:             tupleList = new List<Tuple<int, int>>();
 2918:  
 2919:             done = false;
 2920:             c = start = 0;
 2921:  
 2922:             if (list.Count > 0 && count > 0)
 2923:             {
 2924:                 foreach (int i in list)
 2925:                 {
 2926:                     if (c == 0)
 2927:                     {
 2928:                         start = i;
 2929:                         done = false;
 2930:                     }
 2931:  
 2932:                     if (c == count - 1)
 2933:                     {
 2934:                         end = i;
 2935:  
 2936:                         tuple = new Tuple<int, int>(start, end);
 2937:  
 2938:                         tupleList.Add(tuple);
 2939:  
 2940:                         done = true;
 2941:                         c = 0;
 2942:                     }
 2943:                     else c++;
 2944:                 }
 2945:  
 2946:                 if (!done)
 2947:                 {
 2948:                     end = list[list.Count - 1];
 2949:  
 2950:                     tuple = new Tuple<int, int>(start, end);
 2951:  
 2952:                     tupleList.Add(tuple);
 2953:                 }
 2954:             }
 2955:             else throw new System.ArgumentException("List empty or range too short. ");
 2956:  
 2957:             tupleList.Sort();
 2958:  
 2959:             return tupleList;
 2960:         }
 2961:  
 2962:         ////////////////////////////////////////////////////////////////////////////
 2963:  
 2964:         /// <summary>
 2965:         /// Generate a OptimizedStartEndRangeList() but with a range buffer before start and after end.
 2966:         /// </summary>
 2967:         public static List<Tuple<int, int>> OptimizedStartEndRangeBufferedList(List<int> list, int count, int bufferRange)
 2968:         {
 2969:             int start, end;
 2970:             List<Tuple<int, int>> tupleList;
 2971:  
 2972:             tupleList = OptimizedStartEndRangeTupleList(list, count);
 2973:  
 2974:             if (tupleList != null && tupleList.Count > 0)
 2975:             {
 2976:                 start = tupleList[0].Item1;
 2977:                 end = tupleList[tupleList.Count - 1].Item1;
 2978:  
 2979:                 tupleList.Insert(0, new Tuple<int, int>(start - bufferRange, start - 1));
 2980:                 tupleList.Insert(tupleList.Count - 1, new Tuple<int, int>(end + 1, end + bufferRange + 1));
 2981:             }
 2982:  
 2983:             tupleList.Sort();
 2984:  
 2985:             return tupleList;
 2986:         }
 2987:  
 2988:         ////////////////////////////////////////////////////////////////////////////
 2989:  
 2990:         /// <summary>
 2991:         /// Generate a list of start, end points of all integer ranges with given start, end and range. End included in range.
 2992:         /// </summary>
 2993:         public static List<Tuple<int, int>> StartEndRangeList(int start, int end, int range)
 2994:         {
 2995:             Tuple<int, int> tuple;
 2996:             List<Tuple<int, int>> tupleList;
 2997:  
 2998:             tupleList = new List<Tuple<int, int>>();
 2999:  
 3000:             if (end > start && range < (end - start))
 3001:             {
 3002:                 for (int i = start; i <= end; i += range)
 3003:                 {
 3004:                     if (i + range - 1 < end) tuple = new Tuple<int, int>(i, i + range - 1);
 3005:                     else tuple = new Tuple<int, int>(i, end);
 3006:  
 3007:                     tupleList.Add(tuple);
 3008:                 }
 3009:             }
 3010:             else throw new System.ArgumentException("Start, end, or range is/are invalid. ");
 3011:  
 3012:             tupleList.Sort();
 3013:  
 3014:             return tupleList;
 3015:         }
 3016:  
 3017:         ////////////////////////////////////////////////////////////////////////////
 3018:  
 3019:         /// <summary>
 3020:         /// Generate a StartEndRangeList() but with a range buffer before start and after end.
 3021:         /// </summary>
 3022:         public static List<Tuple<int, int>> StartEndRangeBufferedList(int start, int end, int range, int bufferRange)
 3023:         {
 3024:             List<Tuple<int, int>> tupleList;
 3025:  
 3026:             tupleList = StartEndRangeList(start, end, range);
 3027:  
 3028:             if (tupleList != null && tupleList.Count > 0)
 3029:             {
 3030:                 tupleList.Insert(0, new Tuple<int, int>(start - bufferRange, start - 1));
 3031:                 tupleList.Insert(tupleList.Count - 1, new Tuple<int, int>(end + 1, end + bufferRange + 1));
 3032:             }
 3033:  
 3034:             tupleList.Sort();
 3035:  
 3036:             return tupleList;
 3037:         }
 3038:  
 3039:         ////////////////////////////////////////////////////////////////////////////
 3040:  
 3041:         /// <summary>
 3042:         ///
 3043:         /// </summary>
 3044:         public static List<int> ConvertHyphenAndCommaSeperatedNumberStringToNumberList(string listStringAbbriviation)
 3045:         {
 3046:             int j, start, end;
 3047:             string abbriviation, u;
 3048:             List<int> list;
 3049:             MatchCollection matchCollection;
 3050:  
 3051:             // change number range (e.g. "1-5") to comma seperated numbers like "1,2,3,4,5"
 3052:  
 3053:             list = new List<int>();
 3054:  
 3055:             abbriviation = listStringAbbriviation;
 3056:  
 3057:             matchCollection = Regex.Matches(abbriviation, @"(\d{1,4})\-(\d{1,4})");
 3058:  
 3059:             foreach (Match match in matchCollection)
 3060:             {
 3061:                 start = int.Parse(match.Groups[1].Value);
 3062:                 end = int.Parse(match.Groups[2].Value);
 3063:  
 3064:                 u = "";
 3065:                 for (int i = start; i <= end; i++) u += i + ",";
 3066:  
 3067:                 u = u.TrimEnd(',');
 3068:  
 3069:                 // remove the matched string from the main string
 3070:                 abbriviation = abbriviation.Replace(match.Groups[0].Value, u);
 3071:             }
 3072:  
 3073:             foreach (string s in abbriviation.Split(','))
 3074:             {
 3075:                 if (int.TryParse(s, out j)) list.Add(j);
 3076:             }
 3077:  
 3078:             return list;
 3079:         }
 3080:  
 3081:         ////////////////////////////////////////////////////////////////////////////
 3082:         ////////////////////////////////////////////////////////////////////////////
 3083:  
 3084:         /// <summary>
 3085:         ///
 3086:         /// <see href="https://stackoverflow.com/questions/7688881/convert-list-to-number-range-string"/>
 3087:         /// </summary>
 3088:         public static string ConvertNumberListToHyphenAndCommaSeperatedNumberString(List<int> numberList)
 3089:         {
 3090:             return NumberListToPossiblyDegenerateRanges(numberList).Select(r => PrettyRange(r)).Intersperse(",");
 3091:         }
 3092:  
 3093:         private static IEnumerable<Tuple<int, int>> NumberListToPossiblyDegenerateRanges(IEnumerable<int> numList)
 3094:         {
 3095:             Tuple<int, int> currentRange = null;
 3096:  
 3097:             foreach (var num in numList)
 3098:             {
 3099:                 if (currentRange == null)
 3100:                 {
 3101:                     currentRange = Tuple.Create(num, num);
 3102:                 }
 3103:                 else if (currentRange.Item2 == num - 1)
 3104:                 {
 3105:                     currentRange = Tuple.Create(currentRange.Item1, num);
 3106:                 }
 3107:                 else
 3108:                 {
 3109:                     yield return currentRange;
 3110:                     currentRange = Tuple.Create(num, num);
 3111:                 }
 3112:             }
 3113:             if (currentRange != null)
 3114:             {
 3115:                 yield return currentRange;
 3116:             }
 3117:         }
 3118:  
 3119:         private static string PrettyRange(Tuple<int, int> range)
 3120:         {
 3121:             if (range.Item1 == range.Item2)
 3122:             {
 3123:                 return range.Item1.ToString();
 3124:             }
 3125:             return string.Format("{0}-{1}", range.Item1, range.Item2);
 3126:         }
 3127:  
 3128:         private static string Intersperse(this IEnumerable<string> items, string interspersand)
 3129:         {
 3130:             var currentInterspersand = "";
 3131:             var result = new StringBuilder();
 3132:  
 3133:             foreach (var item in items)
 3134:             {
 3135:                 result.Append(currentInterspersand);
 3136:                 result.Append(item);
 3137:                 currentInterspersand = interspersand;
 3138:             }
 3139:  
 3140:             return result.ToString();
 3141:         }
 3142:  
 3143:         ////////////////////////////////////////////////////////////////////////////
 3144:         ////////////////////////////////////////////////////////////////////////////
 3145:  
 3146:         /// <summary>
 3147:         ///
 3148:         /// </summary>
 3149:         public static string NumberListToCommaSeperatedNumberString(List<int> numberList)
 3150:         {
 3151:             string s;
 3152:  
 3153:             if (numberList != null && numberList.Count > 0)
 3154:             {
 3155:                 s = string.Join(",", numberList.Select(n => n.ToString()).ToArray());
 3156:             }
 3157:             else s = null;
 3158:  
 3159:             return s;
 3160:         }
 3161:  
 3162:         ////////////////////////////////////////////////////////////////////////////
 3163:  
 3164:         /// <summary>
 3165:         ///
 3166:         /// </summary>
 3167:         public static List<int> CommaSeperatedNumberStringToNumberList(string numberListString)
 3168:         {
 3169:             List<int> numberList;
 3170:  
 3171:             if (!string.IsNullOrEmpty(numberListString))
 3172:             {
 3173:                 numberList = (numberListString != string.Empty) ? numberListString.Split(',').Select(Int32.Parse).ToList() : null;
 3174:                 numberList.Sort();
 3175:             }
 3176:             else numberList = new List<int>();
 3177:  
 3178:             return numberList;
 3179:         }
 3180:  
 3181:         /*
 3182:         ////////////////////////////////////////////////////////////////////////////
 3183: 
 3184:         /// <summary>
 3185:         /// Generate HTML table from list of generic class with specified properties
 3186:         /// <see href="http://stackoverflow.com/questions/11126137/generate-html-table-from-list-of-generic-class-with-specified-properties"/>
 3187:         /// </summary>
 3188:         public static string GenerateHtmlTableFromListOfGenericClass<T>(IEnumerable<T> list, List<string> columnNameList, params Func<T, object>[] fxns)
 3189:         {
 3190:             StringBuilder sb;
 3191: 
 3192:             sb = new StringBuilder();
 3193: 
 3194:             sb.Append("<table>\n");
 3195: 
 3196:             // column names
 3197:             if (columnNameList.Count > 0)
 3198:             {
 3199:                 sb.Append("<tr>");
 3200: 
 3201:                 foreach (string column in columnNameList)
 3202:                 {
 3203:                     sb.Append("<td>");
 3204:                     sb.Append(column);
 3205:                     sb.Append("</td>");
 3206:                 }
 3207: 
 3208:                 sb.Append("</tr>\n");
 3209:             }
 3210: 
 3211:             foreach (var item in list)
 3212:             {
 3213:                 sb.Append("<tr>");
 3214: 
 3215:                 foreach (var fxn in fxns)
 3216:                 {
 3217:                     sb.Append("<td>");
 3218:                     sb.Append(fxn(item));
 3219:                     sb.Append("</td>");
 3220:                 }
 3221: 
 3222:                 sb.Append("</tr>\n");
 3223:             }
 3224: 
 3225:             sb.Append("</table>");
 3226: 
 3227:             return sb.ToString();
 3228:         }
 3229:          */
 3230:  
 3231:         ////////////////////////////////////////////////////////////////////////////
 3232:  
 3233:         /// <summary>
 3234:         /// 
 3235:         /// <see href="http://stackoverflow.com/questions/564366/convert-generic-list-enumerable-to-datatable"/>
 3236:         /// </summary>
 3237:         public static DataTable GenerateDataTableFromGenericClassList<T>(this IList<T> data)
 3238:         {
 3239:             DataTable dataTable;
 3240:             PropertyDescriptor propertyDescriptor;
 3241:             PropertyDescriptorCollection propertyDescriptorCollection;
 3242:  
 3243:             dataTable = new DataTable();
 3244:  
 3245:             dataTable.TableName = TypeDescriptor.GetClassName(typeof(T));
 3246:             propertyDescriptorCollection = TypeDescriptor.GetProperties(typeof(T));
 3247:             object[] values = new object[propertyDescriptorCollection.Count];
 3248:  
 3249:             for (int i = 0; i < propertyDescriptorCollection.Count; i++)
 3250:             {
 3251:                 propertyDescriptor = propertyDescriptorCollection[i];
 3252:  
 3253:                 dataTable.Columns.Add(propertyDescriptor.Name, propertyDescriptor.PropertyType);
 3254:             }
 3255:  
 3256:             foreach (T item in data)
 3257:             {
 3258:                 for (int i = 0; i < values.Length; i++) values[i] = propertyDescriptorCollection[i].GetValue(item);
 3259:  
 3260:                 dataTable.Rows.Add(values);
 3261:             }
 3262:  
 3263:             return dataTable;
 3264:         }
 3265:  
 3266:         ////////////////////////////////////////////////////////////////////////////
 3267:  
 3268:         /// <summary>
 3269:         /// 
 3270:         /// <see href="http://stackoverflow.com/questions/564366/convert-generic-list-enumerable-to-datatable"/>
 3271:         /// </summary>
 3272:         public static DataTable GenerateDataTableFromGenericClassList<T>(this IList<T> data, string dataTableName)
 3273:         {
 3274:             // I need to pass the dataTableName here to make the code consitant
 3275:             DataTable dataTable;
 3276:             PropertyDescriptor propertyDescriptor;
 3277:             PropertyDescriptorCollection propertyDescriptorCollection;
 3278:             Type type;
 3279:             PropertyInfo propertyInfo;
 3280:             object value;
 3281:             List<int> itemToBeRemoved;
 3282:  
 3283:             dataTable = new DataTable();
 3284:             itemToBeRemoved = new List<int>();
 3285:  
 3286:             dataTable.TableName = dataTableName; //TypeDescriptor.GetClassName(typeof(T));
 3287:  
 3288:             propertyDescriptorCollection = TypeDescriptor.GetProperties(typeof(T));
 3289:             List<object> valueList = new List<object>(propertyDescriptorCollection.Count);
 3290:  
 3291:             for (int i = 0; i < propertyDescriptorCollection.Count; i++)
 3292:             {
 3293:                 propertyDescriptor = propertyDescriptorCollection[i];
 3294:  
 3295:                 // Important: to handle complicated EF variables that represent foreign keys. I will check the property's full name, if it starts with "Ia."
 3296:                 // then this is a complicated foreign key variables that most likely points to an Id in another table, and I will attach a "_Id" suffix to the name and pass it into the DataTable.
 3297:                 // Also we will get the property type of this "Id" by checking the 0 index property of the parent property (since Id is almost always the first element)
 3298:  
 3299:                 if (propertyDescriptor.PropertyType.FullName.StartsWith("Ia."))
 3300:                 {
 3301:                     dataTable.Columns.Add(propertyDescriptor.Name + "_Id", TypeDescriptor.GetProperties(propertyDescriptor.PropertyType)[0].PropertyType);
 3302:                 }
 3303:                 else if (propertyDescriptor.PropertyType.FullName.StartsWith("System.Collections.Generic.ICollection"))
 3304:                 {
 3305:                     // this is a virtual property to to be converted to anything in table
 3306:                     itemToBeRemoved.Add(i);
 3307:                 }
 3308:                 else dataTable.Columns.Add(propertyDescriptor.Name, propertyDescriptor.PropertyType);
 3309:             }
 3310:  
 3311:             foreach (T d in data)
 3312:             {
 3313:                 valueList.Clear();
 3314:  
 3315:                 for (int i = 0; i < propertyDescriptorCollection.Count; i++)
 3316:                 {
 3317:                     if (!itemToBeRemoved.Contains(i))
 3318:                     {
 3319:                         propertyDescriptor = propertyDescriptorCollection[i];
 3320:  
 3321:                         // below: same as above we will check to see if property full name starts with "Ia." and assign the appropriate value accordingly
 3322:  
 3323:                         if (propertyDescriptor.PropertyType.FullName.StartsWith("Ia."))
 3324:                         {
 3325:                             type = d.GetType();
 3326:  
 3327:                             propertyInfo = type.GetProperty("Designation");
 3328:  
 3329:                             if (propertyInfo != null)
 3330:                             {
 3331:                                 value = propertyInfo.GetValue(d);
 3332:  
 3333:                                 valueList.Add(value.GetType().GetProperty("Id").GetValue(value));
 3334:                             }
 3335:                             else
 3336:                             {
 3337:                                 valueList.Add(null);
 3338:                             }
 3339:                         }
 3340:                         else valueList.Add(propertyDescriptor.GetValue(d));
 3341:                     }
 3342:                 }
 3343:  
 3344:                 dataTable.Rows.Add(valueList.ToArray());
 3345:             }
 3346:  
 3347:             return dataTable;
 3348:         }
 3349:  
 3350:         ////////////////////////////////////////////////////////////////////////////
 3351:  
 3352:         /// <summary>
 3353:         /// 
 3354:         /// <see href="http://stackoverflow.com/questions/564366/convert-generic-list-enumerable-to-datatable"/>
 3355:         /// </summary>
 3356:         public static string GenerateTextFromListOfGenericClass<T>(this IList<T> data, string propertyName)
 3357:         {
 3358:             StringBuilder sb;
 3359:             PropertyDescriptor prop;
 3360:             PropertyDescriptorCollection props;
 3361:  
 3362:             sb = new StringBuilder();
 3363:             props = TypeDescriptor.GetProperties(typeof(T));
 3364:  
 3365:             object value;
 3366:  
 3367:             value = new object();
 3368:  
 3369:             foreach (T item in data)
 3370:             {
 3371:                 for (int i = 0; i < props.Count; i++)
 3372:                 {
 3373:                     prop = props[i];
 3374:  
 3375:                     if (propertyName == prop.Name) value = props[i].GetValue(item);
 3376:                 }
 3377:  
 3378:                 sb.AppendLine(value.ToString());
 3379:             }
 3380:  
 3381:             return sb.ToString();
 3382:         }
 3383:  
 3384:         ////////////////////////////////////////////////////////////////////////////
 3385:  
 3386:         /// <summary>
 3387:         /// 
 3388:         /// <see href="http://stackoverflow.com/questions/564366/convert-generic-list-enumerable-to-datatable"/>
 3389:         /// </summary>
 3390:         public static DataTable GenerateDataTableFromListOfGenericClass<T>(this IList<T> data, params string[] propertyNameList)
 3391:         {
 3392:             int j;
 3393:             DataTable table;
 3394:             PropertyDescriptor prop;
 3395:             PropertyDescriptorCollection props;
 3396:  
 3397:             table = new DataTable();
 3398:             props = TypeDescriptor.GetProperties(typeof(T));
 3399:  
 3400:             for (int i = 0; i < props.Count; i++)
 3401:             {
 3402:                 prop = props[i];
 3403:  
 3404:                 if (propertyNameList.Contains(prop.Name))
 3405:                 {
 3406:                     table.Columns.Add(prop.Name, prop.PropertyType);
 3407:                 }
 3408:             }
 3409:  
 3410:             object[] values = new object[propertyNameList.Length];
 3411:  
 3412:             foreach (T item in data)
 3413:             {
 3414:                 j = 0;
 3415:  
 3416:                 for (int i = 0; i < props.Count; i++)
 3417:                 {
 3418:                     prop = props[i];
 3419:  
 3420:                     if (propertyNameList.Contains(prop.Name))
 3421:                     {
 3422:                         values[j++] = props[i].GetValue(item);
 3423:                     }
 3424:                 }
 3425:  
 3426:                 table.Rows.Add(values);
 3427:             }
 3428:  
 3429:             return table;
 3430:         }
 3431:  
 3432:         ////////////////////////////////////////////////////////////////////////////
 3433:  
 3434:         /// <summary>
 3435:         /// Generate HTML table from DataTable
 3436:         /// <see href="http://stackoverflow.com/questions/19682996/datatable-to-html-table"/>
 3437:         /// </summary>
 3438:         public static string GenerateHtmlTableFromDataTable(DataTable dataTable)
 3439:         {
 3440:             StringBuilder sb;
 3441:  
 3442:             sb = new StringBuilder();
 3443:  
 3444:             sb.Append("<table>\n");
 3445:  
 3446:             // header
 3447:             sb.Append("<tr>");
 3448:  
 3449:             for (int i = 0; i < dataTable.Columns.Count; i++) sb.Append("<td>" + dataTable.Columns[i].ColumnName + "</td>");
 3450:  
 3451:             sb.Append("</tr>");
 3452:  
 3453:             // row
 3454:             for (int i = 0; i < dataTable.Rows.Count; i++)
 3455:             {
 3456:                 sb.Append("<tr>");
 3457:  
 3458:                 for (int j = 0; j < dataTable.Columns.Count; j++) sb.Append("<td>" + dataTable.Rows[i][j].ToString() + "</td>");
 3459:  
 3460:                 sb.Append("</tr>");
 3461:             }
 3462:  
 3463:             sb.Append("</table>");
 3464:  
 3465:             return sb.ToString();
 3466:         }
 3467:  
 3468:         ////////////////////////////////////////////////////////////////////////////
 3469:  
 3470:         /// <summary>
 3471:         /// Generate tab separated text format from DataTable
 3472:         /// </summary>
 3473:         public static string GenerateTabSeparatedTextFromDataTable(DataTable dataTable)
 3474:         {
 3475:             StringBuilder sb;
 3476:  
 3477:             sb = new StringBuilder();
 3478:  
 3479:             for (int i = 0; i < dataTable.Columns.Count; i++) sb.Append(dataTable.Columns[i].ColumnName + "\t");
 3480:  
 3481:             sb.Append("\r\n");
 3482:  
 3483:             // row
 3484:             for (int i = 0; i < dataTable.Rows.Count; i++)
 3485:             {
 3486:                 for (int j = 0; j < dataTable.Columns.Count; j++) sb.Append(dataTable.Rows[i][j].ToString() + "\t");
 3487:  
 3488:                 sb.Append("\r\n");
 3489:             }
 3490:  
 3491:             return sb.ToString();
 3492:         }
 3493:  
 3494:         ////////////////////////////////////////////////////////////////////////////
 3495:  
 3496:         /// <summary>
 3497:         /// Generate DataTable from delimited text
 3498:         /// </summary>
 3499:         public static DataTable GenerateDataTableFromTabDelimitedText(string text, out Result result)
 3500:         {
 3501:             // this will only read rows that have at least 5 distinct columns
 3502:             bool first;
 3503:             string line;
 3504:             string[] columnList, lineSplit;
 3505:             DataTable dataTable;
 3506:  
 3507:             first = true;
 3508:             dataTable = new DataTable();
 3509:             result = new Result();
 3510:  
 3511:             using (StringReader reader = new StringReader(text))
 3512:             {
 3513:                 while ((line = reader.ReadLine()) != null)
 3514:                 {
 3515:                     // lineSplit = line.Split((string[])null, StringSplitOptions.None);
 3516:                     lineSplit = Regex.Split(line, @"\t", RegexOptions.None);
 3517:  
 3518:                     if (IsTableHeaderText(line) && first)
 3519:                     {
 3520:                         columnList = lineSplit;
 3521:  
 3522:                         foreach (var column in columnList) dataTable.Columns.Add(column);
 3523:  
 3524:                         first = false;
 3525:                     }
 3526:                     else if (!first)
 3527:                     {
 3528:                         if (lineSplit.Length == dataTable.Columns.Count)
 3529:                         {
 3530:                             dataTable.Rows.Add(lineSplit);
 3531:                         }
 3532:                         else
 3533:                         {
 3534:                             result.AddWarning("lineSplit.Length != dataTable.Columns.Count for line: " + line);
 3535:                         }
 3536:                     }
 3537:                     else
 3538:                     {
 3539:                     }
 3540:                 }
 3541:             }
 3542:  
 3543:             return dataTable;
 3544:         }
 3545:  
 3546:         ////////////////////////////////////////////////////////////////////////////
 3547:  
 3548:         /// <summary>
 3549:         /// Examine a string to see if it resembles a table header string
 3550:         /// <see href="https://stackoverflow.com/questions/17812566/count-words-and-spaces-in-string-c-sharp"/>
 3551:         /// </summary>
 3552:         private static bool IsTableHeaderText(string text)
 3553:         {
 3554:             bool isHeader;
 3555:             int whitespaceCount, wordCount;
 3556:             Regex regex = new Regex(@"^[a-zA-Z\s]+$");
 3557:  
 3558:             if (!string.IsNullOrEmpty(text))
 3559:             {
 3560:                 text = text.Replace("\t", "     "); // we will replace every tab with 5 spaces
 3561:  
 3562:                 whitespaceCount = text.Count(Char.IsWhiteSpace);
 3563:                 wordCount = CountWordsInText(text);
 3564:  
 3565:                 // if the whitespace count is bigger than word count is probably a header text string
 3566:                 if (whitespaceCount > 2 * wordCount)
 3567:                 {
 3568:                     if (regex.IsMatch(text)) isHeader = true;
 3569:                     else isHeader = false;
 3570:                 }
 3571:                 else isHeader = false;
 3572:             }
 3573:             else
 3574:             {
 3575:                 isHeader = false;
 3576:             }
 3577:  
 3578:             return isHeader;
 3579:         }
 3580:  
 3581:         ////////////////////////////////////////////////////////////////////////////
 3582:  
 3583:         /// <summary>
 3584:         ///
 3585:         /// </summary>
 3586:         private static int CountWordsInText(string text)
 3587:         {
 3588:             int wordCount = 0, index = 0;
 3589:  
 3590:             while (index < text.Length)
 3591:             {
 3592:                 // check if current char is part of a word
 3593:                 while (index < text.Length && !char.IsWhiteSpace(text[index])) index++;
 3594:  
 3595:                 wordCount++;
 3596:  
 3597:                 // skip whitespace until next word
 3598:                 while (index < text.Length && char.IsWhiteSpace(text[index])) index++;
 3599:             }
 3600:  
 3601:             return wordCount;
 3602:         }
 3603:  
 3604:         ////////////////////////////////////////////////////////////////////////////
 3605:  
 3606:         /// <summary>
 3607:         /// Generate tab separated text format from Dictionary
 3608:         /// </summary>
 3609:         public static string GenerateTabSeparatedTextFromDictionary(Dictionary<string, int> dictionary)
 3610:         {
 3611:             StringBuilder sb;
 3612:  
 3613:             sb = new StringBuilder();
 3614:  
 3615:             foreach (KeyValuePair<string, int> kvp in dictionary)
 3616:             {
 3617:                 sb.AppendLine(kvp.Key + "\t" + kvp.Value);
 3618:             }
 3619:  
 3620:             return sb.ToString();
 3621:         }
 3622:  
 3623:         ////////////////////////////////////////////////////////////////////////////
 3624:  
 3625:         /// <summary>
 3626:         /// Generate tab separated text format from SortedDictionary
 3627:         /// </summary>
 3628:         public static string GenerateTabSeparatedTextFromDictionary(SortedDictionary<string, int> sortedDictionary)
 3629:         {
 3630:             StringBuilder sb;
 3631:  
 3632:             sb = new StringBuilder();
 3633:  
 3634:             foreach (KeyValuePair<string, int> kvp in sortedDictionary)
 3635:             {
 3636:                 sb.AppendLine(kvp.Key + "\t" + kvp.Value);
 3637:             }
 3638:  
 3639:             return sb.ToString();
 3640:         }
 3641:  
 3642:         ////////////////////////////////////////////////////////////////////////////
 3643:  
 3644:         /// <summary>
 3645:         /// Generate simple two column name value table from DataTable
 3646:         /// </summary>
 3647:         public static string GenerateTwoColumnNameValueTextFromDataTable(DataTable dataTable)
 3648:         {
 3649:             StringBuilder sb;
 3650:  
 3651:             sb = new StringBuilder();
 3652:  
 3653:             for (int i = 0; i < dataTable.Rows.Count; i++)
 3654:             {
 3655:                 for (int j = 0; j < dataTable.Columns.Count; j++)
 3656:                 {
 3657:                     sb.Append(dataTable.Columns[j].ColumnName + ":\t" + dataTable.Rows[i][j].ToString() + "\r\n");
 3658:                 }
 3659:  
 3660:                 sb.Append("\r\n");
 3661:             }
 3662:  
 3663:             return sb.ToString().Trim();
 3664:         }
 3665:  
 3666:         ////////////////////////////////////////////////////////////////////////////
 3667:  
 3668:         /// <summary>
 3669:         /// Remove non-numeric characters
 3670:         /// http://stackoverflow.com/questions/3977497/stripping-out-non-numeric-characters-in-string
 3671:         /// </summary>
 3672:         public static string RemoveNonNumericCharacters(string line)
 3673:         {
 3674:             string s;
 3675:  
 3676:             if (line != null && line.Length != 0)
 3677:             {
 3678:                 s = new string(line.Where(c => char.IsDigit(c)).ToArray());
 3679:                 //s = Regex.Replace(line, "[^0-9]", "");
 3680:             }
 3681:             else s = line;
 3682:  
 3683:             return s;
 3684:         }
 3685:  
 3686:         ////////////////////////////////////////////////////////////////////////////
 3687:  
 3688:         /// <summary>
 3689:         /// 
 3690:         /// <remarks>http://stackoverflow.com/questions/2475795/check-for-missing-number-in-sequence</remarks>
 3691:         /// </summary>
 3692:         public static List<int> ExcludedNumberListFromNumberListWithinRange(List<int> list, int listSize)
 3693:         {
 3694:             // Check for missing number in sequence
 3695:             List<int> exclusionList;
 3696:  
 3697:             exclusionList = Enumerable.Range(1, listSize).Except(list).ToList();
 3698:  
 3699:             return exclusionList;
 3700:         }
 3701:  
 3702:         ////////////////////////////////////////////////////////////////////////////
 3703:  
 3704:         /// <summary>
 3705:         /// 
 3706:         /// </summary>
 3707:         public static string AutoGeneratedXmlTextStartCommentString
 3708:         {
 3709:             get
 3710:             {
 3711:                 string s;
 3712:  
 3713:                 s = @"<!-- auto generated: start -->";
 3714:  
 3715:                 return s;
 3716:             }
 3717:         }
 3718:  
 3719:         ////////////////////////////////////////////////////////////////////////////
 3720:  
 3721:         /// <summary>
 3722:         /// 
 3723:         /// </summary>
 3724:         public static string AutoGeneratedXmlTextEndCommentString
 3725:         {
 3726:             get
 3727:             {
 3728:                 string s;
 3729:  
 3730:                 s = @"<!-- auto-generated: end -->";
 3731:  
 3732:                 return s;
 3733:             }
 3734:         }
 3735:  
 3736:         ////////////////////////////////////////////////////////////////////////////
 3737:  
 3738:         /// <summary>
 3739:         /// 
 3740:         /// </summary>
 3741:         public static string AutoGeneratedCSharpCodeStartCommentString
 3742:         {
 3743:             get
 3744:             {
 3745:                 string s;
 3746:  
 3747:                 s = @"// auto generated: start";
 3748:  
 3749:                 return s;
 3750:             }
 3751:         }
 3752:  
 3753:         ////////////////////////////////////////////////////////////////////////////
 3754:  
 3755:         /// <summary>
 3756:         /// 
 3757:         /// </summary>
 3758:         public static string AutoGeneratedCSharpCodeEndCommentString
 3759:         {
 3760:             get
 3761:             {
 3762:                 string s;
 3763:  
 3764:                 s = @"// auto-generated: end";
 3765:  
 3766:                 return s;
 3767:             }
 3768:         }
 3769:  
 3770:         ////////////////////////////////////////////////////////////////////////////
 3771:  
 3772:         /// <summary>
 3773:         /// 
 3774:         /// </summary>
 3775:         public static int LevenshteinDistance(string s, string t)
 3776:         {
 3777:             if (string.IsNullOrEmpty(s))
 3778:             {
 3779:                 if (string.IsNullOrEmpty(t)) return 0;
 3780:  
 3781:                 return t.Length;
 3782:             }
 3783:  
 3784:             if (string.IsNullOrEmpty(t))
 3785:             {
 3786:                 return s.Length;
 3787:             }
 3788:  
 3789:             int n = s.Length;
 3790:             int m = t.Length;
 3791:             int[,] d = new int[n + 1, m + 1];
 3792:  
 3793:             // initialize the top and right of the table to 0, 1, 2, ...
 3794:             for (int i = 0; i <= n; d[i, 0] = i++) ;
 3795:             for (int j = 1; j <= m; d[0, j] = j++) ;
 3796:  
 3797:             for (int i = 1; i <= n; i++)
 3798:             {
 3799:                 for (int j = 1; j <= m; j++)
 3800:                 {
 3801:                     int cost = (t[j - 1] == s[i - 1]) ? 0 : 1;
 3802:                     int min1 = d[i - 1, j] + 1;
 3803:                     int min2 = d[i, j - 1] + 1;
 3804:                     int min3 = d[i - 1, j - 1] + cost;
 3805:                     d[i, j] = Math.Min(Math.Min(min1, min2), min3);
 3806:                 }
 3807:             }
 3808:  
 3809:             return d[n, m];
 3810:         }
 3811:  
 3812:         ////////////////////////////////////////////////////////////////////////////
 3813:         ////////////////////////////////////////////////////////////////////////////
 3814:     }
 3815:  
 3816: #if WFA
 3817:     ////////////////////////////////////////////////////////////////////////////
 3818:  
 3819:     /// <summary>
 3820:     ///
 3821:     /// </summary>
 3822:     public class ListItem
 3823:     {
 3824:         // I created this because Windows Forms does not have the equivalent like System.Web ListItem
 3825:  
 3826:         private string name;
 3827:         private string value;
 3828:  
 3829:         ////////////////////////////////////////////////////////////////////////////
 3830:  
 3831:         /// <summary>
 3832:         ///
 3833:         /// </summary>
 3834:         public ListItem(string _name, string _value)
 3835:         {
 3836:             this.name = _name;
 3837:             this.value = _value;
 3838:         }
 3839:  
 3840:         ////////////////////////////////////////////////////////////////////////////
 3841:  
 3842:         /// <summary>
 3843:         ///
 3844:         /// </summary>
 3845:         public string Name
 3846:         {
 3847:             get
 3848:             {
 3849:                 return name;
 3850:             }
 3851:         }
 3852:  
 3853:         ////////////////////////////////////////////////////////////////////////////
 3854:  
 3855:         /// <summary>
 3856:         ///
 3857:         /// </summary>
 3858:         public string Value
 3859:         {
 3860:  
 3861:             get
 3862:             {
 3863:                 return value;
 3864:             }
 3865:         }
 3866:     }
 3867: #else
 3868: #endif
 3869:  
 3870:     ////////////////////////////////////////////////////////////////////////////
 3871:     ////////////////////////////////////////////////////////////////////////////
 3872: }