var viewRecordBeforeSortedList = new Array();
var viewRecordAfterSortedList = new Array();

function checkbrowsertype()
{
  	var Sys = {};
    var ua = navigator.userAgent.toLowerCase();
    if (window.ActiveXObject)
        Sys.ie = ua.match(/msie ([\d.]+)/)[1]
    else if (document.getBoxObjectFor)
        Sys.firefox = ua.match(/firefox\/([\d.]+)/)[1]
    else if (window.MessageEvent && !document.getBoxObjectFor)
        Sys.chrome = ua.match(/chrome\/([\d.]+)/)[1]
    else if (window.opera)
        Sys.opera = ua.match(/opera.([\d.]+)/)[1]
    else if (window.openDatabase)
        Sys.safari = ua.match(/version\/([\d.]+)/)[1];
    

    if(Sys.ie) return 'ie';
    if(Sys.firefox) return 'firefox';
    if(Sys.chrome) return 'chrome';
    if(Sys.opera) return 'opera';
    if(Sys.safari) return 'safari';
	return false;
}

function checkAll(num)
{
	var obj = document.getElementById('checkbox_all');
	var checked = obj.checked;
	var objs = document.getElementsByTagName('input');
	if(objs != null)
	{
		for(var i=0;i<objs.length;i++)
		{
			var tempStr = objs[i].id;
			if(tempStr == 'checkbox_all') 
				continue;
			if(checked)
			{
				if((objs[i] != null)&&(tempStr.indexOf('view_checkbox_') != -1))
					objs[i].checked = true;	
			}
			else
			{
				if((objs[i] != null)&&(tempStr.indexOf('view_checkbox_') != -1))
					objs[i].checked = false;
			}
		}
	}
}


function ltrim(str)
{
   var whitespace = new String(" \t\n\r");
   var s = new String(str);
   if (whitespace.indexOf(s.charAt(0)) != -1) {
      var j=0, i = s.length;
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;
      s = s.substring(j, i);
   }
   return s;
}

//RTrim(string) : Returns a copy of a string without trailing spaces.
function rtrim(str)
{
   var whitespace = new String(" \t\n\r");
   var s = new String(str);
   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      var i = s.length - 1;       // Get length of string
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;
      s = s.substring(0, i+1);
   }
   return s;
}

// Trim(string) : Returns a copy of a string without leading or trailing spaces
function trim(str) {
   return rtrim(ltrim(str));
}

function confirmAction(myString)
{
	alert(myString);
	return false;
}

function checkIsSelectCheckbox(num,allNum)
{
	var tempNum = 0;
	if(num == 'All')
		tempNum = allNum;
	else
		tempNum = parseInt(num);
	var isSelected = false;
	for(var i=0;i<tempNum;i++)
	{
		var obj = document.getElementById('view_checkbox_'+i);
		if((obj != null)&& (obj.checked))
			isSelected = true;
	}
	if(!isSelected)
		return confirmAction("Please select a checkbox.");		
	return true;
}

function getFirstSelectedCheckBox(num,allNum)
{
	var tempNum = 0;
	if(num == 'All')
		tempNum = allNum;
	else
		tempNum = parseInt(num);
	var selectedObjID = 0;
	for(var i=0;i<tempNum;i++)
	{
		var obj = document.getElementById('view_checkbox_'+i);
		if((obj != null)&& (obj.checked))
		{		
			selectedObjID = obj.value;
			break;
		}
	}	
	return selectedObjID;
}

function switchPageNum(num)
{
	var newPageNumOrder = document.getElementById('displayRecordNumberSelect').value;
	if(document.getElementById('displayRecordNumberSelect').options[newPageNumOrder] == null)
		return false;
	var newPageNum = document.getElementById('displayRecordNumberSelect').options[newPageNumOrder].innerHTML;
	if((num == 'All')&&(newPageNum == 'All'))
		return false;
	if(parseInt(num) == parseInt(newPageNum))
		return false;
	document.getElementById('hidden_displayRecordNumber').value = newPageNum;
	return submitForm();
}

