/*
	history
	2003.11.23 created by cho
*/


/*	 Utitlity for making URL parameter...		
	usage :
		var p = new Parameters();
		p.put("NO","123");
		p.put("NAME","cho");
		
		this.location.href = "next.jsp?"+p.toString(); => "next.jsp?NO=123&NAME=cho"
*/
function Parameters() 
{
	this.names = new Array();
	this.values = new Array();
	
	this.put = parametersPut;
	this.get = parametersGet;
	this.toString = parametersToString;
}

/* put name=value pair 			*/
function parametersPut(name, value) 
{
	this.names[this.names.length] = name;
	this.values[this.values.length] = value;
}

/*	get value for given key		*/
function parametersGet(name) 
{
	for(var i=0; i<this.names.length; i++) 
	{
		if(this.names[i] == name) 
		{
			return this.values[i];
		}
	}
}

/* convert to String				*/
function parametersToString() 
{
	var result = "";
	for(var i=0; i<this.names.length; i++)
	{
		result += this.names[i] + "=" + this.values[i];
		result += "&";
	}
	return result;
}

/*
	return to removed character : half-word , full-word space  

	trim(" asb   ") -> "asb"
	trim("@asb ") -> "asb" 
*/
function trim(val)  
{
	if( val == null ) return null;

	len = val.length;
	
	for( var i=0 ; i<val.length && ( val.charAt(i) ==' ' || val.charAt(i) == '@' ) ; i++ ) ;

	for( var j=val.length ; j>0 && ( val.charAt(j-1) ==' ' || val.charAt(j-1) == '@' ) ; j-- ) ;

	return i>=j ? "" : val.substring(i,j);
}

/*
	return the converted string for xml text value
	covertXMLTextStr("111&222") -> "111&amp222";
	covertXMLTextStr("111<222") -> "111&lt222";
*/
function covertXMLTextStr(val)
{
	val = val.replace(/&/g,"&amp");
	val = val.replace(/</g,"&lt");
	return val;
}

/*
	moves the scroll in window specfied point
	
	<td id=abc >

	oneOfTdInTable = document.all("abc");
	moveScroll( oneOfTdInTable );

*/
function moveScroll( obj )
{
	po = 0;
	for( p=obj ; p.nodeName != "BODY" ; p=p.offsetParent )
	{
		po += p.offsetTop;
	}
	halfSize = document.body.offsetHeight/2;
	if ( po - halfSize > 0 )
		po = po - halfSize;
	else
		po = 0;
	window.scroll(0, po );
}

/* default compareMethod => compareOptionByValue
 Usage : sortSelect( document.job.MY_SELECT , myCompare );
 		 sortSelect( document.job.MY_SELECT );
*/
function sortSelect( obj , compareMethod )
{
	if( compareMethod == null )
		compareMethod = compareOptionByValue;

	var arr = new Array();
	for( i=obj.options.length-1 ; i>=0 ; i=obj.options.length-1 )
	{
		arr[i] = obj.options[i];
		obj.remove(i);
	}
	arr.sort( compareMethod );
	for( var i=0 ; i<arr.length ; i++ )
		obj.add( arr[i] );
}

function compareOptionByValue( o1 , o2 )
{
	v1 = parseInt(o1.value);
	v2 = parseInt(o2.value);
	if( v1 > v2 )
		return 1;
	else if( v1 < v2 )
		return -1;
	else
		return 0;
}



/* sample compareMethod For table 
	function compareInt( a , b )
	{
		aa = a.all.item(sortKey);
		bb = b.all.item(sortKey);
		first = aa.childNodes[0].nodeValue*1;
		second = bb.childNodes[0].nodeValue*1;
		if( first > second )
			return descSort;
		else if( first < second )
			return descSort*-1;
		else
			return 0;
	}
*/


function selectOption( selectObj , optionObj )
{
	for( var i=0 ; i<selectObj.options.length ; i++ )
	{
		if( selectObj.options[i] == optionObj )
			selectObj.options[i].selected = true;
		else
			selectObj.options[i].selected = false;
	}
}

function selectOptionByIndex( selectObj , index )
{
	for( var i=0 ; i<selectObj.options.length ; i++ )
	{
		if( i == index )
			selectObj.options[i].selected = true;
		else
			selectObj.options[i].selected = false;
	}
}

/* 
	support '%' , '&' , '#' , '?' , "=" char
	you can use it on GET METHOD

	convertEscape("111%222") -> "111%25222"
	"test.jsp?VALUE=111%222" => "test.jsp?VALUE=111%25222"

*/
function convertEscape(val)
{
	if( val == null ) return null;
	val = val.replace(/%/g,"%25");
	val = val.replace(/&/g,"%26");
	val = val.replace(/#/g,"%23");
	val = val.replace(/\?/g,"%3F");
	val = val.replace(/=/g,"%3D");
	return val;
	
}
function getScreenPosTop(obj)

{
	
	po = 0;
	for(var p=obj ; p.nodeName != "BODY" ; p=p.offsetParent )
	
	{
		
		po += p.offsetTop;
	
	}
	
	return po;

}
function getScreenPosLeft(obj)

{
	
	po = 0;
	for(var p=obj ; p.nodeName != "BODY" ; p=p.offsetParent )
	{
		po += p.offsetLeft;
	}
	return po;

}