//////////////////////////////////////////////////////////////////////////////
//
// Detect Browser
//
//////////////////////////////////////////////////////////////////////////////

var agt = navigator.userAgent.toLowerCase () ;
var is_major = parseInt (navigator.appVersion) ;
var is_minor = parseFloat (navigator.appVersion) ;

var is_safari = (agt.indexOf ("safari") != -1) ;

var is_nav  = ((agt.indexOf ('mozilla') != -1) && (agt.indexOf ('spoofer') == -1) && (agt.indexOf ('compatible') == -1) && (agt.indexOf ('opera') == -1) && (agt.indexOf ('webtv') == -1) && (agt.indexOf ('hotjava') == -1) && (agt.indexOf ('safari') == -1)) ;

var is_nav2 = (is_nav && (is_major == 2)) ;
var is_nav3 = (is_nav && (is_major == 3)) ;
var is_nav4 = (is_nav && (is_major == 4)) ;
var is_nav4up = (is_nav && (is_major >= 4)) ;
var is_navonly = (is_nav && ((agt.indexOf (";nav") != -1) || (agt.indexOf ("; nav") != -1))) ;
var is_nav6 = (is_nav && (is_major == 5) && (agt.indexOf("netscape") != -1) && (agt.indexOf ("netscape/7") == -1));
var is_nav6up = ((is_nav && (is_major >= 5)) || (is_nav && (agt.indexOf ('netscape/7') != -1))) ;
var is_gecko = (agt.indexOf ('gecko') != -1) ;

var is_ie     = ((agt.indexOf ("msie") != -1) && (agt.indexOf ("opera") == -1) && (agt.indexOf ("safari") == -1)) ;
var is_ie3    = (is_ie && (is_major < 4)) ;
var is_ie4    = (is_ie && (is_major == 4) && (agt.indexOf ("msie 4")!=-1) ) ;
var is_ie4up  = (is_ie && (is_major >= 4)) ;
var is_ie5    = (is_ie && (is_major == 4) && (agt.indexOf ("msie 5.0") != -1) ) ;
var is_ie5_5  = (is_ie && (is_major == 4) && (agt.indexOf ("msie 5.5") != -1)) ;
var is_ie5up  = (is_ie && !is_ie3 && !is_ie4) ;
var is_ie5_5up =(is_ie && !is_ie3 && !is_ie4 && !is_ie5) ;
var is_ie6    = (is_ie && (is_major == 4) && (agt.indexOf ("msie 6.") != -1)) ;
var is_ie6up  = (is_ie && !is_ie3 && !is_ie4 && !is_ie5 && !is_ie5_5 && !is_safari) ;

//////////////////////////////////////////////////////////////////////////////
//
// Find Object
//
//////////////////////////////////////////////////////////////////////////////

var arrErrors = new Array();
var dom = ((document.getElementById) ? true : false);
var elementsCache;
var elementCacheInit = false;

function initElementsCache()
{
	if( typeof(elementsCache) != 'undefined' )
		return;

	elementsCache =  new Object();

	if (document.forms.length == 0)
		return;

	
	
	
	
	
	
	
	for( var i=0; i<document.forms[0].elements.length; ++i )
	{
		var id = document.forms[0].elements[i].id;

		var pos = id.lastIndexOf('_');
		if( pos==-1 )
			elementsCache[id] = document.forms[0].elements[i];
		else
		{
			elementsCache[id.substring(pos+1, id.length)] = document.forms[0].elements[i];
		}
	}
        elementCacheInit = true;
}

function findObj (n, d) 
{
	var x;  

	if (!d) 
		d = document; 

	initElementsCache();

	if (dom)
		x = elementsCache[n];

	if( typeof(x)=='undefined' )
	{
		if(document.getElementById)
			x = d.getElementById(n);
		else if (document.all)
			x = document.all[n];
		else if (document.layers)
			x = d.layers[hoo];
	}
	
	return x;
}

//////////////////////////////////////////////////////////////////////////////
//
// Get Object Value
//
//////////////////////////////////////////////////////////////////////////////

function getObjValue (elementID) 
{
	return findObj(elementID).value;
}

//////////////////////////////////////////////////////////////////////////////
//
// Dynamic
//
//////////////////////////////////////////////////////////////////////////////

function setSelectedOption (element, value)
{
	for (var i = 0; i < element.length; i++)
	{
		if (element.options[i].value == value)
		{
			element.options[i].selected = true;
		}
	}
}

function getElementValue (element)
{
	var objectElement;

	if (typeof (element) != 'object')
	{ 
		if (( objectElement = findObj (element) ) == null)
		{
			return false;
		}
	}
	else
	{
		objectElement = element;
	}

	if (objectElement.tagName == 'SELECT')
	{
		return objectElement.options[objectElement.options.selectedIndex].value;
	}

	if (objectElement.tagName == 'INPUT')
	{
		return objectElement.value;
	}
	
	return false;
}

function setElementValue (element, value)
{
	var objectElement;

	if (typeof (element) != 'object')
	{ 
		if (( objectElement = findObj (element) ) == null)
		{
			return false;
		}
	}
	else
	{
		objectElement = element;
	}

	if (objectElement.tagName == 'SELECT')
	{
		setSelectedOption (objectElement, value);

		return true;
	}

	if (objectElement.tagName == 'INPUT')
	{
		objectElement.value = value;

		return true;
	}
	
	return false;
}

//////////////////////////////////////////////////////////////////////////////
//
// Calendar
//
//////////////////////////////////////////////////////////////////////////////

function getDaysInMonth (month, year)
{
	var d = new Date (year, month, 0);
	return d.getDate ();
}

//////////////////////////////////////////////////////////////////////////////
//
// 
//
//////////////////////////////////////////////////////////////////////////////

function windowClose ()
{
	window.close;

	return true;
}

function showPopup (url, name, title, W, H, X, Y)
{
//	W = (W ? W : 300);
//	W = (H ? H : 400);

	X = (window.screen.availWidth / 2) - (W / 2);

	if (window.screen.availHeight - 24 < H)
	{
		Y = 0;
		W += 17;
		H = window.screen.availHeight - 24;
	}
	else
	{
		Y = (window.screen.availHeight / 2) - (H / 2 + 12);
	}

	params = "scrollbars=yes,status=yes,resizable=1,width=" + W + ",height=" + H + ",left=" + X + ",top=" + Y;

	wID = window.open (url, name, params);

	return wID;
}

function trim(txt) {
	if (txt=="") return txt;
	var first=0;
	var last=txt.length;
	while (txt.charAt(first) == " ") first++;
	while (txt.charAt(last-1) == " ") last--;
	//if txt have all spaces then last is < first
	if (last<first)last=first=0;
	return txt.substring(first,last);
}
