﻿// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
/*
	Example
	<script type="text/javascript">
		var js = null;
	
		window.onload=function() {
			js = new JsUtils();
		}
	</script>

	<input type="text" onkeypress="return js.numbericKeyPressFilter()" />
*/
function JsUtils()
{
	
	this.numbericKeyPressFilter = function( )
	{
		var value = String.fromCharCode( event.keyCode );

		if( value.match( /\d/ ) )
			return true;

		return false;
	}

	this.decimalKeyPressFilter = function( )
	{
		var value = String.fromCharCode( event.keyCode );

		if( value.match( /\d|\./ ))
		{
			
			return true;
		}

		return false;
	}


	this.isDecimalFormat = function( obj, length, pricision )
	{
		if( obj.value.match( /(?!^0*$)(?!^0*\.0*$)^\d{1,5}(\.\d{1,2})?$/ ))
		{
			return true;
		}
		
		return false;
	}
	
	this.openWindow = function( url, name, config, x, y )
	{
		var win = window.open ( url , name, config );
		
		if( x != null && y != null )
			win.moveTo( x, y );
	}
	
	//Get Left/Top Postion of element
	this.getTopLeftPos = function(obj) 
	{
		var curleft = curtop = 0;
		if (obj.offsetParent) 
		{
			curleft = obj.offsetLeft
			curtop = obj.offsetTop
			
			while (obj = obj.offsetParent) 
			{
				curleft += obj.offsetLeft
				curtop += obj.offsetTop
			}
		}
		return [curtop, curleft];
	}
	
	this.redirect = function( url )
	{
		window.location = url;
	}


	this.getDropDownListSelectedValue = function( ddlId )
	{
		var  ddl = null;
		
		if( typeof(ddlId) == "object" )
			ddl = ddlId;
		else if( typeof(ddlId) == "string" )
			ddl = document.getElementById( ddlId );
		
		
		return ddl.options[ ddl.selectedIndex ].value;
	}
	

	this.setDropDownListValue = function( ddlId, value, defaultValue )
	{
		var  ddl = null;
		var found = false;
		var defaultOption = null;
		
		if( typeof(ddlId) == "object" )
			ddl = ddlId;
		else if( typeof(ddlId) == "string" )
			ddl = document.getElementById( ddlId );
		
		
		for( var i = 0; i < ddl.options.length; i ++ )
		{
			if( ddl.options[i].value == value )
			{
				ddl.options[i].selected = true;
				found = true;
			}
			
			if( defaultValue != null && defaultValue != "" && ddl.options[i].value.toUpperCase() ==  defaultValue.toUpperCase() )
			{
				defaultOption = ddl.options[i];
			}
		}
		
		if( ! found && defaultOption != null )
			defaultOption.selected = true
		
		return found;
	}
	

	this.getRadioSelectedValue = function( rdoName )
	{
		var input = document.getElementsByTagName( "input" );
		var value = null;
		
		for( var i = 0; i < input.length; i ++ )
		{
			if( input[i].type != null && input[i].type.toUpperCase() == "RADIO" && input[i].name != null && input[i].name.toUpperCase() == rdoName.toUpperCase() )
			{
				if( input[i].checked )
				{
					value = input[i].value;
					break;
				}
			}
		}
		
		return value;
	}
	
	
	this.setRadioValue = function( rdoName, value )
	{
		var radios = document.getElementsByName( rdoName );
		
		for( var i = 0; i < radios.length; i ++ )
		{
			if( radios[i].type != null && radios[i].type.toUpperCase() == "RADIO" )
			{
				if( radios[i].value == value )
				{
					radios[i].checked = true;
					break;
				}
			}
		}
		
		return radios;
	}
	
	this.getValue = function( objId, elementType )
	{
		if( elementType == null )
		{
			var obj = document.getElementById( objId );
		
			if( obj != null && obj.tagName != null )
			{
				if( obj.tagName.toUpperCase() == "SELECT" )
					return this.getDropDownListSelectedValue( objId );
				else
					return obj.value;
			}
		}
		else if( elementType.toUpperCase() == "RADIO" )
		{
			return this.getRadioSelectedValue(objId);
		}
		
		//
		
		
			
		return null;
		
	}
	
	this.setValue = function( objId, value, defaultValue ) {
		var obj = document.getElementById( objId );
		
		if( obj != null && obj.tagName != null )
		{
			var tagName = obj.tagName.toUpperCase();
			
			if( tagName == "DIV" )
			{
				obj.innerHTML = value;
			}
			else if( tagName == "INPUT" || tagName == "TEXTAREA" )
			{
				if( obj.type != null && obj.type.toUpperCase() == "RADIO" )
				{
					this.setRadioValue( objId, value );
				}
				else
					obj.value = value;
			}
			else if( tagName == "SELECT" )
			{
				obj = this.setDropDownListValue( obj, value, defaultValue );
			}
		}
		
		return obj;
	}
}