﻿function PhoneFormat(txtCtrl, eve)  // data allows 999-999-9999
{
   var Text = txtCtrl.value;
   var charset = '0123456789';
   var TempArr;
   var unicode = eve.keyCode ? eve.keyCode : eve.which ? eve.which : eve.charCode;
   //alert(unicode);

   if (unicode == Keys.TAB || unicode == 8 || unicode == 37 || unicode == 39 || unicode == 46) return true; //allow back space and delete
   
   if (Text.length >= 0)
   {
      if (charset.indexOf(String.fromCharCode(unicode), 0) < 0)
      {
         // if the key value is not in the chatset string
         if (eve.preventDefault)
         {
            eve.preventDefault();
            eve.stopPropagation();
         }
         else
         {
            eve.keyCode = 0;
            eve.returnValue = false;
         }
      }
      else if (txtCtrl.maxLength == Text.length)
      {
         //eve.returnValue = false;				
         if (eve.preventDefault)
         {
            eve.preventDefault();
            eve.stopPropagation();
         }
         else
         {
            eve.keyCode = 0;
            eve.returnValue = false;
         }
      }
      else
      {
         //Text = Text + String.fromCharCode(unicode);
         if ((Text.length == 3) || (Text.length == 7))  //999-999-9999
         {
            Text += "-";
         }
      }

   }
   txtCtrl.value = Text;
   return true;
}


function allDigits(txtCtrl, eve, charset, AdjCharset)
{
   var txtLenstr = txtCtrl.value.length;
   var txt = txtCtrl.value;
   var PrevChar = '';
   if (txtLenstr > 0) PrevChar = txt.substr(txt.length - 1);
   var unicode = eve.keyCode ? eve.keyCode : eve.which ? eve.which : eve.charCode;
   
   if (!AdjCharset) { AdjCharset = '.'; }
   if (!charset) { charset = '0123456789'; }

   if (unicode == Keys.BACKSPACE || unicode == Keys.DELETE || unicode == Keys.TAB || unicode == Keys.LEFT || unicode == Keys.RIGHT)
   {
      //alert("o -- " + unicode); 
      return true;
   }

   if (txt.length >= 0)
   {
      if (charset.indexOf(String.fromCharCode(unicode), 0) < 0)
      {
         // if the key value is not in the chatset string
         if (eve.preventDefault)
         {
            eve.preventDefault();
            eve.stopPropagation();
         }
         else
         {
            eve.keyCode = 0;
            eve.returnValue = false;
         }
      }
      else
      {
         if ((PrevChar == String.fromCharCode(unicode)) && (AdjCharset.indexOf(String.fromCharCode(unicode), 0) >= 0))
         {
            if (eve.preventDefault)
            {
               eve.preventDefault();
               eve.stopPropagation();
            }
            else
            {
               eve.keyCode = 0;
               eve.returnValue = false;
            }
         }

         // if the key value is in the chatset string, but don't want AdjCharset 
         //immediately one after one.
         if ((AdjCharset.indexOf(String.fromCharCode(unicode), 0) >= 0) && (txt.length >= 0))
            PrevChar = String.fromCharCode(unicode);
         else
         {
            PrevChar = ''; //alert("3 -- " + unicode);
         }
      }

      return true;
   }
}

var Keys = {
   BACKSPACE: 8, TAB: 9, ENTER: 13, SHIFT: 16,
   CTRL: 17, ALT: 18, PAUSE: 19, CAPS: 20,
   ESC: 27, PAGEUP: 33, PAGEDN: 34, END: 35,
   HOME: 36, LEFT: 37, UP: 38, RIGHT: 39,
   DOWN: 40, INSERT: 45, DELETE: 46,
   n0: 48, n1: 49, n2: 50, n3: 51, n4: 52,
   n5: 53, n6: 54, n7: 55, n8: 56, n9: 57,
   A: 65, B: 66, C: 67, D: 68, E: 68, F: 70, G: 71, H: 72, I: 73, J: 74, K: 75,
   L: 76, M: 77, N: 78, O: 79, P: 80, Q: 81, R: 82, S: 83, T: 84, U: 85, V: 86,
   W: 87, X: 88, Y: 89, Z: 90,
   WINLEFT: 91, WINRIGHT: 92, SELECT: 93, NUM0: 96,
   NUM1: 97, NUM2: 98, NUM3: 99, NUM4: 100,
   NUM5: 101, NUM6: 102, NUM7: 103, NUM8: 104,
   NUM9: 105, MULTIPLY: 106, ADD: 107, SUBTRACT: 109,
   DECIMAL: 110, DIVIDE: 111, F1: 112, F2: 113,
   F3: 114, F4: 115, F5: 116, F6: 117,
   F7: 118, F8: 119, F9: 120, F10: 121,
   F11: 122, F12: 123, NUMLOCK: 144, SCROLLLOCK: 145,
   SEMICOLON: 186, EQUAL: 187, COMMA: 188, DASH: 189,
   PERIOD: 190, FORWARDSLASH: 191, GRAVEACCENT: 192,
   OPENBRACKET: 219, BACKSLASH: 220, CLOSEBRACKET: 221,
   QUOTE: 222
};

