function findElement(elemID)
{
	var obj;
	
	if (document.getElementById) {
		obj = document.getElementById(elemID);
	} else if (document.all) {
		obj = document.all(elemID);
	} else if (document.layers) {
		obj = document.layers[elemID];
	}
	return obj;
}

function openWin(URL,aName,wName,Scroll,W,H,T,L)
{
	var top = T; 
	var left = L;
	if(T == null)
    	top = (screen.availHeight - H)/2;
	if(L == null)
	    left= (screen.availWidth - W)/2;
	window.name = aName; 
	(window.open(URL,wName,"toolbar=no,location=no,directories=no,status=no,,scrollbars="+ Scroll +",width="+ W +",height="+ H +",top="+ top +",left="+ left +",resizable=no,menubar=no")||window).focus();
}

function getForm(formName)
{
	if (window.navigator.appName.toLowerCase().indexOf("netscape") > -1) {
		return document.forms[formName];
	}
	else {
		return document[formName];
	}
}

function bytesToString(bytes)
{
	if( bytes <= 1024 )
		return bytes + ' bytes';

	if( bytes <= 102400 )
		return Math.round( bytes / 1024 ) / 100 + ' KB';
	else
		return Math.round( bytes / 10485.76 ) / 100 + ' MB';
}

function countSelected(form)
{
    var item;
    var itemCount = 0;

    for( var i = 0; i < form.elements.length; i++ )
    {   
        item = form.elements[i];
        
        if( item.type && ( item.type == "checkbox" && item.checked ) )
        {
            itemCount++;
        }            
    }
        
    return itemCount;
}

function buttonClick(button)
{
	if (button.checked) {
	
		if (button.group == null) {		
			button.checked = false;
			button.className = 'borderButton';
			if (findElement(button.datafield)) findElement(button.datafield).value = false;
		}
		
	} else {
	
		button.checked = true;
		button.className = 'selectedBorderButton';

		if (findElement(button.datafield)) findElement(button.datafield).value = true;
		
		if (button.group != null) {

			var grp = findElement(button.group);

			if (grp) {
			
				if (grp.value) var prevItem = findElement(grp.value);
				
				if (prevItem) {
					prevItem.checked = false;
					prevItem.className = 'borderButton';
				}
				
				grp.value = button.id;
			}
		}
	}	
}

function buttonOver(button)
{
	if (!button.checked) {
		button.className = 'highlightBorderButton';
	}
}

function buttonOut(button)
{
	if (!button.checked) {
		button.className = 'borderButton';
	}
}

function checkUncheckAll(check) 
{
    var theForm = document.forms[0];
    for(var z = 0; z < theForm.length; z++ )
    {
        if(theForm[z].type == 'checkbox' && theForm[z].name != 'checkall' )
        {
	        theForm[z].checked = check;
	    }
    }
}

function clickScrollUp(btnID)
{
	var qty = findElement(btnID);
	var val = isNaN(parseInt(qty.value)) ? 1 : parseInt(qty.value);
	if( val < 99 ) val++;
	else val = 99;
	qty.value = val;
}

function clickScrollUpMax(btnID,maxQty)
{
	var qty = findElement(btnID);
	var val = isNaN(parseInt(qty.value)) ? 1 : parseInt(qty.value);
	if( val < maxQty ) val++;
	else val = maxQty;
	qty.value = val;
}

function clickScrollDown(btnID)
{
	var qty = findElement(btnID);
	var val = isNaN(parseInt(qty.value)) ? 1 : parseInt(qty.value);
	if( val > 1 ) val--;
	else val = 1;
	qty.value = val;
}

function oBrowserInfo(){

	this.ua = navigator.userAgent.toLowerCase();

	/*** Browser Version ***/
    if( this.ua.indexOf("msie") != -1) {
        
        this.version = parseInt(this.ua.charAt(this.ua.indexOf("msie") + 5));
        
        this.isIE   = true;
		this.isIE2  = (this.version==2) ? true: false;
		this.isIE3  = (this.version==3) ? true: false;
		this.isIE4  = (this.version==4) ? true: false;
		this.isIE5  = (this.version==5) ? true: false;
		this.isIE6  = (this.version==6) ? true: false;
		this.isIE7  = (this.version>=7) ? true: false;
        
    } else if(this.ua.indexOf("netscape") != -1) {

        this.version = parseInt(this.ua.charAt(this.ua.indexOf("netscape") + 9));
        
        this.isNN   = true;
		this.isNN2  = (this.version==2) ? true: false;
		this.isNN3  = (this.version==3) ? true: false;
		this.isNN4  = (this.version==4) ? true: false;
		this.isNN5  = (this.version==5) ? true: false;
		this.isNN6  = (this.version>=6) ? true: false;
		
	} else if(this.ua.indexOf("aol") != -1) {
	
        this.version = parseInt(this.ua.charAt(this.ua.indexOf("aol") + 4));
		this.isAOL  = true;
		this.isAOL6 = (this.version==6) ? true: false;
		this.isAOL7 = (this.version==7) ? true: false;
		this.isAOL8 = (this.version>=8) ? true: false;
		
    } else if(this.ua.indexOf("opera") != -1) {
		
		this.isOpera = true;
        this.version = parseInt(this.ua.charAt(this.ua.indexOf("opera") + 8));
		
    } else if(this.ua.indexOf("mozilla") != -1) {

		this.isMozilla = true;
        this.version = parseInt(this.ua.charAt(this.ua.indexOf("mozilla") + 8));
    }

	/*** Operating System ***/
    this.isWin   = (this.ua.indexOf("windows") != -1)
	this.isWin16 = (this.ua.indexOf("windows 3.1") != -1 || this.ua.indexOf("win16") != -1) ? true: false;
	this.isWin32 = (this.isWin95 || this.isWin98 || this.isWinNT) ? true: false;
	this.isWinCE = (this.ua.indexOf("windows ce") != -1) ? true: false;
	this.isWin95 = (this.ua.indexOf("windows 95") != -1 || this.ua.indexOf("win95") != -1 ) ? true: false;
	this.isWin98 = (this.ua.indexOf("windows 98") != -1 || this.ua.indexOf("win98") != -1 ) ? true: false;
	this.isWinNT = (this.ua.indexOf("windows nt") != -1 || this.ua.indexOf("winnt") != -1 ) ? true: false;
    this.isWinXP = (this.ua.indexOf("nt 5") != -1);
    this.isWinXPSP2 = (this.ua.indexOf("sv1") != -1) || (this.isIE && this.version >= 7);
    this.isVista = (this.ua.indexOf("nt 6") != -1);
	this.isMac   = (this.ua.indexOf("mac") != -1) ? true: false;
	this.isOSX   = (this.isMac && this.ua.indexOf("mac os x") != -1 ) ? true: false;	
	this.isUnix  = (this.ua.indexOf("sunos") != -1 || this.ua.indexOf("hp-ux") != -1 || this.ua.indexOf("x11") != -1) ? true: false;
}

var oBrowser = new oBrowserInfo();

function viewPort() {
    var h = window.innerHeight || document.documentElement.clientHeight || document.getElementsByTagName('body')[0].clientHeight;
    var w = window.innerWidth || document.documentElement.clientWidth || document.getElementsByTagName('body')[0].clientWidth;
    
    return { width : w , height : h }
}

function centreInbrowser(objID) {
    
    var ctl = findElement(objID);
    
    ctl.style.top = ((viewPort().height - ctl.offsetHeight) /2) + 'px';
    ctl.style.left = ((viewPort().width - ctl.offsetWidth) /2) + 'px'; 	
}
