// Functions used for validation of the Quick Search on the left  menu

function validateSubmit(voButton)
{	
	if(voButton.bSubmited == "true")
	{
		return false;
	}
	else
	{
		return true;
	}
}

function validateSearch(vsIdTextBox, vsDefaultValue)
{
  var bValid = false;
  var txtSearch = document.getElementById(vsIdTextBox);
  var sValue = new String();
  var i = 0;

  if (validateSubmit(this))
  {
		sValue = txtSearch.value;
	
		while(i < sValue.length && !bValid)
		{
			if(sValue.charAt(i) != " ")
			{
				bValid = true;
			}
			i++;
		}
		
		if (sValue == vsDefaultValue)
		{
			bValid = false;
		}
	
		if(!bValid)
		{
			txtSearch.className = "SearchNotGood";
			txtSearch.focus();
			return false;
		}
		else
		{
			txtSearch.className = "SearchGood";
			return true;
		}
  }
	else
	{
		return false;
	}
}

function goSearch(vsSearchString, vsSearchPage)
{
	window.location = vsSearchPage + _URLEncode(vsSearchString);
}

function handleSearchKeyDown(e, vsIdTxtSearch, vsSearchDefaultValue, vsPageSearch)
{
	e = e || event;
	if (e.keyCode == 13)
	{
		if (e.preventDefault)
		{
			e.preventDefault();
		}
		else
		{
			e.returnValue = false;
		}
		
		if (validateSearch(vsIdTxtSearch, vsSearchDefaultValue))
		{
			goSearch(document.getElementById(vsIdTxtSearch).value, vsPageSearch);
		}
	}
}

function _URLEncode (clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString.charAt(x) == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}