function isValidPhone(strPhone) {
    var temp;
    
    strPhone = strPhone.replace(/-/g, "");
    if (strPhone.length < 10) {
        //alert("Please enter a valid phone number");
        return false;
    }

    var phoneRE = /^\(\d{3}\)\d{3}-\d{4}$/;
    var phoneExp = /[^0-9]/;
    if (phoneExp.test(strPhone)) {
        //alert("Please enter a valid phone number");
        return false;
    }
    var areacode = new Array(
	    "201", "202", "203", "204", "205", "206", "207", "208", "209", "210",
	    "212", "213", "214", "215", "216", "217", "218", "219", "224", "225",
	    "228", "229", "231", "234", "239", "240", "248", "250", "251", "252",
	    "253", "254", "256", "260", "262", "267", "269", "270", "276", "281",
	    "289", "301", "302", "303", "304", "305", "306", "307", "308", "309",
	    "310", "312", "313", "314", "315", "316", "317", "318", "319", "320",
	    "321", "323", "325", "330", "331", "334", "336", "337", "339", "340",
	    "347", "351", "352", "360", "361", "386", "401", "402", "403", "404",
	    "405", "406", "407", "408", "409", "410", "412", "413", "414", "415",
	    "416", "417", "418", "419", "423", "424", "425", "432", "434", "435",
	    "440", "443", "450", "469", "478", "479", "480", "484", "501", "502",
	    "503", "504", "505", "506", "507", "508", "509", "510", "512", "513",
	    "514", "515", "516", "517", "518", "519", "520", "530", "540", "541",
	    "551", "557", "559", "561", "562", "563", "567", "570", "571", "573",
	    "574", "580", "585", "586", "601", "602", "603", "604", "605", "606",
	    "607", "608", "609", "610", "612", "613", "614", "615", "616", "617",
	    "618", "619", "620", "623", "626", "630", "631", "636", "641", "646",
	    "647", "650", "651", "660", "661", "662", "671", "678", "682", "701",
	    "702", "703", "704", "705", "706", "707", "708", "709", "712", "713",
	    "714", "715", "716", "717", "718", "719", "720", "724", "727", "731",
	    "732", "734", "740", "754", "757", "760", "763", "765", "770", "772",
	    "773", "774", "775", "778", "780", "781", "785", "786", "787", "801",
	    "802", "803", "804", "805", "806", "807", "808", "810", "812", "813",
	    "814", "815", "816", "817", "818", "819", "828", "830", "831", "832",
	    "843", "845", "847", "848", "850", "856", "857", "858", "859", "860",
	    "862", "863", "864", "865", "867", "870", "878", "901", "902", "903",
	    "904", "905", "906", "907", "908", "909", "910", "912", "913", "914",
	    "915", "916", "917", "918", "919", "920", "925", "928", "931", "936",
	    "937", "939", "940", "941", "947", "949", "951", "952", "954", "956",
	    "970", "971", "972", "973", "978", "979", "980", "985", "989");
    var found = false;
    var userarea = strPhone.substring(0, 3);
    for (i = 0; i < areacode.length; i++) {
        if (userarea == areacode[i]) {
            found = true;
            break;
        }
    }
    
    if (!found) {
        //alert("The area code of phone number appears to be invalid. Please confirm!");		
        return false;
    }

    var phonecheck = strPhone.substring(3);
    var specialcheck = strPhone.substring(3, 6);

    if (phonecheck == "1234567" || phonecheck == "4567890" || phonecheck == "0000000" ||
	   phonecheck == "1111111" || phonecheck == "2222222" || phonecheck == "3333333" ||
	   phonecheck == "4444444" || phonecheck == "5555555" || phonecheck == "6666666" ||
	   phonecheck == "7777777" || phonecheck == "8888888" || phonecheck == "9999999" ||
	   specialcheck == "555" || specialcheck == "000" || specialcheck == "911") {

        return false;
    }
    return true;
}

function isKeyCodeNumeric(keyCode) {
    return (keyCode >= 48 && keyCode <= 57) || (keyCode >= 96 && keyCode <= 105);
}

function isKeyCodeComoon(keyCode) {
    // var KEYS = { BACKSPACE: 8, TAB: 9, RETURN: 13, ESC: 27, SPACE: 32, PAGE_UP: 33, PAGE_DOWN: 34, LEFT: 37, UP: 38, RIGHT: 39, DOWN: 40, INSERT: 45, DELETE: 46 };
    var KEYS = [8, 9, 13, 27, 33, 34, 37, 38, 39, 40, 45, 46];
    return ($.inArray(keyCode, KEYS) >= 0);
}
