var keyCount = 0;

function checkPhoneNumber(doc) {

    //ip = myIP(); //-- extract the IP address
    $('#frmTelephone .error').removeClass('error');

    var ok  = true;
    var num = "";
       
    $.each( $('#frmTelephone input[type="text"].phone'), function(i, n) {
        num += $(n).attr('value');
    });
    //-- check if number if 10 digits
    //--alert (num);

    if (!num.match(/^\d{10}$/)) {
      ok = false;
      alert("Incomplete number entered, please try again");
      $('#frmTelephone input.phone').addClass('error');
      clearForm();
      return false;
    }
        
    $('#thePhone').attr('value', new String(num));
    
    if (num.length == 0) {
      alert("No number entered, please try again");
      clearForm();
      return false;
	}
    else if (!checknumber(num)) {
       alert("Invalid number entered, please try again");
       clearForm();
       return false;
    }
    else if (num.length != 10) {
      alert("Incomplete number entered, please try again");
      clearForm();
      return false;
    }
    
    return ok;
}


function agentTest() 
{  
   var deviceAgent = navigator.userAgent.toLowerCase();  
   var agentID = deviceAgent.match(/(iphone|ipod|ipad)/);            
   
   if (agentID) {
      //alert("I am a smart phone");
	  window.location = "http://www.sociallifenetworks.com/nationwideAPRIL/public_html/mobileApp.html";
    }
    else {
      //-- I am a PC (check the THREE main browsers)
     /* if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)){ //test for Firefox/x.x or Firefox x.x (ignoring remaining digits);
        var ffversion=new Number(RegExp.$1) // capture x.x portion and store as a number
        if (ffversion>=3)
          alert("You're using FF 3.x or above");
        else if (ffversion>=2)
          alert("You're using FF 2.x");
        else if (ffversion>=1)
          alert("You're using FF 1.x");
      }
      else if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
        var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
        if (ieversion>=8)
          alert("You're using IE8 or above");
        else if (ieversion>=7)
          alert("You're using IE7.x");
        else if (ieversion>=6)
          alert("You're using IE6.x");
        else if (ieversion>=5)
         alert("You're using IE5.x");
      }*/
    }
}

$(window).load(function () {
  clearForm();
  //agentTest();
});

function clearForm() {

    $('#frmTelephone input[name="phone1"]').attr('value','');	
    $('#frmTelephone input[name="phone2"]').attr('value',''); 
    $('#frmTelephone input[name="phone3"]').attr('value','');
    
    $('#frmTelephone input[name="phone1"]').focus();
}

function checknumber(x){
  
  var anum=/(^\d+$)|(^\d+\.\d+$)/
  if (anum.test(x))
    return true;
  else
    return false;
}


$(function() {
  $('#frmTelephone input.phone').keyup(function(e) {
   
  
   var len = $('#frmTelephone input.phone').attr('value').length;
   var max = $('#frmTelephone input.phone').attr('maxlength');
   var area_code = $('#area_code').attr('value').length;
   var ph_part1 = $('#phone_part1').attr('value').length;
   var ph_part2 = $('#phone_part2').attr('value').length;


   if (area_code == 3 && ph_part1 == 0 && ph_part2 == 0) {
     $('#area_code').next().focus();
   }
   else if (area_code == 3 && ph_part1 == 3 && ph_part2 == 0) {
     $('#phone_part1').next().focus();
   }
   else if (area_code == 3 && ph_part1 == 3 && ph_part2 == 4) {
     $('#frmTelephone input[type="submit"]').focus();
   }
  });
})


$(function() {
   $('#frmTelephone input.phone').keydown(function(event) {
      if ((!event.shiftKey && !event.ctrlKey && !event.altKey) && ((event.keyCode >= 48 && event.keyCode <= 57) || 
          (event.keyCode >= 96 && event.keyCode <= 105)) || event.keyCode == 8) // 0-9 or numpad 0-9, disallow shift, ctrl, and alt
      {

      }
      // not esc, del, left or right
      else if (event.keyCode != 8 && event.keyCode != 46 && event.keyCode != 37 && event.keyCode != 39)  {
        event.preventDefault();
      }
      // else the key should be handled normally
   });
})

