function tsResubmit(searchText) {
	$("sysparm_search").value = searchText;
	$("kb_view").submit();
}

function changeSort(value) {
	_setUrl("sysparm_order", value);
}

function setPageSize(pageSize) {
	_setUrl("sys_nav_count",pageSize);
}

function setStartRow(startRow) {
	_setUrl("sysparm_current_row",startRow);
}

function changeRowCount(element){
	setPageSize(element.value);
}

function changeRowCountFromMenu(value){
	setPageSize(value);
}

var timerHandle = null;
function gotoRow(pageSize, totalRows, element){
	if (timerHandle != null)
		clearTimeout ( timerHandle );
	timerHandle = setTimeout("_gotoRow(" + pageSize + "," + totalRows + ",'" + element.id + "')", 1000);
}

function _gotoRow(pageSize, totalRows, elementId){		
	var startRow = document.getElementById(elementId).value;

	if (isNaN(startRow))
		startRow = 1;
		
	var pageStart = startRow;
	if (pageStart > totalRows)
		pageStart = totalRows - pageSize + 1;
	if (pageStart <= 0)
		pageStart = 1;
	setStartRow(pageStart);
}

function nextPage(pageSize,totalRows,startRow){
	var pageStart = startRow + pageSize;
	if (pageStart > totalRows)
		pageStart = totalRows - pageSize + 1;
	if (pageStart <= 0)
		pageStart = 1;
	setStartRow(pageStart);
}

function lastPage(pageSize,totalRows,startRow){
	var pageStart = totalRows - pageSize + 1;
	if (pageStart <= 0)
		pageStart = 1;
	setStartRow(pageStart);
}

function previousPage(pageSize,totalRows,startRow){
	var pageStart = startRow - pageSize;
	if (pageStart <= 0)
		pageStart = 1;
	setStartRow(pageStart);
}

function firstPage(pageSize,totalRows,startRow){
	setStartRow(1);
}

function _setUrl(key,value) {
	var url = _getUrlExcludingParam(key);
	if (url.hasParams)
		url.url += "&";
	else
		url.url += "?";
	url.url += key+'='+value;

	window.location.href = url.url;
}

function _getUrlExcludingParam(paramName) {
	var seperator = "";
	var hasParams = false;
	if (window.location.pathname.substring(0,1) != "/")
		seperator = "/";

	var baseURL = window.location.protocol + "//" + window.location.host + seperator + window.location.pathname;
	seperator = "?";
	var parts = window.location.href.replace(/[?&]+([^=&]+)=?([^&]*)/gi, function(full,key,value) {
		if ((key != paramName) && (key != "sysparm_rollbased")) {
			baseURL += seperator + key;
			if ((value != null) &&(value != ""))
				baseURL += "=" + value;
			seperator = "&";
			hasParams = true;
		}
	});
	return {
		url : baseURL,
		hasParams : hasParams
	};
}




// On Load calling the prototype event for observing the iframe loading
/*
var actionName = null;
if($$("iframe").length > 0){
	$$("iframe")[0].observe("load", function(event){
		actionName = this.src;
		if(this.contentWindow.document.forms["back_form"] != undefined){
			this.contentWindow.document.forms["back_form"].action = actionName;
		}
	});
}*/

function loadBruinOnlineTopic() {
	var $kb_knowledge_topic =  $jUC('select[id="kb_knowledge.topic"]');
	$kb_knowledge_topic.val("Bruin OnLine").find("option:not(:selected)").remove();
	ucla_selectChanges('kb_knowledge.category', 'kb_knowledge.topic')
}

function showAdvancedSearch() {
	var $advancedSearch = $jUC("#kb_advanced_search");
	// $advancedSearch.show();
	
}

function hideKnowledgeBox() {
	var $searchEngine =  $jUC("#search_engine");
	$searchEngine.hide();
}

function showHideButtons() {
	var $answer = $jUC("#answer");
	var $askquestion = $jUC("#askquestion");
	var $print = $jUC("#print");
	var URI = window.location.href;
	if(URI.indexOf("kb_view") == -1) {
		$askquestion.hide();
		$print.hide();
		
	}
}

/* :::::::::::::::::::::::::::::::::::::::::::::::::::
WHEN DOM IS LOADED - Jquery
:::::::::::::::::::::::::::::::::::::::::::::::::::*/

(function ($jUC) {
	$jUC(function () {
		loadBruinOnlineTopic();
		showAdvancedSearch();
		hideKnowledgeBox();
		showHideButtons();
		
	});
})($jUC);

function printListURLDecode(href) {
href = href.replace(/@99@/g, "&");
href = href.replace(/@88@/g, "@99@");
href = href.replace(/@77@/g, "@88@");
href = href.replace(/@66@/g, "@77@");
return href;
}

function printReport() {
	
	// Determine which view we want to use - either Dell, Apple or standard
	var viewName = "po_print";
	
	// The url for printing the form
	var frameSrc = "kb_view.do?sys_kb_id=" + $("sys_id").value + "&sysparm_view=" + viewName;
	frameSrc = printListURLDecode(frameSrc);
	
	if (frameSrc.indexOf("?") < 0) {
		frameSrc += "?";
	} else {
		frameSrc += "&";
	}
	
	// we're doing a print; we want the whole report
	frameSrc = frameSrc.replace("partial_page=", "syshint_unimportant=");
	frameSrc = frameSrc.replace("sysparm_media=", "syshint_unimportant=");
	frameSrc += "sysparm_stack=no&sysparm_media=print";
	
	var features = "resizable=yes,scrollbars=yes,status=yes,toolbar=no,menubar=yes,location=no";
	win = window.open(frameSrc, "Printer_friendly_format", features);
	win.focus();
	
	// Return false to ensure that this is the end of the print processing
	return false;
}

