// Helper function.
function replaceAll(str, pcFrom, pcTo){

	var i = str.indexOf(pcFrom);
	var c = str;

	while (i > -1){
		c = c.replace(pcFrom, pcTo);
		i = c.indexOf(pcFrom);
	}
	return c;

}

// Returns the input element that the user types the zip code into.
function getZipInputElement()
{
	if (document.getElementById('docLocatorZipInput'))
		return document.getElementById('docLocatorZipInput');
	else
		return document.getElementById('zipInput');
}

// Send the user to the results page with the correct parameters.
function mySubmitForm() {

    if(document.getElementById('ctl00_ctl14_g_d531e74e_dded_494c_b4cd_ddefec8af2f1_zipInput')) 
	{
       document.getElementById('ctl00_ctl14_g_d531e74e_dded_494c_b4cd_ddefec8af2f1_zipInput').value = TrimWhiteSpaces(getZipInputElement().value);
       //return true;
	}

	var newURL= getRelativeURL(window.location.href) + "?zip=" + TrimWhiteSpaces(getZipInputElement().value) + "&QuerySource=" + document.getElementById('querySource').value;
	window.location.href= newURL;
}

// If the user presses enter with the cursor in the zip code input field, 
// the form submission is cancelled, user is redirected to the results page with zipInput and querySource parameters
function EnterKey(e)
{
     var key;
     if(window.event)
     {
          key = window.event.keyCode;     //IE
      }
     else
     {
          key = e.which;     //firefox

      }
     if(key == 13)
     {

          mySubmitForm();
          return false;
     }
     else
         return false;
}

// Helper function which gets the value of the parameter from the page url given the parameters name.
function getURLParam(ji) {
	hu = window.location.search.substring(1);
	gy = hu.split("&");
	for (i=0;i<gy.length;i++) {
		ft = gy[i].split("=");
		if (ft[0] == ji) {
			return ft[1];
		}
	}
}

function getRelativeURL(curURL)
{

	var findDocURL = "";
	for (i = 0; i < getNumberOfChars(curURL.replace("http://",""),"/")-2; i++)
		findDocURL = findDocURL + "../";
	findDocURL = "/finddoc/pages/Results.aspx";

	return findDocURL;


}

function SetInitialZipCode()
{
	var currentZip = getURLParam("zip");
	if (currentZip != null)
	{
		getZipInputElement().value = currentZip;
	}
}

function getNumberOfChars(str,chr)
{
	return (str.length - replaceAll(str, chr,"").length);
}



//Toggle initial text if user hasn't entered anything into the text box.
var DEFAULT_TEXT = ' your zip code';
function OnFocus(elem)
{ 
   if (elem.value == DEFAULT_TEXT)
      elem.value = "";
}

function OnBlur(elem)
{
   var textValue = elem.value;

   if (textValue == DEFAULT_TEXT || textValue.length == 0)
     elem.value = DEFAULT_TEXT;

}


// Helper function
String.format = function( text )
{
    //check if there are two arguments in the arguments list
    if ( arguments.length <= 1 )
    {
        //if there are not 2 or more arguments there’s nothing to replace
        //just return the original text
        return text;

    }

    //decrement to move to the second argument in the array
    var tokenCount = arguments.length - 2;

    for( var token = 0; token <= tokenCount; token++ )
    {
        //iterate through the tokens and replace their placeholders from the original text in order
        text = text.replace( new RegExp( "\\{" + token + "\\}", "gi" ),
                                                arguments[ token + 1 ] );
    }
    return text;
};


