function goToPage(curPageNo, noOfRecPerPage)
{	
	var curUrl = document.location.href;
	var recNo = 0;
	if (noOfRecPerPage)
	{
		recNo = (curPageNo - 1) * noOfRecPerPage;
	}
	curUrl = addParamToUrl(curUrl, 'No', recNo);
	var keyField = document.getElementById("sortKey");
	var orderField = document.getElementById("sortOrder");
	var existingKey = "";
	var existingOrder = "";
	if (keyField)
	{
		existingKey = keyField.value;
	}
	if (orderField)
	{
		existingOrder = orderField.value;
	}	

	if (existingKey != '' && existingOrder != '')
	{
		curUrl = addParamToUrl(curUrl, 'Ns', existingKey+'|'+existingOrder);
	}

	curUrl = addParamToUrl(curUrl, 'dispatch', 'loadAttribTable');
        
                    // scroll to location
			var curDiv = dojo.byId("fillThisSpace");
			    var curTop = 0;
			    var obj = curDiv;
			    
			    do {
				curTop += obj.offsetTop;
			    } while (obj = obj.offsetParent);
			    window.scroll(0, curTop);
        
	dojo.xhrGet( {
        	url: curUrl,
        	handleAs: "text",
        	//timeout: 60000,
        	load: function(response, ioArgs) {
			var curDiv = dojo.byId("fillThisSpace");
			if (curDiv) {
			    curDiv.innerHTML = response;
			    
			    var curTop = 0;
			    var obj = curDiv;
			    
			    do {
				curTop += obj.offsetTop;
			    } while (obj = obj.offsetParent);

			    //window.scroll(0, curTop);
			    if (curDiv.style.display == 'none') {
				    curDiv.style.display = 'block';
			    }
			}           
            		return response;
            	},
		error: function(response, ioArgs) {
			alert("Error occured");
		}
	});
}


function addParamToUrl(curUrl, paramName, paramValue)
{
	var res = '';
	var paramToCheck = paramName + '='; 
	var ind1 = curUrl.indexOf("?");
	var ind2 = curUrl.lastIndexOf(paramToCheck);
	if (ind1 >= 0 && ind2 >= 0)
	{
		var ind3 = curUrl.indexOf("&", ind2);
		if (ind3 >= 0)
		{	
			res = curUrl.substring(0, ind2) + paramToCheck + paramValue + curUrl.substring(ind3); 
		}
		else
		{
			res = curUrl.substring(0, ind2) + paramToCheck + paramValue;	
		} 
	}
	else if (ind1 >= 0 && ind2 < 0)
	{
		res = curUrl + '&' + paramToCheck + paramValue;
	}
	else
	{
		res = curUrl + '?' + paramToCheck + paramValue;
	}
	return res;
}


function goPrevPage(pageNo, noOfRecPerPage)
{
	var prevPage = '' + (parseInt(pageNo) - 1);
	goToPage(prevPage,  noOfRecPerPage);
	
}


function goNextPage(pageNo, noOfRecPerPage)
{
	var nextPage = '' + (parseInt(pageNo) + 1);
	goToPage(nextPage,  noOfRecPerPage);
}


var sortOrder = '1';

// Ascending/Descending arrow for product results table
var arrowDesc = document.createElement("img");
arrowDesc.setAttribute("src", SITE_ROOT + "common/images/icons/ico-arrow-desc.gif");
arrowDesc.setAttribute("class", "sortArrow");

var arrowAsc = document.createElement("img");
arrowAsc.setAttribute("src", SITE_ROOT + "common/images/icons/ico-arrow-asc.gif");
arrowAsc.setAttribute("class", "sortArrow");

// Sorts product resluts table
function doAjaxSort(sortKey, sortID, hdrCell)
{
	var curUrl = document.location.href;
	var headingCell = hdrCell;
	var keyField = document.getElementById("sortKey");
	var orderField = document.getElementById("sortOrder");
	var pgNoField = document.getElementById("curPageNo");
	var existingKey = "";
	var existingOrder = "";
	var existingPageNo = "";
	var sortIdentifier = sortID;
	
	if (keyField) {
		existingKey = keyField.value;
	} if (orderField) {
		existingOrder = orderField.value;
	} if (pgNoField) {
		existingPageNo = pgNoField.value;
	}
	
	if (sortKey == existingKey) {
	    //toggle the sort order

	    if (existingOrder == '0')  {
		sortOrder = '1';
	    } else {
		sortOrder = '0';
	    }
	}
	
	curUrl = addParamToUrl(curUrl, 'Ns', sortKey + '|' + sortOrder);
	
	if (existingPageNo != '') {
		var recNo = (parseInt(existingPageNo) - 1) * 30;
		curUrl = addParamToUrl(curUrl, 'No', recNo);	
	}
	
	curUrl = addParamToUrl(curUrl, 'dispatch', 'loadAttribTable');
	dojo.xhrGet( {
	    url: curUrl,
	    handleAs: "text",
	    //timeout: 60000,
	    load: function(response, ioArgs) {
		var curDiv = dojo.byId("fillThisSpace");
			
		if (curDiv) {
			curDiv.innerHTML = response;
			
			if (curDiv.style.display == 'none') {
			    curDiv.style.display = 'block';
			}
			
			var headLabel = document.getElementById(rmWhiteSpace(sortID) + "Head");
    
			if (sortOrder == 1) { 
					
			    if (headLabel) {
				insertAfter(arrowDesc, headLabel);
				
				//alert(headingCell.innerHTML);
			    }
			} else {
			
			    if (headLabel) {
				insertAfter(arrowAsc, headLabel);
				
				//alert(headingCell.innerHTML);
			    }
			}
	
			//console.log(response);
		}           
		return response;
	    },
	    error: function(response, ioArgs) {
		    alert("Error occured");
	    }
	});
}	