﻿/*
 * onkeyup event for phone 
 * number text boxes
***********************************/
function PhoneNumberMask(textbox)
{
	var input = trim(textbox.value);
	
	//-- replace non-numerics
	var digitsOnly = input.replace(/\D/g, "");
	    textbox.value = digitsOnly;
	
	FormatPhone(textbox);
}

/*
 * Method called by PhoneNumberMask() 
 *
 ************************************/
function FormatPhone(textbox)
{
	if( cntryDialCode == 1)
	{	FormatUSPhone(textbox);
	} else {
	
		// remove country code
		if(textbox.value.length >= cntryDialCode.length)
			{textbox.value = textbox.value.slice(cntryDialCode.length)}
			
		FormatIntlPhone(textbox, cntryDialCode);
	}
}

/*
 * Formats phone (xxx) xxx-xxxx
 *
 **************************************/
function FormatUSPhone(textbox){
	
	if(textbox.value.length == 10){
		var npa = textbox.value.slice(0,3);
		var nxx = textbox.value.slice(3,6);
		var ext = textbox.value.slice(6);
		textbox.value = "(" + npa + ") " + nxx + "-" + ext;
	}
}

/*
 * Formats phone +xxx xxx xxx xxxx
 *
 **************************************/
function FormatIntlPhone(textbox, intlDialCode)
{
		var CONST_LAST_SECTION_LENGTH = 4
		var CONST_ALL_OTHER_SECTION_LENGTH = 3

		// get the value of the source text box
		var sourceString = textbox.value;

		// reverse the total string from the source string, as we will work back words
		var revString =  reverse( sourceString )

		// this is the varialbe which will store the final result (but in reverse order).
		var finalString = "";

		// to control the first 
		var firstAttempt = true;

		// Loop thru till all the chars are stripped of the reversed string
		while( revString.length != 0)
		{
			// if this is the first attempt and the length of the reversed string is atleast CONST_LAST_SECTION_LENGTH chars
			if(firstAttempt && revString.length >= CONST_LAST_SECTION_LENGTH)
			{
				// get the first four chars of the reversed string.
				finalString = revString.substring(0, CONST_LAST_SECTION_LENGTH)

				// resets the reversed string without the first char, it should have ideally removed the CONST_LAST_SECTION_LENGTH chars, 
				// because we are stripping the other (CONST_ALL_OTHER_SECTION_LENGTH) chars below, i am stripping only one char here.
				revString = revString.substring(CONST_LAST_SECTION_LENGTH - CONST_ALL_OTHER_SECTION_LENGTH, revString.length)
			}
			// if the length of the (unprocessed) reversed string has atleast CONST_ALL_OTHER_SECTION_LENGTH chars
			else if (revString.length >= CONST_ALL_OTHER_SECTION_LENGTH)
			{
				// add first three chars of the reversed string to final string
				finalString += ' ' + revString.substring(0, CONST_ALL_OTHER_SECTION_LENGTH)
			}
			// if the length is lessthan CONST_ALL_OTHER_SECTION_LENGTH
			else
			{
				// add all the chars left in reversed string to the final string
				finalString += ' ' + revString.substring(0, revString.length)
			}

			// now strip off the reversed string with first three by copying from CONST_LAST_SECTION_LENGTHth to end of string.
			revString = revString.substring(CONST_ALL_OTHER_SECTION_LENGTH, revString.length)

			// make the firstattempt = false so that it does not come here again
			firstAttempt = false;
		}

		

	textbox.value = "+" + intlDialCode + " " + reverse( finalString);
	
}

// Reverse string 
function reverse(valueToBeReversed) { 
	  var inp = valueToBeReversed

	  var outp = ""

	  for (i = 0; i < inp.length; i++) { 
		outp = inp.charAt (i) + outp 
	  } 
	  return outp;
} 


/*
 * Trims spaces before and after the supplied text
 *
 *****************************************/

function trim(s) {
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}
	