function clickNext()
{
	var a = parseInt(document.getElementById('hidden_pageNum').value);
	a +=1;
	document.getElementById('hidden_pageNum').value = a;
	return submitForm();
	
}

function clickPrev()
{
	var a = parseInt(document.getElementById('hidden_pageNum').value);
	a -=1;
	document.getElementById('hidden_pageNum').value = a;
	return submitForm();	
}

function clickStart()
{
	document.getElementById('hidden_pageNum').value = 1;
	return submitForm();
}

function sort_order(fieldName)
{
	
	var obj = document.getElementById('hidden_sortType').value;
	if(trim(obj.toLowerCase()) == 'asc')
		document.getElementById('hidden_sortType').value = 'DESC';
	else
		document.getElementById('hidden_sortType').value = 'ASC';
	document.getElementById('hidden_sortField').value = fieldName; 

	
	return submitForm();
}

function showorhide(myObject)
{
	if(myObject.style.display == 'none')
		myObject.style.display = '';
	else
		myObject.style.display = 'none';
}


/**
* Pops up a new window in the middle of the screen
*/
function popupWindow(mypage, myname, w, h, scroll) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',status=no,toolbar=no,scrollbars='+scroll+',titlebar=no,menubar=no,resizable,width=640,height=480,directories=no,location=no'
	win = window.open(mypage, myname, winprops)
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}



function confirmDelete(num,allNum)
{
	var tempNum = 0;
	if(num == 'All')
		tempNum = allNum;
	else
		tempNum = parseInt(num);
	var selectedObjID = 0;
	for(var i=0;i<tempNum;i++)
	{
		var obj = document.getElementById('view_checkbox_'+i);
		if((obj != null)&& (obj.checked))
		{		
			selectedObjID = obj.value;
			break;
		}
	}	
	if(selectedObjID > 0)
	{
		var a = confirm('Are you sure you want to delete this item?');
		
		if(a)
		{
			return true;
		} 
	}
	return false;
}

function GetPageSize() {
  var scrW, scrH;
  if(window.innerHeight && window.scrollMaxY) {
    // Mozilla
    scrW = window.innerWidth + window.scrollMaxX;
    scrH = window.innerHeight + window.scrollMaxY;
  } else if(document.body.scrollHeight > document.body.offsetHeight){
    // all but IE Mac
    scrW = document.body.scrollWidth;
    scrH = document.body.scrollHeight;
  } else if(document.body) { // IE Mac
    scrW = document.body.offsetWidth;
    scrH = document.body.offsetHeight;
  }
  
  var winW, winH;
  if(window.innerHeight) { // all except IE
    winW = window.innerWidth;
    winH = window.innerHeight;
  } else if (document.documentElement 
    && document.documentElement.clientHeight) {
    // IE 6 Strict Mode
    winW = document.documentElement.clientWidth; 
    winH = document.documentElement.clientHeight;
  } else if (document.body) { // other
    winW = document.body.clientWidth;
    winH = document.body.clientHeight;
  }
  
  // for small pages with total size less then the viewport
  var pageW = (scrW<winW) ? winW : scrW;
  var pageH = (scrH<winH) ? winH : scrH;
  
  return {PageW:pageW, PageH:pageH, WinW:winW, WinH:winH};
}

function GetPageScroll() {
  var x, y;
  if(window.pageYOffset) {
    // all except IE
    y = window.pageYOffset;
    x = window.pageXOffset;
  } else if(document.documentElement 
    && document.documentElement.scrollTop) {
    // IE 6 Strict
    y = document.documentElement.scrollTop;
    x = document.documentElement.scrollLeft;
  } else if(document.body) {
    // all other IE
    y = document.body.scrollTop;
    x = document.body.scrollLeft; 
  }
  return {X:x, Y:y};
}


