AstoUtils = new Object();

AstoUtils.convertInputNbsp = function( value ){
	var ret   = '';
	var tag_f = true;
	for( var i=0 ; i<value.length ; i++ ){
	    var ch = value.charAt(i);
	    if( ch == '<' ) tag_f = false;
		if( ch == '>' ) tag_f = true;
	    if( tag_f && ch == ' ' )
	           	ret += '&nbsp;';
		else	ret += ch;
	}
	return ret;
}

AstoUtils.getNextPriority = function( ary, up ){
   	var max = 0;
   	if( ary ){
		for( var i in ary ){
	   		if( !isNaN( ary[i] ) ){
	   			max = Math.max( max, Number( ary[i] ) );
	   		}
   		}
   	}
   	if( up == null ){
   		up = 10;
   	}
   	return max + up;
}

AstoUtils.getHiddenDom = function( name, value ){
	var hidden = document.createElement( 'INPUT' );
	$( hidden ).attr( 'type', 'hidden' );
	$( hidden ).attr( 'name', name );
	$( hidden ).val ( value );
	return hidden;
	
}

AstoUtils.removeChar = function( value, ch ){
	if( value == null ) return null;
    return value.split( ch ).join( "" )
}

AstoUtils.getSelectedIndexById = function( selectElemId ){
	var elem = document.getElementById( selectElemId );
	if( elem ){
		return elem.selectedIndex;
	}
	return -1;
}

AstoUtils.setSelectedIndexById = function( selectElemId, index ){
	var elem = document.getElementById( selectElemId );
	if( elem ){
		elem.selectedIndex = index;
	}
}

AstoUtils.ajaxPost = function( form, callbackFk ){
	var param = {};
	$( "input", form ).each( function(){
	    param[$( this ).attr('name')] = $( this ).val();
	});
    $.post( $( form ).attr( 'action' ), param, callbackFk );
}

AstoUtils.resetOptions = function( targetSelect, options, withBrank ){

	if( targetSelect && options ){
		$( targetSelect ).attr( 'disabled', true );
		$( targetSelect ).empty();
		if( withBrank ){
			AstoUtils.appendOption( targetSelect, { 'value' : '' }, '', true );		
		}
		for( var i in options ){
			AstoUtils.appendOption( targetSelect, options[i].attr, options[i].html, true );		
			$( targetSelect )[0].options.selectedIndex = -1;
		}
		$( targetSelect )[0].options.selectedIndex = 0;
		$( targetSelect ).attr( 'disabled', false );
	}

}

AstoUtils.appendOption = function( targetSelect, attrs, html, disFlag ){

	if( targetSelect && attrs ){
		if( !disFlag ){
			$( targetSelect ).attr( 'disabled', true );
		}
		var attrStr = '';
		for( var key in attrs ){
			attrStr += " " + key + "='" + attrs[key] + "' ";
		}
		$( targetSelect ).append( "<OPTION " + attrStr + " >" + html + "</OPTION>" );
		if( !disFlag ){
			$( targetSelect ).attr( 'disabled', false );
		}
	}

}








