
Event.observe(window, 'load', function(event) {
	if (window.location.href.indexOf('#') == -1) {
		var textCriteria = $('textCriteria');
		if (textCriteria) {
			textCriteria.focus();
		}
	}
});

function redirectToAdvancedSearch(url, searchType, additionalParams) {
	var select = $('categoryId');
	var id = select.value;
	if (!id) {
		id = 1;
	}
	window.location.href = url + '?categoryId=' + id + '&searchType=' + searchType + additionalParams;
	return false;
}


/* -- Utility Classes -- */
function Map() {
  var keys = new Array();
  this.contains = function(key) {
     AssertUtil.assertParamNotNull(key, "key");
     var entry = findEntry(key);
     return !(entry == null || entry instanceof NullKey);
  }
  this.get = function(key) {
   var entry = findEntry(key);
   if ( !(entry == null || entry instanceof NullKey) )
      return entry.value;
    else
      return null;
  }
  this.put = function(key, value) {
    AssertUtil.assertParamNotNull(key, "Map.put.key");
    AssertUtil.assertParamNotNull(value, "Map.put.value");
    var entry = findEntry(key);
    if (entry) {
      entry.value = value;
    } else {
      addNewEntry(key, value);
    }
  };
  this.remove = function (key) {
    AssertUtil.assertParamNotNull(key, "key");
    for (var i=0;i<keys.length;i++) {
      var entry = keys[i];
      if (entry instanceof NullKey) continue;
      if (entry.key == key) {
          keys[i] = NullKey;
      }
    }        
  }
  function findEntry(key) {
    for (var i=0;i<keys.length;i++) {
      var entry = keys[i];
      if (entry instanceof NullKey) continue;
      if (entry.key == key) {
          return entry
      }
    }
    return null;
  }
  function addNewEntry(key, value) {
      var entry = new Object();
      entry.key = key;
      entry.value = value;
      keys[keys.length] = entry; 
  }
}
//replace the entries of map in key array, removing the former value;  
function NullKey() {
}
new NullKey();

  
function AssertUtil() {}

new AssertUtil();

AssertUtil.assertInclude = function (object, jsFile) {
  if (object==null) {
    throw new AssertException( "file " + jsFile + " was not included ");
  }
}

AssertUtil.assertParamNotNull = function (value, paramName) {
  if (value == null) {
    throw new IllegalArgumentException(  "Parameter must not be null: " + paramName );
  }
}
AssertUtil.assertParamIsNotEmpty = function (value, paramName) {
  AssertUtil.assertParamNotNull(value, paramName);
  if (StringUtil.isEmpty(value)) {
    throw new IllegalArgumentException(  "Parameter must not be empty: " + paramName );
  }
}

AssertUtil.assertIntegerParam = function (value, paramName) {
  AssertUtil.assertParamNotNull (value, paramName);
  var i = parseInt(value, 10);
  if ((i == null) || (isNaN(i)))
  {
    throw new IllegalArgumentException( "Invalid Integer Parameter: " + paramName);
  }
}
AssertUtil.assertGreaterThanZeroParam = function (value, paramName) {
  AssertUtil.assertIntegerParam (value, paramName);
  var i = parseInt(value, 10);
  if (i <= 0)
  {
    throw new IllegalArgumentException( " assertGreaterThanZeroParam : " + paramName);
  }
}

AssertUtil.assertInstance = function (param, clazz, clazzName) {
  AssertUtil.assertParamNotNull(param, "param");
  AssertUtil.assertParamFalse(typeof(clazz) == "string", "Invalid clazz parameter");
  if (!(param instanceof clazz)) {
    throw new IllegalArgumentException( " Invalid parameter must be instance of " + clazzName);
  }
} 

AssertUtil.assertParamInstance = function (param, clazz, clazzName) {
  if (!(param instanceof clazz)) {
    throw new IllegalArgumentException( " Invalid parameter must be instance of " + clazzName);
  }
}
AssertUtil.assertResultNotNull = function (valueOfResult, message) {
  if ( valueOfResult == null ) {
    throw new AssertException( "Invalid result value: " + message);
  }
}

AssertUtil.assertParamFalse = function(booleanValue , message) {
  if (booleanValue) {
    throw new IllegalArgumentException( "AssertParamFalse Exception message: " + message);
  }
}
AssertUtil.assertMethodExists = function(object, functionName)
{
  AssertUtil.assertParamNotNull( object, "object");
  if (object[functionName]==null) {
    throw new AssertException(  " Object " + object + " does not have function :" + functionName );
  }
}
AssertUtil.assertMemberState = function(member, memberName, clazz, clazzName, mandatory, valueRange)
{
    if (mandatory) {
      if (member == null) {
        throw new AssertException( " Member " + memberName + " must not be null ");
      }
      if (!(member instanceof clazz)) {
        throw new AssertException( " Invalid state " + memberName + " must be instance of " + clazzName);
      }
    }
    else if (member != null) {
      if (!(member instanceof clazz)) {
        throw new AssertException( "Invalid state " + memberName + " must be instance of " + clazzName);
      }
    }
} 
AssertUtil.assertMemberStateString = function(member, memberName, mandatory, valueRange) {
    
    if (mandatory) {
      if (typeof(member) != typeof(" ")) {
        throw new AssertException( new AssertException( "Invalid state " + memberName + 
                        " must be instance of string and not " + typeof(member)) );
      }
      if (StringUtil.isEmpty(member)) {
        throw new AssertException( "AssertException "  + memberName + " must not be empty");
      }
    }
    else if (member != null) {
      if (typeof(member) != typeof(" ")) {
        throw new AssertException( "Invalid state " + memberName + " must be instance string " );
      }
    }
}

function showBlock(blockId) {
	$(blockId).show();
	return false;
}

function hideBlock(blockId) {
	$(blockId).hide();
	return false;
}


function changeBlockVisibility(blockId) {
	var block = $(blockId);
	if (block) {
		if (block.offsetHeight == 0) {
			block.show();
		} else {
			block.hide();
		}
	}
	return false;
}
