/*-------------------------------------------------------
util.js
depends on prototype.js(http://prototype.conio.net/)
@reversion 1.01 
 Enable to create OptGroup Element 
--------------------------------------------------------- 
@version  V1.0
@author   youngjin.shin (2007/04/20)

Copyright (c) 2007-2008 Rakuten Travel, Inc. All Rights Reserved.
--------------------------------------------------------*/

if (!Util) {
  var Util={
    instances : null,
    
    getInstance:function (id){ 
  	   for (var i = 0; i < this.instances.length ; i++) {
  		    if( this.instances[i].id == id ){
  				return this.instances[i];
  			}
  		}
  		return null;
    },
    
    escapeHTML:function (str){
    	return str.replace(/\&/g, '&amp;').replace( /</g, '&lt;').replace(/>/g, '&gt;').replace(/\"/g, '&quot;').replace(/\'/g, '&#39;');
    },
    
    encoding:function(str){
		var ret = '';
		var tempChr = 0x0000;
		for( var i=0 ; i<str.length ; i++ ){
			tempChr = str.charCodeAt(i) ;
			if ( tempChr > 256 ) {
				ret +=  String.fromCharCode(tempChr);
			} else {
			 	if (tempChr > 122) {
			 	} else if(tempChr > 90 && tempChr < 97){
			 		ret += ' ';
			 	} else if(tempChr > 65 && tempChr < 57){
			 		ret += ' ';
			 	} else if(tempChr < 48){
			 		ret += ' ';
			 	} else {
					ret += this.convertToHex(tempChr);
				}	
			}
		}
		return encodeURIComponent(ret).replace(/%/g,'');
	},
	
	convertToHex:function(num){
    	var hex = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'];
		return hex[(num>>4) & 0x0f] + hex[num & 0x0f];
	},
	
    createOptionList:function ( optionId, valueList, nameList){
     	var index = 0;
 	 	var optionList = $(optionId);
     	for( ; index < valueList.length ; index++ ) {
        	optionList[index] = new Option( nameList[index],valueList[index] );
     	}  
     	while( optionList[index] ){
       		optionList[index] = null;
     	}  
	},
  
    createOptionList:function ( optionId, opList){
     	var index = 0;
 	 	var optionList = $(optionId);
 	 	var optGroup = null;
 	 	var option   = null;
 	 	this.clearChildNodes(optionList);
     	for( ; index < opList.length/2 ; index++ ) {
     		if(opList[index*2] == "GROUP-START"){
     			optGroup = document.createElement("optgroup");
     			optGroup.label = opList[index*2+1];
     			optionList.appendChild(optGroup);
     			index++;
     			for( ; index < opList.length/2 ; index++ ) {
     				if(opList[index*2] == "GROUP-END"){
     				   	break;
     				}
     				option = document.createElement("option");
     				option.innerHTML=opList[index*2+1];
     				option.value=opList[index*2];
     				optGroup.appendChild(option);
     				//optGroup.appendChild(new Option( opList[index*2+1],opList[index*2] ));
     			}
     			continue;
     		} 
        	optionList.options[index] = new Option( opList[index*2+1],opList[index*2] );
     	}
     	while( optionList[index] ){
       		optionList.options[index] = null;
     	}  
  	},
  	
  	clearChildNodes:function (obj){
  	    while( obj.childNodes[0] ){
       		obj.removeChild(obj.childNodes[0]);
     	}  
  	},
  	
 	appendList:function (trgList, srcList){
		if(trgList){
			if(srcList){
				for(i=0;i<srcList.length;i++){
					trgList.push(srcList[i]);
				}
			}
			return trgList;
		}	
	}

  };
}

Util.Suggest    = Class.create();
Util.TreeObject = Class.create();
Util.Date       = Class.create();
Util.LimitNumber= Class.create();